Dynamic Search highlighting

SyntaxEditor for WPF Forum

Posted 10 years ago by John Dunn
Version: 9.1.0505
Avatar
I'm attempting to implement searching like in Chrome or Safari - basically a text field where the user can start typing and then dynamically highlight the found elements in the document. Doing the dynamic search is easy

    private void _FindText_TextChanged(object sender, TextChangedEventArgs e)
    {
      string text = _FindText.Text;
      if (!string.IsNullOrEmpty(text))
      {
        EditorSearchOptions options = new EditorSearchOptions()
        {
          FindText = _FindText.Text
        };
        int count = 0;
        foreach (var res in _Editor.Document.CurrentSnapshot.FindAll(options).Results)
        {
          count++;
        }
        _Output.Text = string.Format("{0} items found", count);
      }
      else
      {
        _Output.Text = "0 items found";
      }
    }
  }
But I'm not sure how ( assuming it's possible ) to highlight the results. Ideally I'd like to dim the document like Safari does but if that's not possible then just highlighting the found items would work.

Once I've highlighted the found items, I'll need to be able to allow the user to jump to the next found item with a button.

Any pointers to accomplishing this task would be great.

Comments (5)

Posted 10 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
Hi John,

We eventually will be working on span indicators that should make this sort of thing much easier. However in the meantime, you could probably create a custom classifier that tracks the result ranges and has a related style that gets applied to the classification type ranges you assign.

Take a look at our "Classification - Layered" QuickStart for a sample on making a classifier. But you'd want to have yours track the text ranges to classify based on the find results.


Actipro Software Support

Posted 10 years ago by John Dunn
Avatar
I have something pretty much working. Will something like this be the most efficient way until you get span indicators working?

    class CustomClassifier : ClassifierBase, IHighlightingStyleRegistryProvider
    {
      private SyntaxEditor _Editor;
      private static IClassificationType _HighlightType = new ClassificationType("Test");
      private static HighlightingStyleRegistry _StyleRegistry;

      static CustomClassifier()
      {
        _StyleRegistry = new HighlightingStyleRegistry();
        _StyleRegistry.Register(_HighlightType, new HighlightingStyle()
          {
            Background = Brushes.Yellow,
            Foreground = Brushes.Black,
          }
        );
      }

      void InvalidateRanges()
      {
        if (_Ranges != null)
        {
          foreach (var range in _Ranges)
          {
            OnClassificationChanged(
              new ClassificationChangedEventArgs(
                new TextSnapshotRange(_Editor.Document.CurrentSnapshot, range.TextRange)));
          }
        }
      }
      List<ITextRangeProvider> _Ranges;
      public List<ITextRangeProvider> Ranges
      {
        set
        {
          InvalidateRanges();
          _Ranges = value;
          InvalidateRanges();
        }
      }
      public CustomClassifier(SyntaxEditor editor)
        : base("Custom Classifier")
      {
        _Editor = editor;
      }
      public override void Classify(TextSnapshotRange snapshotRange, IClassifierResultSet resultSet)
      {
        IClassificationRangeCollection highlightedRanges = new ClassificationRangeCollection();
        if (_Ranges != null)
        {
          foreach (var range in _Ranges)
            highlightedRanges.Add(new ClassificationRange(range.TextRange, _HighlightType));
        }
        resultSet.Results.Add(new ClassifierResult(this, highlightedRanges));
      }
      public IHighlightingStyleRegistry HighlightingStyleRegistry
      {
        get { return _StyleRegistry; }
      }
    }
Any idea on the timeframe of span indicators? That way I can decide if I want to put much work into this approach or just wait for a more correct solution.

Of course the next thing I'll need is the scroll bar tick marks to show where in the document things have been found like Chrome does as well. Is it possible to override the scroll bar used in the editor?
Posted 10 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
Hi John,

It may still be a bit before we get to span indicators. What you have looks good overall. The Classify method could definitely be more efficient by only adding ranges for the ranges that fall within the snapshotRange though.

We use normal WPF ScrollBars so you could create an implicit style for them. Alternatively you can use our custom margin mechanism too.


Actipro Software Support

Posted 8 years ago by Blake Niemyjski - Wisconsin
Avatar
Hello,

I came across Highlight References feature in VS2010 and noticed that we were missing it in our WPF editor. I then came across this thread which is the closest thing I could find to this feature...

I looked through the product samples and didn't see this functionality (could be my eyes). Has this functionality been implemented in the latest version of WPF Studio? If, so do you have a sample of this?

Thanks
-Blake Niemyjski
Posted 8 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
Hi Blake,

We don't have anything built-in for Highlight References as that is more of a language-specific sort of feature (its implementation would differ based on the language being used).

That being said you can use a classification tagger to mark ranges and then associate a style with that classification to implement ranges with a background. It would be up to you to track which ranges need to be highlighted though.


Actipro Software Support

The latest build of this product (v2019.1 build 0683) was released 1 month ago, which was after the last post in this thread.

Add Comment

Please log in to a validated account to post comments.