Issues with Searching

SyntaxEditor for WPF Forum

Posted 4 years ago by SvenG - VIPA
Version: 16.1.0630
Platform: .NET 4.5
Environment: Windows 7 (64-bit)
Avatar

Hi guys,

have an issue with the programmatic upward search in the Document Snapshot. Below please find some UnitTests which show the issue.
Please see the SearchUp2() and SearchUp5() which are not producing the expected results.

Could you please investigate this behavior ?

Regards

Sven

using System;
using System.Linq;
using ActiproSoftware.Text;
using ActiproSoftware.Text.Implementation;
using ActiproSoftware.Text.Searching;
using ActiproSoftware.Windows.Controls.SyntaxEditor;
using ActiproSoftware.Windows.Controls.SyntaxEditor.Implementation;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Tests
{
  [TestClass]
  public class SearchTests
  {
    [TestMethod]
    public void SearchUp1() // working
    {
      #region Arrange

      const string searchString = "77";
      const bool searchUp = true;
      const int searchStartOffset = 25;
      const int searchEndOffset = 0;
      ITextSnapshot snapshot = GetSampleTextSnapshot();
      ISearchOptions searchOptions = GetSearchOptionsFromSearchSettings(searchString, searchUp);

      #endregion
      
      #region Act

      ISearchResultSet searchResultSet = snapshot.FindNext(searchOptions, searchStartOffset, false, new TextRange(searchEndOffset, searchStartOffset));

      #endregion

      #region Assert

      Assert.IsNotNull(searchResultSet);
      Assert.AreEqual(1, searchResultSet.Results.Count);

      ISearchCapture firstCapture = searchResultSet.Results.First().Captures.First();
      Assert.IsNotNull(firstCapture);
      Assert.AreEqual(searchStartOffset-searchString.Length, firstCapture.TextRange.FirstOffset);
      Assert.AreEqual(searchStartOffset, firstCapture.TextRange.LastOffset);
      
      #endregion
    }

    [TestMethod]
    public void SearchUp2() // not working
    {
      #region Arrange

      const string searchString = "77";
      const bool searchUp = true;
      const int searchStartOffset = 75;
      const int searchEndOffset = 0;
      ITextSnapshot snapshot = GetSampleTextSnapshot();
      ISearchOptions searchOptions = GetSearchOptionsFromSearchSettings(searchString, searchUp);

      #endregion

      #region Act

      // the first part "77" of "777" should be found here! but it isnt!
      ISearchResultSet searchResultSet = snapshot.FindNext(searchOptions, searchStartOffset, false, new TextRange(searchStartOffset, searchEndOffset));

      #endregion

      #region Assert

      Assert.IsNotNull(searchResultSet);
      Assert.AreEqual(1, searchResultSet.Results.Count);

      ISearchCapture firstCapture = searchResultSet.Results.First().Captures.First();
      Assert.IsNotNull(firstCapture);
      Assert.AreEqual(searchStartOffset - searchString.Length, firstCapture.TextRange.FirstOffset);
      Assert.AreEqual(searchStartOffset, firstCapture.TextRange.LastOffset);

      #endregion
    }

    [TestMethod]
    public void SearchUp3() // working
    {
      #region Arrange

      const string searchString = "77";
      const bool searchUp = true;
      const int searchStartOffset = 76;
      const int searchEndOffset = 0;
      ITextSnapshot snapshot = GetSampleTextSnapshot();
      ISearchOptions searchOptions = GetSearchOptionsFromSearchSettings(searchString, searchUp);

      #endregion

      #region Act

      ISearchResultSet searchResultSet = snapshot.FindNext(searchOptions, searchStartOffset, false, new TextRange(searchStartOffset, searchEndOffset));

      #endregion

      #region Assert

      Assert.IsNotNull(searchResultSet);
      Assert.AreEqual(1, searchResultSet.Results.Count);

      ISearchCapture firstCapture = searchResultSet.Results.First().Captures.First();
      Assert.IsNotNull(firstCapture);
      Assert.AreEqual(searchStartOffset - searchString.Length, firstCapture.TextRange.FirstOffset);
      Assert.AreEqual(searchStartOffset, firstCapture.TextRange.LastOffset);

      #endregion
    }

    [TestMethod]
    public void SearchUp4() // working
    {
      #region Arrange

      const string searchString = "77";
      const bool searchUp = true;
      const int searchStartOffset = 101;
      const int searchEndOffset = 0;
      ITextSnapshot snapshot = GetSampleTextSnapshot();
      ISearchOptions searchOptions = GetSearchOptionsFromSearchSettings(searchString, searchUp);

      #endregion

      #region Act

      ISearchResultSet searchResultSet = snapshot.FindNext(searchOptions, searchStartOffset, false, new TextRange(searchStartOffset, searchEndOffset));

      #endregion

      #region Assert

      Assert.IsNotNull(searchResultSet);
      Assert.AreEqual(1, searchResultSet.Results.Count);

      ISearchCapture firstCapture = searchResultSet.Results.First().Captures.First();
      Assert.IsNotNull(firstCapture);
      Assert.AreEqual(searchStartOffset - searchString.Length, firstCapture.TextRange.FirstOffset);
      Assert.AreEqual(searchStartOffset, firstCapture.TextRange.LastOffset);

      #endregion
    }

    [TestMethod]
    public void SearchUp5() // not working
    {
      #region Arrange

      const string searchString = "77";
      const bool searchUp = true;
      ITextSnapshot snapshot = GetSampleTextSnapshot();
      ISearchOptions searchOptions = GetSearchOptionsFromSearchSettings(searchString, searchUp, int.MaxValue);

      #endregion

      #region Act

      // there should be 4 occurences of "77" when searchin upwards, but only 3 are found (seems to be the case, that SearchUp is ignored)
      ISearchResultSet searchResultSet = snapshot.FindAll(searchOptions, new TextRange(snapshot.Length, 0));

      #endregion

      #region Assert

      Assert.IsNotNull(searchResultSet);
      Assert.AreEqual(4, searchResultSet.Results.Count);

      #endregion
    }

    private ITextSnapshot GetSampleTextSnapshot()
    {
      string sampleText = "  " + Environment.NewLine +
                          "       O    M       77.0" + Environment.NewLine +
                          "       O    M        7.0" + Environment.NewLine +
                          "" + Environment.NewLine +
                          "       =    M      777.0" + Environment.NewLine +
                          "       O    M       77.0" + Environment.NewLine +
                          "       =    M      100.0" + Environment.NewLine +
                          "       ";

      CodeDocument document = new CodeDocument();
      document.SetText(sampleText);

      return document.CurrentSnapshot;
    }

    private static ISearchOptions GetSearchOptionsFromSearchSettings(string text, bool searchUp, int maximumResultCount = 1)
    {
      return new EditorSearchOptions
      {
        FindText = text,
        MatchCase = false,
        MatchWholeWord = false,
        SearchUp = searchUp,
        PatternProvider = SearchPatternProviders.Normal,
        Scope = EditorSearchScope.Document,
        MaximumResultCount = maximumResultCount
      };
    }
  }
}

Comments (9)

Posted 4 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar

Hi Sven,

Thanks for writing.  We've looked into it and test #2 was a valid bug.  We've fixed it for the next maintenance release.

For #5, a find all will return 3 results and that is correct.  Visual Studio also returns 3 results for a find all in that case.


Actipro Software Support

Posted 4 years ago by Jens H.
Avatar

Hello Support,

is there any chance you can also fix this for a 14.* Version?

Regarding #5: Did you recognize that this is a upward search? For the example text when doing multiple FindNext downward searches one would get 3 matches, but when doing multiple FindNext upward searches one would get 4 single matching steps, which is correct, as every text editor does. But I would expect the method FindAll to find exactly the same matches as FindNext, which is not the case for an upward search!

For FindNext upward searches there are 4 matches but for FindAll upward search there are only 3 matches like in a downward search.

 

Thanks and regards,
Jens

Posted 4 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar

Hi Jens,

I'm sorry but we're not doing updates for the 14.x versions any more.

Yes we saw that, but in a Find All scenario, the upward flag is ignored.  The same is true in Visual Studio where it only searches from the top of the document to the bottom for a Find All.


Actipro Software Support

Posted 4 years ago by Jens H.
Avatar

This is sad news :(

Posted 4 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar

Hi Jens,

Sorry but even other popular editors like Sublime do the same thing we do.  If you need to mimic functionality like what you are asking for, you could just run a loop starting at the snapshot end and keep calling FindNext with upward search active until you hit the beginning of the snapshot or fail to make a match.  Lump all those results into a result set and it will be what you want.


Actipro Software Support

Posted 4 years ago by Jens H.
Avatar

Can you please reply here as soon as a patched version is available? :-)

Posted 4 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar

Hi Jens,

Sorry but the design follows what other popular editors do where Find All and Replace All return the same results as each other, and don't use the SearchUp flag.  We don't have plans to change that, so I would recommend you do what was mentioned in our previous reply and use a looped FindNext with upward search if you need reverse find all functionality.


Actipro Software Support

Posted 4 years ago by Jens H.
Avatar

With "patched version" I meant a version for the patch of issue #2.

It's ok not to patch #5. I know that no other Editor does have a behavior in UI to do a search-all in up-direction. But it would have been nice if you would support on the document level, because a looped FindNext isn't really performant for large documents.

Regards,
Jens

Posted 4 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar

Hi Jens,

The next maintenance release with the #2 fix should be out in the next week or two unless something holds it up.  Please watch our Announcements forum for a notification since we post there on every release.


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.