Skip to content

Commit

Permalink
- Fixed a problem of comment tagger in previous commits
Browse files Browse the repository at this point in the history
  • Loading branch information
wmjordan committed Jun 1, 2019
1 parent 682b6c8 commit 389995b
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 28 deletions.
43 changes: 18 additions & 25 deletions Codist/Classifiers/CommentTagger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<ITagAggregator<IClassificationTag>>(typeof(ITagAggregator<IClassificationTag>))?.Dispose();
textView.Properties.GetProperty<CommentTagger>(typeof(CommentTagger))?.Dispose();
textView.Closed -= TextViewClosed;
}

abstract class CommentTagger : ITagger<IClassificationTag>, IDisposable
Expand Down Expand Up @@ -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
Expand All @@ -194,37 +194,37 @@ 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;
}
}

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) {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -313,21 +311,16 @@ protected override int GetCommentEndIndex(string comment) {
sealed class CSharpCommentTagger : CommentTagger
{
readonly IClassificationType _PreprocessorKeyword;
TaggedContentSpan _PreviousSpan;

public CSharpCommentTagger(IClassificationTypeRegistryService registry, ITagAggregator<IClassificationTag> aggregator, TaggerResult tags) : base(registry, aggregator, tags) {
_PreprocessorKeyword = registry.GetClassificationType("preprocessor keyword");
}
protected override TaggedContentSpan TagComments(SnapshotSpan snapshotSpan, IMappingTagSpan<IClassificationTag> 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);
Expand Down
12 changes: 9 additions & 3 deletions Codist/Classifiers/TaggerResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,12 @@ sealed class TaggedContentSpan : ITagSpan<IClassificationTag>
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; }

Expand All @@ -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;
}
}
}

0 comments on commit 389995b

Please sign in to comment.