Pergunta

Sou novo na construção de Addins para o Visual Studio, mas consegui criar uma ferramenta SIMPE para o VS2010 que faz uma pequena manipulação de texto na janela de código atualmente ativa. Eu tenho ao ponto em que preciso conhecer o idioma (vb.net, c# ou qualquer outra coisa) da visualização atual do texto.

Eu tentei obter o nome do arquivo (para que eu possa olhar para a extensão para determinar o idioma) usando o seguinte código:

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;

Infelizmente, o PathasObject sempre retorna nulo. Existe alguma outra maneira de obter o nome do arquivo / idioma?

Foi útil?

Solução

Parece que isso 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;

Isso retorna "básico" para o vb.net, que é exatamente o que eu preciso saber.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top