cshuaimin / ssr.nvim

Treesitter based structural search and replace plugin for Neovim.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Feature Request: limit operation to buffer region

tzachar opened this issue · comments

Is there any plan on limiting the search and replace to a region instead of the entire buffer?

Maybe also add mappings for "apply on current match"?

Selection is used to get which node to search, and can't be used to limit the region. And I think if I use ssr, I will always replace all matches. If I only want to edit few places, I just edit them one by one manaually.

I'll look to add :s/foo/bar/c like confirm and skip options, as well as dot-repeat, quick-fix etc from this reddit comment.

Adding a confirm option would also be helpful.
I was thinking that just adding keymaps to (1) move to the next match in the buffer and (2) apply the restructuring over the current match would probably solve the issue.

@cshuaimin if you're interested, I've implemented a very similar behaviour for a tree sitter based movement and selection plugin where I find the smallest node that fully covers the visual selection. If this is something you'd like to support I can create a PR.

I found in developing said plugin that it is not always obvious what node the cursor is over and that using visual selection can often be more predictable.

ssr.nvim is already using tsnode:named_descendant_for_range() to find the smallest node that covers the selection (or just cursor position if in normal mode). See https://github.com/cshuaimin/ssr.nvim/blob/main/lua/ssr.lua#L59. You can just select the node then open ssr (though you need to add keymapping in visual mode too). See also #3.

I find if you put the cursor on language keywords it's easier to get correct node. For example in the README video I put cursor on 'function' and 'let'.

As a quick fix you can select the region, hit <leader>yr to create a Yode region, do what you want with ssr and delete the buffer to jump back to your code file ... in theory, not tetsed yet. https://github.com/hoschi/yode-nvim
(disclaimer: I'm the author of Yode)

Hi, it is now possible to get a searchbox.nvim like confirm menu by pressing Enter (instead of <leader> + Enter) to replace.