diff options
Diffstat (limited to '.config/nvim/lua/plugins/lsp.lua')
-rw-r--r-- | .config/nvim/lua/plugins/lsp.lua | 254 |
1 files changed, 254 insertions, 0 deletions
diff --git a/.config/nvim/lua/plugins/lsp.lua b/.config/nvim/lua/plugins/lsp.lua new file mode 100644 index 0000000..1063c5d --- /dev/null +++ b/.config/nvim/lua/plugins/lsp.lua @@ -0,0 +1,254 @@ +local icons = require("icons") + +-- this is horrible and it should be redone someday +-- but that day is not today + +return { + { + "neovim/nvim-lspconfig", + -- enabled = false, + event = { "BufReadPre", "BufNewFile" }, + dependencies = { + { "williamboman/mason.nvim", config = true, lazy = false }, + { "mason-org/mason-lspconfig.nvim", opts = { automatic_enable = false } }, + "hrsh7th/cmp-nvim-lsp", + }, + opts = { + diagnostics = { + }, + servers = { + lua_ls = { + settings = { + Lua = { + -- workspace = { + -- checkThirdParty = false, + -- library = { + -- vim.env.VIMRUNTIME, + -- }, + -- }, + completion = { + showWord = "Disable", + displayContext = 8, + }, + hint = { + enable = true, + }, + diagnostics = { "trailing-space" }, + }, + }, + }, + rust_analyzer = { settings = { completion = { fullFunctionSignatures = true } } }, + ts_ls = {}, + }, + setup = {} + }, + config = function(_, opts) + local function on_attach(fn) + vim.api.nvim_create_autocmd("LspAttach", { + callback = function(args) + local buffer = args.buf + local client = vim.lsp.get_client_by_id(args.data.client_id) + fn(client, buffer) + end, + }) + end + + ---@param method string + ---@param fn fun(client:vim.lsp.Client, buffer) + local function on_supports_method(method, fn) + -- cache[method] = cache[method] or setmetatable({}, { __mode = "k" }) + return vim.api.nvim_create_autocmd("User", { + pattern = "LspSupportsMethod", + callback = function(args) + local client = vim.lsp.get_client_by_id(args.data.client_id) + local buffer = args.data.buffer ---@type number + if client and method == args.data.method then + return fn(client, buffer) + end + end, + }) + end + + -- function _check_methods(client, buffer) + -- if + -- -- don't trigger on invalid buffers + -- not vim.api.nvim_buf_is_valid(buffer) or + -- -- don't trigger on non-listed buffers + -- vim.bo[buffer].buflisted or + -- -- don't trigger on nofile buffers + -- vim.bo[buffer].buftype == "nofile" + -- then + -- return + -- end + -- for method, clients in pairs(M._supports_method) do + -- clients[client] = clients[client] or {} + -- if not clients[client][buffer] then + -- if client.supports_method and client.supports_method(method, { bufnr = buffer }) then + -- clients[client][buffer] = true + -- vim.api.nvim_exec_autocmds("User", { + -- pattern = "LspSupportsMethod", + -- data = { client_id = client.id, buffer = buffer, method = method }, + -- }) + -- end + -- end + -- end + -- end + + on_supports_method("textDocument/inlayHint", function(client, buffer) + if + vim.api.nvim_buf_is_valid(buffer) + and vim.bo[buffer].buftype == "" + and not vim.tbl_contains(opts.inlay_hints.exclude, vim.bo[buffer].filetype) + then + vim.lsp.inlay_hint.enable(true, { bufnr = buffer }) + end + end) + + vim.diagnostic.config({ + underline = true, + update_in_insert = false, + virtual_text = { + spacing = 4, + source = "if_many", + prefix = function(diagnostic) + for name, icon in pairs(icons.diagnostics) do + if diagnostic.severity == vim.diagnostic.severity[name:upper()] then + return icon + end + end + end + + }, + severity_sort = true, + }) + + local servers = opts.servers + local cmp_nvim_lsp = require "cmp_nvim_lsp" + local capabilities = vim.tbl_deep_extend( + "force", + {}, + vim.lsp.protocol.make_client_capabilities(), + cmp_nvim_lsp.default_capabilities() + ) + + local function setup(server) + local server_opts = vim.tbl_deep_extend("force", { + capabilities = vim.deepcopy(capabilities), + }, servers[server] or {}) + + if opts.setup[server] then + if opts.setup[server](server, server_opts) then + return + end + elseif opts.setup["*"] then + if opts.setup["*"](server, server_opts) then + return + end + end + require("lspconfig")[server].setup(server_opts) + end + + local mlsp = require "mason-lspconfig" + local all_mslp_servers = require("mason-lspconfig").get_mappings().lspconfig_to_package + + local ensure_installed = {} + for server, server_opts in pairs(servers) do + if server_opts then + server_opts = server_opts == true and {} or server_opts + if server_opts.mason == false or not vim.tbl_contains(all_mslp_servers, server) then + setup(server) + else + ensure_installed[#ensure_installed + 1] = server + end + end + end + + -- mlsp.setup { ensure_installed = ensure_installed, handlers = { setup } } + mlsp.setup { ensure_installed = ensure_installed } + end, + -- config = function(_, opts) + -- local cache = {} + -- + -- + -- + -- + -- on_attach(function(client, buffer) + -- -- TODO: add keybinds + -- end) + -- + -- local register_capability = vim.lsp.handlers["client/registerCapability"] + -- + -- vim.lsp.handlers["client/registerCapability"] = function(err, res, ctx) + -- local ret = register_capability(err, res, ctx) + -- local client_id = ctx.client_id + -- local client = vim.lsp.get_client_by_id(client_id) + -- local buffer = vim.api.nvim_get_current_buf() + -- -- TODO: add keybinds + -- return ret + -- end + -- + -- for name, icon in pairs(require("icons").diagnostics) do + -- name = "DiagnosticSign" .. name + -- vim.fn.sign_define(name, { text = icon, texthl = name, numhl = "" }) + -- end + -- + -- local inlay_hint = vim.lsp.buf.inlay_hint or vim.lsp.inlay_hint + -- + -- + -- if type(opts.diagnostics.virtual_text) == "table" and opts.diagnostics.virtual_text.prefix == "icons" then + -- opts.diagnostics.virtual_text.prefix = vim.fn.has "nvim-0.10.0" == 0 and "●" + -- end + -- +-- ) + -- + -- local servers = opts.servers + -- local has_cmp, cmp_nvim_lsp = pcall(require, "cmp_nvim_lsp") + -- local capabilities = vim.tbl_deep_extend( + -- "force", + -- {}, + -- vim.lsp.protocol.make_client_capabilities(), + -- has_cmp and cmp_nvim_lsp.default_capabilities() or {}, + -- opts.capabilities or {} + -- ) + -- + -- local function setup(server) + -- local server_opts = vim.tbl_deep_extend("force", { + -- capabilities = vim.deepcopy(capabilities), + -- }, servers[server] or {}) + -- + -- if opts.setup[server] then + -- if opts.setup[server](server, server_opts) then + -- return + -- end + -- elseif opts.setup["*"] then + -- if opts.setup["*"](server, server_opts) then + -- return + -- end + -- end + -- require("lspconfig")[server].setup(server_opts) + -- end + -- + -- local have_mason, mlsp = pcall(require, "mason-lspconfig") + -- local all_mslp_servers = {} + -- if have_mason then + -- all_mslp_servers = vim.tbl_keys(require("mason-lspconfig.mappings.server").lspconfig_to_package) + -- end + -- + -- local ensure_installed = {} + -- for server, server_opts in pairs(servers) do + -- if server_opts then + -- server_opts = server_opts == true and {} or server_opts + -- if server_opts.mason == false or not vim.tbl_contains(all_mslp_servers, server) then + -- setup(server) + -- else + -- ensure_installed[#ensure_installed + 1] = server + -- end + -- end + -- end + -- + -- if have_mason then + -- mlsp.setup { ensure_installed = ensure_installed, handlers = { setup } } + -- end + -- end, + }, +} |