Skip to content

Instantly share code, notes, and snippets.

Last active March 7, 2023 19:17
  • Star 32 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save folke/fe5d28423ea5380929c3f7ce674c41d8 to your computer and use it in GitHub Desktop.
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

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)

Copy link

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.

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