语言在VS2010扩展文本视图
-
26-09-2019 - |
题
我是新来构建加载项适用于Visual Studio,但已设法建立一个VS2010 simpe工具,做一个小的文本操作在当前激活代码窗口。我得,我需要知道当前文本视图的语言(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