Merlin32Language/Coloring/Classification/Merlin32Classifier.cs

78 lines
2.8 KiB
C#
Raw Normal View History

2017-10-15 18:47:30 +00:00
using Microsoft.VisualStudio.Text;
2016-12-31 00:46:52 +00:00
using Microsoft.VisualStudio.Text.Classification;
using Microsoft.VisualStudio.Text.Tagging;
using Microsoft.VisualStudio.Utilities;
2017-10-15 18:47:30 +00:00
using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
2016-12-31 00:46:52 +00:00
namespace VSMerlin32.Coloring.Classification
{
[Export(typeof(ITaggerProvider))]
[ContentType("Merlin32")]
[TagType(typeof(ClassificationTag))]
internal sealed class Merlin32ClassifierProvider : ITaggerProvider
{
[Export]
[Name("Merlin32")]
[BaseDefinition("code")]
internal static ContentTypeDefinition Merlin32ContentType = null;
[Export]
[FileExtension(".s")]
[ContentType("Merlin32")]
internal static FileExtensionToContentTypeDefinition Merlin32FileType = null;
[Import]
internal IClassificationTypeRegistryService ClassificationTypeRegistry = null;
[Import]
2017-01-15 22:44:43 +00:00
internal IBufferTagAggregatorFactoryService AggregatorFactory = null;
2016-12-31 00:46:52 +00:00
public ITagger<T> CreateTagger<T>(ITextBuffer buffer) where T : ITag
{
2017-01-15 22:44:43 +00:00
ITagAggregator<Merlin32TokenTag> merlin32TagAggregator =
AggregatorFactory.CreateTagAggregator<Merlin32TokenTag>(buffer);
2016-12-31 00:46:52 +00:00
2017-01-15 22:44:43 +00:00
return new Merlin32Classifier(buffer, merlin32TagAggregator, ClassificationTypeRegistry) as ITagger<T>;
2016-12-31 00:46:52 +00:00
}
}
internal sealed class Merlin32Classifier : ITagger<ClassificationTag>
{
2017-01-15 22:44:43 +00:00
private ITextBuffer _buffer;
private readonly ITagAggregator<Merlin32TokenTag> _aggregator;
private readonly IDictionary<Merlin32TokenTypes, IClassificationType> _merlin32Types;
2016-12-31 00:46:52 +00:00
internal Merlin32Classifier(ITextBuffer buffer,
2017-01-15 22:44:43 +00:00
ITagAggregator<Merlin32TokenTag> merlin32TagAggregator,
2016-12-31 00:46:52 +00:00
IClassificationTypeRegistryService typeService)
{
_buffer = buffer;
2017-01-15 22:44:43 +00:00
_aggregator = merlin32TagAggregator;
_merlin32Types = new Dictionary<Merlin32TokenTypes, IClassificationType>();
2016-12-31 00:46:52 +00:00
foreach (Merlin32TokenTypes token in Enum.GetValues(typeof(Merlin32TokenTypes)))
2017-10-15 18:47:30 +00:00
{
2017-01-15 22:44:43 +00:00
_merlin32Types[token] = typeService.GetClassificationType(token.ToString());
2017-10-15 18:47:30 +00:00
}
2016-12-31 00:46:52 +00:00
}
public event EventHandler<SnapshotSpanEventArgs> TagsChanged
{
add { }
remove { }
}
public IEnumerable<ITagSpan<ClassificationTag>> GetTags(NormalizedSnapshotSpanCollection spans)
{
2017-01-15 22:44:43 +00:00
foreach (var tagSpan in _aggregator.GetTags(spans))
2016-12-31 00:46:52 +00:00
{
var tagSpans = tagSpan.Span.GetSpans(spans[0].Snapshot);
2017-01-15 22:44:43 +00:00
yield return
new TagSpan<ClassificationTag>(tagSpans[0], new ClassificationTag(_merlin32Types[tagSpan.Tag.Tokentype]));
2016-12-31 00:46:52 +00:00
}
}
}
}