Walkthrough: Displaying QuickInfo Tooltips

 

The new home for Visual Studio documentation is Visual Studio 2017 Documentation on docs.microsoft.com.

The latest version of this topic can be found at Walkthrough: Displaying QuickInfo Tooltips.

QuickInfo is an IntelliSense feature that displays method signatures and descriptions when a user moves the pointer over a method name. You can implement language-based features such as QuickInfo by defining the identifiers for which you want to provide QuickInfo descriptions, and then creating a tooltip in which to display the content. You can define QuickInfo in the context of a language service, or you can define your own file name extension and content type and display the QuickInfo for just that type, or you can display QuickInfo for an existing content type (such as "text"). This walkthrough shows how to display QuickInfo for the "text" content type.

The QuickInfo example in this walkthrough displays the tooltips when a user moves the pointer over a method name. This design requires you to implement these four interfaces:

  • source interface

  • source provider interface

  • controller interface

  • controller provider interface

The source and controller providers are Managed Extensibility Framework (MEF) component parts, and are responsible for exporting the source and controller classes and importing services and brokers such as the ITextBufferFactoryService, which creates the tooltip text buffer, and the IQuickInfoBroker, which triggers the QuickInfo session.

In this example, the QuickInfo source uses a hard-coded list of method names and descriptions, but in full implementations, the language service and the language documentation are responsible for providing that content.

Starting in Visual Studio 2015, you do not install the Visual Studio SDK from the download center. It is included as an optional feature in Visual Studio setup. You can also install the VS SDK later on. For more information, see Installing the Visual Studio SDK.

To create a MEF project

  1. Create a C# VSIX project. (In the New Project dialog, select Visual C# / Extensibility, then VSIX Project.) Name the solution QuickInfoTest.

  2. Add an Editor Classifier item template to the project. For more information, see Creating an Extension with an Editor Item Template.

  3. Delete the existing class files.

The QuickInfo source is responsible for collecting the set of identifiers and their descriptions and adding the content to the tooltip text buffer when one of the identifiers is encountered. In this example, the identifiers and their descriptions are just added in the source constructor.

To implement the QuickInfo source

  1. Add a class file and name it TestQuickInfoSource.

  2. Add a reference to Microsoft.VisualStudio.Language.IntelliSense.

  3. Add the following imports.

    No code example is currently available or this language may not be supported.

  4. Declare a class that implements IQuickInfoSource, and name it TestQuickInfoSource.

    No code example is currently available or this language may not be supported.

  5. Add fields for the QuickInfo source provider, the text buffer, and a set of method names and method signatures. In this example, the method names and signatures are initialized in the TestQuickInfoSource constructor.

    No code example is currently available or this language may not be supported.

  6. Add a constructor that sets the QuickInfo source provider and the text buffer, and populates the set of method names, and method signatures and descriptions.

    No code example is currently available or this language may not be supported.

  7. Implement the AugmentQuickInfoSession method. In this example, the method finds the current word, or the previous word if the cursor is at the end of a line or a text buffer. If the word is one of the method names, the description for that method name is added to the QuickInfo content.

    No code example is currently available or this language may not be supported.

  8. You must also implement a Dispose() method, since IQuickInfoSource implements IDisposable:

    No code example is currently available or this language may not be supported.

The provider of the QuickInfo source serves primarily to export itself as a MEF component part and instantiate the QuickInfo source. Because it is a MEF component part, it can import other MEF component parts.

To implement a QuickInfo source provider

  1. Declare a QuickInfo source provider named TestQuickInfoSourceProvider that implements IQuickInfoSourceProvider, and export it with a NameAttribute of "ToolTip QuickInfo Source", an OrderAttribute of Before="default", and a ContentTypeAttribute of "text".

    No code example is currently available or this language may not be supported.

  2. Import two editor services, ITextStructureNavigatorSelectorService and ITextBufferFactoryService, as properties of TestQuickInfoSourceProvider.

    No code example is currently available or this language may not be supported.

  3. Implement TryCreateQuickInfoSource to return a new TestQuickInfoSource.

    No code example is currently available or this language may not be supported.

QuickInfo controllers determine when QuickInfo should be displayed. In this example, QuickInfo is displayed when the pointer is over a word that corresponds to one of the method names. The QuickInfo controller implements a mouse hover event handler that triggers a QuickInfo session.

To implement a QuickInfo controller

  1. Declare a class that implements IIntellisenseController, and name it TestQuickInfoController.

    No code example is currently available or this language may not be supported.

  2. Add private fields for the text view, the text buffers represented in the text view, the QuickInfo session, and the QuickInfo controller provider.

    No code example is currently available or this language may not be supported.

  3. Add a constructor that sets the fields and adds the mouse hover event handler.

    No code example is currently available or this language may not be supported.

  4. Add the mouse hover event handler that triggers the QuickInfo session.

    No code example is currently available or this language may not be supported.

  5. Implement the Detach method so that it removes the mouse hover event handler when the controller is detached from the text view.

    No code example is currently available or this language may not be supported.

  6. Implement the ConnectSubjectBuffer method and the DisconnectSubjectBuffer method as empty methods for this example.

    No code example is currently available or this language may not be supported.

The provider of the QuickInfo controller serves primarily to export itself as a MEF component part and instantiate the QuickInfo controller. Because it is a MEF component part, it can import other MEF component parts.

To implement the QuickInfo controller provider

  1. Declare a class named TestQuickInfoControllerProvider that implements IIntellisenseControllerProvider, and export it with a NameAttribute of "ToolTip QuickInfo Controller" and a ContentTypeAttribute of "text":

    No code example is currently available or this language may not be supported.

  2. Import the IQuickInfoBroker as a property.

    No code example is currently available or this language may not be supported.

  3. Implement the TryCreateIntellisenseController method by instantiating the QuickInfo controller.

    No code example is currently available or this language may not be supported.

To test this code, build the QuickInfoTest solution and run it in the experimental instance.

To build and test the QuickInfoTest solution

  1. Build the solution.

  2. When you run this project in the debugger, a second instance of Visual Studio is instantiated.

  3. Create a text file and type some text that includes the words "add" and "subtract".

  4. Move the pointer over one of the occurrences of "add". The signature and the description of the add method should be displayed.

Walkthrough: Linking a Content Type to a File Name Extension

Show: