feat(lsp): hot reload of lsp config

This commit is contained in:
2024-04-19 17:22:03 +02:00
parent 15fdc24c15
commit ad7b8848d1
12 changed files with 4970 additions and 478 deletions
+130 -62
View File
@@ -2,10 +2,47 @@ local M = {}
M.os_name = vim.uv.os_uname().sysname
--- Get the module path of a file
---@param file string
---@return string?
local function get_module_path(file)
for _, rtp in ipairs(vim.api.nvim_list_runtime_paths()) do
if file:sub(1, #rtp) == rtp then
file = file:sub(#rtp + 2)
if file:sub(1, 4) == "lua/" then
file = file:sub(5)
end
return file:match("(.*)%.lua$"):gsub("[/\\]", ".")
end
end
end
--- Send a notification
---@param msg string Message to send
---@param title string? Title of notification
---@param level integer Log level
local function notify(msg, title, level)
if not title then
local info = debug.getinfo(3)
local file = info.source
and (info.source:sub(1, 1) == "@" and info.source:sub(2) or info.source)
or nil
local module = file and (get_module_path(file) or file) or nil
title = module and module .. (info.name and info.name ~= "" and ":" .. info.name or "")
or nil
end
if title and not pcall(require, "notify") then
msg = "[" .. title .. "] " .. msg
end
vim.notify(msg, level, { title = title })
end
--- Check that an executable is available
--- @param exe string Array to look for
--- @return boolean
function M.is_installed(exe)
function M.is_executable(exe)
return vim.fn.executable(exe) == 1
end
@@ -14,7 +51,7 @@ end
--- @return boolean
function M.any_installed(exes)
for _, e in ipairs(exes) do
if M.is_installed(e) then
if M.is_executable(e) then
return true
end
end
@@ -26,29 +63,27 @@ end
--- Raises error if missing.
--- @param exe string Array to look for
function M.assert_installed(exe)
if not M.is_installed(exe) then
M.notify("Missing executable '" .. exe .. "'.")
end
assert(M.is_executable(exe), "Missing executable '" .. exe .. "'.")
end
--- Asserts that at least one executable is available
--- Raises error if missing.
--- @param exes table Array of exes
function M.assert_any_installed(exes)
if not M.any_installed(exes) then
error("At least one of the following is required:\n" ..
table.concat(exes, ", "))
end
assert(
M.any_installed(exes),
"At least one of the following is required:\n" .. table.concat(exes, ", ")
)
end
--- Asserts that a python module is installed
---@param mod string The python module to check
function M.python3_module_is_installed(mod)
if not M.is_installed("python3") then
if not M.is_executable("python3") then
return false
end
local resp = vim.system({ "python3", "-c", "import " .. mod, }):wait()
local resp = vim.system({ "python3", "-c", "import " .. mod }):wait()
return resp.code == 0
end
@@ -60,91 +95,124 @@ function M.assert_python3_module_installed(mod)
end
end
--- Send a notification
---@param msg string Message to send
---@param title string Title of notification
---@param level integer Log level
function M.notify(msg, title, level)
if title and not pcall(require, "notify") then
msg = "[" .. title .. "] " .. msg
end
vim.notify(msg, level, { title = title, })
end
--- Send a debug notification
---@param msg string Message to send
---@param title string Title of notification
---@param title string? Title of notification
function M.debug(msg, title)
M.notify(msg, title, vim.log.levels.DEBUG)
notify(msg, title, vim.log.levels.DEBUG)
end
--- Send an info notification
---@param msg string Message to send
---@param title string Title of notification
---@param title string? Title of notification
function M.info(msg, title)
M.notify(msg, title, vim.log.levels.INFO)
notify(msg, title, vim.log.levels.INFO)
end
--- Send a warning notification
---@param msg string Message to send
---@param title string Title of notification
---@param title string? Title of notification
function M.warn(msg, title)
M.notify(msg, title, vim.log.levels.WARN)
notify(msg, title, vim.log.levels.WARN)
end
--- Send an error notification
---@param msg string Message to send
---@param title string Title of notification
---@param title string? Title of notification
function M.err(msg, title)
M.notify(msg, title, vim.log.levels.ERROR)
notify(msg, title, vim.log.levels.ERROR)
end
--- Attempts to load a module and logs errors on failure.
---@param module string The module to attempt to load.
---@param err_title string The error message title.
---@param on_success fun(module: unknown)? Will be called if module was loaded.
---@return unknown? module The loaded module if successful, otherwise nil.
function M.try_require(module, err_title, on_success)
---@return any module The loaded module if successful, otherwise nil.
function M.try_require(module)
local has_module, resp = pcall(require, module)
if has_module then
if on_success then
on_success(resp)
end
return resp
end
M.err(("Failed to load module %s"):format(module), err_title)
M.err(resp, err_title)
M.err(("Failed to load module %s:\n%s"):format(module, resp))
end
--- Update table t1 with values in t2.
---@param table table<any, any> The table to update
---@param values table<any, any> The table with new values
function M.update_table(table, values)
for k, v in pairs(values) do
if type(v) == "table" and type(table[k] or false) == "table" then
M.update_table(table[k], values[k])
else
table[k] = v
end
end
end
--- Check if a table contains a value
---@generic T
---@param table table<any, T> Table to inspect
---@param value T Value to check
--- Check if `val` is a list of type `t` (if given)
---@param val any
---@param t type?
---@return boolean
function M.has_value(table, value)
for _, v in ipairs(table) do
if v == value then
return true
function M.is_list(val, t)
if type(val) ~= "table" then
return false
end
for k, v in pairs(val) do
if type(k) ~= "number" then
return false
end
if t and type(v) ~= t then
return false
end
end
return false
return true
end
--- Check if `val` is a list of type `t` (if given), or nil
---@param val any?
---@param t type?
---@return boolean
function M.is_list_or_nil(val, t)
if val == nil then
return true
else
return M.is_list(val, t)
end
end
--- Creates a debounced function that delays execution of `fn` until after `delay` milliseconds have
--- elapsed since the last time it was invoked.
---@param fn fun(...) Function to be debounced
---@param delay number Debounce delay in milliseconds
---@return fun(...) function Debounced function
function M.debounce(fn, delay)
---@type uv_timer_t?
local timer = nil
return function(...)
local args = vim.F.pack_len(...)
if timer then
timer:stop()
timer = nil
end
timer = vim.defer_fn(function()
timer = nil
fn(vim.F.unpack_len(args))
end, delay)
end
end
--- Creates a debounced function that delays execution of `fn` until after `delay` milliseconds have
--- elapsed since the last time it was invoked with the same unique identifier.
---@param fn fun(...) Function to be debounced
---@param delay number Debounce delay in milliseconds
---@return fun(id: any, ...) function Debounced function, where `id` is a unique identifier
function M.debounce_with_id(fn, delay)
local map = {}
return function(id, ...)
local args = vim.F.pack_len(...)
if map[id] then
map[id]:stop()
map[id] = nil
end
map[id] = vim.defer_fn(function()
map[id] = nil
fn(vim.F.unpack_len(args))
end, delay)
end
end
return M