0

I'm using the nvim-lspconfig plugin to set up *sh, lua, and python LSPs. If I have the setup function at the 'top level' of my init.lua, the servers attach as expected. However, I'd like the lsp servers to only attempt to connect for the appropriate files, rather than attempting the connection every time. To do that, I'm attempting to wrap the setup functions in an autocmd, but when I put it there, the servers won't attach.

Example snippet from init.lua that does work:

require("lspconfig").sumneko_lua.setup(sumneko_settings)

With the above in my init.lua, after opening a lua file, I can run :LspInfo and see that the lua language server has attached.

screenshot of successfully attached lua language server

This, however, does not work:

local lua_lsp_group = vim.api.nvim_create_augroup("lua_lsp", {clear = true})
vim.api.nvim_create_autocmd("FileType", {
  pattern = {"lua"},
  callback = function()
    require("lspconfig").sumneko_lua.setup(sumneko_settings)
  end,
  group = lua_lsp_group
}

Running :LspInfo with this configuration shows no server attached, but does show that the filetype has been set appropriately to lua.

screenshot of failed lua language server attach

In the failed attempt you can see that it's failing to detect the root directory, but I am using identical settings for both examples.

local runtime_path = vim.split(package.path, ";")
table.insert(runtime_path, "lua/?.lua")
table.insert(runtime_path, "lua/?/init.lua")

local sumneko_settings = {
  on_attach = attach_func,
  settings = {
    Lua = {
      runtime = {
        version = "LuaJIT",
        path = runtime_path,
      },
      diagnostics = {
        globals = {"vim"}
      },
      workspace = {
        library = api.nvim_get_runtime_file("", true),
       },
      telemetry = {enable = false},
    },
  },
}

Is there something about the way autocommands work that prevents my runtime path adjustments from taking effect appropriately? I don't think the runtime path is the real problem though, because I experience the same problem with pyright and bashls when I try to contain the setup function for those within an autocommand group.

For additional context, I am also using the Scala language server nvim-metals and having no issues activating it with an autocommand, so I know this should work. (Though nvim-metals does have its own setup for attaching the language server to neovim, so I am not using the nvim-lspconfig plugin for that setup.)

local nvim_metals_group = api.nvim_create_augroup("nvim-metals", {clear = true})
vim.api.nvim_create_autocmd("FileType", {
  pattern = {"scala", "sbt"},
  callback = function()
    require("metals").initialize_or_attach(metals_config)
  end,
  group = nvim_metals_group,
})

Any and all help will be appreciated!

3
  • 1
    I suspect the following difference : metals probably creates or attaches to the lsp when called, whereas the native lspconfig probably creates its own autocommands. Creating autocommands during an autocommand is obviously going to be too late, as they won’t fire until the next file that matches (and if that triggers lspconfig again, and if lspconfig wipes its group before creating it, there’s a chance it won’t work at all even for the next file). Does it work on the second file if you edit one file (say Python) and then edit a second, different Python file ?
    – D. Ben Knoble
    Jul 6, 2022 at 23:49
  • Thanks for the advice - yes, for the first opened buffer, pyright doesn't attach, but then editing a second buffer it will attach. (Interestingly enough, after it attaches to the second buffer, it will go back and attach a language server instance to the first buffer.) Using the setup where I don't define my own augroup, I am seeing strange behavior with the lua and *sh lsp where whenever I source my config it attaches an additional instance of the language server, but not seeing that in python. So perhaps the augroups created by the plugin aren't clearing themselves?
    – btrachey
    Jul 7, 2022 at 13:24
  • Could be. Sounds like using lspconfig directly is still the way to go; you might want to avoid executing again when « reloading » though
    – D. Ben Knoble
    Jul 7, 2022 at 13:32

1 Answer 1

0

Thank you to @D. Ben Knoble for the suggestion to look into whether the config plugin I was using was already creating the autocommands.

That does appear to be the case - https://github.com/neovim/nvim-lspconfig/blob/master/lua/lspconfig/configs.lua#L67.

So the 'answer' here is to use the nvim-lspconfig plugin as explained in the documentation and not try to wrap the setup functions in their own autocommands/groups.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.