Skip to content

addon-search: Add "Find Nth" functionality#5798

Closed
boborrob wants to merge 2 commits into
xtermjs:masterfrom
boborrob:addon_search-arbitrary_access_to_matches
Closed

addon-search: Add "Find Nth" functionality#5798
boborrob wants to merge 2 commits into
xtermjs:masterfrom
boborrob:addon_search-arbitrary_access_to_matches

Conversation

@boborrob

@boborrob boborrob commented Apr 10, 2026

Copy link
Copy Markdown

Summary:

Add "Find Nth" functionality to the terminal search feature in order to access matches arbitrarily.

Motivation:

For large sets of search results, cycling through matches sequentially can be cumbersome and time-consuming, especially when the location of the desired match is strongly suspected or known with precision. An input allows the user to jump arbitrarily to the desired match or to get closer to it much faster.

Changes:

  • Add an n field to searchOptions for locating matches arbitrarily.

  • Add a findNth(...) method analogous to findNext(...) and findPrev(...).

  • Add baseline validations for n (!isNaN, isTruthy, etc.). Rigorous validation is delegated to the client where the bounds are better understood pre-invocation.

  • Update demo app with 'Find Nth' search controls.

Example use cases:

VS Code terminalFindWidget

Screenshot_20260409_225319-2

Demo App Updates

Screenshot_20260409_224725

- Add an `nthMatchPosition` field to  `searchOptions` for locating matches arbitrarily.

- Add a `findNth(...)`  method analogous to  `findNext(...)` and `findPrev(...)`.

- Add baseline validations for `nthMatchPosition` (!isNaN, isTruthy, etc.).
  Rigorous validation is delegated to the client.

- Update demo app with 'Find Nth' search controls.
- Rename `nthMatchPosition` to `n`.
- Use dedicated `Number` class for  parsing input.
- Ensure the fallback value for `n` takes precedence in the failure path.
- Add "Find Nth" test to `SearchAddon.test.ts`

@Tyriar Tyriar left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks for the PR, but we don't want this. If a terminal wanted to implement this, they can achieve it by calling findNext multiple times. I don't think it should or would be added to vscode though.

@Tyriar Tyriar closed this May 28, 2026
@boborrob

Copy link
Copy Markdown
Author

Understandable and thanks for the consideration.

TL;DR
Friendly rebuttal: Why loop with O(n) complexity over a contiguous, indexable array when an O(1) lookup is possible?

--

A looping approach was considered early on, but since the underlying data structure is array-like and indexable, that all seemed unnecessary. A loop would make more sense if the structure were a linked list where only 1 node at a time could be accessed in either direction.

Some more considerations: Calling either find method N times could be costly if the total number of matches is large enough, especially if the current match is far from the target match. Then there's the question of which direction to search in. Since the target match could be behind or ahead of the current one, both the distance and the direction would need to be determined to minimize travel.

Seems like more overhead to me given that the structure already lends itself really well to constant-time lookups.

Anyway, it'd be great if this were added to VS Code. The match counts are nice to look at, but I'm not sure how valuable the information is if it isn't actionable.

@Tyriar

Tyriar commented May 30, 2026

Copy link
Copy Markdown
Member

@boborrob it's not O(n) after #5902, should be pretty fast now.

@boborrob

boborrob commented Jun 8, 2026

Copy link
Copy Markdown
Author

I see. I suspect that resultTracker.searchResults is a higher-level, array-like abstraction of #5902's cache. Correct me if I'm wrong.

In O(1) and from any start location, the desired match can be accessed directly from that structure via an index (N - 1) . The work in #5902 builds and abstracts the cache so well that retrieving matches from it becomes trivial. One call, as opposed to many, can leverage all that work.

It's a compliment, really.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants