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

feat: Add local search in buffer #13053

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

oxcrow
Copy link
Contributor

@oxcrow oxcrow commented Mar 8, 2025

Hi everyone,

I added a feature to do local search in the currently opened document buffer to improve user experience.

This feature is similar to global_search but is only limited to one document.

The command is activated by space+l and seems to work well.

Kindly review and allow this feature to be merged.

Thank you
~oxcrow

Screenshot From 2025-03-08 12-31-58

Reasoning

  • The global_search method is quite nice since it works in a fuzzy finding method and provides a table of matches that we can easily preview to find what we want.
  • A similar feature is needed for searching through the currently opened buffer (i.e only one document). We could use global_search however it produces too many false positive results from other files, which makes it difficult to use.
  • The standard local search method using / does not feel very good to use.
  • Other editors such as Emacs/Neovim provide plugins for local search such as Swiper, which feel better to use https://github.com/abo-abo/swiper
  • This feature is implemented to provide a similar feature as Swiper.

Implementation

  • I took the code of global_search and modified it little bit.
  • Specifically I modified the code to only search through the current document.
  • The code works but it should be improved by someone more experienced (if necessary).

Grep search through a local buffer similar to `global_search`.

The method works but it should be improved by someone more experienced.
@nik-rev
Copy link
Contributor

nik-rev commented Mar 8, 2025

Showing the path to the file in the global search picker where there can be lots of files makes sense. But here it doesn't really make sense, because you're searching in the same file - so all picker items will show the same path

Maybe instead of showing the path, show the line number and the string contents of that line in the 2nd column

As per review comments of helix maintainers,
- The filename and directory path was hidden for local_search
- The : colon separator was also hidden since it is not required
@oxcrow
Copy link
Contributor Author

oxcrow commented Mar 9, 2025

@nik-rev Thank you for your feedback.

I was able to implement and push the changes you requested.

Here is the result.

fix

On another note, I also found a bug that needs to be fixed. The local_search method seems to also search in all opened buffers. As shown in this image the search result contains results from two files README.md and test.txt.

I will try to fix it.

bug

@nik-rev
Copy link
Contributor

nik-rev commented Mar 9, 2025

Nice! So you have a "line number" column. What about having a 2nd column for "text on that line"? Since there is quite a lot of whitespace

@oxcrow
Copy link
Contributor Author

oxcrow commented Mar 9, 2025

I agree that having the "text on the current line" being displayed with the search result will be helpful.

Since I'm new to hacking on helix I do not know how to extract the data from the buffer right now.

Once I know how to extract the data I can display it by modifying the contents of columns.

I will try to look into this and implement this.

Any help regarding which file/struct/variable contains the buffer text data so I can extract (clone) it will be helpful.

Thank you.

@nik-rev
Copy link
Contributor

nik-rev commented Mar 9, 2025

I agree that having the "text on the current line" being displayed with the search result will be helpful.

Since I'm new to hacking on helix I do not know how to extract the data from the buffer right now.

Once I know how to extract the data I can display it by modifying the contents of columns.

I will try to look into this and implement this.

Any help regarding which file/struct/variable contains the buffer text data so I can extract (clone) it will be helpful.

Thank you.

You have access to a cx: &mut Context. doc!(cx.editor) should give you the currently focused Document which has methods like Document::text() which returns a Rope data structure representing the text in the current file

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