Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
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 MEF project
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 the glyph
Define a glyph by running the IGlyphFactory interface.
To define the glyph
Add a class file and name it
TodoGlyphFactory
.Add the following code by using declarations.
using System.ComponentModel.Composition; using System.Windows; using System.Windows.Shapes; using System.Windows.Media; using System.Windows.Controls; using Microsoft.VisualStudio.Text; using Microsoft.VisualStudio.Text.Editor; using Microsoft.VisualStudio.Text.Formatting; using Microsoft.VisualStudio.Text.Tagging; using Microsoft.VisualStudio.Utilities;
Add a class named
TodoGlyphFactory
that implements IGlyphFactory.Add a private field that defines the dimensions of the glyph.
Implement
GenerateGlyph
by defining the glyph user interface (UI) element.TodoTag
is defined later in this walkthrough.public UIElement GenerateGlyph(IWpfTextViewLine line, IGlyphTag tag) { // Ensure we can draw a glyph for this marker. if (tag == null || !(tag is TodoTag)) { return null; } System.Windows.Shapes.Ellipse ellipse = new Ellipse(); ellipse.Fill = Brushes.LightBlue; ellipse.StrokeThickness = 2; ellipse.Stroke = Brushes.DarkBlue; ellipse.Height = m_glyphSize; ellipse.Width = m_glyphSize; return ellipse; }
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.Implement the GetGlyphFactory method by instantiating the
TodoGlyphFactory
.
Define a Todo tag and tagger
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.
To define a todo tag and tagger
Add a new class file to the project and name it
TodoTagger
.Add the following imports.
using System; using System.Collections.Generic; using System.ComponentModel.Composition; using Microsoft.VisualStudio.Text; using Microsoft.VisualStudio.Text.Tagging; using Microsoft.VisualStudio.Text.Editor; using Microsoft.VisualStudio.Text.Classification; using Microsoft.VisualStudio.Utilities;
Add a class named
TodoTag
.Modify the class named
TodoTagger
that implements ITagger<T> of typeTodoTag
.To the
TodoTagger
class, add private fields for an IClassifier and for the text to find in the classification spans.Add a constructor that sets the classifier.
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
.IEnumerable<ITagSpan<TodoTag>> ITagger<TodoTag>.GetTags(NormalizedSnapshotSpanCollection spans) { foreach (SnapshotSpan span in spans) { //look at each classification span \ foreach (ClassificationSpan classification in m_classifier.GetClassificationSpans(span)) { //if the classification is a comment if (classification.ClassificationType.Classification.ToLower().Contains("comment")) { //if the word "todo" is in the comment, //create a new TodoTag TagSpan int index = classification.Span.GetText().ToLower().IndexOf(m_searchText); if (index != -1) { yield return new TagSpan<TodoTag>(new SnapshotSpan(classification.Span.Start + index, m_searchText.Length), new TodoTag()); } } } } }
Declare a
TagsChanged
event.Add a class named
TodoTaggerProvider
that implements ITaggerProvider, and export it with a ContentTypeAttribute of "code" and a TagTypeAttribute of TodoTag.Import the IClassifierAggregatorService.
Implement the CreateTagger method by instantiating the
TodoTagger
.
Build and test the code
To test this code, build the TodoGlyphTest solution and run it in the experimental instance.
To build and test the TodoGlyphTest solution
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.