Open LintaoAmons opened 2 years ago
vim-go has a :GoFreevars command. It's a very useful during large method extraction. As I see it, this plugin does not yet have a similar command.
Those are handled by codeaction with gopls.
Sorry, I quite new to lsp stuff... How to trigger the codeaction like your picture?
Depends on your setup
GoCodeAction
provided by go.nvim
Or other lsp plugins that have code action supported. Above image is captured with navigator.lua which hook to lsp.
I have add go.nvim
and use mason
to installed all options it provided that start with go.*
But when I use GoCodeAction
it told me that there's no action
available?
It works for me with standard code action call (not :GoCodeAction
). You need to use code action on ranges (visual mode on the conditional block)
The visual mode only works with keybindings.
I update the command so it will work in command mode as well
Please update the plugin and use GoCodeAction
in visual mode.
@LintaoAmons Can you confirm if it works for you?
I don't know where I went wrong?
I update the go.nvim
plugin, and be able to use GoCodeAction
command with selected range .
But still No code action available
...
Don't know why
Same here.
I've done the same behaviour by using refactoring.nvim plugin. If you configure the null-ls.nvim plugin buildin, all refactoring suggestions will be passed to LSP code action menu.
not every piece of go code has range codeaction
But you should see something like this if code action available:
I've migrated to my own config from lunarvim (you can see how here https://github.com/gonzaloserrano/dotfiles/tree/master/nvim) and now :GoCodeAction
works!
Is there any way to map one of the actions? I.e I use extract func a lot, so I'd like to do :GoCodeAction 2
or similar.
If you enabled lsp_keymaps
, the default mapping:
buf_set_keymap('n', '<space>ca', "<cmd>lua require('go.codeaction').run_code_action()<CR>", opts)
buf_set_keymap('v', '<space>ca', "<cmd>lua require('go.codeaction').run_range_code_action()<CR>", opts)
You can set mapping key of your own. Please check lsp.lua on how it is mapped.
Sorry, what I mean is: once I map the code action, I'm given several options, i.e
Is there any way to map the options?
yes and no. As there are lots of methods provided by gopls. I did not map those. I can do that later. ATM, in UI menu, you can press 1~9 to navigate to the menu item.
Could you provide a example of lsp and code action map configuration?
Thanks for the great plugin @ray-x 🥇
Any idea why I only get blame code when using :GoCodeAction
but I get more options with vim.lsp.buf.code_action()
, such as the option to extract function?
I am in visual mode, lines selected. I'm using mason with lsp_cfg = false
and
local cfg = require('go.lsp').config()
require('lspconfig').gopls.setup(cfg)
as suggested in the docs.
Thanks 🙏
In Visual mode you should see this:
Please use :'<,'>GoCodeAction
I'm only seeing Blame line, which comes from null-ls
With lsp code actions, I see:
Look to me when you call GoCodeAction
, the gopls was not attached. Could you check your Gopls status when error happens?
You can run :LspInfo
to get info.
Do you see anything suspicious?
It looks good. Could you try to disable null-ls and try it again? I have a feeling is gopls is too slow in your project.
It looks good. Could you try to disable null-ls and try it again? I have a feeling is gopls is too slow in your project.
Now I get a No code actions available
What I don't understand is that the following mapping works:
vim.keymap.set('v', '<leader>ca', vim.lsp.buf.code_action, { desc = "Range code action" })
I turned on the verbose option, and I get this message each time it loads the go file:
My gopls is 0.13.2:
I ran :GoUpdateBinary gopls
, but the message comes back
Any idea?
Something is wrong cause I get No code actions even if the playground go project using the provided init.lua
config
Ok good news, finally figured it out. It turns out the visual selection is pretty picky. For instance it does not work with visual line selection. Also you've got to be pretty precise with your selection (don't include an extra CR for instance) otherwise it won't work.
@ray-x Do you think you could make it more tolerant with the selection (i.e. support visual line mode, tolerate extra spaces and newlines, etc.)
Yeah, I just noticed that too.
A selection with visual line does not work:
But if I do block selection and just get the characters, then it works:
Yes, there is something incorrect with recent release of neovim. This one should have been fix with updates listed above
I saw in the README, that I can do the refactor stuff using your plugin.
But I didn't found the command to do the refactor... So could you please tell me how to achieve that?