From dc511b08615de73cf6c8e7d2cadacdc1c4e7f74e Mon Sep 17 00:00:00 2001 From: Mads Kristensen Date: Sat, 1 Jan 2022 13:51:07 -0800 Subject: [PATCH] Moved token taggers to base class [release] --- .../TokenClassificationTaggerBase.cs | 54 ------------- src/BaseClasses/TokenErrorTaggerBase.cs | 77 ------------------- src/BaseClasses/TokenOutliningTaggerBase.cs | 54 ------------- src/BaseClasses/TokenQuickInfoBase.cs | 67 ---------------- src/BaseClasses/TokenTag.cs | 34 -------- src/BaseClasses/TokenTaggerConsumerBase.cs | 63 --------------- src/Language/EditorFeatures.cs | 1 - src/Language/LanguageFactory.cs | 2 +- src/Language/TokenTagger.cs | 1 - .../PredefinedVariables.cs | 0 src/PkgdefLanguage.csproj | 10 +-- src/PkgdefPackage.cs | 2 +- src/source.extension.cs | 2 +- src/source.extension.vsixmanifest | 2 +- test/PkgdefLanguage.Test.csproj | 2 +- 15 files changed, 7 insertions(+), 364 deletions(-) delete mode 100644 src/BaseClasses/TokenClassificationTaggerBase.cs delete mode 100644 src/BaseClasses/TokenErrorTaggerBase.cs delete mode 100644 src/BaseClasses/TokenOutliningTaggerBase.cs delete mode 100644 src/BaseClasses/TokenQuickInfoBase.cs delete mode 100644 src/BaseClasses/TokenTag.cs delete mode 100644 src/BaseClasses/TokenTaggerConsumerBase.cs rename src/{Language => Parser}/PredefinedVariables.cs (100%) diff --git a/src/BaseClasses/TokenClassificationTaggerBase.cs b/src/BaseClasses/TokenClassificationTaggerBase.cs deleted file mode 100644 index 8a03922..0000000 --- a/src/BaseClasses/TokenClassificationTaggerBase.cs +++ /dev/null @@ -1,54 +0,0 @@ -using System.Collections.Generic; -using System.ComponentModel.Composition; -using Microsoft.VisualStudio.Text; -using Microsoft.VisualStudio.Text.Classification; -using Microsoft.VisualStudio.Text.Tagging; - -namespace BaseClasses -{ - public abstract class TokenClassificationTaggerBase : ITaggerProvider - { - [Import] internal IClassificationTypeRegistryService _classificationRegistry = null; - [Import] internal IBufferTagAggregatorFactoryService _bufferTagAggregator = null; - - public abstract Dictionary ClassificationMap { get; } - - public ITagger CreateTagger(ITextBuffer buffer) where T : ITag - { - ITagAggregator tags = _bufferTagAggregator.CreateTagAggregator(buffer); - return buffer.Properties.GetOrCreateSingletonProperty(() => new TokenClassifier(_classificationRegistry, tags, ClassificationMap)) as ITagger; - } - } - - internal class TokenClassifier : TokenTaggerConsumerBase - { - private static Dictionary _classificationMap; - - internal TokenClassifier(IClassificationTypeRegistryService registry, ITagAggregator tags, Dictionary map) : base(tags) - { - _classificationMap = new(); - - foreach (var key in map.Keys) - { - _classificationMap[key] = new ClassificationTag(registry.GetClassificationType(map[key])); - } - - } - - public override IEnumerable> GetTags(NormalizedSnapshotSpanCollection spans, bool isFullParse) - { - foreach (IMappingTagSpan tag in Tags.GetTags(spans)) - { - if (_classificationMap.TryGetValue(tag.Tag.TokenType, out ClassificationTag classificationTag)) - { - NormalizedSnapshotSpanCollection tagSpans = tag.Span.GetSpans(tag.Span.AnchorBuffer.CurrentSnapshot); - - foreach (SnapshotSpan tagSpan in tagSpans) - { - yield return new TagSpan(tagSpan, classificationTag); - } - } - } - } - } -} diff --git a/src/BaseClasses/TokenErrorTaggerBase.cs b/src/BaseClasses/TokenErrorTaggerBase.cs deleted file mode 100644 index 159199f..0000000 --- a/src/BaseClasses/TokenErrorTaggerBase.cs +++ /dev/null @@ -1,77 +0,0 @@ -using System; -using System.Collections.Generic; -using System.ComponentModel.Composition; -using System.Linq; -using Microsoft.VisualStudio.Text; -using Microsoft.VisualStudio.Text.Adornments; -using Microsoft.VisualStudio.Text.Tagging; - -namespace BaseClasses -{ - public abstract class TokenErrorTaggerBase : ITaggerProvider - { - [Import] internal IBufferTagAggregatorFactoryService _bufferTagAggregator = null; - - public ITagger CreateTagger(ITextBuffer buffer) where T : ITag - { - ITagAggregator tags = _bufferTagAggregator.CreateTagAggregator(buffer); - return buffer.Properties.GetOrCreateSingletonProperty(() => new ErrorTagger(tags)) as ITagger; - } - } - - public class ErrorTagger : TokenTaggerConsumerBase - { - private readonly TableDataSource _dataSource; - - public ErrorTagger(ITagAggregator tags) : base(tags) - { - _dataSource = new TableDataSource(tags.BufferGraph.TopBuffer.ContentType.DisplayName); - } - - public override IEnumerable> GetTags(NormalizedSnapshotSpanCollection spans, bool isFullParse) - { - IEnumerable> tags = Tags.GetTags(spans).Where(t => !t.Tag.IsValid); - - foreach (IMappingTagSpan tag in tags) - { - NormalizedSnapshotSpanCollection tagSpans = tag.Span.GetSpans(tag.Span.AnchorBuffer.CurrentSnapshot); - var tooltip = string.Join(Environment.NewLine, tag.Tag.Errors); - var errorTag = new ErrorTag(PredefinedErrorTypeNames.SyntaxError, tooltip); - - foreach (SnapshotSpan span in tagSpans) - { - yield return new TagSpan(span, errorTag); - } - } - - if (isFullParse) - { - PopulateErrorList(tags); - } - } - - private void PopulateErrorList(IEnumerable> tags) - { - IEnumerable errors = tags.SelectMany(t => t.Tag.Errors); - - if (!errors.Any()) - { - _dataSource.CleanAllErrors(); - } - else - { - _dataSource.AddErrors(errors); - } - } - - public override void Dispose(bool disposing) - { - if (disposing) - { - _dataSource.CleanAllErrors(); - } - - base.Dispose(disposing); - } - } -} diff --git a/src/BaseClasses/TokenOutliningTaggerBase.cs b/src/BaseClasses/TokenOutliningTaggerBase.cs deleted file mode 100644 index 5827de2..0000000 --- a/src/BaseClasses/TokenOutliningTaggerBase.cs +++ /dev/null @@ -1,54 +0,0 @@ -using System.Collections.Generic; -using System.ComponentModel.Composition; -using System.Linq; -using Microsoft.VisualStudio.Text; -using Microsoft.VisualStudio.Text.Adornments; -using Microsoft.VisualStudio.Text.Tagging; - -namespace BaseClasses -{ - public abstract class TokenOutliningTaggerBase : ITaggerProvider - { - [Import] internal IBufferTagAggregatorFactoryService _bufferTagAggregator = null; - - public ITagger CreateTagger(ITextBuffer buffer) where T : ITag - { - ITagAggregator tags = _bufferTagAggregator.CreateTagAggregator(buffer); - return buffer.Properties.GetOrCreateSingletonProperty(() => new StructureTagger(tags)) as ITagger; - } - } - - internal class StructureTagger : TokenTaggerConsumerBase - { - public StructureTagger(ITagAggregator tags) : base(tags) - { } - - public override IEnumerable> GetTags(NormalizedSnapshotSpanCollection spans, bool isFullParse) - { - foreach (IMappingTagSpan tag in Tags.GetTags(spans).Where(t => t.Tag.SupportOutlining)) - { - NormalizedSnapshotSpanCollection tagSpans = tag.Span.GetSpans(tag.Span.AnchorBuffer.CurrentSnapshot); - - foreach (SnapshotSpan tagSpan in tagSpans) - { - yield return CreateTag(tagSpan, tagSpan.GetText().Trim()); - } - } - } - - private static TagSpan CreateTag(SnapshotSpan span, string text) - { - var structureTag = new StructureTag( - span.Snapshot, - outliningSpan: span, - guideLineSpan: span, - guideLineHorizontalAnchor: span.Start, - type: PredefinedStructureTagTypes.Structural, - isCollapsible: true, - collapsedForm: text, - collapsedHintForm: null); - - return new TagSpan(span, structureTag); - } - } -} diff --git a/src/BaseClasses/TokenQuickInfoBase.cs b/src/BaseClasses/TokenQuickInfoBase.cs deleted file mode 100644 index 986a4ca..0000000 --- a/src/BaseClasses/TokenQuickInfoBase.cs +++ /dev/null @@ -1,67 +0,0 @@ -using System.ComponentModel.Composition; -using System.Linq; -using System.Threading; -using System.Threading.Tasks; -using Microsoft.VisualStudio.Language.Intellisense; -using Microsoft.VisualStudio.Text; -using Microsoft.VisualStudio.Text.Adornments; -using Microsoft.VisualStudio.Text.Tagging; - -namespace BaseClasses -{ - internal abstract class TokenQuickInfoBase : IAsyncQuickInfoSourceProvider - { - [Import] internal IBufferTagAggregatorFactoryService _bufferTagAggregator = null; - - public IAsyncQuickInfoSource TryCreateQuickInfoSource(ITextBuffer buffer) - { - ITagAggregator tags = _bufferTagAggregator.CreateTagAggregator(buffer); - return buffer.Properties.GetOrCreateSingletonProperty(() => new TokenQuickInfo(tags)); - } - } - - internal sealed class TokenQuickInfo : IAsyncQuickInfoSource - { - private readonly ITextBuffer _buffer; - private readonly ITagAggregator _tags; - - public TokenQuickInfo(ITagAggregator tags) - { - _buffer = tags.BufferGraph.TopBuffer; - _tags = tags; - } - - public async Task GetQuickInfoItemAsync(IAsyncQuickInfoSession session, CancellationToken cancellationToken) - { - SnapshotPoint? triggerPoint = session.GetTriggerPoint(_buffer.CurrentSnapshot); - - if (triggerPoint.HasValue) - { - var span = new SnapshotSpan(triggerPoint.Value.Snapshot, triggerPoint.Value.Position, 0); - IMappingTagSpan tag = _tags.GetTags(span).FirstOrDefault(t => t.Tag.GetTooltipAsync != null); - - if (tag != null) - { - var tooltip = await tag.Tag.GetTooltipAsync(triggerPoint.Value); - - if (tooltip == null) - { - return null; - } - - var container = new ContainerElement(ContainerElementStyle.Stacked, tooltip); - ITrackingSpan applicapleTo = _buffer.CurrentSnapshot.CreateTrackingSpan(tag.Span.GetSpans(_buffer)[0], SpanTrackingMode.EdgeExclusive); - - return new QuickInfoItem(applicapleTo, container); - } - } - - return null; - } - - public void Dispose() - { - // This provider does not perform any cleanup. - } - } -} diff --git a/src/BaseClasses/TokenTag.cs b/src/BaseClasses/TokenTag.cs deleted file mode 100644 index a094004..0000000 --- a/src/BaseClasses/TokenTag.cs +++ /dev/null @@ -1,34 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; -using Microsoft.VisualStudio.Text; -using Microsoft.VisualStudio.Text.Tagging; - -namespace BaseClasses -{ - public class TokenTag : ITag - { - public TokenTag(object tokenType, bool supportOutlining, Func> getTooltipAsync, params ErrorListItem[] errors) - { - TokenType = tokenType; - SupportOutlining = supportOutlining; - GetTooltipAsync = getTooltipAsync; - Errors = errors; - } - - public TokenTag(object tokenType) - : this(tokenType, false, null) - { } - - public TokenTag(object tokenType, bool supportOutlining) - : this(tokenType, supportOutlining, null) - { } - - public virtual object TokenType { get; set; } - public virtual bool SupportOutlining { get; set; } - public virtual IList Errors { get; set; } - public virtual bool IsValid => Errors?.Any() == false; - public virtual Func> GetTooltipAsync { get; set; } - } -} diff --git a/src/BaseClasses/TokenTaggerConsumerBase.cs b/src/BaseClasses/TokenTaggerConsumerBase.cs deleted file mode 100644 index ca64032..0000000 --- a/src/BaseClasses/TokenTaggerConsumerBase.cs +++ /dev/null @@ -1,63 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using Microsoft.VisualStudio.Text; -using Microsoft.VisualStudio.Text.Tagging; - -namespace BaseClasses -{ - public abstract class TokenTaggerConsumerBase : ITagger, IDisposable where TTag : ITag - { - private bool _isDisposed; - - public TokenTaggerConsumerBase(ITagAggregator tags) - { - Tags = tags; - Tags.TagsChanged += TokenTagsChanged; - } - - public ITagAggregator Tags { get; } - - private void TokenTagsChanged(object sender, TagsChangedEventArgs e) - { - ITextBuffer buffer = e.Span.BufferGraph.TopBuffer; - SnapshotSpan span = new(buffer.CurrentSnapshot, 0, buffer.CurrentSnapshot.Length); - - TagsChanged?.Invoke(this, new SnapshotSpanEventArgs(span)); - } - - public IEnumerable> GetTags(NormalizedSnapshotSpanCollection spans) - { - if (spans[0].IsEmpty) - { - return null; - } - - var isFullParse = spans.First().Start == 0 && spans.Last().End == spans[0].Snapshot.Length; - return GetTags(spans, isFullParse); - } - - public abstract IEnumerable> GetTags(NormalizedSnapshotSpanCollection spans, bool isFullParse); - - public virtual void Dispose(bool disposing) - { - if (!_isDisposed) - { - if (disposing) - { - Tags.TagsChanged -= TokenTagsChanged; - } - - _isDisposed = true; - } - } - - public void Dispose() - { - Dispose(true); - GC.SuppressFinalize(this); - } - - public event EventHandler TagsChanged; - } -} diff --git a/src/Language/EditorFeatures.cs b/src/Language/EditorFeatures.cs index 1260c8c..ab4cc6c 100644 --- a/src/Language/EditorFeatures.cs +++ b/src/Language/EditorFeatures.cs @@ -1,6 +1,5 @@ using System.Collections.Generic; using System.ComponentModel.Composition; -using BaseClasses; using Microsoft.VisualStudio.Language.Intellisense; using Microsoft.VisualStudio.Language.Intellisense.AsyncCompletion; using Microsoft.VisualStudio.Language.StandardClassification; diff --git a/src/Language/LanguageFactory.cs b/src/Language/LanguageFactory.cs index 937b59f..446d515 100644 --- a/src/Language/LanguageFactory.cs +++ b/src/Language/LanguageFactory.cs @@ -34,7 +34,7 @@ public override void SetDefaultPreferences(LanguagePreferences preferences) preferences.IndentStyle = IndentingStyle.Smart; preferences.ShowNavigationBar = false; - preferences.WordWrap = false; + preferences.WordWrap = true; preferences.WordWrapGlyphs = true; preferences.AutoListMembers = true; diff --git a/src/Language/TokenTagger.cs b/src/Language/TokenTagger.cs index dd017d9..8f8e2cc 100644 --- a/src/Language/TokenTagger.cs +++ b/src/Language/TokenTagger.cs @@ -3,7 +3,6 @@ using System.ComponentModel.Composition; using System.Linq; using System.Threading.Tasks; -using BaseClasses; using Microsoft.VisualStudio.Core.Imaging; using Microsoft.VisualStudio.Imaging; using Microsoft.VisualStudio.Shell; diff --git a/src/Language/PredefinedVariables.cs b/src/Parser/PredefinedVariables.cs similarity index 100% rename from src/Language/PredefinedVariables.cs rename to src/Parser/PredefinedVariables.cs diff --git a/src/PkgdefLanguage.csproj b/src/PkgdefLanguage.csproj index c23b44c..1a7abaa 100644 --- a/src/PkgdefLanguage.csproj +++ b/src/PkgdefLanguage.csproj @@ -69,7 +69,7 @@ - 16.0.361 + 16.0.365 compile; build; native; contentfiles; analyzers; buildtransitive @@ -79,23 +79,17 @@ - - - + - - - - diff --git a/src/PkgdefPackage.cs b/src/PkgdefPackage.cs index 2390ae5..e40b5ee 100644 --- a/src/PkgdefPackage.cs +++ b/src/PkgdefPackage.cs @@ -14,7 +14,7 @@ namespace PkgdefLanguage [Guid(PackageGuids.PkgdefLanguageString)] [ProvideMenuResource("Menus.ctmenu", 1)] - [ProvideLanguageService(typeof(LanguageFactory), Constants.LanguageName, 0, EnableLineNumbers = true, MatchBraces = true, MatchBracesAtCaret = true, EnableAsyncCompletion = true, EnableCommenting = true, ShowCompletion = true, ShowMatchingBrace = true)] + [ProvideLanguageService(typeof(LanguageFactory), Constants.LanguageName, 0, DefaultToInsertSpaces = true, EnableLineNumbers = true, MatchBraces = true, MatchBracesAtCaret = true, EnableAsyncCompletion = true, EnableCommenting = true, ShowCompletion = true, ShowMatchingBrace = true)] [ProvideLanguageExtension(typeof(LanguageFactory), Constants.PkgDefExt)] [ProvideLanguageExtension(typeof(LanguageFactory), Constants.PkgUndefExt)] [ProvideFileIcon(Constants.PkgDefExt, "KnownMonikers.RegistrationScript")] diff --git a/src/source.extension.cs b/src/source.extension.cs index 8e741bf..5f11414 100644 --- a/src/source.extension.cs +++ b/src/source.extension.cs @@ -11,7 +11,7 @@ internal sealed partial class Vsix public const string Name = "Pkgdef Language"; public const string Description = @"Basic language support for .pkgdef and .pkgundef files"; public const string Language = "en-US"; - public const string Version = "1.2"; + public const string Version = "1.2.999"; public const string Author = "Mads Kristensen"; public const string Tags = "vsix, pkgdef, textmate"; } diff --git a/src/source.extension.vsixmanifest b/src/source.extension.vsixmanifest index 7c31800..32d6c5c 100644 --- a/src/source.extension.vsixmanifest +++ b/src/source.extension.vsixmanifest @@ -1,7 +1,7 @@  - + Pkgdef Language Basic language support for .pkgdef and .pkgundef files https://github.com/madskristensen/PkgdefLanguage diff --git a/test/PkgdefLanguage.Test.csproj b/test/PkgdefLanguage.Test.csproj index 3b98acb..a1a8eee 100644 --- a/test/PkgdefLanguage.Test.csproj +++ b/test/PkgdefLanguage.Test.csproj @@ -7,7 +7,7 @@ - +