1

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.

user353gre3
  • 2,747
  • 4
  • 24
  • 27
Ivan Kochurkin
  • 4,413
  • 8
  • 45
  • 80

1 Answers1

6

You're looking for the SymbolFinder.FindSymbolAtPosition method.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964