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 2 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
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 S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants