coc.nvim icon indicating copy to clipboard operation
coc.nvim copied to clipboard

Changed CodeAction behaviour makes it harder to use with rust-analyzer

Open TimoFreiberg opened this issue 6 years ago • 16 comments

A few days ago, coc-codeaction was changed to run on the whole buffer, and coc-codeaction-line was introduced. Rust-analyzer only offers its assists if the selection is small enough, therefore now I have to use something like nmap <leader>a v<Plug>(coc-codeaction-selected) to make <leader>a request codeactions for the current character. (A normal mode binding to just <Plug>(coc-codeaction-selected) will wait until i hit a movement key like L)

Is that expected behaviour? If so, I can deal with it, I just wanted to report the small inconvenience :)

TimoFreiberg avatar May 23 '20 09:05 TimoFreiberg

Use motion and text object or visual mode is recommended

chemzqm avatar May 23 '20 09:05 chemzqm

coc-codeaction-line should provide former behavior? If so, why not pick it instead?

oblitum avatar May 23 '20 21:05 oblitum

Not sure if this is exactly the right place to mention it, but for many of rust-analyzer's code actions, it depends on which character the start of the range is on, so for many actions (including the "Merge import" action), it's very difficult to trigger this from coc. You need to use <Plug>coc-codeaction-selected to trigger it, and it's even more difficult to simply get the list of available actions for the current cursor position, as you would need for :CocList actions. The only way I've found is to manually set the '[ and '] marks and call CocAction("cocActions","v") (Interestingly as well, CocAction with visual mode doesn't use '< and '> which are the visual selection marks, and instead uses '[ and '] which is for motion operators AFAIK?). Some other way of doing this, such as having a way to trigger the code actions for just the cursor position, or at least some easier way to get them to appear when using :CocList actions would be great

FuZzledd avatar May 27 '20 14:05 FuZzledd

The same thing happens with flutter lsp too. Actions like Wrap with Padding etc. only appear when at least one character is selected. I tried using coc-codeaction-line. But that does not provide this behavior. @TimoFreiberg 's keybinding of nmap <leader>a v<Plug>(coc-codeaction-selected) works perfect though.(Thanks a lot for that). On a side note, I use CocFzf and have <leader> a mapped to CocFzfList actions so that the actions show up in an fzf popup. This no longer works.

ninjawithaneedle avatar May 30 '20 18:05 ninjawithaneedle

The same thing happens with flutter lsp too. Actions like Wrap with Padding etc. only appear when at least one character is selected. I tried using coc-codeaction-line. But that does not provide this behavior. @TimoFreiberg 's keybinding of nmap <leader>a v<Plug>(coc-codeaction-selected) works perfect though.(Thanks a lot for that). On a side note, I use CocFzf and have <leader> a mapped to CocFzfList actions so that the actions show up in an fzf popup. This no longer works.

Does CocFzf not work with all lists, or just actions? If it's the former then that should be fixed in master

FuZzledd avatar May 30 '20 18:05 FuZzledd

It does work with all lists. Also with actions. But the behavior has changed. It used to show actions for the word under cursor too. Those don't come up now.

ninjawithaneedle avatar May 30 '20 18:05 ninjawithaneedle

I was looking for why codeactions are no longer working all over the place. I can confirm that the behavior of the new coc-codeaction-line is different from the old coc-codeaction.

For an example:

fn foo() -> S|tring { // the | character being the cursor position
}

In the older versions, executing coc-codeaction would bring up "Convert return type to Result". In the current version, neither coc-codeaction, or coc-codeaction-line brings up this option, you only get "Change visibility to pub(crate)".

norbertkeri avatar Jul 06 '20 14:07 norbertkeri

@TimoFreiberg 's keybinding of nmap <leader>a v<Plug>(coc-codeaction-selected) works perfect though

This is also working for me. Just wanted to repeat it here as I overlooked it above before.

But there is a minor issue which is that if you accept a code action you will still be in visual mode after.

JoshMcguigan avatar Oct 23 '20 04:10 JoshMcguigan

I'm glad my tip is helping people :) ~I started using https://github.com/iamcco/coc-actions a while ago instead of the quoted binding, and I'm very happy with that too.~ Nevermind, the default implementation is at least as good as coc-actions now

TimoFreiberg avatar Oct 23 '20 06:10 TimoFreiberg

@TimoFreiberg do you mind sharing the advantages you see using coc-actions over the built-in popup list for actions in coc.nvim? I've reviewed the readme but I'm not quite sure what the benefits are. Was this perhaps created before coc.nvim supported vim popups?

JoshMcguigan avatar Oct 24 '20 02:10 JoshMcguigan

@JoshMcguigan floating windows for code actions is quite recent, I was a coc-actions user but just removed it due to the new built-in implementation.

oblitum avatar Oct 24 '20 02:10 oblitum

Well thanks for updating me :D true, I no longer need coc-actions

TimoFreiberg avatar Oct 24 '20 11:10 TimoFreiberg

But there is a minor issue which is that if you accept a code action you will still be in visual mode after.

Just put your command key sequence at the end of it like so: nmap <leader>a v<Plug>(coc-codeaction-selected)<C-C> Or replace <C-C> with <Esc>

This will take you out of visual mode

jeanlucthumm avatar Dec 21 '20 04:12 jeanlucthumm

@chemzqm I think this is still an open problem in CoC. (Note that I'm not a CoC user, but a rust-analyzer maintainer.) I think providing special commands for running code action on the whole buffer or the current line doesn't make sense; and on the other hand, it seems there is no way of running code actions with an empty range?

My impression is that there's a different interpretation of what the range in the code action request means at play here. Getting code actions with a certain range is not the same thing as getting all possible code actions for that range (/ that can affect that range). At least rust-analyzer does not interpret it that way, and instead chooses to use the range basically as an input to the code actions (so we'll return actions that will do something with the given range, e.g. extract it to a new variable). I think the spec is unclear on this, but VSCode uses it that way (sending the current selection as the range, or an empty range if there's no selection), and Emacs does the same. rust-analyzer will send lots of code actions if you provide it an empty range at a certain point.

flodiebold avatar Feb 22 '21 12:02 flodiebold

@flodiebold coc haș coc-codeaction-selected.

rust-analyzer will send lots of code actions if you provide it an empty range at a certain point

Yes, tested with VSCode and coc-codeaction-selected, RA will send only a code action, but coc-codeaction-selected - char will send more code actions.

fannheyward avatar Feb 24 '21 10:02 fannheyward

As of #2914, I've replaced the workarounds above with <Plug>(coc-codeaction-cursor). Thanks all for the work on this!

JoshMcguigan avatar Mar 04 '21 04:03 JoshMcguigan