使用 roslyn 从文本位置转到定义 [英] Go to definition from text position with roslyn
问题描述
如何获取当前自定义位置和文件(在项目或解决方案内)下任何符号的定义文件名和位置?
How to get file name and position of definition of any symbol under the current custom position and file (within project or solution)?
我做了以下步骤(简化说明):
I do the following steps (simplified explanation):
通过以下方式创建语法树集合并编译:
Create collection of syntax trees and compilation by the following way:
SyntaxTrees = new List<SyntaxTree>();
foreach (var file in projectFiles)
syntaxTrees.Add(SyntaxTree.ParseText(File.ReadAllText(file));
Compilation = Compilation.Create("temp.cs", null, SyntaxTrees, new MetadataReference[] { mscorlib });
在语法树中获取当前标记:
Get current token in syntax tree:
var token = currentTree.GetRoot().FindToken(textPos, false);
Symbol 有适当的定义获取方法:DeclaringSyntaxNodes
.如何在 SemanticModel
中解析 Symbol
上的 token
?谢谢.
Symbol have appropriate method for definition getting: DeclaringSyntaxNodes
.
How can I resolve token
on Symbol
in SemanticModel
? Thanks.
推荐答案
您正在寻找 SymbolFinder.FindSymbolAtPosition
方法.
You're looking for the SymbolFinder.FindSymbolAtPosition
method.
这篇关于使用 roslyn 从文本位置转到定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!