brenton-leighton / multiple-cursors.nvim

A multi-cursor plugin for Neovim that works in normal, insert/replace, or visual modes, and with almost every command
Apache License 2.0
152 stars 4 forks source link

Feature: Ctrl-D #37

Closed bokenator closed 5 months ago

bokenator commented 5 months ago

Hi @brenton-leighton,

First of all, thank you very much for creating this plugin. It's by far the most intuitive multi-cursor plugin I've come across for neovim.

This is a quick attempt at adding the Ctrl-D functionality from VSCode. There are a couple of problems with the current approach. I had to disable exiting visual mode because otherwise the search pattern gets changed to the word under the cursor rather than the original selection. As a result, while the real cursor selects the full search pattern, the virtual cursors are only added to the start of each match. Ideally, adding virtual cursors that cover the entire original selection would be a better approach, but I couldn't figure out how to do that. An alternative I considered was to track the pattern with a local variable, allowing us to leave visual mode without losing the search pattern. However, this solution complicates things because the pattern may need to be reset for different reasons.

There's also quite a bit of overlap in functionality between M.add_cursor_by_search and _add_cursors_by_search, I'll clean it up once the approach is finalized.

I'm open to any question/comments. And please let me know if I can be helpful in anyway.

brenton-leighton commented 5 months ago

Hey @bokenator, thanks for the contribution. I understand what Ctrl+D in VS Code does now.

I've made some changes in this branch: feat_add_cursor_to_next_match

I renamed the command to MultipleCursorsAddToNextMatch (I don't really I like MultipleCursorsAddBySearch but I don't know what else to call it) but more significantly I changed some things so that the search function is only called the first time the add_cursor_to_next_match function is called. So for every subsequent call of add_cursor_to_next_match it'll add a new cursor to the initial matches. This fixes the issue of working in visual mode, but it also means that if the cursor is moved to a new word, the next cursor is added to the next match of the original word.

It doesn't match what VS Code does, but is that OK?

bokenator commented 5 months ago

Thank you very much. This is amazing. Maybe you should call it MultipleCursorsAddNextMatch

Now that you've gotten it to work by running the search function just once, I'm going to implement another function called MultipleCursorSkipNextMatch to skip the next match (in case the user doesn't want every single match to be added as MC).

I'll close this PR and work off of the new branch.

Thanks again!

brenton-leighton commented 5 months ago

Thank you very much. This is amazing. Maybe you should call it MultipleCursorsAddNextMatch

OK done.

Now that you've gotten it to work by running the search function just once, I'm going to implement another function called MultipleCursorSkipNextMatch to skip the next match (in case the user doesn't want every single match to be added as MC).

I think you would just need to delete the first element from the _matches table.