ray-x / go.nvim

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

How to extract method, variable etc. using this plugin? #207

Open LintaoAmons opened 2 years ago

LintaoAmons commented 2 years ago

I saw in the README, that I can do the refactor stuff using your plugin.

Code refactor made easy: GoFixPlural, FixStruct, FixSwitch, Add comment, IfErr, ModTidy, GoGet, extract function/block with codeactions... Most of the tools are built on top of treesitter AST or go AST. It is fast and accurate.

But I didn't found the command to do the refactor... So could you please tell me how to achieve that?

alphatroya commented 2 years ago

Снимок экрана 2022-09-01 в 22 11 26 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.

ray-x commented 2 years ago

Those are handled by codeaction with gopls.

image
LintaoAmons commented 2 years ago

Sorry, I quite new to lsp stuff... How to trigger the codeaction like your picture?

ray-x commented 2 years ago

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.

LintaoAmons commented 2 years ago

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?

go-code-action

etrnal70 commented 2 years ago

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)

ray-x commented 2 years ago

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.

ray-x commented 2 years ago

@LintaoAmons Can you confirm if it works for you?

LintaoAmons commented 2 years ago

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

gonzaloserrano commented 1 year ago

Same here.

alphatroya commented 1 year ago

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.

ray-x commented 1 year ago

not every piece of go code has range codeaction But you should see something like this if code action available: image

gonzaloserrano commented 1 year ago

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.

ray-x commented 1 year ago

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.

gonzaloserrano commented 1 year ago

Sorry, what I mean is: once I map the code action, I'm given several options, i.e CleanShot 2022-11-22 at 11 33 03 Is there any way to map the options?

ray-x commented 1 year ago

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.

phonzia commented 1 year ago

Could you provide a example of lsp and code action map configuration?

testinfected commented 1 year ago

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 🙏

ray-x commented 1 year ago

In Visual mode you should see this:

image

Please use :'<,'>GoCodeAction

testinfected commented 1 year ago

I'm only seeing Blame line, which comes from null-ls

Screenshot 2023-08-22 at 00 07 31

With lsp code actions, I see:

Screenshot 2023-08-22 at 00 08 46
ray-x commented 1 year ago

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.

testinfected commented 1 year ago

Do you see anything suspicious?

Screenshot 2023-08-23 at 09 06 40
ray-x commented 1 year ago

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.

testinfected commented 1 year ago

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

testinfected commented 1 year ago

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" })

Screenshot 2023-08-23 at 09 27 17
testinfected commented 1 year ago

I turned on the verbose option, and I get this message each time it loads the go file:

Screenshot 2023-08-23 at 09 34 05

My gopls is 0.13.2:

Screenshot 2023-08-23 at 09 36 15

I ran :GoUpdateBinary gopls, but the message comes back

Any idea?

testinfected commented 1 year ago

Something is wrong cause I get No code actions even if the playground go project using the provided init.lua config

testinfected commented 1 year ago

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

Screenshot 2023-08-23 at 16 09 44
gonzaloserrano commented 11 months ago

Yeah, I just noticed that too.

A selection with visual line does not work:

CleanShot 2023-10-06 at 16 57 57@2x

But if I do block selection and just get the characters, then it works:

CleanShot 2023-10-06 at 16 58 11@2x
ray-x commented 11 months ago

Yes, there is something incorrect with recent release of neovim. This one should have been fix with updates listed above