ray-x / go.nvim

G'day Nvimer, Joyful Gopher: Discover the Feature-Rich Go Plugin for Neovim
MIT License
2.12k stars 123 forks source link

Bug: GoRename does not work consistently #482

Open hendrikiseke1979 opened 3 months ago

hendrikiseke1979 commented 3 months ago

Hi, I'm not sure, if I'm missing anything, but I ran into issues when using GoRename (using vim.lsp.buf.rename works). These are the steps to reproduce:

Create a main function calling a function Foo() in some package:

Screenshot_20240714_131948

Renaming the function to FooBar work out pretty well:

Screenshot_20240714_132201

Renaming the function again results in an error:

Screenshot_20240714_132352

Neovim: 0.9.5 go.nvim: current master

ray-x commented 3 months ago

It works fine for me image Also could you update to neovim 0.10 as 0.9 is no longer supported.

hendrikiseke1979 commented 3 months ago

Having the same issue with 0.10: image

If I can provide anymore information, please let me know.

ray-x commented 3 months ago

Can this issue be reproduced with the following config init_lazy.lua

with

nvim -u init_lazy.lua main.go
hendrikiseke1979 commented 3 months ago

Can this issue be reproduced with the following config init_lazy.lua

with

nvim -u init_lazy.lua main.go

Unfortunately yes, the problem persists: image

antoineco commented 3 months ago

GoRename shells out to an external command, and should probably be avoided if you have a LSP client attached.

My intuition is that the file you are editing isn't being written after performing the first rename, so the file that is passed to the command on the second rename differs from what is shown in the Neovim buffer.

hendrikiseke1979 commented 3 months ago

My intuition is that the file you are editing isn't being written after performing the first rename, so the file that is passed to the command on the second rename differs from what is shown in the Neovim buffer.

I explicitly saved both files after the first rename, without any success. However, when I restart nvim after the first rename, things work just fine. Unfortunately, I don't know enough about the internals of neovim and plugins to debug this any further.

antoineco commented 3 months ago

Actually it seems like I was wrong about this:

GoRename shells out to an external command -- https://github.com/ray-x/go.nvim/issues/482#issuecomment-2247743882

The documentation might be a bit outdated (?). GoRename seems to consistently call lsprename(): https://github.com/ray-x/go.nvim/blob/033344ddfa3cd5cfd55037903264b2bb86691619/lua/go/commands.lua#L378-L380

Which itself calls vim.lsp.buf.rename(): https://github.com/ray-x/go.nvim/blob/033344ddfa3cd5cfd55037903264b2bb86691619/lua/go/rename.lua#L6-L10

I just tried performing :GoRename twice in a row on the same symbol, without writing the buffer between the two actions, and everything worked as expected across multiple project packages.


I did find some issue with the LSP key bindings provided by the plugin though.

If you have lsp_keymaps set to true (the default), <space>rn will call the old implementation (which I suspect to be broken):

https://github.com/ray-x/go.nvim/blob/033344ddfa3cd5cfd55037903264b2bb86691619/lua/go/lsp.lua#L66

This does seem like a bug, but you didn't mention anything about using this key binding, so I assume the problem is unrelated.

hendrikiseke1979 commented 3 months ago

Thanks @antoineco for your explanation. I do have this keymapping in my init.lua:

nmap('<leader>rn', vim.lsp.buf.rename, '[R]e[n]ame')

When I use this keymapping, it does not open the popup prompts for the new name on the bottom of the screen. This way, the renaming works, if I however call :GoRename it does not. I didn't mention the binding, because I could reproduce the issue with @ray-x's init_lazy.lua as well.