Язык для вида текста в расширении VS2010
-
26-09-2019 - |
Вопрос
Я новичок в строительстве дополнений для Visual Studio, но удалось создать инструмент Simpe для VS2010, который делает небольшие текстовые манипулирования в окне текущего активного кода. Я дошел до такой степени, что мне нужно знать язык (vb.net, c # или что-то еще) текущего представления текста.
Я пытался получить имя файла (поэтому я могу посмотреть на расширение, чтобы определить язык), используя следующий код:
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;
К сожалению, PathAsObject всегда возвращает NULL. Есть ли другой способ получить имя файла / язык?
Решение
Похоже, это работает:
// 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;
Это возвращает «Основные» для VB.NET, что именно то, что мне нужно знать.
Не связан с StackOverflow