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

Surrounding Function Rename: Trigger LSP #327

Open
coxley opened this issue May 28, 2024 · 3 comments
Open

Surrounding Function Rename: Trigger LSP #327

coxley opened this issue May 28, 2024 · 3 comments
Assignees
Labels
enhancement New feature or request

Comments

@coxley
Copy link

coxley commented May 28, 2024

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.

@coxley coxley added the enhancement New feature or request label May 28, 2024
@kylechui
Copy link
Owner

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

@coxley
Copy link
Author

coxley commented May 28, 2024

@kylechui Unfortunately the default "target" for vim.lsp.buf.rename() is <cword>, which means this can't work in the context of surround. :\

@kylechui
Copy link
Owner

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().

@kylechui kylechui self-assigned this May 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants