Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Correct sumneko lua lsp setup for init.lua and plugin development
-- put this file somewhere in your nvim config, like: ~/.config/nvim/lua/config/lua-lsp.lua
-- usage: require'lspconfig'.sumneko_lua.setup(require("config.lua-lsp"))
local library = {}
local path = vim.split(package.path, ";")
-- this is the ONLY correct way to setup your path
table.insert(path, "lua/?.lua")
table.insert(path, "lua/?/init.lua")
local function add(lib)
for _, p in pairs(vim.fn.expand(lib, false, true)) do
p = vim.loop.fs_realpath(p)
library[p] = true
end
end
-- add runtime
add("$VIMRUNTIME")
-- add your config
add("~/.config/nvim")
-- add plugins
-- if you're not using packer, then you might need to change the paths below
add("~/.local/share/nvim/site/pack/packer/opt/*")
add("~/.local/share/nvim/site/pack/packer/start/*")
return {
-- delete root from workspace to make sure we don't trigger duplicate warnings
on_new_config = function(config, root)
local libs = vim.tbl_deep_extend("force", {}, library)
libs[root] = nil
config.settings.Lua.workspace.library = libs
return config
end,
settings = {
Lua = {
runtime = {
-- Tell the language server which version of Lua you're using (most likely LuaJIT in the case of Neovim)
version = "LuaJIT",
-- Setup your lua path
path = path
},
completion = { callSnippet = "Both" },
diagnostics = {
-- Get the language server to recognize the `vim` global
globals = { "vim" }
},
workspace = {
-- Make the server aware of Neovim runtime files
library = library,
maxPreload = 2000,
preloadFileSize = 50000
},
-- Do not send telemetry data containing a randomized but unique identifier
telemetry = { enable = false }
}
}
}
-- put this file somewhere in your nvim config, like: ~/.config/nvim/lua/types.lua
-- DONT require this file anywhere. It's simply there for the lsp server.
-- this code seems weird, but it hints the lsp server to merge the required packages in the vim global variable
vim = require("vim.shared")
vim = require("vim.uri")
vim = require("vim.inspect")
-- let sumneko know where the sources are for the global vim runtime
vim.lsp = require("vim.lsp")
vim.treesitter = require("vim.treesitter")
vim.highlight = require("vim.highlight")
@mnarrell
Copy link

mnarrell commented May 12, 2021

This merging of the "required" packages into the vim global variable seems to cause problems:

attempt to index field 'g' (a nil value)
attempt to call field 'split' (a nil value)

@folke
Copy link
Author

folke commented May 12, 2021

@mnarell where do you get that? You should NOT require this file anywhere, so it should never execute.

This file is something the lsp server will pickup, but again, DONT require this file. That is not needed and will obviously lead to problems.

@mnarrell
Copy link

mnarrell commented May 12, 2021

Hrm. Ok, I'll try that.

@dhruvmanila
Copy link

dhruvmanila commented May 17, 2021

There seems to be a small problem. So, here's my setup: ~/.config/nvim is a symlink to ~/dotfiles/config/nvim and when I open neovim with ~/dotfiles as the root directory, the renaming does not work.

Removing the line ~/.config/nvim fixes the issue and I think that's what you are trying to do with on_new_config callback, I suppose. But, in this case the root dir expands into ~/dotfiles and config dir expands into ~/dotfiles/config/nvim. I think the fix would be to suggest to include the dotfiles root dir instead of the config dir if that's a symlink.

@folke
Copy link
Author

folke commented May 19, 2021

@dhruvmanila I've turned this gist in a plugin. It probably also fixes your issue. I have a similar setup.

On top of a working lua config, the plugin also comes with EmmyLua annotations for the nvim Lua API

@dhruvmanila
Copy link

dhruvmanila commented May 19, 2021

That's awesome, thanks for doing this! I will install it and check soon.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment