Search code examples
visual-studioroslynvsixroslyn-code-analysis

Visual Studio vsix package for custom command, similar to "Go To Definition" (F12)


My company wants a custom command in Visual Studio that would behave rather like the Go To Definition (F12) feature (from context menu in code editor window), in order to navigate to a specific implementation of a method inside the solution.

So I created a VSIX package from a sample on git hub and added a custom command item, and tweak the .vsct so it shows my new Navigation item in the context menu along with the other items ('Quick Actions and Refactorings', 'Copy', 'Paste', etc.). This part is ok.

Question 1 : In the call back of the command how can I retrieve the syntax and symbol classes from the piece of code that is under the carret of the code editor window ? object sender and EventArgs e parameters looks inacurate for my needs. :/

Question 2 : Using code analysis and compiler API features, how would I analyse semantics and then jumps to the right file and and class through the solution ? Are there any samples of code analyse with "jump to" functionality ?

Any tutorials, samples or recommandations are very welcome.


Solution

  • Call the GetOpenDocumentInCurrentContextWithChanges() extension method (in Microsoft.CodeAnalysis.EditorFeatures.Text.dll) on an ITextSnapshot to get a Roslyn Document.

    You can then use normal Roslyn APIs to get the semantic model, resolve symbols, examine the syntax tree, etc.

    Example