Surrounding Function Rename: Trigger LSP
Summary
First, love this plugin.
I've relied too much on the built-in surround capabilities, mostly for changing content within. This is really cool for changing the surrounding characters/names themselves.
csf -> change surrounding function is awesome, but it isn't quite usable in an LSP environment. When using an LSP, you can issue vim.lsp.buf.rename() and be able to rename the closest token to your cursor. The new name will be updated everywhere in the project.
Is it possible to have the default handler invoke something similar? Or if anyone is familiar, simply recommend a way to do so? I looked into the vim API docs and didn't see a straightforward way to trigger an LSP rename without navigating to the token first.
Haven't really tested this much, but maybe try this:
require("nvim-surround").setup({
surrounds = {
["f"] = {
change = {
target = "^()().*()()$",
replacement = function()
vim.lsp.buf.rename()
return { { "" }, { "" } }
end,
},
},
},
})
It's basically just hijacking the change key to map csf to call vim.lsp.buf.rename(). The modification to target is to prevent nvim-surround from deleting any text before the LSP rename gets applied. You can probably make this more complex to detect if any attached servers have the rename functionality, otherwise deferring to the "dumb" default functionality
@kylechui Unfortunately the default "target" for vim.lsp.buf.rename() is <cword>, which means this can't work in the context of surround. :\
I'm not quite sure what you mean by that---the find key remains unchanged so the cursor will "jump" to the nearest function call, then call vim.lsp.buf.rename().