From 5e76cf4796a6ef044957bcaa77ebfbfca94af25c Mon Sep 17 00:00:00 2001 From: Martin Larsson Date: Sat, 20 Jan 2024 15:18:17 +0100 Subject: [PATCH] Rework how LSP's are configured, it now finds all of the entries in the lua/language_servers dir and use those instead. --- nvim/lua/plugs/mason_lsp.lua | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/nvim/lua/plugs/mason_lsp.lua b/nvim/lua/plugs/mason_lsp.lua index 0e67b6c..ca60001 100644 --- a/nvim/lua/plugs/mason_lsp.lua +++ b/nvim/lua/plugs/mason_lsp.lua @@ -7,29 +7,34 @@ return { "RubixDev/mason-update-all", }, config = function() - -- Missing Rust because rust-analyzer is deprecated, and rustaceanvim is not included in mason yet, so it needs a custom setup. - -- Make sure that these are named according to lspconfig and not mason packages - local servers_names = { - "lua_ls", - } + -- Find all files in lua/language_servers and require them + -- We use them to ensure that the servers are installed and configured + -- Make sure that the files use the lspconfig naming convention + local lua_files_str = vim.fn.globpath(vim.fn.stdpath("config") .. "/lua/language_servers", "*.lua", true) + local has_line_breaks = vim.fn.match(lua_files_str, [[\n]]) > -1 + -- Get an array of all the files in the directory, make sure to account for single file + local lua_files = has_line_breaks and vim.fn.split(lua_files_str, "\n") or { lua_files_str } + -- Remove path and extension and only keep the filename + local server_names = vim.tbl_map(function(file) + return vim.fn.fnamemodify(file, ":t:r") + end, lua_files) require("mason").setup() require("mason-lspconfig").setup() require("mason-tool-installer").setup({ - ensure_installed = servers_names, + ensure_installed = server_names, }) require("mason-update-all").setup() -- Iterate each server and setup local lspconfig = require("lspconfig") - for _, server_name in ipairs(servers_names) do + for _, server_name in ipairs(server_names) do local server = lspconfig[server_name] if server then - server.setup(require("lua/language_servers/" .. server_name)) + server.setup(require("language_servers/" .. server_name)) else error("LSP server not found: " .. server_name) end - end end, }