addon-search: Add "Find Nth" functionality#5798
Conversation
- 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
left a comment
There was a problem hiding this comment.
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.
|
Understandable and thanks for the consideration. TL;DR -- 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. |
|
I see. I suspect that 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. |
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
nfield tosearchOptionsfor locating matches arbitrarily.Add a
findNth(...)method analogous tofindNext(...)andfindPrev(...).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
terminalFindWidgetDemo App Updates