SixPairs

July 15, 2014

Simpler VS Editor Classifier

Filed under: VS Editor — Ceyhun Ciper @ 20:18
namespace Ciper
{
  using EnvDTE;
  using EnvDTE80;
  using Microsoft.VisualStudio.Language.StandardClassification;
  using Microsoft.VisualStudio.Shell;
  using Microsoft.VisualStudio.Text;
  using Microsoft.VisualStudio.Text.Classification;
  using System;
  using System.ComponentModel.Composition;
 
  [Export(typeof(IClassifierProvider))]
  partial class SimpleClassifier : IClassifierProviderIClassifier
  {
    [Import]
    IClassificationTypeRegistryService classificationTypeRegistry = null;
 
    [Import]
    IStandardClassificationService standardClassifications = null;
 
    [Import]
    SVsServiceProvider serviceProvider = null;
 
    DTE2 dte { get { return (DTE2)serviceProvider.GetService(typeof(DTE)); } }
    OutputWindowPane outputWindowPane { get { return dte.ToolWindows.OutputWindow.ActivePane; } }
 
    public IClassifier GetClassifier(ITextBuffer textBuffer)
    {
      return this;
    }
 
    public event EventHandler<ClassificationChangedEventArgs> ClassificationChanged;
  }
}
 
namespace Ciper
{
  using Microsoft.VisualStudio.Text;
  using Microsoft.VisualStudio.Text.Classification;
  using Microsoft.VisualStudio.Utilities;
  using System.Collections.Generic;
  using System.ComponentModel.Composition;
  using System.Text.RegularExpressions;
 
  [ContentType("csharp")]
  partial class SimpleClassifier
  {
    [ExportName("kelime")]
    ClassificationTypeDefinition kelime;
 
    public IList<ClassificationSpan> GetClassificationSpans(SnapshotSpan span)
    {
      var l = new List<ClassificationSpan>();
      var text = span.GetText();
      foreach (Match m in Regex.Matches(text, @"\w+"))
      {
        var wordSpan = new SnapshotSpan(span.Start + m.Index, m.Length);
        l.Add(new ClassificationSpan(wordSpan, classificationTypeRegistry.GetClassificationType("kelime")));
      }
      return l;
    }
  }
}

Exercises

  1. “// re: ” ile baslayan her comment’in sag tarafini “re sample” olarak classify et.
    namespace Ciper
    {
      using Microsoft.VisualStudio.Text;
      using Microsoft.VisualStudio.Text.Classification;
      using Microsoft.VisualStudio.Utilities;
      using System.Collections.Generic;
      using System.ComponentModel.Composition;
      using System.Text.RegularExpressions;
     
      [ContentType("csharp")]
      partial class SimpleClassifier
      {
        [ExportName("re sample")]
        ClassificationTypeDefinition reSample;
     
        public IList<ClassificationSpan> GetClassificationSpans(SnapshotSpan span)
        {
          var l = new List<ClassificationSpan>();
          var text = span.GetText();
          foreach (Match m in Regex.Matches(text, @"//\s*re\:?\s*(.+)")) // Dikkat, burada re'yi $ ile bitirme!
          {
            var sampleSpan = new SnapshotSpan(span.Start + m.Groups[1].Index, m.Groups[1].Length);
            l.Add(new ClassificationSpan(sampleSpan, classificationTypeRegistry.GetClassificationType("re sample")));
          }
          return l;
        }
      }
    }
    
  2. IP ve email’leri classify et.
    namespace Ciper
    {
      using Microsoft.VisualStudio.Text;
      using Microsoft.VisualStudio.Text.Classification;
      using Microsoft.VisualStudio.Utilities;
      using System.Collections.Generic;
      using System.ComponentModel.Composition;
      using System.Text.RegularExpressions;
     
      [ContentType("csharp")]
      partial class SimpleClassifier
      {
        [ExportName("ip")]
        ClassificationTypeDefinition ip;
        [ExportName("email")]
        ClassificationTypeDefinition email;
     
        public IList<ClassificationSpan> GetClassificationSpans(SnapshotSpan span)
        {
          var l = new List<ClassificationSpan>();
          var text = span.GetText();
          foreach (Match m in Regex.Matches(text, @"\d+\.\d+\.\d+\.\d+"))
          {
            var ipSpan = new SnapshotSpan(span.Start + m.Index, m.Length);
            l.Add(new ClassificationSpan(ipSpan, classificationTypeRegistry.GetClassificationType("ip")));
          }
          foreach (Match m in Regex.Matches(text, @"\w+\@\w+\.\w+"))
          {
            var emailSpan = new SnapshotSpan(span.Start + m.Index, m.Length);
            l.Add(new ClassificationSpan(emailSpan, classificationTypeRegistry.GetClassificationType("email")));
          }
          return l;
        }
      }
    }
    

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.