title | description | ms.date | ms.topic | helpviewer_keywords | author | ms.author | manager | ms.subservice | |
---|---|---|---|---|---|---|---|---|---|
Walkthrough: Creating a Margin Glyph |
Learn how to customize the appearance of editor margins by using custom editor extensions by using this walkthrough. |
11/04/2016 |
how-to |
|
maiak |
maiak |
mijacobs |
extensibility-integration |
You can customize the appearance of editor margins by using custom editor extensions. This walkthrough puts a custom glyph on the indicator margin whenever the word "todo" appears in a code comment.
-
Create a C# VSIX project. (In the New Project dialog, select Visual C# / Extensibility, then VSIX Project.) Name the solution
TodoGlyphTest
. -
Add an Editor Classifier project item. For more information, see Create an extension with an editor item template.
-
Delete the existing class files.
Define a glyph by running the xref:Microsoft.VisualStudio.Text.Editor.IGlyphFactory interface.
-
Add a class file and name it
TodoGlyphFactory
. -
Add the following code by using declarations.
:::code language="csharp" source="../snippets/csharp/VS_Snippets_VSSDK/vssdktodoglyphtest/cs/todoglyphfactory.cs" id="Snippet1":::
-
Add a class named
TodoGlyphFactory
that implements xref:Microsoft.VisualStudio.Text.Editor.IGlyphFactory.:::code language="csharp" source="../snippets/csharp/VS_Snippets_VSSDK/vssdktodoglyphtest/cs/todoglyphfactory.cs" id="Snippet2":::
-
Add a private field that defines the dimensions of the glyph.
:::code language="csharp" source="../snippets/csharp/VS_Snippets_VSSDK/vssdktodoglyphtest/cs/todoglyphfactory.cs" id="Snippet3":::
-
Implement
GenerateGlyph
by defining the glyph user interface (UI) element.TodoTag
is defined later in this walkthrough.:::code language="csharp" source="../snippets/csharp/VS_Snippets_VSSDK/vssdktodoglyphtest/cs/todoglyphfactory.cs" id="Snippet4":::
-
Add a class named
TodoGlyphFactoryProvider
that implements xref:Microsoft.VisualStudio.Text.Editor.IGlyphFactoryProvider. Export this class with a xref:Microsoft.VisualStudio.Utilities.NameAttribute of "TodoGlyph", an xref:Microsoft.VisualStudio.Utilities.OrderAttribute of After VsTextMarker, a xref:Microsoft.VisualStudio.Utilities.ContentTypeAttribute of "code", and a xref:Microsoft.VisualStudio.Text.Tagging.TagTypeAttribute of TodoTag.:::code language="csharp" source="../snippets/csharp/VS_Snippets_VSSDK/vssdktodoglyphtest/cs/todoglyphfactory.cs" id="Snippet5":::
-
Implement the xref:Microsoft.VisualStudio.Text.Editor.IGlyphFactoryProvider.GetGlyphFactory%2A method by instantiating the
TodoGlyphFactory
.:::code language="csharp" source="../snippets/csharp/VS_Snippets_VSSDK/vssdktodoglyphtest/cs/todoglyphfactory.cs" id="Snippet6":::
Define the relationship between the UI element that you defined in the previous steps and the indicator margin. Create a tag type and tagger and export it by using a tagger provider.
-
Add a new class file to the project and name it
TodoTagger
. -
Add the following imports.
:::code language="csharp" source="../snippets/csharp/VS_Snippets_VSSDK/vssdktodoglyphtest/cs/todotagger.cs" id="Snippet7":::
-
Add a class named
TodoTag
.:::code language="csharp" source="../snippets/csharp/VS_Snippets_VSSDK/vssdktodoglyphtest/cs/todotagger.cs" id="Snippet8":::
-
Modify the class named
TodoTagger
that implements xref:Microsoft.VisualStudio.Text.Tagging.ITagger%601 of typeTodoTag
.:::code language="csharp" source="../snippets/csharp/VS_Snippets_VSSDK/vssdktodoglyphtest/cs/todotagger.cs" id="Snippet9":::
-
To the
TodoTagger
class, add private fields for an xref:Microsoft.VisualStudio.Text.Classification.IClassifier and for the text to find in the classification spans.:::code language="csharp" source="../snippets/csharp/VS_Snippets_VSSDK/vssdktodoglyphtest/cs/todotagger.cs" id="Snippet10":::
-
Add a constructor that sets the classifier.
:::code language="csharp" source="../snippets/csharp/VS_Snippets_VSSDK/vssdktodoglyphtest/cs/todotagger.cs" id="Snippet11":::
-
Implement the xref:Microsoft.VisualStudio.Text.Tagging.ITagger%601.GetTags%2A method by finding all the classification spans whose names include the word "comment" and whose text includes the search text. Whenever the search text is found, yield back a new xref:Microsoft.VisualStudio.Text.Tagging.TagSpan%601 of type
TodoTag
.:::code language="csharp" source="../snippets/csharp/VS_Snippets_VSSDK/vssdktodoglyphtest/cs/todotagger.cs" id="Snippet12":::
-
Declare a
TagsChanged
event.:::code language="csharp" source="../snippets/csharp/VS_Snippets_VSSDK/vssdktodoglyphtest/cs/todotagger.cs" id="Snippet13":::
-
Add a class named
TodoTaggerProvider
that implements xref:Microsoft.VisualStudio.Text.Tagging.ITaggerProvider, and export it with a xref:Microsoft.VisualStudio.Utilities.ContentTypeAttribute of "code" and a xref:Microsoft.VisualStudio.Text.Tagging.TagTypeAttribute of TodoTag.:::code language="csharp" source="../snippets/csharp/VS_Snippets_VSSDK/vssdktodoglyphtest/cs/todotagger.cs" id="Snippet14":::
-
Import the xref:Microsoft.VisualStudio.Text.Classification.IClassifierAggregatorService.
:::code language="csharp" source="../snippets/csharp/VS_Snippets_VSSDK/vssdktodoglyphtest/cs/todotagger.cs" id="Snippet15":::
-
Implement the xref:Microsoft.VisualStudio.Text.Tagging.ITaggerProvider.CreateTagger%2A method by instantiating the
TodoTagger
.:::code language="csharp" source="../snippets/csharp/VS_Snippets_VSSDK/vssdktodoglyphtest/cs/todotagger.cs" id="Snippet16":::
To test this code, build the TodoGlyphTest solution and run it in the experimental instance.
-
Build the solution.
-
Run the project by pressing F5. A second instance of Visual Studio starts.
-
Make sure that the indicator margin is showing. (On the Tools menu, click Options. On the Text Editor page, make sure that Indicator margin is selected.)
-
Open a code file that has comments. Add the word "todo" to one of the comment sections.
-
A light blue circle with a dark blue outline appears in the indicator margin to the left of the code window.