From 389995b9bc640e55bef5791979e334f6b5fe5864 Mon Sep 17 00:00:00 2001 From: WMJ Date: Sat, 1 Jun 2019 16:54:02 +0800 Subject: [PATCH] - Fixed a problem of comment tagger in previous commits --- Codist/Classifiers/CommentTagger.cs | 43 ++++++++++++----------------- Codist/Classifiers/TaggerResult.cs | 12 ++++++-- 2 files changed, 27 insertions(+), 28 deletions(-) diff --git a/Codist/Classifiers/CommentTagger.cs b/Codist/Classifiers/CommentTagger.cs index 5213bf13..2f4046ab 100644 --- a/Codist/Classifiers/CommentTagger.cs +++ b/Codist/Classifiers/CommentTagger.cs @@ -49,9 +49,9 @@ static CodeType GetCodeType(IContentType contentType) { void TextViewClosed(object sender, EventArgs args) { var textView = sender as ITextView; + textView.Closed -= TextViewClosed; textView.Properties.GetProperty>(typeof(ITagAggregator))?.Dispose(); textView.Properties.GetProperty(typeof(CommentTagger))?.Dispose(); - textView.Closed -= TextViewClosed; } abstract class CommentTagger : ITagger, IDisposable @@ -171,11 +171,11 @@ protected virtual TaggedContentSpan TagComments(SnapshotSpan snapshotSpan, IMapp } } - var endOfContent = GetCommentEndIndex(text); + var contentEnd = GetCommentEndIndex(text); ClassificationTag ctag = null; CommentLabel label = null; - var startOfContent = 0; + var contentStart = 0; foreach (var item in Config.Instance.Labels) { var c = commentStart + item.LabelLength; if (c >= tl @@ -194,26 +194,26 @@ protected virtual TaggedContentSpan TagComments(SnapshotSpan snapshotSpan, IMapp if (label == null || label.LabelLength < item.LabelLength) { ctag = __CommentClassifications[(int)item.StyleID]; label = item; - startOfContent = c; + contentStart = c; } } - if (startOfContent == 0 || ctag == null) { + if (contentStart == 0 || ctag == null) { return null; } // ignore whitespaces in content - while (startOfContent < tl) { - if (Char.IsWhiteSpace(text[startOfContent])) { - ++startOfContent; + while (contentStart < tl) { + if (Char.IsWhiteSpace(text[contentStart])) { + ++contentStart; } else { break; } } - while (endOfContent > startOfContent) { - if (Char.IsWhiteSpace(text[endOfContent - 1])) { - --endOfContent; + while (contentEnd > contentStart) { + if (Char.IsWhiteSpace(text[contentEnd - 1])) { + --contentEnd; } else { break; @@ -221,10 +221,10 @@ protected virtual TaggedContentSpan TagComments(SnapshotSpan snapshotSpan, IMapp } return label.StyleApplication == CommentStyleApplication.Tag - ? new TaggedContentSpan(snapshotSpan.Snapshot, ctag, snapshotSpan.Start + commentStart, label.LabelLength, startOfContent, endOfContent - startOfContent) + ? new TaggedContentSpan(snapshotSpan.Snapshot, ctag, snapshotSpan.Start + commentStart, label.LabelLength, contentStart - commentStart, contentEnd - contentStart) : label.StyleApplication == CommentStyleApplication.Content - ? new TaggedContentSpan(snapshotSpan.Snapshot, ctag, snapshotSpan.Start + startOfContent, endOfContent - startOfContent, startOfContent, endOfContent - startOfContent) - : new TaggedContentSpan(snapshotSpan.Snapshot, ctag, snapshotSpan.Start + commentStart, endOfContent - commentStart, startOfContent, endOfContent - startOfContent); + ? new TaggedContentSpan(snapshotSpan.Snapshot, ctag, snapshotSpan.Start + contentStart, contentEnd - contentStart, contentStart - commentStart, contentEnd - contentStart) + : new TaggedContentSpan(snapshotSpan.Snapshot, ctag, snapshotSpan.Start + commentStart, contentEnd - commentStart, contentStart - commentStart, contentEnd - contentStart); } protected static bool Matches(SnapshotSpan span, string text) { @@ -258,9 +258,7 @@ static bool IsComment(IClassificationType classification) { } void AggregatorBatchedTagsChanged(object sender, EventArgs args) { - if (Margin != null) { - Margin.InvalidateVisual(); - } + Margin?.InvalidateVisual(); } #region IDisposable Support @@ -313,6 +311,7 @@ protected override int GetCommentEndIndex(string comment) { sealed class CSharpCommentTagger : CommentTagger { readonly IClassificationType _PreprocessorKeyword; + TaggedContentSpan _PreviousSpan; public CSharpCommentTagger(IClassificationTypeRegistryService registry, ITagAggregator aggregator, TaggerResult tags) : base(registry, aggregator, tags) { _PreprocessorKeyword = registry.GetClassificationType("preprocessor keyword"); @@ -320,14 +319,8 @@ public CSharpCommentTagger(IClassificationTypeRegistryService registry, ITagAggr protected override TaggedContentSpan TagComments(SnapshotSpan snapshotSpan, IMappingTagSpan tagSpan) { if (Config.Instance.MarkerOptions.MatchFlags(MarkerOptions.CompilerDirective) && tagSpan.Tag.ClassificationType == _PreprocessorKeyword) { - return Matches(snapshotSpan, "region") - ? null - : Matches(snapshotSpan, "pragma") - ? new TaggedContentSpan(snapshotSpan.Snapshot, tagSpan.Tag, snapshotSpan.Start, snapshotSpan.Span.Length, 6, snapshotSpan.Span.Length - 6) - : Matches(snapshotSpan, "if") - ? new TaggedContentSpan(snapshotSpan.Snapshot, tagSpan.Tag, snapshotSpan.Start, snapshotSpan.Span.Length, 2, snapshotSpan.Span.Length - 2) - : Matches(snapshotSpan, "else") - ? new TaggedContentSpan(snapshotSpan.Snapshot, tagSpan.Tag, snapshotSpan.Start, snapshotSpan.Span.Length, 4, snapshotSpan.Span.Length - 4) + return Matches(snapshotSpan, "pragma") || Matches(snapshotSpan, "if") || Matches(snapshotSpan, "else") /*|| Matches(snapshotSpan, "region")*/ + ? new TaggedContentSpan(snapshotSpan.Snapshot, tagSpan.Tag, snapshotSpan.Start, snapshotSpan.Length, 0, 0) : null; } return base.TagComments(snapshotSpan, tagSpan); diff --git a/Codist/Classifiers/TaggerResult.cs b/Codist/Classifiers/TaggerResult.cs index 6b152431..6ef74c2e 100644 --- a/Codist/Classifiers/TaggerResult.cs +++ b/Codist/Classifiers/TaggerResult.cs @@ -44,11 +44,12 @@ sealed class TaggedContentSpan : ITagSpan public SnapshotSpan Span => new SnapshotSpan(TextSnapshot, Start, Length); public int Start { get; private set; } public int Length { get; } - public int ContentOffset { get; } - public int ContentLength { get; } + public int ContentOffset { get; private set; } + public int ContentLength { get; private set; } public int End => Start + Length; - public string Text => Span.GetText(); + public string Text => TextSnapshot.GetText(Start, Length); + public string ContentText => TextSnapshot.GetText(Start + ContentOffset, ContentLength); public ITextSnapshot TextSnapshot { get; } @@ -67,5 +68,10 @@ public bool Contains(int position) { public void Shift(int delta) { Start += delta; } + + public void SetContent(int start, int length) { + ContentOffset = start; + ContentLength = length; + } } }