Go to definition from text position with roslyn

1k views Asked by At

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

There are 1 answers

0
SLaks On