How to apply multiple auto fixes with nvim LSP

2.3k views Asked by At

In nvim, using LSP, i want to be able to apply a 'fix all of the same type' similar to what can be done in VSCode (see picture). How would one go about adding this to LSP code actions?

Edit: I know this can be done using COC. But i want it for LSP. VSCode example

1

There are 1 answers

1
Philippe Turner On

It depends on the language server you are using, in my current NeoVim configuration I'm running both ESLint and tsserver and with a default configuration of ESLint in my root of my project, I get these types of code action.

Assuming you are using lspconfig plugin you need to make sure you have the server configured :

local capabilities = vim.lsp.protocol.make_client_capabilities()
capabilities.textDocument.completion.completionItem.snippetSupport = true
require('lspconfig')['tsserver'].setup {
  on_attach = function() end,
  capabilities = capabilities,
}
require('lspconfig')['eslint'].setup {
  on_attach = function() end,
  capabilities = capabilities,
}

You can then run in neovim : :lua vim.lsp.buf.code_action() in command mode (n)

You need both LSP installed manually

Ref :

N.B. You should probably add your keymap related to LSP in the on_attach callback