As title.
Is that luacheck or sumnekko_lua or somehting else?
Yes, exactly. it’s sumnekko installed from a plug-in called nvim-lsp-installer. I want to do this because it triggers so many same warnings and looks ugly.
Pretty much what’s listed in the lspconfig docs: nvim-lspconfig/server_configurations.md at master · neovim/nvim-lspconfig · GitHub
But you just need to include vim
within it’s diagnostics.global
key settings:
require'lspconfig'.sumneko_lua.setup {
settings = {
Lua = {
diagnostics = {
-- Get the language server to recognize the `vim` global
globals = {'vim'},
},
},
},
}
Are these the default settings? it doesn’t work by default in my case. Now my issue become: where should I put this line? This is my current settings:
use {
'neovim/nvim-lspconfig',
config = function ()
vim.diagnostic.config({
virtual_text = false,
signs = true,
underline = true,
update_in_insert = true
})
end
}
Nevermind, I’m using williamboman/nvim-lsp-installer
, and the config that works is:
use {
-- WARNING: this plugin doesn't provide server update.
'williamboman/nvim-lsp-installer',
requires = {
'neovim/nvim-lspconfig',
'hrsh7th/nvim-cmp',
},
config = function()
local capabilities = require('cmp_nvim_lsp').update_capabilities(vim.lsp.protocol.make_client_capabilities())
local on_attach = require'config.on_attach'
require('nvim-lsp-installer').on_server_ready(
function(server)
local config = {
on_attach = on_attach[server.name],
capabilities = capabilities,
autostart = true,
settings = {
Lua = {
diagnostics = { globals = {'vim'} }
}
}
}
server:setup(config)
end
)
end
}
Hmmm, this will allow the vim
global when editing any Lua file at all, right? Not just Lua files in e.g. my dotfiles repository or my neovim configuration?
Yes, all lua files. If you want only for dotfiles/nvim config then you’ll have to configure it on a folder/project basis with a .nvimrc
file within the folder. But be careful with that as the guide mentions.
I was hoping to tackle that using per-project .luacheckrc files, like this one: .luacheckrc · main · Ron Waldon / dotfiles · GitLab
The problem, is that this works perfectly when running the linter manually from the command line, but for some reason the linter does not seem to use per-project .luacheckrc files when executed via LSP
The sumneko lua LSP and luacheck are two different tools. What was shown was for sumneko and not for luacheck.
If you also want to see luacheck diagnostics within nvim - instead of getting those messages from the command line - you can try out null-ls (GitHub - jose-elias-alvarez/null-ls.nvim: Use Neovim as a language server to inject LSP diagnostics, code actions, and more via Lua.) or efm-langserver (GitHub - mattn/efm-langserver: General purpose Language Server) which will give you luacheck diagnostics in addition to diagnostics provided from sumneko.
FWIW for anyone encountering this with lsp-zero and lua_ls the solution is listed here from their readme: https://github.com/VonHeikemen/lsp-zero.nvim/blob/v2.x/doc/md/api-reference.md#nvim_lua_lsopts
I highly recommend everyone use this instead of simply adding vim
to your globals. It has SO much more useful stuff, such as documentation for all lua functions, etc.
It’s leveled up my neovim config like 100x, and im surprised not to see it mentioned more
it fixes the global issue plus a lot more
Hi i had the same warning setting up LSP for lua, lua_ls, Undefined global ‘vim’.
i am using lua_ls
NVIM v0.10.0-dev
You could use one of the code actions that provide a few ways to avoid the warning
With lua_ls you can add global in configuration. If you are using lspconfig it would look like this:
require('lspconfig').lua_ls.setup({
settings = {
Lua = {
diagnostics = {
globals = {'vim'}
}
}
}
})
On vim.lsp.config setup
vim.lsp.config('lua_ls', {
settings = {
Lua = {
diagnostics = {
globals = { "vim" }
}
}
}
})
or just add .luarc.json on your config directory and get autocomplete.
{
"$schema": "https://raw.githubusercontent.com/LuaLS/vscode-lua/master/setting/schema.json",
"runtime": {
"version": "LuaJIT"
},
"workspace": {
"library": [
"/usr/share/nvim/runtime/lua",
"~/.local/share/nvim"
],
"checkThirdParty": "Disable"
},
"diagnostics": {
"groupFileStatus": {
"strict": "Opened",
"strong": "Opened"
},
"groupSeverity": {
"strong": "Warning",
"strict": "Warning"
},
"unusedLocalExclude": [
"_*"
]
}
}
Autocomplete for the nvim API would be sweet! I tried using this .luarc.json with the following lua_ls config, but that produces a lot of “cannot infer type” diagnostics.
Without the above .luarc.json, I get fewer diagnostics. Some examples:
Undefined field 'fs_stat'. [undefined-field]
onvim.uv.fs_stat
(see below in lua_ls.lua)- A similar message for
:get
ofvim.opt_local.colorcolumn:get()
Setting up type definitions for the Nvim API is surprisingly hard to research, so any help is appreciated.
LSP setup
- I use the
vim.lsp
API, mason to install LSP and mason-lsp just for theensure_installed
- LSP configs live in
lsp/
and then in init.luavim.lsp.enable({...lsps...})
lsp/lua_ls.lua
-- Settings copied from the starter file.
return {
cmd = { "lua-language-server" },
filetypes = { "lua" },
root_markers = {
".luarc.json",
".luarc.jsonc",
".luacheckrc",
".stylua.toml",
"stylua.toml",
"selene.toml",
"selene.yml",
".git",
},
settings = {
Lua = {
diagnostics = {
globals = {
"vim",
},
},
},
},
on_init = function(client)
if client.workspace_folders then
local path = client.workspace_folders[1].name
if
path ~= vim.fn.stdpath("config")
and (vim.uv.fs_stat(path .. "/.luarc.json") or vim.uv.fs_stat(path .. "/.luarc.jsonc"))
then
return
end
end
client.config.settings.Lua = vim.tbl_deep_extend("force", client.config.settings.Lua, {
runtime = {
-- Tell the language server which version of Lua you're using (most
-- likely LuaJIT in the case of Neovim)
version = "LuaJIT",
-- Tell the language server how to find Lua modules same way as Neovim
-- (see `:h lua-module-load`)
path = {
"lua/?.lua",
"lua/?/init.lua",
},
},
-- Make the server aware of Neovim runtime files
workspace = {
checkThirdParty = false,
library = {
vim.env.VIMRUNTIME,
},
},
})
end,
}