Walkthrough: Creating a Margin Glyph

 

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: Creating a Margin Glyph.

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.

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.

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

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

  3. Delete the existing class files.

Define a glyph by implementing the IGlyphFactory interface.

To define the glyph

  1. Add a class file and name it TodoGlyphFactory.

  2. Add the following using declarations.

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

  3. Add a class named TodoGlyphFactory that implements IGlyphFactory.

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

  4. Add a private field that defines the dimensions of the glyph.

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

  5. Implement GenerateGlyph by defining the glyph user interface (UI) element. TodoTag is defined later in this walkthrough.

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

  6. Add a class named TodoGlyphFactoryProvider that implements IGlyphFactoryProvider. Export this class with a NameAttribute of "TodoGlyph", an OrderAttribute of After VsTextMarker, a ContentTypeAttribute of "code", and a TagTypeAttribute of TodoTag.

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

  7. Implement the GetGlyphFactory method by instantiating the TodoGlyphFactory.

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

Define the relationship between the UI element that you defined in the previous steps and the indicator margin by creating a tag type and tagger, and exporting it by using a tagger provider.

To define a todo tag and tagger

  1. Add a new class file to the project and name it TodoTagger.

  2. Add the following imports.

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

  3. Add a class named TodoTag.

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

  4. Modify the class named TodoTagger that implements ITagger<T> of type TodoTag.

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

  5. To the TodoTagger class, add private fields for an IClassifier and for the text to find in the classification spans.

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

  6. Add a constructor that sets the classifier.

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

  7. Implement the GetTags 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 TagSpan<T> of type TodoTag.

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

  8. Declare a TagsChanged event.

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

  9. Add a class named TodoTaggerProvider that implements ITaggerProvider, and export it with a ContentTypeAttribute of "code" and a TagTypeAttribute of TodoTag.

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

  10. Import the IClassifierAggregatorService.

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

  11. Implement the CreateTagger<T> method by instantiating the TodoTagger.

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

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

To build and test the TodoGlyphTest solution

  1. Build the solution.

  2. Run the project by pressing F5. A second instance of Visual Studio is instantiated.

  3. 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.)

  4. Open a code file that has comments. Add the word "todo" to one of the comment sections.

  5. A light blue circle that has a dark blue outline should appear in the indicator margin to the left of the code window.

Show: