Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/visual-studio-2010/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# VS2010扩展中的文本视图语言_C#_Visual Studio 2010_Visual Studio Addins_Vsix - Fatal编程技术网

C# VS2010扩展中的文本视图语言

C# VS2010扩展中的文本视图语言,c#,visual-studio-2010,visual-studio-addins,vsix,C#,Visual Studio 2010,Visual Studio Addins,Vsix,我不熟悉为VisualStudio构建加载项,但已经为VS2010构建了一个simpe工具,该工具在当前活动的代码窗口中执行一些文本操作。我需要了解当前文本视图的语言(VB.Net、C#或其他) 我已尝试使用以下代码获取文件名(以便查看扩展名以确定语言): IVsTextManager txtMgr = (IVsTextManager)GetService(typeof(SVsTextManager)); int mustHaveFocus = 1;//means true txtMgr.Get

我不熟悉为VisualStudio构建加载项,但已经为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的“Basic”,这正是我需要知道的。

看起来像这样:

// 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的“Basic”,这正是我需要知道的。

仅供参考,您应该尽量避免使用snapshot.GetText()。将缓冲区自身转换为一个大字符串(它存储在“片段树”中,而不是作为一个隐藏的字符串)的成本相对较高。仅供参考,您应该尽量避免使用snapshot.GetText()。将缓冲区自身转换为一个大字符串(它存储在一个“片段树”中,而不是作为一个单独的字符串隐藏)的成本相对较高。