Skip to content

Commit 438992f

Browse files
2 parents f69543d + 547beec commit 438992f

File tree

2 files changed

+41
-14
lines changed

2 files changed

+41
-14
lines changed

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

Lines changed: 37 additions & 10 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,18 +119,22 @@ 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))
100124
{
101-
FindOptions = FindOptions.WholeWord | FindOptions.MatchCase
102-
};
125+
FindData findData = new(text, word.Span.Snapshot)
126+
{
127+
FindOptions = _tagger.FindOptions
128+
};
103129

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

106-
if (wordSpans.Count == 1)
107-
{
108-
wordSpans.Clear();
133+
if (wordSpans.Count == 1)
134+
{
135+
wordSpans.Clear();
136+
}
109137
}
110-
111138
//If another change hasn't happened, do a real update
112139
if (currentRequest == _requestedPoint)
113140
{

src/toolkit/Community.VisualStudio.Toolkit.Shared/Solution/SolutionFolder.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,11 @@ public async Task<bool> TryRemoveAsync()
5858

5959
if (solution != null)
6060
{
61-
GetItemInfo(out IVsHierarchy hierarchy, out _, out _);
62-
63-
if (hierarchy is IVsSolution ivsSolution)
61+
GetItemInfo(out IVsHierarchy folderHierarchy, out _, out _);
62+
solution.GetItemInfo(out IVsHierarchy solutionHierarchy, out _, out _);
63+
if (solutionHierarchy is IVsSolution ivsSolution)
6464
{
65-
int hr = ivsSolution.CloseSolutionElement(0, hierarchy, 0);
65+
int hr = ivsSolution.CloseSolutionElement(0, folderHierarchy, 0);
6666
return hr == VSConstants.S_OK;
6767
}
6868
}

0 commit comments

Comments
 (0)