Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs(lua_ls): change suggested setup to work on both nightly and 0.9.5 #3054

Merged
merged 1 commit into from
Mar 5, 2024
Merged

docs(lua_ls): change suggested setup to work on both nightly and 0.9.5 #3054

merged 1 commit into from
Mar 5, 2024

Conversation

abeldekat
Copy link

lsp_on_init.mp4

In neovim nightly, the current suggested config fails. I added a video demo, using this repro:

--- CHANGE THESE
local pattern = "lua"
local cmd = { "lua-language-server" }
-- Add files/folders here that indicate the root of a project
local root_markers = { ".git", ".editorconfig" }
-- Change to table with settings if required
local settings = {
  Lua = {
    runtime = {
      -- Tell the language server which version of Lua you're using
      -- (most likely LuaJIT in the case of Neovim)
      version = "LuaJIT",
    },
    -- Make the server aware of Neovim runtime files
    workspace = {
      checkThirdParty = false,
      library = {
        vim.env.VIMRUNTIME,
        -- Depending on the usage, you might want to add additional paths here.
        -- E.g.: For using `vim.*` functions, add vim.env.VIMRUNTIME/lua.
        -- "${3rd}/luv/library"
        -- "${3rd}/busted/library",
      },
      -- or pull in all of 'runtimepath'. NOTE: this is a lot slower
      -- library = vim.api.nvim_get_runtime_file("", true)
    },
  },
}
local function on_init(client) -- recommended in nvim-lspconfig
  local path = client.workspace_folders[1].name
  if not vim.loop.fs_stat(path .. "/.luarc.json") and not vim.loop.fs_stat(path .. "/.luarc.jsonc") then
    client.config.settings = vim.tbl_deep_extend("force", client.config.settings, settings)
  end
  return true
end

vim.api.nvim_create_autocmd("FileType", {
  pattern = pattern,
  callback = function(args)
    local match = vim.fs.find(root_markers, { path = args.file, upward = true })[1]
    local root_dir = match and vim.fn.fnamemodify(match, ":p:h") or nil
    vim.lsp.start({
      name = "bugged-ls",
      cmd = cmd,
      root_dir = root_dir,
      on_init = on_init, -- either use on_init
      -- settings = settings, -- or the settings directly
    })
  end,
})

I applied the config suggested in this Neovim issue and tested that it works on 0.9.5 and nightly.

@abeldekat
Copy link
Author

@MariaSolOs,

In this PR you added:

-- Depending on the usage, you might want to add additional paths here.
-- E.g.: For using vim.* functions, add vim.env.VIMRUNTIME/lua.

I tested with and without that addition and could not detect a difference.
Is that line still needed?

@lewis6991
Copy link
Member

Yes, this change makes sense since vim.lsp.Client now has its own reference to settings.

Copy link
Member

@lewis6991 lewis6991 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Squash and fix the commit messages and we can merge.

@abeldekat
Copy link
Author

@lewis6991,

I find the following hard to read:

    if not vim.loop.fs_stat(path..'/.luarc.json') and not vim.loop.fs_stat(path..'/.luarc.jsonc') then
      client.config.settings.Lua = vim.tbl_deep_extend('force', client.config.settings.Lua, {

As a suggestion:

require'lspconfig'.lua_ls.setup {
  on_init = function(client)
    local path = client.workspace_folders[1].name
    if vim.loop.fs_stat(path..'/.luarc.json') or vim.loop.fs_stat(path..'/.luarc.jsonc') then
      return true
    end

    client.settings.Lua = vim.tbl_deep_extend('force', client.settings.Lua, {
      runtime = {
        -- Tell the language server which version of Lua you're using
        -- (most likely LuaJIT in the case of Neovim)
        version = 'LuaJIT'
      },
      -- Make the server aware of Neovim runtime files
      workspace = {
        checkThirdParty = false,
        library = {
          vim.env.VIMRUNTIME
          -- Depending on the usage, you might want to add additional paths here.
          -- E.g.: For using `vim.*` functions, add vim.env.VIMRUNTIME/lua.
          -- "${3rd}/luv/library"
          -- "${3rd}/busted/library",
        }
        -- or pull in all of 'runtimepath'. NOTE: this is a lot slower
        -- library = vim.api.nvim_get_runtime_file("", true)
      }
    })
    end
    return true
  end
  settings = {
    Lua = {}
  }
}

Also: Is return true needed?

@lewis6991
Copy link
Member

No it's not.

@abeldekat
Copy link
Author

Then I will also remove the return true if you agree

What is your opinion on my early return suggestion?

if vim.loop.fs_stat(path..'/.luarc.json') or vim.loop.fs_stat(path..'/.luarc.jsonc') then
      return true
    end

@lewis6991
Copy link
Member

Yeah, looks fine. Again don't need to return true.

@abeldekat
Copy link
Author

@lewis6991, I added a commit.
Just one final question:

-- E.g.: For using vim.* functions, add vim.env.VIMRUNTIME/lua.

Is this still needed? I did not observe any difference.

@lewis6991
Copy link
Member

No I don't think that does anything.

@abeldekat
Copy link
Author

There is one problem: By using client.settings, the suggested config will not work on version 0.9.5 anymore...

@lewis6991
Copy link
Member

lewis6991 commented Mar 5, 2024

Ok, don't use that for now. Do what you did initially.

@abeldekat
Copy link
Author

I restored client.config.settings and removed the comment mentioning VIMRUNTIME/lua

Squash and fix the commit messages and we can merge.

Do I need to do this or is this done when merging?

@lewis6991
Copy link
Member

Yes please

@abeldekat
Copy link
Author

Done. Thanks, @lewis6991!

@lewis6991 lewis6991 merged commit 48364fb into neovim:master Mar 5, 2024
9 checks passed
@abeldekat abeldekat deleted the lua_doc branch March 5, 2024 15:30
@MariaSolOs
Copy link
Member

@MariaSolOs,

In this PR you added:

-- Depending on the usage, you might want to add additional paths here. -- E.g.: For using vim.* functions, add vim.env.VIMRUNTIME/lua.

I tested with and without that addition and could not detect a difference. Is that line still needed?

Sorry for the late response. @lewis6991 is correct.

And thanks for the change!

antoineco added a commit to antoineco/dotfiles that referenced this pull request Mar 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants