SixPairs

July 10, 2014

ToDo Margin Glyph

Filed under: VS Editor — Ceyhun Ciper @ 02:16

From MSDN…

namespace Microsoft.VisualStudio.Text.Editor
{
  using Microsoft.VisualStudio.Text.Classification;
  using Microsoft.VisualStudio.Text.Formatting;
  using Microsoft.VisualStudio.Text.Tagging;
  using Microsoft.VisualStudio.Utilities;
  using System;
  using System.Collections.Generic;
  using System.ComponentModel.Composition;
  using System.Windows;
  using System.Windows.Media;
  using System.Windows.Shapes;
 
  [Export(typeof(IGlyphFactoryProvider))]
  [Name("TodoGlyph")]
  [Order(After = "VsTextMarker")]
  [ContentType("code")]
  [TagType(typeof(TodoTag))]
  internal sealed class TodoGlyphFactoryProvider : IGlyphFactoryProvider
  {
    public IGlyphFactory GetGlyphFactory(IWpfTextView view, IWpfTextViewMargin margin)
    {
      return new TodoGlyphFactory();
    }
  }
 
  internal class TodoGlyphFactory : IGlyphFactory
  {
    public UIElement GenerateGlyph(IWpfTextViewLine line, IGlyphTag tag)
    {
      if (tag == null || !(tag is TodoTag)) return null;
      return new Ellipse { Fill = Brushes.Blue, Height = 16, Width = 16 };
    }
  }
 
  internal class TodoTag : IGlyphTag { }
 
  internal class TodoTagger : ITagger<TodoTag>
  {
    private IClassifier m_classifier;
    private const string m_searchText = "todo";
    internal TodoTagger(IClassifier classifier)
    {
      m_classifier = classifier;
    }
 
    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());
            }
          }
        }
      }
    }
 
    public event EventHandler<SnapshotSpanEventArgs> TagsChanged;
  }
 
  [Export(typeof(ITaggerProvider))]
  [ContentType("code")]
  [TagType(typeof(TodoTag))]
  class TodoTaggerProvider : ITaggerProvider
  {
    [Import]
    internal IClassifierAggregatorService AggregatorService { getset; }
 
    public ITagger<T> CreateTagger<T>(ITextBuffer buffer) where T : ITag
    {
      return new TodoTagger(AggregatorService.GetClassifier(buffer)) as ITagger<T>;
    }
  }
}

Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a comment

Create a free website or blog at WordPress.com.