SixPairs

July 9, 2014

Simplest WpfTextViewCreationListener

Filed under: VS Editor — Ceyhun Ciper @ 17:32
namespace Ciper
{
  using Microsoft.VisualStudio.Text;
  using Microsoft.VisualStudio.Text.Classification;
  using Microsoft.VisualStudio.Text.Editor;
  using System.Collections.Generic;
  using System.ComponentModel.Composition;
  using System.Linq;
 
  [Export(typeof(IWpfTextViewCreationListener)), TextViewRole(PredefinedTextViewRoles.Interactive)]
  public partial class WpfTextViewCreationListener : IWpfTextViewCreationListener
  {
    IWpfTextView view;
    IClassifier classifier;
    ITextSnapshot snapshot { get { return view.TextSnapshot; } }
 
    [Import]
    IViewClassifierAggregatorService classifierAggregator { getset; }
 
    SnapshotSpan GetSnapshotSpan()
    {
      return new SnapshotSpan(snapshot, Span.FromBounds(0, snapshot.Length));
    }
 
    IEnumerable<ClassificationSpan> GetClassificationSpans()
    {
      return classifier.GetClassificationSpans(GetSnapshotSpan());
    }
 
    IEnumerable<ClassificationSpan> GetClassificationSpans(string type)
    {
      return GetClassificationSpans().Where(s => s.ClassificationType.IsOfType(type));
    }
 
    public void TextViewCreated(IWpfTextView textView)
    {
      view = textView;
      classifier = classifierAggregator.GetClassifier(view);
    }
  }
}
 
namespace Ciper
{
  using Microsoft.VisualStudio.Utilities;
 
  [ContentType("csharp")]
  partial class WpfTextViewCreationListener
  {
  }
}

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.