Skip to content

Commit 872c873

Browse files
Added methods and property to SameWordHighlighterBase to allow to customize the search process
1 parent d62a983 commit 872c873

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
@@ -20,12 +20,32 @@ public class SameWordHighlighterBase : IViewTaggerProvider
2020

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

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

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

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

49-
public SameWordHighlighterTagger(ITextView view, ITextBuffer sourceBuffer, ITextSearchService? textSearchService, ITextStructureNavigator? textStructureNavigator)
70+
public SameWordHighlighterTagger(ITextView view, ITextBuffer sourceBuffer, ITextSearchService? textSearchService,
71+
ITextStructureNavigator? textStructureNavigator, SameWordHighlighterBase tagger)
5072
{
5173
_view = view;
5274
_buffer = sourceBuffer;
5375
_textSearchService = textSearchService;
5476
_textStructureNavigator = textStructureNavigator;
77+
_tagger = tagger;
5578
_wordSpans = new NormalizedSnapshotSpanCollection();
5679
_currentWord = null;
5780
_view.Caret.PositionChanged += CaretPositionChanged;
@@ -97,12 +120,17 @@ private void UpdateWordAdornments(TextExtent word)
97120
SnapshotPoint currentRequest = _requestedPoint;
98121
List<SnapshotSpan>? wordSpans = new();
99122

100-
FindData findData = new(word.Span.GetText(), word.Span.Snapshot)
123+
string? text = word.Span.GetText();
124+
if (!_tagger.ShouldHighlight(text))
125+
return;
126+
127+
FindData findData = new(text, word.Span.Snapshot)
101128
{
102-
FindOptions = FindOptions.WholeWord | FindOptions.MatchCase
129+
FindOptions = _tagger.FindOptions
103130
};
104131

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

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

0 commit comments

Comments
 (0)