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.
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
.
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!
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?