Coc.nvim: Organize Imports on save?

Created on 20 Jun 2019  路  15Comments  路  Source: neoclide/coc.nvim

Is your feature request related to a problem? Please describe.
I use coc.nvim for Go and the recommended (and as far as I can tell the only one that works properly) server is a fork of gopls (here). That tool supports organizeImports through something other than the format action (for now at least?).
I'd like to be able to run the organizeImports command on save.

Describe the solution you'd like
Either something similar to coc.preferences.formatOnSaveFiletypes or a simple way to run the organizeImport command that doesn't pollute the screen. More generally, a way to silence commands properly (or to respect :silent) would be really nice.

Describe alternatives you've considered
I've done:

" use `:OR` for organize import of current buffer
command! -nargs=0 OR :call CocAction('runCommand', 'editor.action.organizeImport')
autocmd BufWritePre *.go :OR

But there's no good way to silence that command, and it will error with [coc.nvim] Orgnize import action not found. when there are no actions to be taken, additionally, it always prints out [coc.nvim] 1 buffers changed. even when coc.preferences.messageLevel is set to error.

All 15 comments

I managed to silence those messages with the config file. coc#config() didn't serve to silence those for some reason.

It's neovim's bug that silent doesn't work for echo during rpc request

Makes sense, I managed to get around the issue by using the config file. Not sure why coc#config() doesn't do it for coc.preferences.messageLevel.

I think what's really left of this "request" at this point is maybe a feature equivalent to codeActionsOnSave that VSCode has where we could put organizeImport instead of setting up our own hooks.

I tried

  call coc#config('coc.preferences', {
    \ 'messageLevel': 'error',
    \})

works as expected.

think what's really left of this "request" at this point is maybe a feature equivalent to codeActionsOnSave that VSCode

Will not add codeActionsOnSave, because you can't configure it according to filetype and some extensions already provide some kind of on save support.

Use organize import on save is a bad practice IMO, since it will remove unused import you have just added.

call coc#config('coc.preferences.messageLevel', 'error') did not work for me. I can try your way later on and debug why that's the case.
What about codeActionsOnSave is not configurable by filetype? we already have a per file type languageserver configuration block, VSCode seems to have a per-filetype general configuration block as well although I haven't really used it to see how well it works.

As for "bad practice" that's very dependent on language/process. In Go, most of the developers I've seen will use the package before the import line is added, and rely on the organizeImports action to add the missing import. Up until gopls I've relied solely on goimports to do both formatting and importing at the same time (this is easily doable through vim-go, but that plugin has a few issues with resetting folds when auto formatting). I think this is a common process for Go developers and not at all bad practice.

But there's no per-filetype configuration support with coc.nvim.
It's just my opinion, you can use BufWritePre of cause.

I've created the issue report: https://github.com/neovim/neovim/issues/10310

How about providing coc.preferences.organizeImportsOnSave and coc.preferences.organizeImportsOnSaveFiletypes like we have for formatOnSave?

No, because I think organize import on save is a bad idea, it might remove import you have just added which is not using.

I understand that's your opinion @chemzqm but this is pretty common for go developers, for example, I almost never add imports manually and rely on goimports or gopls to do that for me.
I'm not asking to make it the default, I'm asking to make it available with coc.preferences.organizeImportsOnSave and coc.preferences.organizeImportsOnSaveFiletypes we can choose to turn it on and specify for which file types to turn it on for.

I think coc should enable developers to choose their workflow.

Isn't it just a simple configuration you can do with autocmd? Besides agreeing with @ chemzqm, I think it seems trivial to setup yourself, and too specific of gopls, which should be provided, if ever, in a coc-go extension? If coc.nvim does anything, it should do as @ luan suggested, which is to provide a generic codeActionsOnSave, then you can put organizeImports yourself, but it wouldn't be that much simpler than setting up an autocmd.

That's a valid point but in that case why is format on save provided and why not use an autocmd for that as well?

Can't state why, but personally I've always viewed formatting as a more general task and command to have, hence many other tools/clients provide it, and not some random action.

But coc.nvim could go the generalization route in the end, in case to provide cocActionsOnSave, and put formatting under the same belt.

That's a valid point but in that case why is format on save provided and why not use an autocmd for that as well?

Organize imports have side effect and different extensions could have different code action names for organize imports.

You can use autocmd for format on save as well.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

svenstaro picture svenstaro  路  4Comments

marene picture marene  路  3Comments

zhou13 picture zhou13  路  3Comments

skylite21 picture skylite21  路  3Comments

MacGuffinLife picture MacGuffinLife  路  4Comments