[英]How do I retrieve text from the Visual Studio editor for use with Roslyn SyntaxTree?
I am attempting to write a Visual Studio extension that will analyze the C# code displayed in the editor and possibly update the code based on what I find.我正在尝试编写一个 Visual Studio 扩展来分析编辑器中显示的 C# 代码,并可能根据我发现的内容更新代码。 This would be on demand (via a menu item), and not using an analyzer and code fix.
这将是按需(通过菜单项),而不是使用分析器和代码修复。
There are a number of examples and samples on the Internet, but they all start either with the source code hard-coded in the samples, or create a new document, or look at each file in the VS solution that is open.网上有很多示例和示例,但它们都是从示例中硬编码的源代码开始,或者创建一个新文档,或者查看打开的VS解决方案中的每个文件。 How do I access the source code from the active editor window?
如何从活动编辑器窗口访问源代码?
First, you need to install the Microsoft.CodeAnalysis.EditorFeatures.Text
package.首先,您需要安装
Microsoft.CodeAnalysis.EditorFeatures.Text
包。
Then you need to add the appropriate using statement:然后你需要添加适当的 using 语句:
using Microsoft.CodeAnalysis.Text;
Now you can map between Visual Studio concepts ( ITextSnapshot
, ITextBuffer
etc.) and Roslyn concepts ( Document
, SourceText
etc.) with the extension methods found here: https://github.com/dotnet/roslyn/blob/master/src/EditorFeatures/Text/Extensions.cs现在,您可以使用此处找到的扩展方法在 Visual Studio 概念(
ITextSnapshot
、 ITextBuffer
等)和 Roslyn 概念( Document
、 SourceText
等)之间进行映射: https : //github.com/dotnet/roslyn/blob/master/src/ EditorFeatures/Text/Extensions.cs
For example:例如:
ITextSnapshot snapshot = ... //Get this from Visual Studio
var documents = snapshot.GetRelatedDocuments(); //There may be more than one
In a comment to my original question, @SJP gave a link to @Frank Bakker's answer to the question at Calling Roslyn from VSIX Command .在对我的原始问题的评论中,@SJP 提供了一个链接,指向@Frank Bakker 在Calling Roslyn from VSIX Command 中对问题的回答。 This does work as outlined.
这确实如概述的那样工作。
@JoshVarty provided a hint of the direction to go in his answer above. @JoshVarty 在上面的回答中提供了方向的提示。 I combined that with code provided by @user1912383 for how to get an IWpfTextView answering the question Find an IVsTextView or IWpfTextView for a given ProjectItem, in 2010 RC extension .
我将其与@user1912383 提供的代码结合起来,了解如何在 2010 RC 扩展中获得 IWpfTextView回答问题Find an IVsTextView or IWpfTextView for a given ProjectItem 。 Here is the code I came up with:
这是我想出的代码:
var componentModel = (IComponentModel)Package.GetGlobalService(typeof(SComponentModel));
var textManager = (IVsTextManager)Package.GetGlobalService(typeof(SVsTextManager));
IVsTextView activeView = null;
ErrorHandler.ThrowOnFailure(textManager.GetActiveView(1, null, out activeView));
var editorAdapter = componentModel.GetService<IVsEditorAdaptersFactoryService>();
var textView = editorAdapter.GetWpfTextView(activeView);
var document = (textView.TextBuffer.ContentType.TypeName.Equals("CSharp"))
? textView : null;
In a comment after @user1912383's code mentioned above, @kman mentioned that this does not work for document types such as .sql files.在@user1912383 上面提到的代码之后的评论中,@kman 提到这不适用于 .sql 文件等文档类型。 It does, however, work for .cs files which is what I will be using it with.
但是,它确实适用于我将使用它的 .cs 文件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.