I recently encountered an interesting challenge: The need to identify the word currently under the mouse cursor in the Visual Studio editor.

This turned out harder than I thought. But after some work and a few tricks needed to make it work well, the result is this:

 

Let’s see how this can be done.

Reacting to mouse events in a Visual Studio extension

To react to mouse events, you’ll need to implement IMouseProcessorProvider. The implemented class should be exported with MEF, so if you’re starting a new project, see here how to add MEF support.

You’ll need the following NuGet packages for this:

  • Microsoft.VisualStudio.Text.UI.Wpf 
  • Microsoft.VisualStudio.Editor
  • Microsoft.CodeAnalysis
  • Microsoft.CodeAnalysis.EditorFeatures.Text
  • System.Xaml (A reference, not a NuGet package)
If you have extension exceptions, this might be due to load assembly errors. One way to fix this is to use an earlier version of the Microsoft.CodeAnalysis.XXX NuGet packages, like version 1.0.

So as mentioned, to react to mouse events in a VS extension, we will need to implement  IMouseProcessorProvider like this:

This is called once and provides Visual Studio with additional Mouse-Hover-Processors. In our case, MyMouseHoverProcessor.

Since this is a MEF exported class, we can use ImportingConstructor to import Visual Studio services. In our case, we will need IVsEditorAdaptersFactoryService.

MyMouseHoverProcessor

The Mouse-Hover-Processors is the class that actually handles mouse events. This is the implementation:

Here’s what happened here:

  1. IVsTextView _viewAdapter  is another abstraction of Visual Studio’s editor tab.
  2. e.GetPosition()  finds the mouse coordinates.
  3. MouseHelper.GetMousePositionInTextView()  will find the stream position from mouse coordinates – We will see the implementation next. Stream position is the position type VS SDK usually works with. It represents the number of characters from the start of the document.
  4. The additional code finds the token from the stream position and prints it.

Simple enough, though there’s some complexity in  MouseHelper.GetMousePositionInTextView() – Let’s see the implementation

Getting stream position from mouse coordinates

This is what happens in general terms:

  1. We try to find the text line in the editor (represented by  ITextViewLine)
  2. We find the mouse position within text buffer by subtracting the left column (which includes the scroll bar) from the initial mouse position.
  3. We get a SnapshotPoint from the relative mouse position.
  4. We get the desired stream position.

Not the most pleasant code to research and discover.

The code is available on GitHub.

This is box title

SUBSCRIBE VIA EMAIL

Join the mailing list and get updates on new articles

Highlighting the code

The additional touch here is to highlight the code under the mouse position.

Once we found the token, it’s a simple matter of finding its start and end position and highlighting them. I wrote a tutorial on highlighting text, check it out here.

The code in GitHub also includes the highlighting, so you can see the full code there.

 

Hope this tutorial helped you, happy coding.

Get Exclusive Articles and Level-up Your C# Game Performance Optimizations in C#: 10 Best Practices