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

Add ability to match in next and previous char pairs (2) #11695

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

Conversation

thomasschafer
Copy link
Contributor

@thomasschafer thomasschafer commented Sep 14, 2024

Adds the ability to match inside/around the next or previous matching character pair: for instance, with the cursor as below:

let x = "bar";

typing ]" would select the contents of the string (i.e. bar).

Screen.Recording.2024-09-15.at.10.20.34.mov

Supersedes #11260 based on discussion there.

Partially resolves:

@thomasschafer
Copy link
Contributor Author

Note: the first commit is essentially just the changes linked in the comment here. I left the two TODO comments in as I couldn't see a nice way to tidy things up without adding unnecessary complexity, but happy to hear any pointers.

@the-mikedavis the-mikedavis added A-keymap Area: Keymap and keybindings A-command Area: Commands labels Sep 14, 2024
@thomasschafer
Copy link
Contributor Author

Bumping this 🙏

@daedroza
Copy link
Contributor

If I jump to a function, would this patch still support jumping to next and previous function?

@thomasschafer
Copy link
Contributor Author

If I jump to a function, would this patch still support jumping to next and previous function?

Yes, that still works as it does currently on master

@thomasschafer
Copy link
Contributor Author

@the-mikedavis apologies for the ping, wondering if you'd mind taking a look at this? 🙏

@the-mikedavis
Copy link
Member

I do want to take a look at this soon but I'm about to be away for a while. I have a note written down to give this a look once I'm back in a 2ish weeks.

(I'm in favor of the feature, I just haven't looked at the changes yet)

@@ -172,6 +178,17 @@ pub fn find_nth_pairs_pos(

let (open, close) = get_pair(ch);
let pos = range.cursor(text);
let (pos, n) = match find_type {
FindType::Surround(n) => (pos, n),
FindType::Next(n) => match search::find_nth_next(text, open, pos, n) {
Copy link
Member

Choose a reason for hiding this comment

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

Using plaintext search routines isn't right here. This should be based on tree-sitter. The plaintext search gets thrown off by string literals.

In general I think reusing the surround function won't work too well here. The surround function mostly reuses other functions (match_bracket:: and search::) to find pairs and only adds logical necessary for surround. For forwards/reverse search you are looking for a pair fist and then a single (simpler) call to match_bracket/search would suffice.

I would probably be ok with not implementing the tree-sitter based forward/reverse search for now but I would definitly want to see this as a seperate function (likely directly within textobject.rs).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sorry not sure I'm fully following - mi{ doesn't use treesitter as far as I can tell, so would we want ]{ to behave any differently?

Also, I decided to go with the option of finding the first char within a pair and then using textobject_pair_surround as this allows selection of ranges between characters where the direction is not clear from the bracket e.g. between | characters. ]| works fine to jump between pairs of | in something like foo|bar|baz|, but that wouldn't work if first searching for the | and then calling one of the match_brackets functions. I'm sure I could overcome this by making a bunch of changes in match_brackets but I thought that would add a lot of code - do you think that option would be preferable, or is there a different approach I'm missing?

@kirawi kirawi added the S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. label Oct 19, 2024
@thomasschafer thomasschafer force-pushed the tschafer-add-go-to-next-inner-matching-chars-2 branch from 97910ca to 66f0f38 Compare December 4, 2024 17:31
@thomasschafer
Copy link
Contributor Author

Bump on this? 🙏

@nik-rev
Copy link
Contributor

nik-rev commented Dec 7, 2024

this is going to be a big quality of life improvement, appreciate it!

@Axlefublr
Copy link
Contributor

can't wait for this ngl

@thomasschafer
Copy link
Contributor Author

I do want to take a look at this soon but I'm about to be away for a while. I have a note written down to give this a look once I'm back in a 2ish weeks.

(I'm in favor of the feature, I just haven't looked at the changes yet)

@the-mikedavis sorry for another ping - will you have a chance to look at this PR? 🙏

@dedebenui
Copy link

At first I was a bit confused as to why this feature wasn't bound to the mi and ma commands, but after trying it a bit, I think in the end that your approach is more powerful 👍

@@ -154,14 +154,20 @@ fn find_nth_closest_pairs_plain(
Err(Error::PairNotFound)
}

pub enum FindType {
Copy link
Member

Choose a reason for hiding this comment

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

It looks like each of these enum variants are storing the count, right? Let's separate the enum from the count and pass that as a separate parameter even if it means adding #[allow(clippy::too_many_arguments)] for some functions

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sounds good, done

Copy link
Member

Choose a reason for hiding this comment

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

The fallback commands change will be a regression for the ability to remap mi/ma - currently you can remap those but with the fallback commands change you would be unable to remap the fallback behavior until we expose it in keymap configuration (i.e. in TOML). We should expose more of the keymap type anyways and allow remapping the names of minor modes and whether or not they are sticky. I'm not sure it's a straightforward change though since we want to revisit and potentially further refactor minor modes for #12281

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not sure I'm fully understanding - I've tried remapping with specific keys e.g.

[keys.normal]
m.i.z = "..."

(similarly for just keys.normal.m.i) and it seems to work fine, are you saying specifically that we should have a way to remap all fallback characters, something like

[keys.normal]
m.i.fallback = "..."

which would accept a command that listens for a character?

@the-mikedavis the-mikedavis removed the S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. label Jan 23, 2025
@thomasschafer thomasschafer force-pushed the tschafer-add-go-to-next-inner-matching-chars-2 branch 3 times, most recently from a567fdc to 62519b4 Compare January 29, 2025 13:28
@thomasschafer thomasschafer force-pushed the tschafer-add-go-to-next-inner-matching-chars-2 branch from 4f8f49c to 196989f Compare February 23, 2025 16:06
@thomasschafer
Copy link
Contributor Author

@the-mikedavis sorry for another ping, I've responded to/made changes based on your comments if you can take a look 🙏

@Axlefublr
Copy link
Contributor

ngl this would straight up be revolutionary
go to next pair of quotes is a qol feature that helps a lot

@thomasschafer thomasschafer force-pushed the tschafer-add-go-to-next-inner-matching-chars-2 branch from 196989f to 67a4bae Compare February 25, 2025 12:59
@thomasschafer thomasschafer force-pushed the tschafer-add-go-to-next-inner-matching-chars-2 branch from 67a4bae to c39007e Compare February 28, 2025 16:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-command Area: Commands A-keymap Area: Keymap and keybindings
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants