Skip to content

Commit

Permalink
The HighLigth word feature stopped working after the window was split…
Browse files Browse the repository at this point in the history
… and unsplit or after the preview margin was shown because these all share the same buffer.

We now add a counter to the SameWordHighlighterTagger and when the Counter reaches zero then the tagger stops tagging.
Also added a HighlightWord tagger to the example project that tags words inside text documents.
  • Loading branch information
RobertvanderHulst committed Nov 9, 2022
1 parent a6a7b50 commit 50398a7
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 5 deletions.
29 changes: 29 additions & 0 deletions demo/VSSDK.TestExtension/MEF/HighlightWord.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using Microsoft.VisualStudio.Text.Operations;
using Microsoft.VisualStudio.Text.Tagging;
using Microsoft.VisualStudio.Utilities;
using Community.VisualStudio.Toolkit;
using System.ComponentModel.Composition;
using Microsoft.VisualStudio.Text.Editor;

namespace TestExtension.MEF
{
/// <summary>
/// This class demonstrates a HighlightWord tagger for text files
/// and it only highlights whole words starting with a Letter
/// </summary>
[Export(typeof(IViewTaggerProvider))]
[ContentType("Text")]
[TagType(typeof(TextMarkerTag))]
[TextViewRole(PredefinedTextViewRoles.PrimaryDocument)]
internal class HighlightWordTaggerProvider : SameWordHighlighterBase
{
public override FindOptions FindOptions => FindOptions.WholeWord;
public override bool ShouldHighlight(string text)
{
if (text?.Length > 0)
return char.IsLetter(text[0]);
return false;
}

}
}
1 change: 1 addition & 0 deletions demo/VSSDK.TestExtension/VSSDK.TestExtension.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
<Compile Include="Commands\RunnerWindowCommand.cs" />
<Compile Include="Commands\ThemeWindowCommand.cs" />
<Compile Include="Commands\UnloadSelectedProject.cs" />
<Compile Include="MEF\HighlightWord.cs" />
<Compile Include="MEF\TextViewCreationListener.cs" />
<Compile Include="Options\General.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

namespace Community.VisualStudio.Toolkit
{

/// <summary>
/// A base class for providing same-word highlighting.
/// </summary>
Expand Down Expand Up @@ -48,8 +49,11 @@ public ITagger<T> CreateTagger<T>(ITextView textView, ITextBuffer buffer) where
{
ITextStructureNavigator? navigator = _textStructureNavigatorSelector?.GetTextStructureNavigator(textView.TextBuffer);

return (ITagger<T>)buffer.Properties.GetOrCreateSingletonProperty(() =>
var tagger = textView.Properties.GetOrCreateSingletonProperty(() =>
new SameWordHighlighterTagger(textView, buffer, _textSearchService, navigator, this));
tagger.Counter += 1;

return (ITagger<T>)tagger;
}
}

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

internal class SameWordHighlighterTagger : ITagger<HighlightWordTag>, IDisposable
{
internal int Counter;
private readonly ITextView _view;
private readonly ITextBuffer _buffer;
private readonly ITextSearchService? _textSearchService;
Expand All @@ -83,6 +88,7 @@ public SameWordHighlighterTagger(ITextView view, ITextBuffer sourceBuffer, IText
_currentWord = null;
_view.Caret.PositionChanged += CaretPositionChanged;
_view.LayoutChanged += ViewLayoutChanged;
Counter = 0;
}

private void ViewLayoutChanged(object sender, TextViewLayoutChangedEventArgs e)
Expand Down Expand Up @@ -226,11 +232,14 @@ public void Dispose()
{
if (!_isDisposed)
{
_view.Caret.PositionChanged -= CaretPositionChanged;
_view.LayoutChanged -= ViewLayoutChanged;
this.Counter -= 1;
if (this.Counter == 0)
{
_view.Caret.PositionChanged -= CaretPositionChanged;
_view.LayoutChanged -= ViewLayoutChanged;
_isDisposed = true;
}
}

_isDisposed = true;
}

public event EventHandler<SnapshotSpanEventArgs>? TagsChanged;
Expand Down

0 comments on commit 50398a7

Please sign in to comment.