commit 666740c50d57d576205b20ffbda06ad7aca376d4
parent 906fd9e03d71cb761bf9198187422a0ef52c44b1
Author: Thomas Vigouroux <thomas.vigouroux@protonmail.com>
Date: Tue, 26 Jul 2022 16:01:37 +0200
fix(lsp): correctly handle root_dir
Diffstat:
1 file changed, 28 insertions(+), 7 deletions(-)
diff --git a/lua/lsp_config.lua b/lua/lsp_config.lua
@@ -188,16 +188,28 @@ end
local augroup = vim.api.nvim_create_augroup("User_LSP", {})
+local find_root = function(markers, file)
+ local dirs = vim.fs.find(markers, { upward = true, path = vim.fs.dirname(file) })
+ local source = dirs[1] or file
+ return vim.fn.fnamemodify(source, ":p:h")
+end
+
local function setup_lsp(name, filetypes, command, config)
- local cfg = vim.deepcopy(config)
- cfg.name = name
- cfg.cmd = command
vim.api.nvim_create_autocmd("Filetype", {
pattern = filetypes,
group = augroup,
- callback = function()
- for name, func in pairs(config.ucommands or {}) do
- vim.api.nvim_create_user_command(name, func)
+ callback = function(args)
+ for cname, func in pairs(config.ucommands or {}) do
+ vim.api.nvim_create_user_command(cname, function(...)
+ local client = vim.lsp.get_active_clients { name = name }[1]
+ func(client, ...)
+ end)
+ end
+ local cfg = vim.deepcopy(config)
+ cfg.name = name
+ cfg.cmd = command
+ if not cfg.root_dir then
+ cfg.root_dir = find_root(cfg.root_markers or { ".git" }, args.file)
end
vim.lsp.start(cfg)
end
@@ -210,6 +222,13 @@ local capabilities = vim.lsp.protocol.make_client_capabilities()
capabilities.textDocument.completion.completionItem.snippetSupport = true
local default_config = { capabilities = capabilities, on_attach = on_attach }
+-- A bit of notes for the future me
+-- filetypes (table) List of filetypes for this lsp
+-- command (table|nil) command to run to start the server, defaults to server name
+-- cfg (table) config as in vim.lsp.start_client()
+-- root_markers (list|nil) Files that are markers for the root directory
+-- ucommands (table) Mapping from valid command names to lua functions (as in
+-- nvim_create_user_command, with first argument being the client) to create in the buffer where the thing is started
local system_lsps = {
hls = {
filetypes = { "haskell" },
@@ -223,6 +242,7 @@ local system_lsps = {
clangd = {
filetypes = { "c", "cpp" },
+ root_markers = { "CMakeLists.txt", ".git" },
cfg = default_config,
},
@@ -236,7 +256,7 @@ local system_lsps = {
cfg = default_config,
},
- vimls = {
+ ["vim-language-server"] = {
filetypes = { "vim" },
cfg = default_config,
},
@@ -298,6 +318,7 @@ local system_lsps = {
texlab = {
filetypes = { "tex", "bib" },
+ root_markers = { ".latexmkrc", ".git" },
cfg = {
capabilities = capabilities,
on_attach = texlab_attach,