在 Visual Studio 扩展中访问当前代码窗格

问题描述:

我正在编写一个带有右键菜单的 Visual Studio (2010) 扩展,同时在代码视图中.我希望能够从我的菜单项事件处理程序中检查当前代码,但无法在对象模型中找到执行此操作的某处.

Im writing a visual studio (2010) extension with a right click menu whilst in a code view. I want to be able to examine the current code from my menu item event handler but havent been able to find somewhere in the object model to do this.

如何在 Visual Studio 扩展中访问当前窗口中的代码?

How do i access the code in the current window in a visual studio extension?

这是我用来获取当前文档文本的代码

Heres the code i used to get the current document text

 DTE dte = Package.GetGlobalService(typeof(DTE)) as DTE ;
 TextDocument activeDoc = dte.ActiveDocument.Object() as TextDocument;

 var text = activeDoc.CreateEditPoint(activeDoc.StartPoint).GetText(activeDoc.EndPoint);

您可能正在寻找

Document doc = DTE.ActiveDocument;
TextDocument txt = doc.Object() as TextDocument;

然后您应该能够根据需要使用 TextDocument 编辑作品.

You should then be able to edit work with the TextDocument as needed.