Advertisement
Guest User

Simple VSIX test for VS2012

a guest
May 7th, 2013
307
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.82 KB | None | 0 0
  1. using Microsoft.VisualStudio.Language.StandardClassification;
  2. using Microsoft.VisualStudio.Text;
  3. using Microsoft.VisualStudio.Text.Classification;
  4. using Microsoft.VisualStudio.Utilities;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.ComponentModel.Composition;
  8.  
  9. namespace VSIXProject1
  10. {
  11.     internal static class MyLangLanguage
  12.     {
  13.         public const string ContentType = "mylang";
  14.  
  15.         public const string FileExtension = ".mylang";
  16.  
  17.         [Export(typeof(ClassificationTypeDefinition))]
  18.         [Name(ContentType)]
  19.         [BaseDefinition("code")]
  20.         internal static ContentTypeDefinition MyLangSyntaxContentTypeDefinition = null;
  21.  
  22.         [Export]
  23.         [FileExtension(FileExtension)]
  24.         [ContentType(ContentType)]
  25.         internal static FileExtensionToContentTypeDefinition MyLangSyntaxFileExtensionDefinition = null;
  26.     }
  27.  
  28.     [Export(typeof(IClassifierProvider))]
  29.     [ContentType(MyLangLanguage.ContentType)]
  30.     [Name("MyLangSyntaxProvider")]
  31.     internal sealed class MyLangSyntaxProvider : IClassifierProvider
  32.     {
  33.         [Import]
  34.         internal IClassificationTypeRegistryService ClassificationRegistry = null;
  35.  
  36.         public IClassifier GetClassifier(ITextBuffer buffer)
  37.         {
  38.             return buffer.Properties.GetOrCreateSingletonProperty(() => new MyLangSyntax(ClassificationRegistry, buffer));
  39.         }
  40.     }
  41.  
  42.     internal sealed class MyLangSyntax : IClassifier
  43.     {
  44.         private ITextBuffer buffer;
  45.         private IClassificationType identifierType;
  46.         private IClassificationType keywordType;
  47.  
  48.         public event EventHandler<ClassificationChangedEventArgs> ClassificationChanged;
  49.  
  50.         internal MyLangSyntax(IClassificationTypeRegistryService registry, ITextBuffer buffer)
  51.         {
  52.             this.identifierType = registry.GetClassificationType(PredefinedClassificationTypeNames.Identifier);
  53.             this.keywordType = registry.GetClassificationType(PredefinedClassificationTypeNames.Keyword);
  54.             this.buffer = buffer;
  55.             this.buffer.Changed += OnBufferChanged;
  56.         }
  57.  
  58.         public IList<ClassificationSpan> GetClassificationSpans(SnapshotSpan snapshotSpan)
  59.         {
  60.             var classifications = new List<ClassificationSpan>();
  61.  
  62.             string text = snapshotSpan.GetText();
  63.             var span = new SnapshotSpan(snapshotSpan.Snapshot, snapshotSpan.Start.Position, text.Length);
  64.             classifications.Add(new ClassificationSpan(span, keywordType));
  65.  
  66.             return classifications;
  67.         }
  68.  
  69.         private void OnBufferChanged(object sender, TextContentChangedEventArgs e)
  70.         {
  71.             foreach (var change in e.Changes)
  72.                 ClassificationChanged(this, new ClassificationChangedEventArgs(new SnapshotSpan(e.After, change.NewSpan)));
  73.         }
  74.     }
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement