Skip to content

Commit 5459846

Browse files
Merge pull request #261 from X-Sharp/master
Added methods and property to SameWordHighlighterBase to allow to cus…
2 parents dc848b0 + 872c873 commit 5459846

File tree

1 file changed

+33
-5
lines changed

1 file changed

+33
-5
lines changed

src/toolkit/Community.VisualStudio.Toolkit.Shared/MEF/SameWordHighlighterBase.cs

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,32 @@ public class SameWordHighlighterBase : IViewTaggerProvider
1919

2020
[Import] internal ITextStructureNavigatorSelectorService? _textStructureNavigatorSelector = null;
2121

22+
/// <summary>
23+
/// The Options that are used to find the matching words. The default implementation returns
24+
/// FindOptions.WholeWord | FindOptions.MatchCase
25+
/// </summary>
26+
public virtual FindOptions FindOptions => FindOptions.WholeWord | FindOptions.MatchCase;
27+
/// <summary>
28+
/// Filter the results.
29+
/// </summary>
30+
/// <param name="results">Collection of the results</param>
31+
/// <returns>Filtered list of results. The default implementation returns all the results</returns>
32+
public virtual IEnumerable<SnapshotSpan>? FilterResults(IEnumerable<SnapshotSpan>? results) => results;
33+
/// <summary>
34+
/// Should the Highlight code be triggered for this word
35+
/// </summary>
36+
/// <param name="text">The word to highlight</param>
37+
/// <returns>true to continue the highlight or false to prevent the highlight.
38+
/// The default implementation always returns true.</returns>
39+
public virtual bool ShouldHighlight(string? text) => true;
40+
2241
/// <inheritdoc/>
2342
public ITagger<T> CreateTagger<T>(ITextView textView, ITextBuffer buffer) where T : ITag
2443
{
2544
ITextStructureNavigator? navigator = _textStructureNavigatorSelector?.GetTextStructureNavigator(textView.TextBuffer);
2645

27-
return (ITagger<T>)buffer.Properties.GetOrCreateSingletonProperty(() => new SameWordHighlighterTagger(textView, buffer, _textSearchService, navigator));
46+
return (ITagger<T>)buffer.Properties.GetOrCreateSingletonProperty(() =>
47+
new SameWordHighlighterTagger(textView, buffer, _textSearchService, navigator, this));
2848
}
2949
}
3050

@@ -39,18 +59,21 @@ internal class SameWordHighlighterTagger : ITagger<HighlightWordTag>, IDisposabl
3959
private readonly ITextBuffer _buffer;
4060
private readonly ITextSearchService? _textSearchService;
4161
private readonly ITextStructureNavigator? _textStructureNavigator;
62+
private readonly SameWordHighlighterBase _tagger;
4263
private NormalizedSnapshotSpanCollection _wordSpans;
4364
private SnapshotSpan? _currentWord;
4465
private SnapshotPoint _requestedPoint;
4566
private bool _isDisposed;
4667
private readonly object _syncLock = new();
4768

48-
public SameWordHighlighterTagger(ITextView view, ITextBuffer sourceBuffer, ITextSearchService? textSearchService, ITextStructureNavigator? textStructureNavigator)
69+
public SameWordHighlighterTagger(ITextView view, ITextBuffer sourceBuffer, ITextSearchService? textSearchService,
70+
ITextStructureNavigator? textStructureNavigator, SameWordHighlighterBase tagger)
4971
{
5072
_view = view;
5173
_buffer = sourceBuffer;
5274
_textSearchService = textSearchService;
5375
_textStructureNavigator = textStructureNavigator;
76+
_tagger = tagger;
5477
_wordSpans = new NormalizedSnapshotSpanCollection();
5578
_currentWord = null;
5679
_view.Caret.PositionChanged += CaretPositionChanged;
@@ -96,12 +119,17 @@ private void UpdateWordAdornments(TextExtent word)
96119
SnapshotPoint currentRequest = _requestedPoint;
97120
List<SnapshotSpan>? wordSpans = new();
98121

99-
FindData findData = new(word.Span.GetText(), word.Span.Snapshot)
122+
string? text = word.Span.GetText();
123+
if (!_tagger.ShouldHighlight(text))
124+
return;
125+
126+
FindData findData = new(text, word.Span.Snapshot)
100127
{
101-
FindOptions = FindOptions.WholeWord | FindOptions.MatchCase
128+
FindOptions = _tagger.FindOptions
102129
};
103130

104-
wordSpans.AddRange(_textSearchService!.FindAll(findData));
131+
System.Collections.ObjectModel.Collection<SnapshotSpan>? found = _textSearchService!.FindAll(findData);
132+
wordSpans.AddRange(_tagger.FilterResults(found));
105133

106134
if (wordSpans.Count == 1)
107135
{

0 commit comments

Comments
 (0)