diff options
Diffstat (limited to 'lua')
-rw-r--r-- | lua/lvim/config/defaults.lua | 2 | ||||
-rw-r--r-- | lua/lvim/core/autocmds.lua | 72 | ||||
-rw-r--r-- | lua/lvim/core/which-key.lua | 13 | ||||
-rw-r--r-- | lua/lvim/lsp/config.lua | 25 | ||||
-rw-r--r-- | lua/lvim/lsp/handlers.lua | 6 | ||||
-rw-r--r-- | lua/lvim/lsp/init.lua | 47 | ||||
-rw-r--r-- | lua/lvim/lsp/utils.lua | 114 | ||||
-rw-r--r-- | lua/lvim/plugin-loader.lua | 4 |
8 files changed, 163 insertions, 120 deletions
diff --git a/lua/lvim/config/defaults.lua b/lua/lvim/config/defaults.lua index 54bbc67e..7bccc895 100644 --- a/lua/lvim/config/defaults.lua +++ b/lua/lvim/config/defaults.lua @@ -7,6 +7,8 @@ return { pattern = "*", ---@usage timeout number timeout in ms for the format request (Default: 1000) timeout = 1000, + ---@usage filter func to select client + filter = require("lvim.lsp.handlers").format_filter, }, keys = {}, diff --git a/lua/lvim/core/autocmds.lua b/lua/lvim/core/autocmds.lua index e10a42db..17c9bc22 100644 --- a/lua/lvim/core/autocmds.lua +++ b/lua/lvim/core/autocmds.lua @@ -70,79 +70,44 @@ local get_format_on_save_opts = function() } end -function M.enable_format_on_save(opts) - local fmd_cmd = string.format(":silent lua vim.lsp.buf.formatting_sync({}, %s)", opts.timeout) - M.define_augroups { - format_on_save = { { "BufWritePre", opts.pattern, fmd_cmd } }, - } +function M.enable_format_on_save() + local opts = get_format_on_save_opts() + vim.api.nvim_create_augroup("lsp_format_on_save", {}) + vim.api.nvim_create_autocmd("BufWritePre", { + group = "lsp_format_on_save", + pattern = opts.pattern, + callback = function() + vim.lsp.buf.format { timeout_ms = opts.timeout, filter = opts.filter } + end, + }) Log:debug "enabled format-on-save" end function M.disable_format_on_save() - M.disable_augroup "format_on_save" + pcall(vim.api.nvim_del_augroup_by_name, "lsp_format_on_save") Log:debug "disabled format-on-save" end function M.configure_format_on_save() if lvim.format_on_save then - local opts = get_format_on_save_opts() - M.enable_format_on_save(opts) + M.enable_format_on_save() else M.disable_format_on_save() end end function M.toggle_format_on_save() - if vim.fn.exists "#format_on_save#BufWritePre" == 0 then - local opts = get_format_on_save_opts() - M.enable_format_on_save(opts) + local status, _ = pcall(vim.api.nvim_get_autocmds, { + group = "lsp_format_on_save", + event = "BufWritePre", + }) + if not status then + M.enable_format_on_save() else M.disable_format_on_save() end end -function M.enable_lsp_document_highlight(client_id) - M.define_augroups({ - lsp_document_highlight = { - { - "CursorHold", - "<buffer>", - string.format("lua require('lvim.lsp.utils').conditional_document_highlight(%d)", client_id), - }, - { - "CursorMoved", - "<buffer>", - "lua vim.lsp.buf.clear_references()", - }, - }, - }, true) -end - -function M.disable_lsp_document_highlight() - M.disable_augroup "lsp_document_highlight" -end - -function M.enable_code_lens_refresh() - M.define_augroups({ - lsp_code_lens_refresh = { - { - "InsertLeave ", - "<buffer>", - "lua vim.lsp.codelens.refresh()", - }, - { - "InsertLeave ", - "<buffer>", - "lua vim.lsp.codelens.display()", - }, - }, - }, true) -end - -function M.disable_code_lens_refresh() - M.disable_augroup "lsp_code_lens_refresh" -end - function M.enable_transparent_mode() vim.cmd "au ColorScheme * hi Normal ctermbg=none guibg=none" vim.cmd "au ColorScheme * hi SignColumn ctermbg=none guibg=none" @@ -170,7 +135,6 @@ end --- Create autocommand groups based on the passed definitions ---@param definitions table contains trigger, pattern and text. The key will be used as a group name ----@param buffer boolean indicate if the augroup should be local to the buffer function M.define_augroups(definitions, buffer) for group_name, definition in pairs(definitions) do vim.cmd("augroup " .. group_name) diff --git a/lua/lvim/core/which-key.lua b/lua/lvim/core/which-key.lua index 21620b01..3c3cc66b 100644 --- a/lua/lvim/core/which-key.lua +++ b/lua/lvim/core/which-key.lua @@ -153,32 +153,31 @@ M.config = function() "Git Diff", }, }, - l = { name = "LSP", a = { "<cmd>lua vim.lsp.buf.code_action()<cr>", "Code Action" }, d = { "<cmd>Telescope diagnostics bufnr=0 theme=get_ivy<cr>", "Buffer Diagnostics" }, w = { "<cmd>Telescope diagnostics<cr>", "Diagnostics" }, - f = { "<cmd>lua vim.lsp.buf.formatting()<cr>", "Format" }, + f = { require("lvim.lsp.utils").format, "Format" }, i = { "<cmd>LspInfo<cr>", "Info" }, I = { "<cmd>LspInstallInfo<cr>", "Installer Info" }, j = { - "<cmd>lua vim.diagnostic.goto_next()<cr>", + vim.diagnostic.goto_next, "Next Diagnostic", }, k = { - "<cmd>lua vim.diagnostic.goto_prev()<cr>", + vim.diagnostic.goto_prev, "Prev Diagnostic", }, - l = { "<cmd>lua vim.lsp.codelens.run()<cr>", "CodeLens Action" }, + l = { vim.lsp.codelens.run, "CodeLens Action" }, p = { name = "Peek", d = { "<cmd>lua require('lvim.lsp.peek').Peek('definition')<cr>", "Definition" }, t = { "<cmd>lua require('lvim.lsp.peek').Peek('typeDefinition')<cr>", "Type Definition" }, i = { "<cmd>lua require('lvim.lsp.peek').Peek('implementation')<cr>", "Implementation" }, }, - q = { "<cmd>lua vim.diagnostic.setloclist()<cr>", "Quickfix" }, - r = { "<cmd>lua vim.lsp.buf.rename()<cr>", "Rename" }, + q = { vim.diagnostic.setloclist, "Quickfix" }, + r = { vim.lsp.buf.rename, "Rename" }, s = { "<cmd>Telescope lsp_document_symbols<cr>", "Document Symbols" }, S = { "<cmd>Telescope lsp_dynamic_workspace_symbols<cr>", diff --git a/lua/lvim/lsp/config.lua b/lua/lvim/lsp/config.lua index eada4ce7..a0e22107 100644 --- a/lua/lvim/lsp/config.lua +++ b/lua/lvim/lsp/config.lua @@ -94,15 +94,24 @@ return { }, buffer_mappings = { normal_mode = { - ["K"] = { "<cmd>lua vim.lsp.buf.hover()<CR>", "Show hover" }, - ["gd"] = { "<cmd>lua vim.lsp.buf.definition()<CR>", "Goto Definition" }, - ["gD"] = { "<cmd>lua vim.lsp.buf.declaration()<CR>", "Goto declaration" }, - ["gr"] = { "<cmd>lua vim.lsp.buf.references()<CR>", "Goto references" }, - ["gI"] = { "<cmd>lua vim.lsp.buf.implementation()<CR>", "Goto Implementation" }, - ["gs"] = { "<cmd>lua vim.lsp.buf.signature_help()<CR>", "show signature help" }, - ["gp"] = { "<cmd>lua require'lvim.lsp.peek'.Peek('definition')<CR>", "Peek definition" }, + ["K"] = { vim.lsp.buf.hover, "Show hover" }, + ["gd"] = { vim.lsp.buf.definition, "Goto Definition" }, + ["gD"] = { vim.lsp.buf.declaration, "Goto declaration" }, + ["gr"] = { vim.lsp.buf.references, "Goto references" }, + ["gI"] = { vim.lsp.buf.implementation, "Goto Implementation" }, + ["gs"] = { vim.lsp.buf.signature_help, "show signature help" }, + ["gp"] = { + function() + require("lvim.lsp.peek").Peek "definition" + end, + "Peek definition", + }, ["gl"] = { - "<cmd>lua require'lvim.lsp.handlers'.show_line_diagnostics()<CR>", + function() + local config = lvim.lsp.diagnostics.float + config.scope = "line" + vim.diagnostic.open_float(0, config) + end, "Show line diagnostics", }, }, diff --git a/lua/lvim/lsp/handlers.lua b/lua/lvim/lsp/handlers.lua index 5da0b21e..84f2ba5f 100644 --- a/lua/lvim/lsp/handlers.lua +++ b/lua/lvim/lsp/handlers.lua @@ -16,10 +16,4 @@ function M.setup() vim.lsp.handlers["textDocument/signatureHelp"] = vim.lsp.with(vim.lsp.handlers.signature_help, lvim.lsp.float) end -function M.show_line_diagnostics() - local config = lvim.lsp.diagnostics.float - config.scope = "line" - return vim.diagnostic.open_float(0, config) -end - return M diff --git a/lua/lvim/lsp/init.lua b/lua/lvim/lsp/init.lua index 44923123..13fafae0 100644 --- a/lua/lvim/lsp/init.lua +++ b/lua/lvim/lsp/init.lua @@ -3,23 +3,6 @@ local Log = require "lvim.core.log" local utils = require "lvim.utils" local autocmds = require "lvim.core.autocmds" -local function lsp_highlight_document(client) - if lvim.lsp.document_highlight == false then - return -- we don't need further - end - autocmds.enable_lsp_document_highlight(client.id) -end - -local function lsp_code_lens_refresh(client) - if lvim.lsp.code_lens_refresh == false then - return - end - - if client.resolved_capabilities.code_lens then - autocmds.enable_code_lens_refresh() - end -end - local function add_lsp_buffer_keybindings(bufnr) local mappings = { normal_mode = "n", @@ -65,28 +48,12 @@ function M.common_capabilities() return capabilities end -local function select_default_formater(client) - if client.name == "null-ls" or not client.resolved_capabilities.document_formatting then - return - end - Log:debug("Checking for formatter overriding for " .. client.name) - local formatters = require "lvim.lsp.null-ls.formatters" - local client_filetypes = client.config.filetypes or {} - for _, filetype in ipairs(client_filetypes) do - if #vim.tbl_keys(formatters.list_registered(filetype)) > 0 then - Log:debug("Formatter overriding detected. Disabling formatting capabilities for " .. client.name) - client.resolved_capabilities.document_formatting = false - client.resolved_capabilities.document_range_formatting = false - end - end -end - function M.common_on_exit(_, _) if lvim.lsp.document_highlight then - autocmds.disable_lsp_document_highlight() + pcall(vim.api.nvim_del_augroup_by_name, "lsp_document_highlight") end if lvim.lsp.code_lens_refresh then - autocmds.disable_code_lens_refresh() + pcall(vim.api.nvim_del_augroup_by_name, "lsp_code_lens_refresh") end end @@ -96,7 +63,6 @@ function M.common_on_init(client, bufnr) Log:debug "Called lsp.on_init_callback" return end - select_default_formater(client) end function M.common_on_attach(client, bufnr) @@ -104,8 +70,13 @@ function M.common_on_attach(client, bufnr) lvim.lsp.on_attach_callback(client, bufnr) Log:debug "Called lsp.on_attach_callback" end - lsp_highlight_document(client) - lsp_code_lens_refresh(client) + local lu = require "lvim.lsp.utils" + if lvim.lsp.document_highlight then + lu.setup_document_highlight(client, bufnr) + end + if lvim.lsp.code_lens_refresh == false then + lu.setup_codelens_refresh(client, bufnr) + end add_lsp_buffer_keybindings(bufnr) end diff --git a/lua/lvim/lsp/utils.lua b/lua/lvim/lsp/utils.lua index ebc682e5..c976ff72 100644 --- a/lua/lvim/lsp/utils.lua +++ b/lua/lvim/lsp/utils.lua @@ -40,7 +40,7 @@ function M.get_client_capabilities(client_id) end local enabled_caps = {} - for capability, status in pairs(client.resolved_capabilities) do + for capability, status in pairs(client.server_capabilities or client.resolved_capabilities) do if status == true then table.insert(enabled_caps, capability) end @@ -84,14 +84,116 @@ function M.get_all_supported_filetypes() return vim.tbl_keys(lsp_installer_filetypes or {}) end -function M.conditional_document_highlight(id) - local client_ok, method_supported = pcall(function() - return vim.lsp.get_client_by_id(id).resolved_capabilities.document_highlight +function M.setup_document_highlight(client, bufnr) + local status_ok, highlight_supported = pcall(function() + return client.supports_method "textDocument/documentHighlight" end) - if not client_ok or not method_supported then + if not status_ok or not highlight_supported then return end - vim.lsp.buf.document_highlight() + local augroup_exist, _ = pcall(vim.api.nvim_get_autocmds, { + group = "lsp_document_highlight", + }) + if not augroup_exist then + vim.api.nvim_create_augroup("lsp_document_highlight", {}) + end + vim.api.nvim_create_autocmd({ "CursorHold", "CursorHoldI" }, { + group = "lsp_document_highlight", + buffer = bufnr, + callback = vim.lsp.buf.document_highlight, + }) + vim.api.nvim_create_autocmd("CursorMoved", { + group = "lsp_document_highlight", + buffer = bufnr, + callback = vim.lsp.buf.clear_references, + }) +end + +function M.setup_codelens_refresh(client, bufnr) + local status_ok, codelens_supported = pcall(function() + return client.supports_method "textDocument/codeLens" + end) + if not status_ok or not codelens_supported then + return + end + local augroup_exist, _ = pcall(vim.api.nvim_get_autocmds, { + group = "lsp_code_lens_refresh", + }) + if not augroup_exist then + vim.api.nvim_create_augroup("lsp_code_lens_refresh", {}) + end + vim.api.nvim_create_autocmd("InsertLeave", { + group = "lsp_code_lens_refresh", + buffer = bufnr, + callback = vim.lsp.codelens.refresh, + }) + vim.api.nvim_create_autocmd("InsertLeave", { + group = "lsp_code_lens_refresh", + buffer = bufnr, + callback = vim.lsp.codelens.display, + }) +end + +---filter passed to vim.lsp.buf.format +---gives higher priority to null-ls +---@param clients table clients attached to a buffer +---@return table chosen clients +function M.format_filter(clients) + return vim.tbl_filter(function(client) + local status_ok, formatting_supported = pcall(function() + return client.supports_method "textDocument/formatting" + end) + -- give higher prio to null-ls + if status_ok and formatting_supported and client.name == "null-ls" then + return "null-ls" + else + return status_ok and formatting_supported and client.name + end + end, clients) +end + +---Provide vim.lsp.buf.format for nvim <0.8 +---@param opts table +function M.format(opts) + opts = opts or { filter = M.format_filter } + + if vim.lsp.buf.format then + vim.lsp.buf.format(opts) + end + + local bufnr = opts.bufnr or vim.api.nvim_get_current_buf() + local clients = vim.lsp.buf_get_clients(bufnr) + + if opts.filter then + clients = opts.filter(clients) + elseif opts.id then + clients = vim.tbl_filter(function(client) + return client.id == opts.id + end, clients) + elseif opts.name then + clients = vim.tbl_filter(function(client) + return client.name == opts.name + end, clients) + end + + clients = vim.tbl_filter(function(client) + return client.supports_method "textDocument/formatting" + end, clients) + + if #clients == 0 then + vim.notify "[LSP] Format request failed, no matching language servers." + end + + local timeout_ms = opts.timeout_ms or 1000 + for _, client in pairs(clients) do + local params = vim.lsp.util.make_formatting_params(opts.formatting_options) + local result, err = client.request_sync("textDocument/formatting", params, timeout_ms, bufnr) + if result and result.result then + vim.lsp.util.apply_text_edits(result.result, bufnr, client.offset_encoding) + elseif err then + vim.notify(string.format("[LSP][%s] %s", client.name, err), vim.log.levels.WARN) + end + end end return M diff --git a/lua/lvim/plugin-loader.lua b/lua/lvim/plugin-loader.lua index f4d659d6..aa275da2 100644 --- a/lua/lvim/plugin-loader.lua +++ b/lua/lvim/plugin-loader.lua @@ -143,7 +143,9 @@ function plugin_loader.sync_core_plugins() vim.api.nvim_create_autocmd("User", { pattern = "PackerComplete", once = true, - callback = require("lvim.plugin-loader").load_snapshot, + callback = function() + require("lvim.plugin-loader").load_snapshot(default_snapshot) + end, }) pcall_packer_command "sync" end |