title | description | ms.date | ms.topic | helpviewer_keywords | author | ms.author | manager | ms.subservice | dev_langs | |||
---|---|---|---|---|---|---|---|---|---|---|---|---|
Walkthrough: Displaying QuickInfo Tooltips |
Learn how to display QuickInfo for text content by using this walkthrough. QuickInfo displays method signatures and descriptions for a method name. |
11/04/2016 |
how-to |
|
maiak |
maiak |
mijacobs |
extensibility-integration |
|
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 xref:Microsoft.VisualStudio.Text.ITextBufferFactoryService, which creates the tooltip text buffer, and the xref:Microsoft.VisualStudio.Language.Intellisense.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.
-
Create a C# VSIX project. (In the New Project dialog, select Visual C# / Extensibility, then VSIX Project.) Name the solution
QuickInfoTest
. -
Add an Editor Classifier item template to the project. For more information, see Create an extension with an editor item template.
-
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.
-
Add a class file and name it
TestQuickInfoSource
. -
Add a reference to Microsoft.VisualStudio.Language.IntelliSense.
-
Add the following imports.
:::code language="csharp" source="../snippets/csharp/VS_Snippets_VSSDK/vssdkquickinfotest/cs/testquickinfosource.cs" id="Snippet1":::
-
Declare a class that implements xref:Microsoft.VisualStudio.Language.Intellisense.IQuickInfoSource, and name it
TestQuickInfoSource
.:::code language="csharp" source="../snippets/csharp/VS_Snippets_VSSDK/vssdkquickinfotest/cs/testquickinfosource.cs" id="Snippet2":::
-
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.:::code language="csharp" source="../snippets/csharp/VS_Snippets_VSSDK/vssdkquickinfotest/cs/testquickinfosource.cs" id="Snippet3":::
-
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.
:::code language="csharp" source="../snippets/csharp/VS_Snippets_VSSDK/vssdkquickinfotest/cs/testquickinfosource.cs" id="Snippet4":::
-
Implement the xref:Microsoft.VisualStudio.Language.Intellisense.IQuickInfoSource.AugmentQuickInfoSession%2A 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.
:::code language="csharp" source="../snippets/csharp/VS_Snippets_VSSDK/vssdkquickinfotest/cs/testquickinfosource.cs" id="Snippet5":::
-
You must also implement a Dispose() method, since xref:Microsoft.VisualStudio.Language.Intellisense.IQuickInfoSource implements xref:System.IDisposable:
:::code language="csharp" source="../snippets/csharp/VS_Snippets_VSSDK/vssdkquickinfotest/cs/testquickinfosource.cs" id="Snippet6":::
The provider of the QuickInfo source serves primarily to export itself as a MEF component part and instantiate the QuickInfo source. Because it's a MEF component part, it can import other MEF component parts.
-
Declare a QuickInfo source provider named
TestQuickInfoSourceProvider
that implements xref:Microsoft.VisualStudio.Language.Intellisense.IQuickInfoSourceProvider, and export it with a xref:Microsoft.VisualStudio.Utilities.NameAttribute of "ToolTip QuickInfo Source", an xref:Microsoft.VisualStudio.Utilities.OrderAttribute of Before="default", and a xref:Microsoft.VisualStudio.Utilities.ContentTypeAttribute of "text".:::code language="csharp" source="../snippets/csharp/VS_Snippets_VSSDK/vssdkquickinfotest/cs/testquickinfosource.cs" id="Snippet7":::
-
Import two editor services, xref:Microsoft.VisualStudio.Text.Operations.ITextStructureNavigatorSelectorService and xref:Microsoft.VisualStudio.Text.ITextBufferFactoryService, as properties of
TestQuickInfoSourceProvider
.:::code language="csharp" source="../snippets/csharp/VS_Snippets_VSSDK/vssdkquickinfotest/cs/testquickinfosource.cs" id="Snippet8":::
-
Implement xref:Microsoft.VisualStudio.Language.Intellisense.IQuickInfoSourceProvider.TryCreateQuickInfoSource%2A to return a new
TestQuickInfoSource
.:::code language="csharp" source="../snippets/csharp/VS_Snippets_VSSDK/vssdkquickinfotest/cs/testquickinfosource.cs" id="Snippet9":::
QuickInfo controllers determine when QuickInfo is displayed. In this example, QuickInfo appears 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.
-
Declare a class that implements xref:Microsoft.VisualStudio.Language.Intellisense.IIntellisenseController, and name it
TestQuickInfoController
.:::code language="csharp" source="../snippets/csharp/VS_Snippets_VSSDK/vssdkquickinfotest/cs/testquickinfosource.cs" id="Snippet10":::
-
Add private fields for the text view, the text buffers represented in the text view, the QuickInfo session, and the QuickInfo controller provider.
:::code language="csharp" source="../snippets/csharp/VS_Snippets_VSSDK/vssdkquickinfotest/cs/testquickinfosource.cs" id="Snippet11":::
-
Add a constructor that sets the fields and adds the mouse hover event handler.
:::code language="csharp" source="../snippets/csharp/VS_Snippets_VSSDK/vssdkquickinfotest/cs/testquickinfosource.cs" id="Snippet12":::
-
Add the mouse hover event handler that triggers the QuickInfo session.
:::code language="csharp" source="../snippets/csharp/VS_Snippets_VSSDK/vssdkquickinfotest/cs/testquickinfosource.cs" id="Snippet13":::
-
Implement the xref:Microsoft.VisualStudio.Language.Intellisense.IIntellisenseController.Detach%2A method so that it removes the mouse hover event handler when the controller is detached from the text view.
:::code language="csharp" source="../snippets/csharp/VS_Snippets_VSSDK/vssdkquickinfotest/cs/testquickinfosource.cs" id="Snippet14":::
-
Implement the xref:Microsoft.VisualStudio.Language.Intellisense.IIntellisenseController.ConnectSubjectBuffer%2A method and the xref:Microsoft.VisualStudio.Language.Intellisense.IIntellisenseController.DisconnectSubjectBuffer%2A method as empty methods for this example.
:::code language="csharp" source="../snippets/csharp/VS_Snippets_VSSDK/vssdkquickinfotest/cs/testquickinfosource.cs" id="Snippet15":::
The provider of the QuickInfo controller serves primarily to export itself as a MEF component part and instantiate the QuickInfo controller. Because it's a MEF component part, it can import other MEF component parts.
-
Declare a class named
TestQuickInfoControllerProvider
that implements xref:Microsoft.VisualStudio.Language.Intellisense.IIntellisenseControllerProvider, and export it with a xref:Microsoft.VisualStudio.Utilities.NameAttribute of "ToolTip QuickInfo Controller" and a xref:Microsoft.VisualStudio.Utilities.ContentTypeAttribute of "text"::::code language="csharp" source="../snippets/csharp/VS_Snippets_VSSDK/vssdkquickinfotest/cs/testquickinfosource.cs" id="Snippet16":::
-
Import the xref:Microsoft.VisualStudio.Language.Intellisense.IQuickInfoBroker as a property.
:::code language="csharp" source="../snippets/csharp/VS_Snippets_VSSDK/vssdkquickinfotest/cs/testquickinfosource.cs" id="Snippet17":::
-
Implement the xref:Microsoft.VisualStudio.Language.Intellisense.IIntellisenseControllerProvider.TryCreateIntellisenseController%2A method by instantiating the QuickInfo controller.
:::code language="csharp" source="../snippets/csharp/VS_Snippets_VSSDK/vssdkquickinfotest/cs/testquickinfosource.cs" id="Snippet18":::
To test this code, build the QuickInfoTest solution and run it in the experimental instance.
-
Build the solution.
-
When you run this project in the debugger, a second instance of Visual Studio is started.
-
Create a text file and type some text that includes the words "add" and "subtract".
-
Move the pointer over one of the occurrences of "add". The signature and the description of the
add
method should be displayed.