Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support searching the model instead of the view #52

Merged
merged 5 commits into from
Jun 23, 2024

Conversation

stefanhaller
Copy link
Collaborator

Needed for jesseduffield/lazygit#3642, and reviewed as part of it.

Previously, jumping to the next search result with 'n' would not show the search
result in the selected line. This is especially annoying when there is more than
one search result on a single line, because pressing 'n' seemingly does nothing.

Now we highlight the search result on top of the selection highlight, which
solves the problem nicely.
It is unnecessary to call it every time we draw, but we do need to call it every
time the view content changes.
We will need it again when searching the model; extracting the change as a
separate commit just to make the diffs of the following commits smaller.
When we add support for searching the model, the length of a highlighted search
result isn't necessarily the same as the length of the search string. As an
example, consider that you are searching for a full commit hash; the view only
renders an abbreviated hash, so when the hash is found in the model, we only
want to highlight the abbreviated version.

Prepare for this by adding an XEnd field to cellPos; when searching the view,
this is always XStart + len(searchString).

Also, rename cellPos to SearchPosition and uppercase its fields to expose them;
we will use the struct in the API to set a model search function in the next
commit.
Add a method View.SetModelSearchFunc; if set, it will be used to look for search
results instead of searching the view lines.

This will be needed when we optimize some list views to render only the visible
portion of the view instead of all lines of the list. However, it also has the
benefit that it allows searching for the full text of something that is only
rendered in an abbreviated form in the view; the most notable example being
commit hashes.

This is opt-in, so we still support searching the view as before. This is needed
for two reasons:
- adding a model search func can be a bit of work, so we should only do it when
  there's a benefit
- some views don't even have a model
@stefanhaller stefanhaller merged commit a42926c into master Jun 23, 2024
1 check passed
@stefanhaller stefanhaller deleted the support-searching-the-model branch June 23, 2024 09:48
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.

1 participant