Skip to content

Commit fa985b3

Browse files
Merge pull request #387 from X-Sharp/master
The HighLigth word feature stopped working after the window was split…
2 parents a6a7b50 + 50398a7 commit fa985b3

File tree

3 files changed

+44
-5
lines changed

3 files changed

+44
-5
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
using Microsoft.VisualStudio.Text.Operations;
2+
using Microsoft.VisualStudio.Text.Tagging;
3+
using Microsoft.VisualStudio.Utilities;
4+
using Community.VisualStudio.Toolkit;
5+
using System.ComponentModel.Composition;
6+
using Microsoft.VisualStudio.Text.Editor;
7+
8+
namespace TestExtension.MEF
9+
{
10+
/// <summary>
11+
/// This class demonstrates a HighlightWord tagger for text files
12+
/// and it only highlights whole words starting with a Letter
13+
/// </summary>
14+
[Export(typeof(IViewTaggerProvider))]
15+
[ContentType("Text")]
16+
[TagType(typeof(TextMarkerTag))]
17+
[TextViewRole(PredefinedTextViewRoles.PrimaryDocument)]
18+
internal class HighlightWordTaggerProvider : SameWordHighlighterBase
19+
{
20+
public override FindOptions FindOptions => FindOptions.WholeWord;
21+
public override bool ShouldHighlight(string text)
22+
{
23+
if (text?.Length > 0)
24+
return char.IsLetter(text[0]);
25+
return false;
26+
}
27+
28+
}
29+
}

demo/VSSDK.TestExtension/VSSDK.TestExtension.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
<Compile Include="Commands\RunnerWindowCommand.cs" />
6262
<Compile Include="Commands\ThemeWindowCommand.cs" />
6363
<Compile Include="Commands\UnloadSelectedProject.cs" />
64+
<Compile Include="MEF\HighlightWord.cs" />
6465
<Compile Include="MEF\TextViewCreationListener.cs" />
6566
<Compile Include="Options\General.cs" />
6667
<Compile Include="Properties\AssemblyInfo.cs" />

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

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
namespace Community.VisualStudio.Toolkit
1212
{
13+
1314
/// <summary>
1415
/// A base class for providing same-word highlighting.
1516
/// </summary>
@@ -48,8 +49,11 @@ public ITagger<T> CreateTagger<T>(ITextView textView, ITextBuffer buffer) where
4849
{
4950
ITextStructureNavigator? navigator = _textStructureNavigatorSelector?.GetTextStructureNavigator(textView.TextBuffer);
5051

51-
return (ITagger<T>)buffer.Properties.GetOrCreateSingletonProperty(() =>
52+
var tagger = textView.Properties.GetOrCreateSingletonProperty(() =>
5253
new SameWordHighlighterTagger(textView, buffer, _textSearchService, navigator, this));
54+
tagger.Counter += 1;
55+
56+
return (ITagger<T>)tagger;
5357
}
5458
}
5559

@@ -60,6 +64,7 @@ public HighlightWordTag(string tagName) : base(tagName) { }
6064

6165
internal class SameWordHighlighterTagger : ITagger<HighlightWordTag>, IDisposable
6266
{
67+
internal int Counter;
6368
private readonly ITextView _view;
6469
private readonly ITextBuffer _buffer;
6570
private readonly ITextSearchService? _textSearchService;
@@ -83,6 +88,7 @@ public SameWordHighlighterTagger(ITextView view, ITextBuffer sourceBuffer, IText
8388
_currentWord = null;
8489
_view.Caret.PositionChanged += CaretPositionChanged;
8590
_view.LayoutChanged += ViewLayoutChanged;
91+
Counter = 0;
8692
}
8793

8894
private void ViewLayoutChanged(object sender, TextViewLayoutChangedEventArgs e)
@@ -226,11 +232,14 @@ public void Dispose()
226232
{
227233
if (!_isDisposed)
228234
{
229-
_view.Caret.PositionChanged -= CaretPositionChanged;
230-
_view.LayoutChanged -= ViewLayoutChanged;
235+
this.Counter -= 1;
236+
if (this.Counter == 0)
237+
{
238+
_view.Caret.PositionChanged -= CaretPositionChanged;
239+
_view.LayoutChanged -= ViewLayoutChanged;
240+
_isDisposed = true;
241+
}
231242
}
232-
233-
_isDisposed = true;
234243
}
235244

236245
public event EventHandler<SnapshotSpanEventArgs>? TagsChanged;

0 commit comments

Comments
 (0)