added some development tools
[windows-sources.git] / developer / VSSDK / Samples / Ook_Language_Integration / C# / Classification / OokClassifier.cs
blobcbc6cb09dd9e31b9dd91e01a03ae928ca16a917c
1 //***************************************************************************
2 //
3 // Copyright (c) Microsoft Corporation. All rights reserved.
4 // This code is licensed under the Visual Studio SDK license terms.
5 // THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
6 // ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
7 // IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
8 // PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
9 //
10 //***************************************************************************
12 namespace OokLanguage
14 using System;
15 using System.Collections.Generic;
16 using System.ComponentModel.Composition;
17 using Microsoft.VisualStudio.Text;
18 using Microsoft.VisualStudio.Text.Classification;
19 using Microsoft.VisualStudio.Text.Editor;
20 using Microsoft.VisualStudio.Text.Tagging;
21 using Microsoft.VisualStudio.Utilities;
23 [Export(typeof(ITaggerProvider))]
24 [ContentType("ook!")]
25 [TagType(typeof(ClassificationTag))]
26 internal sealed class OokClassifierProvider : ITaggerProvider
29 [Export]
30 [Name("ook!")]
31 [BaseDefinition("code")]
32 internal static ContentTypeDefinition OokContentType = null;
34 [Export]
35 [FileExtension(".ook")]
36 [ContentType("ook!")]
37 internal static FileExtensionToContentTypeDefinition OokFileType = null;
39 [Import]
40 internal IClassificationTypeRegistryService ClassificationTypeRegistry = null;
42 [Import]
43 internal IBufferTagAggregatorFactoryService aggregatorFactory = null;
45 public ITagger<T> CreateTagger<T>(ITextBuffer buffer) where T : ITag
48 ITagAggregator<OokTokenTag> ookTagAggregator =
49 aggregatorFactory.CreateTagAggregator<OokTokenTag>(buffer);
51 return new OokClassifier(buffer, ookTagAggregator, ClassificationTypeRegistry) as ITagger<T>;
55 internal sealed class OokClassifier : ITagger<ClassificationTag>
57 ITextBuffer _buffer;
58 ITagAggregator<OokTokenTag> _aggregator;
59 IDictionary<OokTokenTypes, IClassificationType> _ookTypes;
61 /// <summary>
62 /// Construct the classifier and define search tokens
63 /// </summary>
64 internal OokClassifier(ITextBuffer buffer,
65 ITagAggregator<OokTokenTag> ookTagAggregator,
66 IClassificationTypeRegistryService typeService)
68 _buffer = buffer;
69 _aggregator = ookTagAggregator;
70 _ookTypes = new Dictionary<OokTokenTypes, IClassificationType>();
71 _ookTypes[OokTokenTypes.OokExclamation] = typeService.GetClassificationType("ook!");
72 _ookTypes[OokTokenTypes.OokPeriod] = typeService.GetClassificationType("ook.");
73 _ookTypes[OokTokenTypes.OokQuestion] = typeService.GetClassificationType("ook?");
76 public event EventHandler<SnapshotSpanEventArgs> TagsChanged
78 add { }
79 remove { }
82 /// <summary>
83 /// Search the given span for any instances of classified tags
84 /// </summary>
85 public IEnumerable<ITagSpan<ClassificationTag>> GetTags(NormalizedSnapshotSpanCollection spans)
87 foreach (var tagSpan in _aggregator.GetTags(spans))
89 var tagSpans = tagSpan.Span.GetSpans(spans[0].Snapshot);
90 yield return
91 new TagSpan<ClassificationTag>(tagSpans[0],
92 new ClassificationTag(_ookTypes[tagSpan.Tag.type]));