How to apply multiple auto fixes with nvim LSP - neovim

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.

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 :
https://github.com/neovim/nvim-lspconfig#quickstart
https://github.com/neovim/nvim-lspconfig/blob/master/doc/server_configurations.md#eslint
https://github.com/neovim/nvim-lspconfig/blob/master/doc/server_configurations.md#tsserver
N.B. You should probably add your keymap related to LSP in the on_attach callback

Related

mason lsp pyright seems to be download but dosen't work on vim

I have a problem on my nvim LSP.
according to mason plugin the pyright is downloaded and I on other IDE the lsp works great.
when i open python file with neovim it didn't recognized import and basic functions.
someone can handle this situation?
maybe it's because Lspinfo tell me that it didn't get the python root directory?import don't auto complete pythonpyright is downloaded via masonLSPInfo description
on other languages it works so don't really understand the problem
Same as me, at the moment I solved like this (it happens in some languages for me too):
local status_ok, mason = pcall(require, "mason")
if not status_ok then
return
end
local status_masonlsp_ok, mason_lspconfig = pcall(require, "mason-lspconfig")
if not status_masonlsp_ok then
return
end
local servers = {
"sumneko_lua",
"rust_analyzer",
"tailwindcss",
"bashls",
"pyright",
-- "csharp_ls",
"html",
"omnisharp",
"gopls",
}
mason.setup(settings)
mason_lspconfig.setup({
ensure_installed = servers,
automatic_installation = true,
})
So when I open nvim they are installed automatically.

Spawning language server with cmd`diagnostic-languageserver`failed. The language server is either not installed, missing from PATH, or not executable

enter image description here
" Spawning language server with cmd: diagnostic-languageserver failed. The language server is either not installed, missing from PATH, or not executable "
nvim problem
i copied devaslife's dotfiles and installed plugin
yesterday it work, but today i entered LSP info after that when i Enter shows that the error
I've resolved this problem by executing the below command
npm install -g diagnostic-languageserver
Use this:
yarn global add diagnostic-languageserver
Probably, it's because you have not set up the right programming language that you are using in the lspconfig.rc.vim, precisely in the nvim_lsp.tsserver.setup part.
I had the same problem because I was in javascript file (.js) and the lsp config that I was using from someone else only supports typescript files. So, I just had to add the javascript, javascriptreact and javascript.jsx. For example :
nvim_lsp.tsserver.setup {
on_attach = on_attach,
filetypes = { "typescript", "typescriptreact", "typescript.tsx", "javascript", "javascriptreact", "javascript.jsx" },
capabilities = capabilities
}
That is not working for me. In the lspconfig.re.vim, i had to delete the "javascipt", "javasriptreact", "javascript.jsx" types from the nvim_lsp.diagnosticls.setup/filetypes, and then put them in the nvim_lsp.tsserver.setup>filetypes
For windows you can use this:
nvim_lsp.tsserver.setup {
on_attach = on_attach,
filetypes = { "typescript", "typescriptreact", "typescript.tsx" },
cmd = { "typescript-language-server.cmd", "--stdio" }
}
This will make sure it will run the .cmd file that is along side the .ps1.

How to use neovim lsp keybindings?

I have neovim 0.6.1 configured with some LSPs and using the default configurations found here: https://github.com/neovim/nvim-lspconfig#suggested-configuration. Some of the key bindings such as vim.api.nvim_buf_set_keymap(bufnr, 'n', '<space>rn', '<cmd>lua vim.lsp.buf.rename()<CR>', opts) are not working.
The vim.lsp.buf.rename() command works correctly if I call it manually like :lua vim.lsp.buf.rename().
If I do :noremap <space>rn :lua vim.lsp.buf.rename()<CR> during a session the rename keybinding works correctly.
The default config from nvim-cmp was also calling setup{} which effectively cleared the on_attach callback required to enable the lsp keybindings.

How do I setup elixir-ls using nvim-lspconfig with autocompletion in neovim?

I would like to set up the Elixir language server in Neovim using the built-in language server client and nvim-lspconfig.
Documentation for this seems to be spread out in multiple places:
nvim-lspconfig README
nvim-lspconfig wiki about autocomplete
nvim-lspconfig elixir-ls server configuration documentation
elixir-ls installation instructions
I am a little overwhelmed and have made multiple attempts to do this, but always give up without success. I also found a useful looking guide: How to Set Up Neovim for Elixir Development, but it makes quite a few assumptions, seems to eroneously do some configuration twice, and also switches config format halfway through, so wasn't a usable summary for me (after following the instructions, documentation popups were not working, and I was unable to scroll inside the autocomplete popups - I also had a lot of copy/pasted config I didn't understand).
So far I understand the required steps are:
Install neovim
Install elixir-ls manually (it doesn't seem to be possible currently to install via asdf due to a lack of ability to ask elixir-ls for its version)
Install required neovim plugins: nvim-lspconfig + whatever is required for autocomplete
Set up necessary config for nvim-lspconfig and autocomplete.
I have managed to do up to part-way through step 3, but have not sucesfully worked out the required dependencies and configuration for autocomplete.
What do I need to do to have a working elixir-ls setup in neovim, with autocomplete, using nvim-lspconfig and neovim's built-in language server client?
I managed to get everything working with the combination of some plugins for completion in neovim. They are:
hrsh7th/nvim-cmp - the autocompletion plugin
hrsh7th/cmp-nvim-lsp - the integration of nvim-cmp with neovim's LSP
L3MON4D3/LuaSnip - a snippets plugin
saadparwaiz1/cmp_luasnip - the snippets source for nvim-cmp
This is the same list of plugins from the nvim-cmp recommendation of neovim's Wiki.
I'm using packer to manage the plugins. I'm also installing the plugins before configuring them.
Here is the installation lines:
use 'neovim/nvim-lspconfig'
use 'nvim-treesitter/completion-treesitter' -- Only if you are using TS
use 'hrsh7th/nvim-cmp' -- Autocompletion plugin
use 'hrsh7th/cmp-nvim-lsp' -- LSP source for nvim-cmp
use 'saadparwaiz1/cmp_luasnip' -- Snippets source for nvim-cmp
use 'L3MON4D3/LuaSnip' -- Snippets plugin
And then the configuration part:
-- Add additional capabilities supported by nvim-cmp
local capabilities = vim.lsp.protocol.make_client_capabilities()
capabilities = require('cmp_nvim_lsp').update_capabilities(capabilities)
-- Configure ElixirLS as the LSP server for Elixir.
require'lspconfig'.elixirls.setup{
cmd = { "/home/my-user/path-to/elixir-ls/release/language_server.sh" },
-- on_attach = custom_attach, -- this may be required for extended functionalities of the LSP
capabilities = capabilities,
flags = {
debounce_text_changes = 150,
},
elixirLS = {
dialyzerEnabled = false,
fetchDeps = false,
};
}
local luasnip = require 'luasnip'
-- nvim-cmp
local cmp = require 'cmp'
cmp.setup {
snippet = {
expand = function(args)
require('luasnip').lsp_expand(args.body)
end,
},
mapping = {
['<C-p>'] = cmp.mapping.select_prev_item(),
['<C-n>'] = cmp.mapping.select_next_item(),
['<C-d>'] = cmp.mapping.scroll_docs(-4),
['<C-f>'] = cmp.mapping.scroll_docs(4),
['<C-Space>'] = cmp.mapping.complete(),
['<C-e>'] = cmp.mapping.close(),
['<CR>'] = cmp.mapping.confirm {
behavior = cmp.ConfirmBehavior.Replace,
select = true,
},
['<Tab>'] = function(fallback)
if cmp.visible() then
cmp.select_next_item()
elseif luasnip.expand_or_jumpable() then
luasnip.expand_or_jump()
else
fallback()
end
end,
['<S-Tab>'] = function(fallback)
if cmp.visible() then
cmp.select_prev_item()
elseif luasnip.jumpable(-1) then
luasnip.jump(-1)
else
fallback()
end
end,
},
sources = {
{ name = 'nvim_lsp' },
{ name = 'luasnip' },
},
}
You can find all my neovim config in my dotfiles repo: https://github.com/philss/dotfiles
For reference, here is the nvim version I'm using (installed from source):
NVIM v0.7.0-dev+864-g2818de8b7
Build type: RelWithDebInfo
LuaJIT 2.1.0-beta3
Features: +acl +iconv +tui
In addition to the accepted answer, I suggest installing language server via Homebrew or similar instead of cloning repo and compiling. Philip's answer is correct. Just make your life easier by using a package manager for language server installation.
If you use LunarVim, which is like a NeoVim IDE, the configuration is minimal.
Nearly all dependencies for completion are already installed.
I also found it is better for Mac users to use Homebrew to install Elixir Language Server like so:
brew install elixir-ls
You don't need to keep track of the updates manually. It is usually upgraded along with other dev dependencies via brew update && brew upgrade.
Try this for LunarVim:
-- generic LSP settings
--
-- ---#usage disable automatic installation of servers
-- lvim.lsp.automatic_servers_installation = false
local lspconfig = require("lspconfig")
-- Neovim doesn't support snippets out of the box, so we need to mutate the
-- capabilities we send to the language server to let them know we want snippets.
local capabilities = vim.lsp.protocol.make_client_capabilities()
capabilities.textDocument.completion.completionItem.snippetSupport = true
-- ---configure a server manually. !!Requires `:LvimCacheReset` to take effect!!
-- ---see the full default list `:lua print(vim.inspect(lvim.lsp.automatic_configuration.skipped_servers))`
-- vim.list_extend(lvim.lsp.automatic_configuration.skipped_servers, { "pyright" })
-- local opts = {} -- check the lspconfig documentation for a list of all possible options
-- require("lvim.lsp.manager").setup("pyright", opts)
-- Elixir LS
lspconfig.elixirls.setup {
cmd = { "/usr/local/Cellar/elixir-ls/0.10.0/libexec/language_server.sh" },
-- on_attach = custom_attach, -- this may be required for extended functionalities of the LSP
capabilities = capabilities,
flags = {
debounce_text_changes = 150,
},
elixirLS = {
dialyzerEnabled = false,
fetchDeps = false,
};
}

parcel watch only detects first file change

I have the following in ./js/parcel/build-js.js (it is more or less a simplification of exactly what the API docs example does, except that it takes an optional --watch argument):
#!/usr/bin/env node
const Bundler = require('parcel-bundler');
const path = require('path');
const watch = process.argv.indexOf('--watch') > 0;
if (watch) console.log('Watching files...');
(async function bundleJs() {
const jsBundler = new Bundler(path.join(__dirname, '../src/common.js'), {
watch,
hmr: false,
});
jsBundler.on('bundled', () => {
console.log('bundled!');
});
const bundle = await jsBundler.bundle();
console.log('done');
})();
When I run node js/parcel/build-js.js --watch, it detects the first change to src/common.js and prints:
Watching files...
✨ Built in 585ms.
bundled!
done
This is as I'd expect. When I edit and save src/common.js, it sees that and then the total output becomes (done gets deleted):
Watching files...
✨ Built in 585ms.
bundled!
✨ Built in 86ms.
bundled!
But after that, no file changes are detected. I make changes and save but it just sits there, producing no more output or updating the build. Why only once?
Note: If I do strace node js/parcel/build-js.js --watch, it seems to just sit on an unfinished epoll_wait(3,, which I guess means it's waiting for something, but maybe watching the wrong file...
Edit: Versions!
parcel-bundler: 1.12.3
node: 10.15.1
Ubuntu 18.04
Edit: using parcel watch
This appears to be a system-wide thing for me. I did yarn globals add parcel (which also installed 1.12.3), and now watching any JS file with parcel watch path/to/file.js does the same thing.
It turned out to be a conflict between Parcel's change detection and the default Vim setup. From the Hot Module Replacement docs:
Some text editors and IDE's have a feature called safe write that basically prevents data loss, by taking a copy of the file and renaming it when saved.
When using Hot Module Reload (HMR) this feature blocks the automatic detection of file updates, to disable safe write use the options provided below:
I added set backupcopy=yes to my .vimrc and it started working.
The solution for other editors is documented there as well.
It is a Parcel issue! I dropped it (until they fix it)
IMHO: I do not have to change my editor's behavior just to make bundler work correctly. (webpack works fine in the situation)