简体   繁体   English

如何从 Visual Studio 编辑器中检索文本以与 Roslyn SyntaxTree 一起使用?

[英]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 概念( ITextSnapshotITextBuffer等)和 Roslyn 概念( DocumentSourceText等)之间进行映射: 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.

相关问题 如何使用 Roslyn 将 SyntaxTree 或 UnitCompilationRoot 包装在区域或注释中? - How can I wrap a SyntaxTree or UnitCompilationRoot in a region or comment using Roslyn? 如何在Visual Studio中使用更改后的Roslyn - How to use an altered Roslyn in Visual Studio 如何在给定的Roslyn SyntaxNode上打开Visual Studio代码编辑器? - How to open Visual Studio code editor at given Roslyn SyntaxNode? 从 Roslyn SyntaxTree 中删除所有 SyntaxTrivia 节点 - Remove all SyntaxTrivia nodes from Roslyn SyntaxTree 从Visual Studio文本选择中获取Roslyn SyntaxToken(插入位置) - Get Roslyn SyntaxToken from Visual Studio Text Selection (caret position) 我可以在Visual Studio 2017和.NET 4.7中使用“ Roslyn”吗? - Can I use “Roslyn” in Visual Studio 2017 with .NET 4.7? 如何在Visual Studio 2015中使用roslyn C#编译器? - How to use roslyn c# compiler with visual studio 2015? 如何获得visual studio文本编辑器的背景颜色? - How can I get visual studio text editor background colour? 并发修改Roslyn工作区? Visual Studio如何做到这一点? - Concurrency of modifying a Roslyn workspace? How does Visual studio do it? 如何从 Visual Studio 的文本编辑器中删除虚线? - How to remove dotted line from text editor in visual studio?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM