Skip to content

Fix wrong matches in multiline file search#160665

Merged
roblourens merged 2 commits intomicrosoft:mainfrom
bvschaik:bugfix/multiline-search
Sep 21, 2022
Merged

Fix wrong matches in multiline file search#160665
roblourens merged 2 commits intomicrosoft:mainfrom
bvschaik:bugfix/multiline-search

Conversation

@bvschaik
Copy link
Contributor

When multiple consecutive lines match a regular expression, RipGrep will return them as one match with multiple submatches. The parser that converts the absolute positions of the submatches to (line number, column number) pairs was not taking into account that inBetweenChars may contain newlines, leading to wrong matches, and search/replace actions that gave unexpected results.

Fixes #131507

Copy link
Member

@roblourens roblourens left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks great, thanks for investigating this. re: your comment about tests, I agree that we should add a test, and I do see tests for the class in ripgrepTextSearchEngineUtils.test.ts. This is testing the whole parser end to end, I think you should be able to construct the right input and test that it gets parsed correctly.

@roblourens roblourens modified the milestones: September Patch Tuesday, September 2022 Sep 12, 2022
@ghost
Copy link

ghost commented Sep 12, 2022

CLA assistant check
All CLA requirements met.

@bvschaik
Copy link
Contributor Author

Thanks for the pointer, I totally overlooked those tests.

I have added two tests:

  • multiple submatches without newline in between: already succeeds on main
  • multiple submatches with newline in between: fails on main, succeeds with my changes

When multiple consecutive lines match a regular expression, RipGrep will return them  as one match with multiple submatches. The parser that converts the absolute positions of the submatches to (line number, column number) pairs was not taking into account that `inBetweenChars` may contain newlines, leading to wrong matches, and search/replace actions that gave unexpected results.

Fixes #131507
Copy link
Member

@roblourens roblourens left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

Copy link
Contributor

@andreamah andreamah left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the contribution 🙂

@roblourens roblourens merged commit e09c5ce into microsoft:main Sep 21, 2022
@bvschaik bvschaik deleted the bugfix/multiline-search branch September 21, 2022 16:34
@github-actions github-actions bot locked and limited conversation to collaborators Nov 5, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Ctrl+Shift+F with regex show wrong results

3 participants