Pregunta

soy nuevo en la construcción de complementos para Visual Studio, pero han logrado construir una herramienta simpe para VS2010 que hace un poco de manipulación de texto en la ventana de código activo en ese momento. Tengo hasta el punto en que necesito saber el idioma (VB.Net, C # o lo que sea) de la vista de texto actual.

Me han tratado de conseguir el nombre del archivo (para que pueda mirar a la extensión para determinar el idioma), utilizando el código siguiente:

IVsTextManager txtMgr = (IVsTextManager)GetService(typeof(SVsTextManager));
int mustHaveFocus = 1;//means true
txtMgr.GetActiveView(mustHaveFocus, null, out currentTextView);

userData = currentTextView as IVsUserData;
if (userData == null)// no text view 
{
    Console.WriteLine("No text view is currently open");
    return;
}

object pathAsObject;
Guid monikerGuid = typeof(IVsUserData).GUID;
userData.GetData(ref monikerGuid, out pathAsObject);
string docPath = (string)pathAsObject;

Desafortunadamente pathAsObject siempre devuelve un valor nulo. ¿Hay alguna otra manera de obtener el nombre del archivo / idioma?

¿Fue útil?

Solución

Parece que este funciona:

// Get the current text view.
IVsTextManager txtMgr = (IVsTextManager)GetService(typeof(SVsTextManager));
int mustHaveFocus = 1;//means true
txtMgr.GetActiveView(mustHaveFocus, null, out currentTextView);

userData = currentTextView as IVsUserData;
if (userData == null)// no text view 
{
    Console.WriteLine("No text view is currently open");
    return;
}

// In the next 4 statments, I am trying to get access to the editor's view 
object holder;
Guid guidViewHost = DefGuidList.guidIWpfTextViewHost;
userData.GetData(ref guidViewHost, out holder);
viewHost = (IWpfTextViewHost)holder;

// Get a snapshot of the current editor's text.
allText = viewHost.TextView.TextSnapshot.GetText();

// Get the language for the current editor.
string language = viewHost.TextViewtextView.TextDataModel.ContentType.TypeName;

Esto devuelve "básicos" para VB.Net, que es exactamente lo que necesito saber.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top