feat(lsp): hot reload of lsp config
This commit is contained in:
+40
-37
@@ -1,25 +1,5 @@
|
|||||||
# MIT License
|
# vim: set ft=toml:
|
||||||
#
|
# spec: https://github.com/CppCXY/EmmyLuaCodeStyle/blob/master/docs/format_config_EN.md
|
||||||
# Copyright (c) 2022 CppCXY
|
|
||||||
#
|
|
||||||
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
||||||
# of this software and associated documentation files (the "Software"), to deal
|
|
||||||
# in the Software without restriction, including without limitation the rights
|
|
||||||
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
||||||
# copies of the Software, and to permit persons to whom the Software is
|
|
||||||
# furnished to do so, subject to the following conditions:
|
|
||||||
#
|
|
||||||
# The above copyright notice and this permission notice shall be included in all
|
|
||||||
# copies or substantial portions of the Software.
|
|
||||||
#
|
|
||||||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
||||||
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
||||||
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
||||||
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
||||||
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
||||||
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
||||||
# SOFTWARE.
|
|
||||||
|
|
||||||
|
|
||||||
# see https://github.com/CppCXY/EmmyLuaCodeStyle
|
# see https://github.com/CppCXY/EmmyLuaCodeStyle
|
||||||
[*.lua]
|
[*.lua]
|
||||||
@@ -34,7 +14,14 @@ tab_width = 4
|
|||||||
# none/single/double
|
# none/single/double
|
||||||
quote_style = double
|
quote_style = double
|
||||||
|
|
||||||
|
# keep/remove/remove_table_only/remove_string_only
|
||||||
|
call_arg_parentheses = keep
|
||||||
|
|
||||||
continuation_indent = 4
|
continuation_indent = 4
|
||||||
|
## extend option
|
||||||
|
# continuation_indent.before_block = 4
|
||||||
|
# continuation_indent.in_expr = 4
|
||||||
|
# continuation_indent.in_table = 4
|
||||||
|
|
||||||
# this mean utf8 length , if this is 'unset' then the line width is no longer checked
|
# this mean utf8 length , if this is 'unset' then the line width is no longer checked
|
||||||
# this option decides when to chopdown the code
|
# this option decides when to chopdown the code
|
||||||
@@ -44,15 +31,12 @@ max_line_length = 100
|
|||||||
# in neovim the value 'auto' is not a valid option, please use 'unset'
|
# in neovim the value 'auto' is not a valid option, please use 'unset'
|
||||||
end_of_line = lf
|
end_of_line = lf
|
||||||
|
|
||||||
|
#optional keep/never/always/smart
|
||||||
|
trailing_table_separator = smart
|
||||||
|
|
||||||
# none/ comma / semicolon / only_kv_colon
|
# none/ comma / semicolon / only_kv_colon
|
||||||
table_separator_style = comma
|
table_separator_style = comma
|
||||||
|
|
||||||
#optional keep/never/always/smart
|
|
||||||
trailing_table_separator = always
|
|
||||||
|
|
||||||
# keep/remove/remove_table_only/remove_string_only
|
|
||||||
call_arg_parentheses = keep
|
|
||||||
|
|
||||||
detect_end_of_line = false
|
detect_end_of_line = false
|
||||||
|
|
||||||
# this will check text end with new line
|
# this will check text end with new line
|
||||||
@@ -61,17 +45,22 @@ insert_final_newline = true
|
|||||||
# [space]
|
# [space]
|
||||||
space_around_table_field_list = true
|
space_around_table_field_list = true
|
||||||
|
|
||||||
space_before_attribute = true
|
space_before_attribute = false
|
||||||
|
|
||||||
space_before_function_open_parenthesis = false
|
space_before_function_open_parenthesis = false
|
||||||
|
|
||||||
space_before_function_call_open_parenthesis = false
|
space_before_function_call_open_parenthesis = false
|
||||||
|
|
||||||
space_before_closure_open_parenthesis = true
|
space_before_closure_open_parenthesis = false
|
||||||
|
|
||||||
# optional always/only_string/only_table/none
|
# optional always/only_string/only_table/none
|
||||||
# or true/false
|
# or true/false
|
||||||
space_before_function_call_single_arg = always
|
space_before_function_call_single_arg = always
|
||||||
|
## extend option
|
||||||
|
## always/keep/none
|
||||||
|
# space_before_function_call_single_arg.table = always
|
||||||
|
## always/keep/none
|
||||||
|
# space_before_function_call_single_arg.string = always
|
||||||
|
|
||||||
space_before_open_square_bracket = false
|
space_before_open_square_bracket = false
|
||||||
|
|
||||||
@@ -86,28 +75,39 @@ space_around_table_append_operator = false
|
|||||||
|
|
||||||
ignore_spaces_inside_function_call = false
|
ignore_spaces_inside_function_call = false
|
||||||
|
|
||||||
space_before_inline_comment = 1
|
# detail number or 'keep'
|
||||||
|
space_before_inline_comment = 2
|
||||||
|
|
||||||
|
# convert '---' to '--- ' or '--' to '-- '
|
||||||
|
space_after_comment_dash = false
|
||||||
|
|
||||||
# [operator space]
|
# [operator space]
|
||||||
space_around_math_operator = true
|
space_around_math_operator = true
|
||||||
|
# space_around_math_operator.exponent = false
|
||||||
|
|
||||||
space_after_comma = true
|
space_after_comma = true
|
||||||
|
|
||||||
space_after_comma_in_for_statement = true
|
space_after_comma_in_for_statement = true
|
||||||
|
|
||||||
|
# true/false or none/always/no_space_asym
|
||||||
space_around_concat_operator = true
|
space_around_concat_operator = true
|
||||||
|
|
||||||
|
space_around_logical_operator = true
|
||||||
|
|
||||||
|
# true/false or none/always/no_space_asym
|
||||||
|
space_around_assign_operator = true
|
||||||
|
|
||||||
# [align]
|
# [align]
|
||||||
|
|
||||||
align_call_args = false
|
align_call_args = false
|
||||||
|
|
||||||
align_function_params = true
|
align_function_params = false
|
||||||
|
|
||||||
align_continuous_assign_statement = true
|
align_continuous_assign_statement = false
|
||||||
|
|
||||||
align_continuous_rect_table_field = true
|
align_continuous_rect_table_field = false
|
||||||
|
|
||||||
align_continuous_line_space = 2
|
align_continuous_line_space = 4
|
||||||
|
|
||||||
align_if_branch = false
|
align_if_branch = false
|
||||||
|
|
||||||
@@ -118,7 +118,7 @@ align_continuous_similar_call_args = false
|
|||||||
|
|
||||||
align_continuous_inline_comment = true
|
align_continuous_inline_comment = true
|
||||||
# option none / always / only_call_stmt
|
# option none / always / only_call_stmt
|
||||||
align_chain_expr = none
|
align_chain_expr = always
|
||||||
|
|
||||||
# [indent]
|
# [indent]
|
||||||
|
|
||||||
@@ -127,6 +127,8 @@ never_indent_before_if_condition = false
|
|||||||
never_indent_comment_on_if_branch = false
|
never_indent_comment_on_if_branch = false
|
||||||
|
|
||||||
keep_indents_on_empty_lines = false
|
keep_indents_on_empty_lines = false
|
||||||
|
|
||||||
|
allow_non_indented_comments = false
|
||||||
# [line space]
|
# [line space]
|
||||||
|
|
||||||
# The following configuration supports four expressions
|
# The following configuration supports four expressions
|
||||||
@@ -161,9 +163,10 @@ break_all_list_when_line_exceed = false
|
|||||||
auto_collapse_lines = false
|
auto_collapse_lines = false
|
||||||
|
|
||||||
break_before_braces = false
|
break_before_braces = false
|
||||||
|
|
||||||
# [preference]
|
# [preference]
|
||||||
ignore_space_after_colon = false
|
ignore_space_after_colon = false
|
||||||
|
|
||||||
remove_call_expression_list_finish_comma = false
|
remove_call_expression_list_finish_comma = false
|
||||||
|
# keep / always / same_line / replace_with_newline / never
|
||||||
end_statement_with_semicolon = keep
|
end_statement_with_semicolon = keep
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
local module_name = "base"
|
local module_name = "core"
|
||||||
|
|
||||||
local utils = require("utils")
|
local utils = require("utils")
|
||||||
|
|
||||||
|
|||||||
+11
-25
@@ -1,36 +1,22 @@
|
|||||||
local module_name = "core.user_commands"
|
local module_name = "core.user_commands"
|
||||||
local utils = require("utils")
|
local utils = require("utils")
|
||||||
|
|
||||||
vim.api.nvim_create_user_command(
|
vim.api.nvim_create_user_command("Update", function(_)
|
||||||
"Update",
|
local lazy = utils.try_require("lazy")
|
||||||
function (_)
|
if lazy then
|
||||||
utils.try_require(
|
|
||||||
"lazy",
|
|
||||||
module_name,
|
|
||||||
function (lazy)
|
|
||||||
lazy.update()
|
lazy.update()
|
||||||
end
|
end
|
||||||
)
|
|
||||||
|
|
||||||
utils.try_require(
|
local treesitter_install = utils.try_require("nvim-treesitter.install")
|
||||||
"nvim-treesitter.install",
|
if treesitter_install then
|
||||||
module_name,
|
treesitter_install.update({ with_sync = true })("all")
|
||||||
function (treesitter_install)
|
|
||||||
treesitter_install.update({ with_sync = true, })("all")
|
|
||||||
end
|
end
|
||||||
)
|
|
||||||
|
|
||||||
utils.try_require(
|
local mason_update_all = utils.try_require("mason-update-all")
|
||||||
"mason-update-all",
|
if mason_update_all then
|
||||||
module_name,
|
|
||||||
function (mason_update_all)
|
|
||||||
mason_update_all.update_all()
|
mason_update_all.update_all()
|
||||||
end
|
end
|
||||||
)
|
end, {
|
||||||
end,
|
desc = "Update lazy plugins, treesitter parsers and mason language servers",
|
||||||
{
|
|
||||||
desc =
|
|
||||||
"Update lazy plugins, treesitter parsers and mason language servers",
|
|
||||||
force = false,
|
force = false,
|
||||||
}
|
})
|
||||||
)
|
|
||||||
|
|||||||
+109
-30
@@ -1,36 +1,114 @@
|
|||||||
local module_name = "lsp"
|
|
||||||
local utils = require("utils")
|
local utils = require("utils")
|
||||||
|
|
||||||
---@class ServerConfig
|
local CONFIG_DIR = vim.fn.stdpath("config") .. "/lua/lsp/config"
|
||||||
|
|
||||||
|
---@class Server
|
||||||
local Server = require("lsp.server")
|
local Server = require("lsp.server")
|
||||||
|
|
||||||
local M = {}
|
local M = {}
|
||||||
|
|
||||||
---@type table<string, ServerConfig>
|
---@type table<string, Server>
|
||||||
local servers = {
|
local servers = {}
|
||||||
bashls = {},
|
|
||||||
clangd = {},
|
|
||||||
cmake = {},
|
|
||||||
diagnosticls = {},
|
|
||||||
gopls = {},
|
|
||||||
groovyls = {},
|
|
||||||
intelephense = {},
|
|
||||||
pylsp = {},
|
|
||||||
lemminx = {},
|
|
||||||
lua_ls = {},
|
|
||||||
rust_analyzer = {},
|
|
||||||
zls = {},
|
|
||||||
}
|
|
||||||
|
|
||||||
for name, _ in pairs(servers) do
|
function M.get_servers()
|
||||||
utils.try_require(
|
return servers
|
||||||
"lsp.config." .. name,
|
end
|
||||||
module_name,
|
|
||||||
---@param cfg ServerConfig
|
local function get_module_name(filepath)
|
||||||
function (cfg)
|
return filepath:match("([^/\\]+)%.lua$")
|
||||||
cfg.name = name
|
end
|
||||||
servers[name] = Server:new(cfg)
|
|
||||||
|
local function get_server_config(name)
|
||||||
|
local module = "lsp.config." .. name
|
||||||
|
package.loaded[module] = nil
|
||||||
|
return utils.try_require("lsp.config." .. name)
|
||||||
|
end
|
||||||
|
|
||||||
|
local reload_server_config = utils.debounce_with_id(function(name, events)
|
||||||
|
utils.debug(("Reloading server %s"):format(name))
|
||||||
|
---@type Server?
|
||||||
|
local server = servers[name]
|
||||||
|
|
||||||
|
if server and server.config.enable then
|
||||||
|
server:unload()
|
||||||
|
servers[name] = nil
|
||||||
end
|
end
|
||||||
|
|
||||||
|
if events.rename then
|
||||||
|
local _, _, err_name = vim.uv.fs_stat(("%s/%s.lua"):format(CONFIG_DIR, name))
|
||||||
|
if err_name == "ENOENT" then
|
||||||
|
return
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
local config = get_server_config(name)
|
||||||
|
if not config then
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
|
server = Server.new(name, config)
|
||||||
|
if not server or not server.config.enable then
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
|
if #server:get_ft_buffers() ~= 0 then
|
||||||
|
server:setup()
|
||||||
|
else
|
||||||
|
server:register()
|
||||||
|
end
|
||||||
|
|
||||||
|
servers[name] = server
|
||||||
|
end, 100)
|
||||||
|
|
||||||
|
local function process_change(error, filename, events)
|
||||||
|
utils.debug(("Got event: %s, %s, %s"):format(filename, vim.inspect(events), error))
|
||||||
|
if error then
|
||||||
|
utils.err(("Error on change for %s:\n%s"):format(filename, error), "lsp.on_config_change")
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
|
local name = get_module_name(filename)
|
||||||
|
if not name or name == "init" then
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
|
reload_server_config(name, name, events)
|
||||||
|
end
|
||||||
|
|
||||||
|
local function load_configs()
|
||||||
|
local handle = vim.uv.fs_scandir(CONFIG_DIR)
|
||||||
|
while handle do
|
||||||
|
local filepath = vim.uv.fs_scandir_next(handle)
|
||||||
|
|
||||||
|
if not filepath then
|
||||||
|
break
|
||||||
|
end
|
||||||
|
|
||||||
|
local name = get_module_name(filepath)
|
||||||
|
|
||||||
|
if name == "init" then
|
||||||
|
goto continue
|
||||||
|
end
|
||||||
|
|
||||||
|
local config = get_server_config(name)
|
||||||
|
|
||||||
|
if not config then
|
||||||
|
goto continue
|
||||||
|
end
|
||||||
|
|
||||||
|
local server = Server.new(name, config)
|
||||||
|
if server then
|
||||||
|
servers[name] = server
|
||||||
|
end
|
||||||
|
|
||||||
|
::continue::
|
||||||
|
end
|
||||||
|
|
||||||
|
vim.uv.fs_event_start(
|
||||||
|
vim.uv.new_fs_event(),
|
||||||
|
CONFIG_DIR,
|
||||||
|
{},
|
||||||
|
vim.schedule_wrap(process_change)
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -42,7 +120,7 @@ local function setup_diagnostics()
|
|||||||
signs = true,
|
signs = true,
|
||||||
virtual_text = {
|
virtual_text = {
|
||||||
prefix = "",
|
prefix = "",
|
||||||
format = function (diagnostic)
|
format = function(diagnostic)
|
||||||
return diagnostic.message
|
return diagnostic.message
|
||||||
end,
|
end,
|
||||||
},
|
},
|
||||||
@@ -51,25 +129,26 @@ local function setup_diagnostics()
|
|||||||
source = true,
|
source = true,
|
||||||
border = "single",
|
border = "single",
|
||||||
focusable = false,
|
focusable = false,
|
||||||
format = function (diagnostic)
|
format = function(diagnostic)
|
||||||
return string.format("%s", diagnostic.message)
|
return string.format("%s", diagnostic.message)
|
||||||
end,
|
end,
|
||||||
},
|
},
|
||||||
update_in_insert = false,
|
update_in_insert = false,
|
||||||
severity_sort = false,
|
severity_sort = false,
|
||||||
})
|
})
|
||||||
local signs = { Error = " ", Warn = " ", Hint = " ", Info = " ", }
|
local signs = { Error = " ", Warn = " ", Hint = " ", Info = " " }
|
||||||
for type, icon in pairs(signs) do
|
for type, icon in pairs(signs) do
|
||||||
local hl = "DiagnosticSign" .. type
|
local hl = "DiagnosticSign" .. type
|
||||||
vim.fn.sign_define(hl, { text = icon, texthl = hl, numhl = hl, })
|
vim.fn.sign_define(hl, { text = icon, texthl = hl, numhl = hl })
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
function M.setup()
|
function M.setup()
|
||||||
|
load_configs()
|
||||||
setup_diagnostics()
|
setup_diagnostics()
|
||||||
|
|
||||||
for _, server in pairs(servers) do
|
for _, server in pairs(servers) do
|
||||||
if server.enable then
|
if server.config.enable then
|
||||||
server:register()
|
server:register()
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -3,9 +3,6 @@ return {
|
|||||||
dependencies = {
|
dependencies = {
|
||||||
"python3",
|
"python3",
|
||||||
},
|
},
|
||||||
py_module_deps = {
|
|
||||||
"venv",
|
|
||||||
},
|
|
||||||
mason = {
|
mason = {
|
||||||
name = "cmake-language-server",
|
name = "cmake-language-server",
|
||||||
-- version = "",
|
-- version = "",
|
||||||
|
|||||||
@@ -3,9 +3,6 @@ return {
|
|||||||
dependencies = {
|
dependencies = {
|
||||||
"python3",
|
"python3",
|
||||||
},
|
},
|
||||||
py_module_deps = {
|
|
||||||
"venv",
|
|
||||||
},
|
|
||||||
mason = {
|
mason = {
|
||||||
name = "jedi-language-server",
|
name = "jedi-language-server",
|
||||||
-- version = "",
|
-- version = "",
|
||||||
|
|||||||
@@ -0,0 +1,102 @@
|
|||||||
|
-- Mappings.
|
||||||
|
-- See `:help vim.lsp.*` for documentation on any of the below functions
|
||||||
|
|
||||||
|
local utils = require("utils")
|
||||||
|
|
||||||
|
---@class Keymap
|
||||||
|
---@field mode string|string[]
|
||||||
|
---@field lhs string
|
||||||
|
---@field rhs string|function
|
||||||
|
---@field opts? vim.keymap.set.Opts
|
||||||
|
|
||||||
|
local MODE_TYPES = { "n", "v", "s", "x", "o", "i", "l", "c" }
|
||||||
|
|
||||||
|
local M = {}
|
||||||
|
|
||||||
|
---@type table<number, vim.api.keyset.keymap[]>
|
||||||
|
M.old = {}
|
||||||
|
|
||||||
|
---@type table<number, Keymap[]>
|
||||||
|
M.new = {}
|
||||||
|
|
||||||
|
--- Load LSP keybinds
|
||||||
|
---@param server Server
|
||||||
|
function M:load(server, bufnr)
|
||||||
|
self.old[bufnr] = {}
|
||||||
|
for _, mode in ipairs(MODE_TYPES) do
|
||||||
|
vim.tbl_extend("error", self.old[bufnr], vim.api.nvim_buf_get_keymap(bufnr, mode))
|
||||||
|
end
|
||||||
|
|
||||||
|
self.new[bufnr] = {
|
||||||
|
{ mode = { "n" }, lhs = "<leader>df", rhs = vim.diagnostic.open_float },
|
||||||
|
{ mode = { "n" }, lhs = "[d", rhs = vim.diagnostic.goto_prev },
|
||||||
|
{ mode = { "n" }, lhs = "]d", rhs = vim.diagnostic.goto_next },
|
||||||
|
{ mode = { "n" }, lhs = "gD", rhs = vim.lsp.buf.declaration },
|
||||||
|
{ mode = { "n", "i" }, lhs = "<C-k>", rhs = vim.lsp.buf.hover },
|
||||||
|
{ mode = { "n", "i" }, lhs = "<C-j>", rhs = vim.lsp.buf.signature_help },
|
||||||
|
{ mode = { "n", "i" }, lhs = "<C-h>", rhs = vim.lsp.buf.document_highlight },
|
||||||
|
{ mode = { "n" }, lhs = "<leader>lr", rhs = server.ca_rename },
|
||||||
|
{ mode = { "n" }, lhs = "<leader>la", rhs = vim.lsp.buf.code_action },
|
||||||
|
{ mode = { "n", "x" }, lhs = "<leader>lf", rhs = vim.lsp.buf.format },
|
||||||
|
{
|
||||||
|
mode = { "n", "i" },
|
||||||
|
lhs = "<C-l>",
|
||||||
|
rhs = function()
|
||||||
|
vim.lsp.buf.clear_references()
|
||||||
|
vim.cmd.nohlsearch()
|
||||||
|
vim.schedule(vim.cmd.diffupdate)
|
||||||
|
return "<C-l>"
|
||||||
|
end,
|
||||||
|
opts = { expr = true },
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
local telescope = utils.try_require("telescope.builtin")
|
||||||
|
|
||||||
|
if telescope then
|
||||||
|
vim.list_extend(self.new[bufnr], {
|
||||||
|
{ mode = "n", lhs = "<leader>dl", rhs = telescope.diagnostics },
|
||||||
|
{ mode = "n", lhs = "<leader>lD", rhs = telescope.lsp_type_definitions },
|
||||||
|
{ mode = "n", lhs = "gd", rhs = telescope.lsp_definitions },
|
||||||
|
{ mode = "n", lhs = "gi", rhs = telescope.lsp_implementations },
|
||||||
|
{ mode = "n", lhs = "gr", rhs = telescope.lsp_references },
|
||||||
|
})
|
||||||
|
else
|
||||||
|
vim.list_extend(self.new[bufnr], {
|
||||||
|
{ mode = "n", lhs = "<leader>dl", rhs = vim.diagnostic.setloclist },
|
||||||
|
{ mode = "n", lhs = "<leader>ld", rhs = vim.lsp.buf.type_definition },
|
||||||
|
{ mode = "n", lhs = "gd", rhs = vim.lsp.buf.definition },
|
||||||
|
{ mode = "n", lhs = "gi", rhs = vim.lsp.buf.implementation },
|
||||||
|
{ mode = "n", lhs = "gr", rhs = vim.lsp.buf.references },
|
||||||
|
})
|
||||||
|
end
|
||||||
|
|
||||||
|
if server.config.keymaps then
|
||||||
|
vim.list_extend(self.new[bufnr], server.config.keymaps)
|
||||||
|
end
|
||||||
|
|
||||||
|
for _, keymap in ipairs(self.new[bufnr]) do
|
||||||
|
keymap.opts = vim.tbl_extend("force", keymap.opts or {}, { buffer = bufnr, remap = true })
|
||||||
|
vim.keymap.set(keymap.mode, keymap.lhs, keymap.rhs, keymap.opts)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
function M:unload(bufnr)
|
||||||
|
if self.new[bufnr] then
|
||||||
|
for _, keymap in ipairs(self.new[bufnr]) do
|
||||||
|
-- pcall to avoid error if keymap was already removed,
|
||||||
|
-- for example if server.config.keymaps overrides a default LSP keymap
|
||||||
|
pcall(vim.keymap.del, keymap.mode, keymap.lhs, { buffer = bufnr })
|
||||||
|
end
|
||||||
|
self.new[bufnr] = nil
|
||||||
|
end
|
||||||
|
|
||||||
|
if self.old[bufnr] then
|
||||||
|
for _, keymap in ipairs(self.old[bufnr]) do
|
||||||
|
vim.cmd.mapset(keymap)
|
||||||
|
end
|
||||||
|
self.old[bufnr] = nil
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
return M
|
||||||
+170
-37
@@ -1,46 +1,158 @@
|
|||||||
local module_name = "lsp.package"
|
|
||||||
local utils = require("utils")
|
local utils = require("utils")
|
||||||
|
|
||||||
---@class PostInstallStep
|
---@class PostInstallStep
|
||||||
---@field command string
|
---@field command string
|
||||||
---@field args string[]
|
---@field args string[]
|
||||||
|
|
||||||
|
---@class MasonPackage
|
||||||
|
---@field dependencies MasonPackage[]?
|
||||||
|
---@field config MasonPackageConfig
|
||||||
|
local M = {}
|
||||||
|
M.__index = M
|
||||||
|
|
||||||
---@class MasonPackageConfig
|
---@class MasonPackageConfig
|
||||||
---@field name string?
|
---@field name string?
|
||||||
---@field version string?
|
---@field version string?
|
||||||
---@field dependencies MasonPackageConfig[]?
|
---@field dependencies MasonPackageConfig[]?
|
||||||
---@field post_install PostInstallStep[]?
|
---@field post_install PostInstallStep[]?
|
||||||
local M = {}
|
M.config = {}
|
||||||
M.__index = M
|
|
||||||
|
--- Validate MasonPackageConfig
|
||||||
|
---@param config MasonPackageConfig
|
||||||
|
---@return boolean
|
||||||
|
function M.validate(config)
|
||||||
|
local ok, resp = pcall(vim.validate, { config = { config, { "table" } } })
|
||||||
|
if not ok then
|
||||||
|
goto check_resp
|
||||||
|
end
|
||||||
|
|
||||||
|
ok, resp = pcall(
|
||||||
|
vim.validate, {
|
||||||
|
name = { config.name, { "string" }, true },
|
||||||
|
version = { config.version, { "string" }, true },
|
||||||
|
dependencies = {
|
||||||
|
config.dependencies, function(f)
|
||||||
|
if not f then
|
||||||
|
return true
|
||||||
|
end
|
||||||
|
|
||||||
|
if not utils.is_list(f) then
|
||||||
|
return false
|
||||||
|
end
|
||||||
|
|
||||||
|
for _, dep in ipairs(f) do
|
||||||
|
if not M.validate(dep) then
|
||||||
|
return false
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
return true
|
||||||
|
end,
|
||||||
|
},
|
||||||
|
post_install = {
|
||||||
|
config.post_install, function(field)
|
||||||
|
if not field then
|
||||||
|
return true
|
||||||
|
end
|
||||||
|
|
||||||
|
if not utils.is_list(field) then
|
||||||
|
return false
|
||||||
|
end
|
||||||
|
|
||||||
|
for _, step in ipairs(field) do
|
||||||
|
local o, r = pcall(vim.validate, { step = { step, { "table" } } })
|
||||||
|
if not ok then
|
||||||
|
goto check_r
|
||||||
|
end
|
||||||
|
|
||||||
|
o, r = pcall(
|
||||||
|
vim.validate, {
|
||||||
|
command = { step.command, { "string" } },
|
||||||
|
args = {
|
||||||
|
step.args, function(f)
|
||||||
|
return utils.is_list_or_nil(f, "string")
|
||||||
|
end, "list of strings or nil",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
::check_r::
|
||||||
|
|
||||||
|
if not o then
|
||||||
|
utils.err(("Invalid config for post_install step: %s"):format(r))
|
||||||
|
return false
|
||||||
|
end
|
||||||
|
|
||||||
|
return true
|
||||||
|
end
|
||||||
|
|
||||||
|
return true
|
||||||
|
end,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
::check_resp::
|
||||||
|
if not ok then
|
||||||
|
utils.err(("Invalid config for %s:\n%s"):format(config.name, resp))
|
||||||
|
return false
|
||||||
|
end
|
||||||
|
|
||||||
|
return true
|
||||||
|
end
|
||||||
|
|
||||||
--- Run post installation steps
|
--- Run post installation steps
|
||||||
---@param pkg Package
|
---@param pkg Package
|
||||||
function M:run_post_install(pkg)
|
function M:run_post_install(pkg)
|
||||||
if self.post_install then
|
---@param step PostInstallStep
|
||||||
for _, step in ipairs(self.post_install) do
|
---@param msg string
|
||||||
local job = require("plenary.job"):new({
|
local function log_err(step, msg)
|
||||||
command = step.command,
|
|
||||||
args = step.args,
|
|
||||||
cwd = pkg:get_install_path(),
|
|
||||||
enabled_recording = true,
|
|
||||||
on_exit = function (job, code, signal)
|
|
||||||
if code ~= 0 or signal ~= 0 then
|
|
||||||
local cmd = step.command
|
local cmd = step.command
|
||||||
|
|
||||||
if step.args then
|
if step.args then
|
||||||
cmd = cmd .. " " .. table.concat(step.args, " ")
|
cmd = cmd .. " " .. table.concat(step.args, " ")
|
||||||
end
|
end
|
||||||
|
|
||||||
utils.err(
|
utils.err(
|
||||||
("Post installation step for %s:\n`%s`\nfailed with:\n%s"):format(
|
("Post installation step for %s:\n`%s`\nfailed with:\n%s"):format(
|
||||||
self.name,
|
self.config.name, cmd, msg
|
||||||
cmd,
|
), "lsp.package:run_post_install"
|
||||||
table.concat(job:stderr_result(), "\n")
|
|
||||||
),
|
|
||||||
module_name
|
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
if self.config.post_install then
|
||||||
|
utils.info("running post install")
|
||||||
|
for _, step in ipairs(self.config.post_install) do
|
||||||
|
local cwd = pkg:get_install_path()
|
||||||
|
local command = step.command
|
||||||
|
|
||||||
|
if command:find("[/\\]") then
|
||||||
|
command = vim.fn.resolve(("%s/%s"):format(cwd, command))
|
||||||
|
end
|
||||||
|
|
||||||
|
if not utils.is_executable(command) then
|
||||||
|
log_err(step, "command not executable")
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
|
local job = require("plenary.job"):new(
|
||||||
|
{
|
||||||
|
command = step.command,
|
||||||
|
args = step.args,
|
||||||
|
cwd = pkg:get_install_path(),
|
||||||
|
enabled_recording = true,
|
||||||
|
on_exit = function(job, code, signal)
|
||||||
|
if code ~= 0 or signal ~= 0 then
|
||||||
|
local cmd = command
|
||||||
|
if step.args then
|
||||||
|
cmd = cmd .. " " .. table.concat(step.args, " ")
|
||||||
|
end
|
||||||
|
|
||||||
|
log_err(step, table.concat(job:stderr_result(), "\n"))
|
||||||
|
end
|
||||||
end,
|
end,
|
||||||
})
|
}
|
||||||
|
)
|
||||||
job:start()
|
job:start()
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@@ -50,9 +162,9 @@ end
|
|||||||
---@param on_done fun(success: boolean)?
|
---@param on_done fun(success: boolean)?
|
||||||
function M:mason_install(on_done)
|
function M:mason_install(on_done)
|
||||||
local registry = require("mason-registry")
|
local registry = require("mason-registry")
|
||||||
local ok, pkg = pcall(registry.get_package, self.name)
|
local ok, pkg = pcall(registry.get_package, self.config.name)
|
||||||
if not ok then
|
if not ok then
|
||||||
utils.err("Could not locate package " .. self.name, module_name)
|
utils.err("Could not locate package " .. self.config.name)
|
||||||
|
|
||||||
if on_done then
|
if on_done then
|
||||||
on_done(false)
|
on_done(false)
|
||||||
@@ -69,20 +181,29 @@ function M:mason_install(on_done)
|
|||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
utils.info(("Installing %s"):format(self.name), module_name)
|
utils.info(("Installing %s"):format(self.config.name))
|
||||||
local handle = pkg:install({ version = self.version, })
|
local handle = pkg:install({ version = self.config.version })
|
||||||
|
|
||||||
local err
|
local err
|
||||||
handle:on("stderr", vim.schedule_wrap(function (msg)
|
handle:on(
|
||||||
|
"stderr", vim.schedule_wrap(
|
||||||
|
function(msg)
|
||||||
err = (err or "") .. msg
|
err = (err or "") .. msg
|
||||||
end))
|
end
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
handle:once("closed", vim.schedule_wrap(function ()
|
handle:once(
|
||||||
|
"closed", vim.schedule_wrap(
|
||||||
|
function()
|
||||||
local is_installed = pkg:is_installed()
|
local is_installed = pkg:is_installed()
|
||||||
|
|
||||||
if is_installed then
|
if is_installed then
|
||||||
|
utils.info(
|
||||||
|
("Successfully installed %s"):format(self.config.name),
|
||||||
|
"lsp.package:mason_install"
|
||||||
|
)
|
||||||
self:run_post_install(pkg)
|
self:run_post_install(pkg)
|
||||||
utils.info(("Successfully installed %s"):format(self.name), module_name)
|
|
||||||
else
|
else
|
||||||
if err then
|
if err then
|
||||||
err = ":\n" .. err
|
err = ":\n" .. err
|
||||||
@@ -91,15 +212,17 @@ function M:mason_install(on_done)
|
|||||||
end
|
end
|
||||||
|
|
||||||
utils.err(
|
utils.err(
|
||||||
("Failed to install %s%s"):format(self.name, err),
|
("Failed to install %s%s"):format(self.config.name, err),
|
||||||
module_name
|
"lsp.package:mason_install"
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
if on_done then
|
if on_done then
|
||||||
on_done(is_installed)
|
on_done(is_installed)
|
||||||
end
|
end
|
||||||
end))
|
end
|
||||||
|
)
|
||||||
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
--- Install package dependencies
|
--- Install package dependencies
|
||||||
@@ -153,19 +276,29 @@ function M:install(on_done)
|
|||||||
end
|
end
|
||||||
|
|
||||||
--- Create a new instance
|
--- Create a new instance
|
||||||
---@param config MasonPackageConfig
|
---@param config MasonPackageConfig?
|
||||||
---@return MasonPackageConfig
|
---@return MasonPackage?
|
||||||
function M:new(config)
|
function M.new(config)
|
||||||
config = config or {}
|
config = config or {}
|
||||||
|
|
||||||
if config.dependencies then
|
if not M.validate(config) then
|
||||||
for i, dep in ipairs(config.dependencies) do
|
return
|
||||||
config.dependencies[i] = M:new(dep)
|
end
|
||||||
|
|
||||||
|
local pkg = { config = config }
|
||||||
|
|
||||||
|
if pkg.config.dependencies then
|
||||||
|
pkg.dependencies = {}
|
||||||
|
|
||||||
|
for _, dep_cfg in ipairs(config.dependencies) do
|
||||||
|
local dep = M.new(dep_cfg)
|
||||||
|
if dep then
|
||||||
|
table.insert(pkg.dependencies, dep)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
setmetatable(config, self)
|
return setmetatable(pkg, M)
|
||||||
return config
|
|
||||||
end
|
end
|
||||||
|
|
||||||
return M
|
return M
|
||||||
|
|||||||
+202
-214
@@ -1,98 +1,131 @@
|
|||||||
local module_name = "lsp.server"
|
|
||||||
local utils = require("utils")
|
local utils = require("utils")
|
||||||
|
local keymap = require("lsp.keymap")
|
||||||
---@class MasonPackageConfig
|
---@class MasonPackage
|
||||||
local Package = require("lsp.package")
|
local MasonPackage = require("lsp.package")
|
||||||
|
|
||||||
-- override type, seems to be incorrect in either lspconfig or vim.lsp
|
-- override type, seems to be incorrect in either lspconfig or vim.lsp
|
||||||
---@class lspconfig.Config
|
---@class lspconfig.Config
|
||||||
---@field root_dir function
|
---@field root_dir function
|
||||||
|
|
||||||
---@class ServerConfig
|
---@class Server
|
||||||
---@field name string?
|
---@field name string?
|
||||||
---@field enable boolean?
|
---@field mason MasonPackage?
|
||||||
---@field dependencies string[]
|
---@field client vim.lsp.Client?
|
||||||
---@field py_module_deps string[]
|
---@field attached_buffers number[]?
|
||||||
---@field mason MasonPackageConfig?
|
---@field manager lspconfig.Manager
|
||||||
---@field root_patterns string[]?
|
---@field config ServerConfig
|
||||||
---@field lspconfig lspconfig.Config
|
|
||||||
local M = {}
|
local M = {}
|
||||||
M.__index = M
|
M.__index = M
|
||||||
|
---@class ServerConfig
|
||||||
--- Reload all buffers attached by a server
|
---@field enable boolean?
|
||||||
function M:reload_buffers()
|
---@field dependencies string[]?
|
||||||
local ft_map = {}
|
---@field mason MasonPackageConfig?
|
||||||
for _, ft in ipairs(self.lspconfig.filetypes) do
|
---@field root_patterns string[]?
|
||||||
ft_map[ft] = true
|
---@field keymaps Keymap[]?
|
||||||
|
---@field lspconfig lspconfig.Config?
|
||||||
|
M.config = {}
|
||||||
|
--- Validate ServerConfig
|
||||||
|
---@param config ServerConfig
|
||||||
|
---@return boolean
|
||||||
|
function M.validate(name, config)
|
||||||
|
local ok, resp = pcall(vim.validate, { config = { config, { "table" } } })
|
||||||
|
if ok then
|
||||||
|
ok, resp = pcall(vim.validate, {
|
||||||
|
enable = { config.enable, { "boolean" }, true },
|
||||||
|
dependencies = {
|
||||||
|
config.dependencies,
|
||||||
|
function(f)
|
||||||
|
return utils.is_list_or_nil(f, "string")
|
||||||
|
end, "list of strings or nil",
|
||||||
|
},
|
||||||
|
mason = {
|
||||||
|
config.mason, function(f)
|
||||||
|
if f == nil then return true end
|
||||||
|
return MasonPackage.validate(f)
|
||||||
|
end,
|
||||||
|
},
|
||||||
|
root_patterns = {
|
||||||
|
config.root_patterns,
|
||||||
|
function(f)
|
||||||
|
return utils.is_list_or_nil(f, "string")
|
||||||
|
end, "list of strings or nil",
|
||||||
|
},
|
||||||
|
keymaps = {
|
||||||
|
config.keymaps, function(f)
|
||||||
|
if not f then return true end
|
||||||
|
if not utils.is_list(f, "table") then
|
||||||
|
return false
|
||||||
end
|
end
|
||||||
for _, bufnr in ipairs(vim.api.nvim_list_bufs()) do
|
for _, key in ipairs(f) do
|
||||||
if vim.api.nvim_buf_is_loaded(bufnr) then
|
local o, r = pcall(vim.validate, {
|
||||||
local buf_ft = vim.api.nvim_get_option_value("filetype", { buf = bufnr, })
|
mode = { key.mode, { "s", "t" } },
|
||||||
if ft_map[buf_ft] then
|
lhs = { key.lhs, "s" },
|
||||||
vim.api.nvim_buf_call(bufnr, vim.cmd.edit)
|
rhs = { key.rhs, { "s", "f" } },
|
||||||
|
opts = { key.opts, "t", true },
|
||||||
|
})
|
||||||
|
if not o then
|
||||||
|
utils.err(("Invalid keymap:\n%s"):format(r))
|
||||||
|
return false
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
return true
|
||||||
|
end, "list of keymaps",
|
||||||
|
},
|
||||||
|
lspconfig = { config.lspconfig, { "table" }, true },
|
||||||
|
})
|
||||||
end
|
end
|
||||||
|
if not ok then
|
||||||
|
utils.err(("Invalid config for %s:\n%s"):format(name, resp))
|
||||||
|
return false
|
||||||
|
end
|
||||||
|
return true
|
||||||
end
|
end
|
||||||
|
|
||||||
--- Rename Code Action
|
--- Rename Code Action
|
||||||
function M.ca_rename()
|
function M.ca_rename()
|
||||||
local ts_utils = utils.try_require("nvim-treesitter.ts_utils", module_name)
|
local ts_utils = utils.try_require("nvim-treesitter.ts_utils")
|
||||||
if not ts_utils then
|
if not ts_utils then return end
|
||||||
return
|
local identifier_types = {
|
||||||
end
|
"IDENTIFIER", "identifier", "variable_name", "word",
|
||||||
|
}
|
||||||
local identifier_types = { "IDENTIFIER", "identifier", "variable_name", "word", }
|
|
||||||
|
|
||||||
local node = ts_utils.get_node_at_cursor()
|
local node = ts_utils.get_node_at_cursor()
|
||||||
if not node or not utils.has_value(identifier_types, node:type()) then
|
if not node or not vim.list_contains(identifier_types, node:type()) then
|
||||||
utils.info("Only identifiers may be renamed", module_name)
|
utils.info("Only identifiers may be renamed")
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
vim.lsp.buf.document_highlight()
|
vim.lsp.buf.document_highlight()
|
||||||
|
|
||||||
local old = vim.fn.expand("<cword>")
|
local old = vim.fn.expand("<cword>")
|
||||||
local buf = vim.api.nvim_create_buf(false, true)
|
local buf = vim.api.nvim_create_buf(false, true)
|
||||||
local min_width = 10
|
local min_width = 10
|
||||||
local max_width = 50
|
local max_width = 50
|
||||||
local default_width = math.min(
|
local default_width = math.min(max_width, math.max(min_width,
|
||||||
max_width,
|
vim.str_utfindex(old) + 1))
|
||||||
math.max(min_width, vim.str_utfindex(old) + 1)
|
|
||||||
)
|
|
||||||
local row, col, _, _ = node:range()
|
local row, col, _, _ = node:range()
|
||||||
local win = vim.api.nvim_open_win(
|
local win = vim.api.nvim_open_win(buf, true, {
|
||||||
buf,
|
|
||||||
true,
|
|
||||||
{
|
|
||||||
relative = "win",
|
relative = "win",
|
||||||
anchor = "NW",
|
anchor = "NW",
|
||||||
width = default_width,
|
width = default_width,
|
||||||
height = 1,
|
height = 1,
|
||||||
bufpos = { row, col - 1, },
|
bufpos = { row, col - 1 },
|
||||||
focusable = true,
|
focusable = true,
|
||||||
zindex = 50,
|
zindex = 50,
|
||||||
style = "minimal",
|
style = "minimal",
|
||||||
border = "rounded",
|
border = "rounded",
|
||||||
title = "Rename",
|
title = "Rename",
|
||||||
title_pos = "center",
|
title_pos = "center",
|
||||||
}
|
})
|
||||||
)
|
vim.api.nvim_buf_set_lines(buf, 0, -1, false, { old })
|
||||||
|
|
||||||
vim.api.nvim_buf_set_lines(buf, 0, -1, false, { old, })
|
vim.api.nvim_create_autocmd({ "TextChanged", "TextChangedI", "TextChangedP" },
|
||||||
|
{
|
||||||
vim.api.nvim_create_autocmd(
|
|
||||||
{ "TextChanged", "TextChangedI", "TextChangedP", }, {
|
|
||||||
buffer = buf,
|
buffer = buf,
|
||||||
callback = function ()
|
callback = function()
|
||||||
local win_width = vim.api.nvim_win_get_width(win)
|
local win_width = vim.api.nvim_win_get_width(win)
|
||||||
local content = vim.api.nvim_buf_get_lines(buf, 0, -1, false)
|
local content = vim.api.nvim_buf_get_lines(buf, 0, -1, false)
|
||||||
if #content > 0 then
|
if #content > 0 then
|
||||||
local cwidth = vim.str_utfindex(content[1] or "") + 1
|
local cwidth = vim.str_utfindex(content[1] or "") + 1
|
||||||
local new_width = math.min(
|
local new_width = math.min(max_width,
|
||||||
max_width,
|
math.max(min_width, cwidth))
|
||||||
math.max(min_width, cwidth)
|
|
||||||
)
|
|
||||||
if new_width ~= win_width then
|
if new_width ~= win_width then
|
||||||
vim.api.nvim_win_set_width(win, new_width)
|
vim.api.nvim_win_set_width(win, new_width)
|
||||||
end
|
end
|
||||||
@@ -100,10 +133,7 @@ function M.ca_rename()
|
|||||||
end,
|
end,
|
||||||
})
|
})
|
||||||
|
|
||||||
vim.keymap.set(
|
vim.keymap.set({ "n", "i", "x" }, "<cr>", function()
|
||||||
{ "n", "i", "x", },
|
|
||||||
"<cr>",
|
|
||||||
function ()
|
|
||||||
local content = vim.api.nvim_buf_get_lines(buf, 0, -1, false)
|
local content = vim.api.nvim_buf_get_lines(buf, 0, -1, false)
|
||||||
vim.api.nvim_win_close(win, true)
|
vim.api.nvim_win_close(win, true)
|
||||||
vim.cmd.stopinsert()
|
vim.cmd.stopinsert()
|
||||||
@@ -111,88 +141,47 @@ function M.ca_rename()
|
|||||||
local new_name = content[1]
|
local new_name = content[1]
|
||||||
vim.lsp.buf.rename(new_name)
|
vim.lsp.buf.rename(new_name)
|
||||||
end
|
end
|
||||||
end,
|
end, { buffer = buf })
|
||||||
{ buffer = buf, }
|
vim.keymap.set({ "n", "i", "x" }, "<C-c>", function()
|
||||||
)
|
|
||||||
vim.keymap.set(
|
|
||||||
{ "n", "i", "x", },
|
|
||||||
"<C-c>",
|
|
||||||
function ()
|
|
||||||
vim.api.nvim_win_close(win, true)
|
vim.api.nvim_win_close(win, true)
|
||||||
vim.cmd.stopinsert()
|
vim.cmd.stopinsert()
|
||||||
end,
|
end, { buffer = buf })
|
||||||
{ buffer = buf, }
|
vim.keymap.set({ "n", "x" }, "<esc>",
|
||||||
)
|
function() vim.api.nvim_win_close(win, true) end,
|
||||||
vim.keymap.set(
|
{ buffer = buf })
|
||||||
{ "n", "x", },
|
vim.keymap.set({ "n", "x" }, "q",
|
||||||
"<esc>",
|
function() vim.api.nvim_win_close(win, true) end,
|
||||||
function ()
|
{ buffer = buf })
|
||||||
vim.api.nvim_win_close(win, true)
|
|
||||||
end,
|
|
||||||
{ buffer = buf, }
|
|
||||||
)
|
|
||||||
vim.keymap.set(
|
|
||||||
{ "n", "x", },
|
|
||||||
"q",
|
|
||||||
function ()
|
|
||||||
vim.api.nvim_win_close(win, true)
|
|
||||||
end,
|
|
||||||
{ buffer = buf, }
|
|
||||||
)
|
|
||||||
|
|
||||||
vim.api.nvim_feedkeys(
|
vim.api.nvim_feedkeys(vim.api.nvim_replace_termcodes("^v$<C-g>", true,
|
||||||
vim.api.nvim_replace_termcodes("^v$<C-g>", true, false, true),
|
false, true), "n", true)
|
||||||
"n",
|
|
||||||
true
|
|
||||||
)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
--- Called when language server attaches
|
--- Called when language server attaches
|
||||||
---@param client vim.lsp.Client
|
---@param client vim.lsp.Client
|
||||||
---@param bufnr integer
|
---@param bufnr integer
|
||||||
function M:on_attach(client, bufnr)
|
function M:on_attach(client, bufnr)
|
||||||
-- Mappings.
|
if self.client and self.client.id ~= client.id then
|
||||||
-- See `:help vim.lsp.*` for documentation on any of the below functions
|
self.client.stop(true)
|
||||||
local opts = { buffer = bufnr, }
|
|
||||||
vim.keymap.set("n", "<leader>df", vim.diagnostic.open_float, opts)
|
|
||||||
vim.keymap.set("n", "[d", vim.diagnostic.goto_prev, opts)
|
|
||||||
vim.keymap.set("n", "]d", vim.diagnostic.goto_next, opts)
|
|
||||||
vim.keymap.set("n", "gD", vim.lsp.buf.declaration, opts)
|
|
||||||
vim.keymap.set({ "n", "i", }, "<C-k>", vim.lsp.buf.hover, opts)
|
|
||||||
vim.keymap.set({ "n", "i", }, "<C-j>", vim.lsp.buf.signature_help, opts)
|
|
||||||
vim.keymap.set({ "n", "i", }, "<C-h>", vim.lsp.buf.document_highlight, opts)
|
|
||||||
vim.keymap.set("n", "<leader>lr", self.ca_rename, opts)
|
|
||||||
vim.keymap.set("n", "<leader>la", vim.lsp.buf.code_action, opts)
|
|
||||||
vim.keymap.set({ "n", "x", }, "<leader>lf", vim.lsp.buf.format, opts)
|
|
||||||
|
|
||||||
---@module "telescope.builtin"
|
|
||||||
local telescope = utils.try_require("telescope.builtin", module_name)
|
|
||||||
if telescope then
|
|
||||||
vim.keymap.set("n", "<leader>dl", telescope.diagnostics, opts)
|
|
||||||
vim.keymap.set("n", "<leader>lD", telescope.lsp_type_definitions, opts)
|
|
||||||
vim.keymap.set("n", "gd", telescope.lsp_definitions, opts)
|
|
||||||
vim.keymap.set("n", "gi", telescope.lsp_implementations, opts)
|
|
||||||
vim.keymap.set("n", "gr", telescope.lsp_references, opts)
|
|
||||||
else
|
|
||||||
vim.keymap.set("n", "<leader>dl", vim.diagnostic.setloclist, opts)
|
|
||||||
vim.keymap.set("n", "<leader>ld", vim.lsp.buf.type_definition, opts)
|
|
||||||
vim.keymap.set("n", "gd", vim.lsp.buf.definition, opts)
|
|
||||||
vim.keymap.set("n", "gi", vim.lsp.buf.implementation, opts)
|
|
||||||
vim.keymap.set("n", "gr", vim.lsp.buf.references, opts)
|
|
||||||
end
|
end
|
||||||
|
self.client = client
|
||||||
|
self.attached_buffers = self.attached_buffers or {}
|
||||||
|
table.insert(self.attached_buffers, bufnr)
|
||||||
|
|
||||||
|
keymap:load(self, bufnr)
|
||||||
|
|
||||||
-- For document highlight
|
-- For document highlight
|
||||||
vim.cmd.highlight({ "link LspReferenceRead Visual", bang = true, })
|
vim.cmd.highlight({ "link LspReferenceRead Visual", bang = true })
|
||||||
vim.cmd.highlight({ "link LspReferenceText Visual", bang = true, })
|
vim.cmd.highlight({ "link LspReferenceText Visual", bang = true })
|
||||||
vim.cmd.highlight({ "link LspReferenceWrite Visual", bang = true, })
|
vim.cmd.highlight({ "link LspReferenceWrite Visual", bang = true })
|
||||||
-- vim.api.nvim_create_autocmd({ "CursorHold", "CursorHoldI", }, {
|
-- vim.api.nvim_create_autocmd({ "CursorHold", "CursorHoldI", }, {
|
||||||
-- buffer = bufnr,
|
-- buffer = bufnr,
|
||||||
-- callback = vim.lsp.buf.document_highlight,
|
-- callback = vim.lsp.buf.document_highlight,
|
||||||
-- })
|
-- })
|
||||||
vim.api.nvim_create_autocmd({ "CursorMoved", "CursorMovedI", }, {
|
-- vim.api.nvim_create_autocmd({ "CursorMoved", "CursorMovedI" }, {
|
||||||
buffer = bufnr,
|
-- buffer = bufnr,
|
||||||
callback = vim.lsp.buf.clear_references,
|
-- callback = vim.lsp.buf.clear_references,
|
||||||
})
|
-- })
|
||||||
|
|
||||||
-- Auto show signature on insert in function parameters
|
-- Auto show signature on insert in function parameters
|
||||||
-- if client.server_capabilities.signatureHelpProvider then
|
-- if client.server_capabilities.signatureHelpProvider then
|
||||||
@@ -207,85 +196,76 @@ function M:on_attach(client, bufnr)
|
|||||||
-- end
|
-- end
|
||||||
|
|
||||||
vim.opt.updatetime = 300
|
vim.opt.updatetime = 300
|
||||||
|
|
||||||
require("lsp-inlayhints").on_attach(client, bufnr, false)
|
require("lsp-inlayhints").on_attach(client, bufnr, false)
|
||||||
|
|
||||||
vim.lsp.handlers["textDocument/hover"] = vim.lsp.with(
|
vim.lsp.handlers["textDocument/hover"] = vim.lsp.with(
|
||||||
vim.lsp.handlers.hover, { border = "single", }
|
vim.lsp.handlers.hover,
|
||||||
)
|
{ border = "single" })
|
||||||
vim.lsp.handlers["textDocument/signatureHelp"] = vim.lsp.with(
|
vim.lsp.handlers["textDocument/signatureHelp"] =
|
||||||
vim.lsp.handlers.signature_help, { border = "single", }
|
vim.lsp.with(vim.lsp.handlers.signature_help, { border = "single" })
|
||||||
)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
--- Configure the LSP client
|
--- Configure the LSP client
|
||||||
function M:configure_client()
|
function M:configure_client()
|
||||||
local ok, ret = pcall(require, "lspconfig")
|
local lspconfig = require("lspconfig")
|
||||||
if not ok then
|
if self.config.root_patterns then
|
||||||
utils.err("Missing required plugin lspconfig", module_name)
|
self.config.lspconfig.root_dir =
|
||||||
return
|
lspconfig.util.root_pattern(unpack(self.config.root_patterns))
|
||||||
end
|
|
||||||
local lspconfig = ret
|
|
||||||
|
|
||||||
if self.root_patterns then
|
|
||||||
self.lspconfig.root_dir = lspconfig.util.root_pattern(unpack(self.root_patterns))
|
|
||||||
else
|
else
|
||||||
self.lspconfig.root_dir = lspconfig.util.find_git_ancestor
|
self.config.lspconfig.root_dir = lspconfig.util.find_git_ancestor
|
||||||
end
|
end
|
||||||
|
|
||||||
local capabilities = vim.lsp.protocol.make_client_capabilities()
|
local capabilities = vim.lsp.protocol.make_client_capabilities()
|
||||||
utils.try_require("cmp_nvim_lsp", module_name, function (cmp_nvim_lsp)
|
local cmp_nvim_lsp = utils.try_require("cmp_nvim_lsp")
|
||||||
capabilities = vim.tbl_deep_extend(
|
if cmp_nvim_lsp then
|
||||||
"force",
|
capabilities = vim.tbl_deep_extend("force", capabilities,
|
||||||
capabilities,
|
cmp_nvim_lsp.default_capabilities())
|
||||||
cmp_nvim_lsp.default_capabilities()
|
end
|
||||||
)
|
self.config.lspconfig.capabilities = capabilities
|
||||||
end)
|
self.config.lspconfig.on_attach = function(client, bufnr)
|
||||||
self.lspconfig.capabilities = capabilities
|
local ok, ret = pcall(self.on_attach, self, client, bufnr)
|
||||||
|
|
||||||
self.lspconfig.on_attach = function (...)
|
|
||||||
ok, ret = pcall(self.on_attach, self, ...)
|
|
||||||
if not ok then
|
if not ok then
|
||||||
utils.err(
|
utils.err(
|
||||||
("Failed to load on_attach for %s:\n%s"):format(self.name, ret),
|
("Failed to load on_attach for %s:\n%s"):format(self.name, ret),
|
||||||
module_name
|
"lsp.server:configure_client")
|
||||||
)
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
local ok, ret = pcall(lspconfig[self.name].setup, self.config.lspconfig)
|
||||||
ok, ret = pcall(lspconfig[self.name].setup, self.lspconfig)
|
|
||||||
if not ok then
|
if not ok then
|
||||||
utils.err(
|
utils.err(("Failed to setup LSP server %s with lspconfig: %s"):format(
|
||||||
("Failed to setup LSP server %s with lspconfig: %s"):format(self.name, ret),
|
self.name, ret))
|
||||||
module_name
|
|
||||||
)
|
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
self.manager = lspconfig[self.name].manager
|
||||||
|
for _, bufnr in ipairs(self:get_ft_buffers()) do
|
||||||
|
utils.debug(("Trying to attach buffer %d"):format(bufnr))
|
||||||
|
self.manager:try_add_wrapper(bufnr)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
self:reload_buffers()
|
function M:get_ft_buffers()
|
||||||
|
local filetypes = self.config.lspconfig.filetypes or {}
|
||||||
|
if not vim.list_contains(filetypes, self.config.lspconfig.filetype) then
|
||||||
|
table.insert(filetypes, self.config.lspconfig.filetype)
|
||||||
|
end
|
||||||
|
if #filetypes == 0 then
|
||||||
|
return {}
|
||||||
|
end
|
||||||
|
return vim.tbl_filter(function(bufnr)
|
||||||
|
return vim.list_contains(filetypes, vim.bo[bufnr].filetype)
|
||||||
|
end, vim.api.nvim_list_bufs())
|
||||||
end
|
end
|
||||||
|
|
||||||
--- Check for and return missing dependencies
|
--- Check for and return missing dependencies
|
||||||
---@return table<string>
|
---@return table<string>
|
||||||
function M:get_missing_unmanaged_deps()
|
function M:get_missing_unmanaged_deps()
|
||||||
local missing_deps = {}
|
local missing_deps = {}
|
||||||
|
if self.config.dependencies ~= nil then
|
||||||
if self.dependencies ~= nil then
|
for _, dep in ipairs(self.config.dependencies) do
|
||||||
for _, dep in ipairs(self.dependencies) do
|
if not utils.is_executable(dep) then
|
||||||
if not utils.is_installed(dep) then
|
|
||||||
table.insert(missing_deps, dep)
|
table.insert(missing_deps, dep)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
if self.py_module_deps ~= nil then
|
|
||||||
for _, mod in ipairs(self.py_module_deps) do
|
|
||||||
if not utils.python3_module_is_installed(mod) then
|
|
||||||
table.insert(missing_deps, "python3-" .. mod)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
return missing_deps
|
return missing_deps
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -295,13 +275,8 @@ function M:install(on_done)
|
|||||||
--- Handle install result
|
--- Handle install result
|
||||||
---@param success boolean
|
---@param success boolean
|
||||||
local function handle_result(success)
|
local function handle_result(success)
|
||||||
if not success then
|
if not success then self.config.enable = false end
|
||||||
self.enable = false
|
if on_done then on_done(success) end
|
||||||
end
|
|
||||||
|
|
||||||
if on_done then
|
|
||||||
on_done(success)
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
self.mason:install(handle_result)
|
self.mason:install(handle_result)
|
||||||
@@ -312,57 +287,70 @@ function M:setup()
|
|||||||
local missing_deps = self:get_missing_unmanaged_deps()
|
local missing_deps = self:get_missing_unmanaged_deps()
|
||||||
if #missing_deps > 0 then
|
if #missing_deps > 0 then
|
||||||
utils.warn(
|
utils.warn(
|
||||||
("Disabling %s because the following package(s) are not installed: %s")
|
("Disabling %s because the following package(s) are not installed: %s"):format(
|
||||||
:format(self.name, table.concat(missing_deps, ", ")),
|
self.name, table.concat(missing_deps, ", ")))
|
||||||
module_name
|
self.config.enable = false
|
||||||
)
|
|
||||||
self.enable = false
|
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
if vim.fn.executable(self.lspconfig.cmd[1]) == 1 then
|
|
||||||
self:configure_client()
|
|
||||||
return
|
|
||||||
end
|
|
||||||
|
|
||||||
if self.mason then
|
if self.mason then
|
||||||
self:install(function (success)
|
self:install(function(success)
|
||||||
if success then
|
if success then self:configure_client() end
|
||||||
self:configure_client()
|
|
||||||
end
|
|
||||||
end)
|
end)
|
||||||
|
elseif vim.fn.executable(self.config.lspconfig.cmd[1]) == 1 then
|
||||||
|
self:configure_client()
|
||||||
else
|
else
|
||||||
utils.warn(self.name .. " not installed, disabling", module_name)
|
utils.warn(self.name .. " not installed, disabling")
|
||||||
self.enable = false
|
self.config.enable = false
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
--- Register autocmd for setting up LSP server upon entering a buffer of related filetype
|
--- Register autocmd for setting up LSP server upon entering a buffer of related filetype
|
||||||
function M:register()
|
function M:register()
|
||||||
local augroup = vim.api.nvim_create_augroup("LSP-" .. self.name, {})
|
local group = vim.api.nvim_create_augroup("lsp_bootstrap_" .. self.name, {})
|
||||||
vim.api.nvim_create_autocmd("FileType", {
|
vim.api.nvim_create_autocmd("FileType", {
|
||||||
once = true,
|
once = true,
|
||||||
pattern = table.concat(self.lspconfig.filetypes, ","),
|
pattern = self.config.lspconfig.filetypes or {},
|
||||||
callback = vim.schedule_wrap(function ()
|
callback = function() self:setup() end,
|
||||||
self:setup()
|
group = group,
|
||||||
vim.api.nvim_del_augroup_by_id(augroup)
|
|
||||||
end),
|
|
||||||
group = augroup,
|
|
||||||
})
|
})
|
||||||
end
|
end
|
||||||
|
|
||||||
--- Create a new instance
|
function M:unload()
|
||||||
---@param config ServerConfig
|
if self.attached_buffers then
|
||||||
---@return ServerConfig
|
for _, bufnr in ipairs(self.attached_buffers) do
|
||||||
function M:new(config)
|
keymap:unload(bufnr)
|
||||||
config = config or {}
|
|
||||||
|
|
||||||
if config.mason then
|
|
||||||
config.mason = Package:new(config.mason)
|
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
if self.client then
|
||||||
|
self.client.stop()
|
||||||
|
self.client = nil
|
||||||
|
end
|
||||||
|
vim.api.nvim_clear_autocmds({ group = "lsp_bootstrap_" .. self.name })
|
||||||
|
|
||||||
setmetatable(config, self)
|
require("lspconfig")[self.name] = nil
|
||||||
return config
|
end
|
||||||
|
|
||||||
|
--- Create a new instance
|
||||||
|
---@param name string
|
||||||
|
---@param config ServerConfig?
|
||||||
|
---@return Server?
|
||||||
|
function M.new(name, config)
|
||||||
|
config = config or {}
|
||||||
|
if not M.validate(name, config) then return end
|
||||||
|
local ok, resp = pcall(require, "lspconfig.server_configurations." .. name)
|
||||||
|
if not ok then
|
||||||
|
utils.err(("Server with name %s does not exist in lspconfig"):format(
|
||||||
|
name))
|
||||||
|
return
|
||||||
|
end
|
||||||
|
config.lspconfig = vim.tbl_deep_extend("keep", config.lspconfig or {},
|
||||||
|
resp.default_config)
|
||||||
|
local server = { name = name, config = config }
|
||||||
|
if server.config.mason then
|
||||||
|
local pkg = MasonPackage.new(server.config.mason)
|
||||||
|
if pkg then server.mason = pkg end
|
||||||
|
end
|
||||||
|
return setmetatable(server, M)
|
||||||
end
|
end
|
||||||
|
|
||||||
return M
|
return M
|
||||||
|
|||||||
+4144
File diff suppressed because it is too large
Load Diff
+8
-13
@@ -36,11 +36,7 @@ local plugins = {
|
|||||||
"L3MON4D3/LuaSnip",
|
"L3MON4D3/LuaSnip",
|
||||||
config = require("plugins.luasnip"),
|
config = require("plugins.luasnip"),
|
||||||
-- comment out on windows and install jsregexp manually
|
-- comment out on windows and install jsregexp manually
|
||||||
build = (
|
build = (require("utils").os_name ~= "Windows_NT" and "make install_jsregexp" or nil),
|
||||||
require("utils").os_name ~= "Windows_NT"
|
|
||||||
and "make install_jsregexp"
|
|
||||||
or nil
|
|
||||||
),
|
|
||||||
version = "2.*",
|
version = "2.*",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -128,10 +124,9 @@ local plugins = {
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"nvim-telescope/telescope-fzf-native.nvim",
|
"nvim-telescope/telescope-fzf-native.nvim",
|
||||||
build =
|
build = "cmake -S. -Bbuild -DCMAKE_BUILD_TYPE=Release"
|
||||||
"cmake -S. -Bbuild -DCMAKE_BUILD_TYPE=Release" ..
|
.. " && cmake --build build --config Release"
|
||||||
" && cmake --build build --config Release" ..
|
.. " && cmake --install build --prefix build",
|
||||||
" && cmake --install build --prefix build",
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"numToStr/Comment.nvim",
|
"numToStr/Comment.nvim",
|
||||||
@@ -143,13 +138,13 @@ local plugins = {
|
|||||||
"simeji/winresizer",
|
"simeji/winresizer",
|
||||||
config = require("plugins.winresizer"),
|
config = require("plugins.winresizer"),
|
||||||
lazy = true,
|
lazy = true,
|
||||||
keys = { "<C-W>r", },
|
keys = { "<C-W>r" },
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"sindrets/winshift.nvim",
|
"sindrets/winshift.nvim",
|
||||||
config = require("plugins.winshift"),
|
config = require("plugins.winshift"),
|
||||||
lazy = true,
|
lazy = true,
|
||||||
keys = { "<C-W>m", },
|
keys = { "<C-W>m" },
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"dstein64/vim-startuptime",
|
"dstein64/vim-startuptime",
|
||||||
@@ -198,7 +193,7 @@ local plugins = {
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cbochs/grapple.nvim",
|
"cbochs/grapple.nvim",
|
||||||
dependencies = { "nvim-lua/plenary.nvim", },
|
dependencies = { "nvim-lua/plenary.nvim" },
|
||||||
config = require("plugins.grapple"),
|
config = require("plugins.grapple"),
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -213,7 +208,7 @@ local plugins = {
|
|||||||
|
|
||||||
local opts = {
|
local opts = {
|
||||||
install = {
|
install = {
|
||||||
colorscheme = { "moonfly", },
|
colorscheme = { "moonfly" },
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+130
-62
@@ -2,10 +2,47 @@ local M = {}
|
|||||||
|
|
||||||
M.os_name = vim.uv.os_uname().sysname
|
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
|
--- Check that an executable is available
|
||||||
--- @param exe string Array to look for
|
--- @param exe string Array to look for
|
||||||
--- @return boolean
|
--- @return boolean
|
||||||
function M.is_installed(exe)
|
function M.is_executable(exe)
|
||||||
return vim.fn.executable(exe) == 1
|
return vim.fn.executable(exe) == 1
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -14,7 +51,7 @@ end
|
|||||||
--- @return boolean
|
--- @return boolean
|
||||||
function M.any_installed(exes)
|
function M.any_installed(exes)
|
||||||
for _, e in ipairs(exes) do
|
for _, e in ipairs(exes) do
|
||||||
if M.is_installed(e) then
|
if M.is_executable(e) then
|
||||||
return true
|
return true
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@@ -26,29 +63,27 @@ end
|
|||||||
--- Raises error if missing.
|
--- Raises error if missing.
|
||||||
--- @param exe string Array to look for
|
--- @param exe string Array to look for
|
||||||
function M.assert_installed(exe)
|
function M.assert_installed(exe)
|
||||||
if not M.is_installed(exe) then
|
assert(M.is_executable(exe), "Missing executable '" .. exe .. "'.")
|
||||||
M.notify("Missing executable '" .. exe .. "'.")
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
--- Asserts that at least one executable is available
|
--- Asserts that at least one executable is available
|
||||||
--- Raises error if missing.
|
--- Raises error if missing.
|
||||||
--- @param exes table Array of exes
|
--- @param exes table Array of exes
|
||||||
function M.assert_any_installed(exes)
|
function M.assert_any_installed(exes)
|
||||||
if not M.any_installed(exes) then
|
assert(
|
||||||
error("At least one of the following is required:\n" ..
|
M.any_installed(exes),
|
||||||
table.concat(exes, ", "))
|
"At least one of the following is required:\n" .. table.concat(exes, ", ")
|
||||||
end
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
--- Asserts that a python module is installed
|
--- Asserts that a python module is installed
|
||||||
---@param mod string The python module to check
|
---@param mod string The python module to check
|
||||||
function M.python3_module_is_installed(mod)
|
function M.python3_module_is_installed(mod)
|
||||||
if not M.is_installed("python3") then
|
if not M.is_executable("python3") then
|
||||||
return false
|
return false
|
||||||
end
|
end
|
||||||
|
|
||||||
local resp = vim.system({ "python3", "-c", "import " .. mod, }):wait()
|
local resp = vim.system({ "python3", "-c", "import " .. mod }):wait()
|
||||||
return resp.code == 0
|
return resp.code == 0
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -60,91 +95,124 @@ function M.assert_python3_module_installed(mod)
|
|||||||
end
|
end
|
||||||
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
|
--- Send a debug notification
|
||||||
---@param msg string Message to send
|
---@param msg string Message to send
|
||||||
---@param title string Title of notification
|
---@param title string? Title of notification
|
||||||
function M.debug(msg, title)
|
function M.debug(msg, title)
|
||||||
M.notify(msg, title, vim.log.levels.DEBUG)
|
notify(msg, title, vim.log.levels.DEBUG)
|
||||||
end
|
end
|
||||||
|
|
||||||
--- Send an info notification
|
--- Send an info notification
|
||||||
---@param msg string Message to send
|
---@param msg string Message to send
|
||||||
---@param title string Title of notification
|
---@param title string? Title of notification
|
||||||
function M.info(msg, title)
|
function M.info(msg, title)
|
||||||
M.notify(msg, title, vim.log.levels.INFO)
|
notify(msg, title, vim.log.levels.INFO)
|
||||||
end
|
end
|
||||||
|
|
||||||
--- Send a warning notification
|
--- Send a warning notification
|
||||||
---@param msg string Message to send
|
---@param msg string Message to send
|
||||||
---@param title string Title of notification
|
---@param title string? Title of notification
|
||||||
function M.warn(msg, title)
|
function M.warn(msg, title)
|
||||||
M.notify(msg, title, vim.log.levels.WARN)
|
notify(msg, title, vim.log.levels.WARN)
|
||||||
end
|
end
|
||||||
|
|
||||||
--- Send an error notification
|
--- Send an error notification
|
||||||
---@param msg string Message to send
|
---@param msg string Message to send
|
||||||
---@param title string Title of notification
|
---@param title string? Title of notification
|
||||||
function M.err(msg, title)
|
function M.err(msg, title)
|
||||||
M.notify(msg, title, vim.log.levels.ERROR)
|
notify(msg, title, vim.log.levels.ERROR)
|
||||||
end
|
end
|
||||||
|
|
||||||
--- Attempts to load a module and logs errors on failure.
|
--- Attempts to load a module and logs errors on failure.
|
||||||
---@param module string The module to attempt to load.
|
---@param module string The module to attempt to load.
|
||||||
---@param err_title string The error message title.
|
---@return any module The loaded module if successful, otherwise nil.
|
||||||
---@param on_success fun(module: unknown)? Will be called if module was loaded.
|
function M.try_require(module)
|
||||||
---@return unknown? module The loaded module if successful, otherwise nil.
|
|
||||||
function M.try_require(module, err_title, on_success)
|
|
||||||
local has_module, resp = pcall(require, module)
|
local has_module, resp = pcall(require, module)
|
||||||
|
|
||||||
if has_module then
|
if has_module then
|
||||||
if on_success then
|
|
||||||
on_success(resp)
|
|
||||||
end
|
|
||||||
|
|
||||||
return resp
|
return resp
|
||||||
end
|
end
|
||||||
|
|
||||||
M.err(("Failed to load module %s"):format(module), err_title)
|
M.err(("Failed to load module %s:\n%s"):format(module, resp))
|
||||||
M.err(resp, err_title)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
--- Update table t1 with values in t2.
|
--- Check if `val` is a list of type `t` (if given)
|
||||||
---@param table table<any, any> The table to update
|
---@param val any
|
||||||
---@param values table<any, any> The table with new values
|
---@param t type?
|
||||||
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
|
|
||||||
---@return boolean
|
---@return boolean
|
||||||
function M.has_value(table, value)
|
function M.is_list(val, t)
|
||||||
for _, v in ipairs(table) do
|
if type(val) ~= "table" then
|
||||||
if v == value then
|
return false
|
||||||
return true
|
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
|
||||||
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
|
end
|
||||||
|
|
||||||
return M
|
return M
|
||||||
|
|||||||
Reference in New Issue
Block a user