-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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: find closest pairs using tree-sitter #8294
Conversation
This comment was marked as outdated.
This comment was marked as outdated.
2674caf
to
edf9474
Compare
I haven't revied this too much yet but it strikes me as a bit odd that Perhpahs instead of fixing the broken code here it might be more worthwhile to change that? Thaughts @the-mikedavis |
Under the hood, both commands use helix/helix-term/src/commands.rs Line 5163 in 2284bce
helix/helix-core/src/textobject.rs Line 230 in 2284bce
Though, as @the-mikedavis mentioned, helix/helix-core/src/match_brackets.rs Line 54 in 2284bce
find_nth_closest_pairs_pos to use/reuse something like find_pair (but n times) when TS is present and fall back to the current implementation otherwise. Is it a good one?
|
7c6d268
to
844baa1
Compare
I think I'm done. I've created a new function
Now "mim" or "mrm" seems more natural than before. If you have a selection that crosses multiple brackets, a pair of brackets will be found that completely covers the selection, which was not the case with the plain implementation (which assumed that the selection was just the cursor head). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I still need to do some testing but from reading trough the changes this looks great.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changes look go to me. I will do some testing in my daily driver branch (I use mim/mam quite a bit)
I found one discrepancy. Since I combined the pairs into one list, quotes are now considered as pairs too. But if you use a file without the tree-sitter syntax, it will still skip
because current impl of nearest brackets first checks if the char is open and continues to look for a closed char if it is true. helix/helix-core/src/surround.rs Line 70 in 9f6e9a1
The code used to work correctly because we had a different set of pairs (without the quotes). But now that the pairs are merged into one, I believe it should also accept quotes as pairs so that all pairs from PAIRS are treated the same (they do with tree-sitter, this "bug" only applies to non-tree-sitter files). The solution is simple - give the check of the closing element a higher priority. Do I need to do this?
|
Hmm good qustion we might wnat to just not support quotes for plaintext files since its not clear whether we are currently "inside" the pair or not. We do the same for match_bracket. |
If so then it works as expected, but for clarity I might add something like |
I think a comment would be good 👍 |
So I took a closer look at the behavior of the code with gdb and found that this is not true:
On the master (this is not a side effect of my PR🙂), it's still going through all the text trying to find closing quote. But it just never finds it (redundant cpu cycles), because there is no opposite state for quotes. I don't want to do it in this PR, but I'll probably make another one when I have some free time to clean up match_brackets.rs and surround.rs a bit.
|
hmm I think you are right reading the code again. I think that is an oversight tough the original PR stated it worked that way. I think I assumed from the naming of helix/helix-core/src/match_brackets.rs Line 145 in 9f6e9a1
It doesn't actually work tough if you test it (pressing mm on quotes in plaintext files does nothing). |
would really love to see this land and think its almost there. Are you interested in finishing this @woojiq? |
Sure, afair this pr is almost ready, I'm going to look again and rebase in 1-2 days. |
I've rebased and changed this PR a bit. To me it looks much better than before. I wrote few tests for |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thanks for finishing that this will be great to have!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree, looks great! Could you rebase / merge master to resolve the conflicts?
c5d5d9e
to
d9ba95d
Compare
@woojiq, I found that this doesn't work for quotation symbols (") in Python, and only on the opening quotation mark in Rust (for If you have the time and energy, would you mind taking a look? |
Only the rudt case is abug either this PR, the pythpm case is an existing bug with |
Closes #8325
closes #3615
closes #3432
closes #4475
closes #7012
Finding the closest surround pairs (
mim
,mam
) was done without being aware of the syntax. This PR adds a tree-sitter implementation that may be faster (untested), but most important it's more correct.If the syntax distinct strings than
mim
andmam
will work inside strings (didn't work with plaintext impl), matching should also work inside injections.OLD
`find_nth_closest_pairs_pos` produces unsorted tuple. The tuple must be sorted before passing it to the change Transaction. Otherwise, this statement will fail: https://github.com/helix-editor/helix/blob/13d4463e4146473391e37b7f75e99b731aa55878/helix-core/src/transaction.rs#L582-L585MRE:
hx
( <esc>hvhmdm
. Make sureauto-pairs
is turn on.