Go to definition from text position with roslyn

早过忘川 提交于 2019-12-10 12:13:45

问题


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):

  1. 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 });
    
  2. Get current token in syntax tree:

    var token = currentTree.GetRoot().FindToken(textPos, false);
    

Symbol have appropriate method for definition getting: DeclaringSyntaxNodes. How can I resolve token on Symbol in SemanticModel? Thanks.


回答1:


You're looking for the SymbolFinder.FindSymbolAtPosition method.



来源:https://stackoverflow.com/questions/27259600/go-to-definition-from-text-position-with-roslyn

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!