small refactor to lsp.lua (lsp/setup.lua), rename lsp/language_servers

-> lsp/servers. utils now has a function for "get_file_names_in_dir"
This commit is contained in:
Martin Larsson 2025-01-11 18:01:15 +01:00
parent fc08128c55
commit b7796e9a5e
6 changed files with 22 additions and 14 deletions

View file

@ -66,23 +66,15 @@ vim.lsp.config("*", {
root_markers = { ".git" },
})
-- Find all files in lua/language_servers and require them
-- Find all files in lua/lsp/servers and require them
-- We use them to ensure that the servers are installed and configured
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)
local errors = {}
utils.foreach(server_names, function(server_name)
local path = "language_servers/" .. server_name
local dir_path = "lsp/servers"
utils.foreach(utils.get_file_names_in_dir(dir_path, "*.lua", true), function(server_name)
local server_path = dir_path .. "/" .. server_name
local result, conf = utils.xpcallmsg(
function() return require(path) end,
"Failed to require " .. path,
function() return require(server_path) end,
"Failed to require " .. server_path,
errors
)

View file

@ -5,6 +5,22 @@ local function is_single_keymap_table(map_table)
return map_table.n or map_table.t or map_table.i or map_table.v or map_table.x or map_table.o
end
function M.get_file_names_in_dir(dir, expr, strip_extension)
local path = vim.fn.stdpath("config") .. "/lua/" .. dir
local files_str = vim.fn.globpath(path, expr, true)
local has_line_breaks = vim.fn.match(files_str, [[\n]]) > -1
local files = has_line_breaks and vim.fn.split(files_str, "\n") or { files_str }
local should_strip_extension = strip_extension or false
if should_strip_extension then
return vim.tbl_map(function(file)
return vim.fn.fnamemodify(file, ":t:r")
end, files)
else
return files
end
end
function M.validate_bufnr(bufnr)
vim.validate('bufnr', bufnr, 'number')
return bufnr == 0 and vim.api.nvim_get_current_buf() or bufnr