Skip to content

Instantly share code, notes, and snippets.

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
-- add runtime
-- add your config
-- add plugins
-- if you're not using packer, then you might need to change the paths below
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
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")
Copy link

Hrm. Ok, I'll try that.

Copy link

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.

Copy link

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

Copy link

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