LSP configuration for lua

vim.api.nvim_create_autocmd('FileType', {
  group = lspautocmds,
  pattern = 'lua',
  callback = function(ev)
    vim.lsp.start({
      name = "lua_ls",
      cmd = { 'lua-language-server' },
      root_dir = vim.fs.root(ev.buf, { '.luarc.json', '.luarc.jsonc' }),
      on_init = function(client)
        client.settings.Lua = {
          runtime = {
            version = "LuaJIT",
          },
          workspace = {
            library = {
              "/usr/share/nvim/runtime",
              "/usr/lib/lua-language-server/meta/3rd/luv/library"
            },
          },
        }
      end,
      settings = {
        Lua = {},
      },
    })
  end,
})

Note that we declare an empty settings = { Lua = {} }. This is required and it is the reason why we're able to set client.settings.Lua in on_init function.


Comments