fastmultiplication
fastmultiplication

Reputation: 3081

Writing a VS extension and want to get the fully qualified type of the C# text in the editor I am mousing over

  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)

Upvotes: 0

Views: 48

Answers (1)

Sergey Vlasov
Sergey Vlasov

Reputation: 27910

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.

Upvotes: 1

Related Questions