0
  1. I have a working VS extension in VS 2019
  2. I trigger events when mousing over text in the editor (in this case, C#)
  3. My extension receives an EventArgs (actually MouseHoverEventArgs), which I can use to access the textPosition, and can get the line I'm mousing over as text

Question: How do I get this with syntactic context? i.e. a set of tokens for the line / area I am mousing over, in a way that I can generate the fully scoped name of the type? i.e. how do I hook into the syntactic content of the editor's live evaluation of the syntax of the file?

I'd want something like: private string GetTypeOfMousedOverToken(EventArgs e)

fastmultiplication
  • 2,951
  • 1
  • 31
  • 39

1 Answers1

1

You can use following code to get syntactic context:

Microsoft.VisualStudio.Text.SnapshotPoint caretPosition = textView.Caret.Position.BufferPosition;
Microsoft.CodeAnalysis.Document document = caretPosition.Snapshot.GetOpenDocumentInCurrentContextWithChanges();
document.GetSyntaxRootAsync().Result.FindToken(caretPosition)

See https://vlasovstudio.com/visual-commander/commands.html#CreateTypedVariable for a complete example.

Sergey Vlasov
  • 26,641
  • 3
  • 64
  • 66