From 79e6cbc401cf3017f8104dd41e9720bbc2a8112d Mon Sep 17 00:00:00 2001 From: Oscar Wallberg Date: Wed, 15 Apr 2026 01:41:09 +0200 Subject: [PATCH] refactor(lsp): split lsp.lua into an lsp/ package --- lua/lsp.lua | 335 -------------------------------------- lua/lsp/completion.lua | 223 +++++++++++++++++++++++++ lua/lsp/diagnostic.lua | 35 ++++ lua/lsp/init.lua | 109 +++++++++++++ lua/plugins/nvim-tree.lua | 2 +- 5 files changed, 368 insertions(+), 336 deletions(-) delete mode 100644 lua/lsp.lua create mode 100644 lua/lsp/completion.lua create mode 100644 lua/lsp/diagnostic.lua create mode 100644 lua/lsp/init.lua diff --git a/lua/lsp.lua b/lua/lsp.lua deleted file mode 100644 index f98ba09..0000000 --- a/lua/lsp.lua +++ /dev/null @@ -1,335 +0,0 @@ -local log = require("log") - -local WORD_CHARS = - "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_" - -local function fence(ft, text) - return string.format("```%s\n%s\n```", ft, text) -end - -local M = {} - -M.diagnostic_signs = { - text = { - [vim.diagnostic.severity.ERROR] = "E", - [vim.diagnostic.severity.WARN] = "W", - [vim.diagnostic.severity.INFO] = "I", - [vim.diagnostic.severity.HINT] = "H", - }, -} - ---- Load a JSON file and return a parsed table merged with settings ----@param path string ----@param settings? table ----@return table? -local function with_file(path, settings) - local file = io.open(path, "r") - if not file then - return - end - - local json = file:read("*all") - file:close() - local ok, resp = pcall( - vim.json.decode, - json, - { luanil = { object = true, array = true } } - ) - if not ok then - log.warning("Failed to parse json file %s: %s", path, resp) - return - end - - return vim.tbl_deep_extend("force", settings or {}, resp) -end - -function M.on_attach(client, buf) - client.settings = with_file( - string.format(".%s.json", client.name), - client.settings - ) or client.settings - - if - client:supports_method(vim.lsp.protocol.Methods.textDocument_completion) - then - local provider = client.server_capabilities.completionProvider - provider.triggerCharacters = provider.triggerCharacters or {} - if not provider._word_chars_added then - provider._word_chars_added = true - for c in WORD_CHARS:gmatch(".") do - table.insert(provider.triggerCharacters, c) - end - end - local group = vim.api.nvim_create_augroup( - "lsp_completion_" .. buf, - { clear = true } - ) - vim.api.nvim_create_autocmd("TextChangedI", { - buffer = buf, - group = group, - callback = function() - if vim.fn.pumvisible() ~= 0 then - return - end - local col = vim.fn.col(".") - 1 - if col <= 0 then - return - end - local char = vim.api.nvim_get_current_line():sub(col, col) - if char:match("[%w_]") then - vim.lsp.completion.get() - end - end, - }) - vim.lsp.completion.enable(true, client.id, buf, { - autotrigger = true, - convert = function(item) - local signature = vim.tbl_get( - item, - "labelDetails", - "description" - ) or item.detail or "" - return { - abbr = item.label:match("[^(]+") or item.label, - menu = "", - kind = "", - info = signature ~= "" - and fence(vim.bo[buf].filetype, signature) - or " ", - } - end, - }) - end - - vim.api.nvim_create_autocmd("LspProgress", { - buffer = buf, - callback = function(ev) - local value = ev.data.params.value - vim.api.nvim_echo({ { value.message or "done" } }, false, { - id = "lsp." .. ev.data.params.token, - kind = "progress", - source = "vim.lsp", - title = value.title, - status = value.kind ~= "end" and "running" or "success", - percent = value.percentage, - }) - end, - }) -end - -function M.setup() - vim.diagnostic.config({ - underline = true, - signs = M.diagnostic_signs, - virtual_text = false, - float = { - show_header = false, - source = true, - border = "rounded", - focusable = true, - format = function(diagnostic) - return diagnostic.message - end, - width = 80, - }, - update_in_insert = false, - severity_sort = true, - jump = { - wrap = false, - }, - }) - - vim.lsp.enable({ - "bashls", - "clangd", - "cmake", - "gopls", - -- "hyprls", - "intelephense", - -- "jedi_language_server", - "lemminx", - -- "xml_ls", - "lua_ls", - "mesonlsp", - "oxfmt", - "oxlint", - -- "phpactor", - -- "pyrefly", - "pyright", - "ruff", - "rust_analyzer", - "svelte", - "tailwindcss", - "tsgo", - "zls", - }) - - local capabilities = vim.lsp.protocol.make_client_capabilities() - capabilities.textDocument.completion.completionItem.snippetSupport = false - vim.lsp.config("*", { - capabilities = capabilities, - }) - - vim.api.nvim_create_autocmd("LspAttach", { - callback = function(ev) - local client = vim.lsp.get_client_by_id(ev.data.client_id) - if client then - M.on_attach(client, ev.buf) - end - end, - }) - - local function style_popup(winid, bufnr, width) - if - not winid - or winid <= 0 - or not vim.api.nvim_win_is_valid(winid) - or vim.api.nvim_win_get_config(winid).relative == "" - then - return - end - local cfg = { border = "rounded" } - if width then - cfg.width = math.min(width, 80) - end - if bufnr and vim.api.nvim_buf_is_valid(bufnr) then - vim.wo[winid].wrap = true - vim.wo[winid].linebreak = true - vim.wo[winid].conceallevel = 2 - pcall(vim.treesitter.start, bufnr, "markdown") - end - pcall(vim.api.nvim_win_set_config, winid, cfg) - end - - vim.api.nvim_create_autocmd("CompleteChanged", { - callback = function(ev) - local cinfo = vim.fn.complete_info({ - "selected", - "preview_winid", - "preview_bufnr", - }) - style_popup(cinfo.preview_winid, cinfo.preview_bufnr) - - local completed = vim.v.event.completed_item or {} - local lsp_item = vim.tbl_get( - completed, - "user_data", - "nvim", - "lsp", - "completion_item" - ) - local client_id = - vim.tbl_get(completed, "user_data", "nvim", "lsp", "client_id") - if not lsp_item or not client_id then - return - end - - local client = vim.lsp.get_client_by_id(client_id) - if - not client - or not client:supports_method( - vim.lsp.protocol.Methods.completionItem_resolve - ) - then - return - end - - local selected = cinfo.selected - local word = completed.word - client:request( - vim.lsp.protocol.Methods.completionItem_resolve, - lsp_item, - function(err, result) - if err or not result then - return - end - - local cur = vim.fn.complete_info({ - "selected", - "completed", - "preview_winid", - }) - if - cur.selected ~= selected - or (vim.tbl_get(cur, "completed", "word") or "") - ~= word - then - return - end - - local signature = vim.tbl_get( - result, - "labelDetails", - "description" - ) or result.detail or "" - local doc = result.documentation - if type(doc) == "table" then - doc = doc.value - end - doc = doc or "" - - local ft = vim.bo[ev.buf].filetype - local code_parts = {} - if result.additionalTextEdits then - for _, edit in ipairs(result.additionalTextEdits) do - local text = (edit.newText or ""):gsub("%s+$", "") - if text ~= "" then - table.insert(code_parts, fence(ft, text)) - end - end - end - if signature ~= "" then - table.insert(code_parts, fence(ft, signature)) - end - local sections = {} - if #code_parts > 0 then - table.insert(sections, table.concat(code_parts, "\n\n")) - end - if doc ~= "" then - table.insert(sections, doc) - end - if #sections == 0 then - if - cur.preview_winid - and cur.preview_winid > 0 - and vim.api.nvim_win_is_valid(cur.preview_winid) - then - pcall( - vim.api.nvim_win_close, - cur.preview_winid, - true - ) - end - return - end - - local max_w = 0 - for _, s in ipairs(sections) do - for _, line in - ipairs(vim.split(s, "\n", { plain = true })) - do - max_w = - math.max(max_w, vim.fn.strdisplaywidth(line)) - end - end - local sep = "\n" - .. string.rep("─", math.min(max_w, 80)) - .. "\n" - local combined = table.concat(sections, sep) - - local windata = vim.api.nvim__complete_set(selected, { - info = combined, - }) - if windata then - style_popup(windata.winid, windata.bufnr, max_w) - end - end, - ev.buf - ) - end, - }) - - vim.lsp.log.set_level(vim.log.levels.WARN) -end - -return M diff --git a/lua/lsp/completion.lua b/lua/lsp/completion.lua new file mode 100644 index 0000000..26f3479 --- /dev/null +++ b/lua/lsp/completion.lua @@ -0,0 +1,223 @@ +local WORD_CHARS = + "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_" + +local GROUP = vim.api.nvim_create_augroup("ow.lsp.completion", { clear = true }) + +local function fence(ft, text) + return string.format("```%s\n%s\n```", ft, text) +end + +local function style_popup(winid, bufnr, width) + if + not winid + or winid <= 0 + or not vim.api.nvim_win_is_valid(winid) + or vim.api.nvim_win_get_config(winid).relative == "" + then + return + end + local cfg = { border = "rounded" } + if width then + cfg.width = math.min(width, 80) + end + if bufnr and vim.api.nvim_buf_is_valid(bufnr) then + vim.wo[winid].wrap = true + vim.wo[winid].linebreak = true + vim.wo[winid].conceallevel = 2 + pcall(vim.treesitter.start, bufnr, "markdown") + end + pcall(vim.api.nvim_win_set_config, winid, cfg) +end + +local M = {} + +function M.apply_capabilities(capabilities) + capabilities.textDocument.completion.completionItem.snippetSupport = false +end + +function M.on_attach(client, buf) + if + not client:supports_method( + vim.lsp.protocol.Methods.textDocument_completion + ) + then + return + end + + local provider = client.server_capabilities.completionProvider + provider.triggerCharacters = provider.triggerCharacters or {} + if not provider._word_chars_added then + provider._word_chars_added = true + for c in WORD_CHARS:gmatch(".") do + table.insert(provider.triggerCharacters, c) + end + end + + vim.api.nvim_clear_autocmds({ group = GROUP, buffer = buf }) + vim.api.nvim_create_autocmd("TextChangedI", { + buffer = buf, + group = GROUP, + callback = function() + if vim.fn.pumvisible() ~= 0 then + return + end + local col = vim.fn.col(".") - 1 + if col <= 0 then + return + end + local char = vim.api.nvim_get_current_line():sub(col, col) + if char:match("[%w_]") then + vim.lsp.completion.get() + end + end, + }) + + vim.lsp.completion.enable(true, client.id, buf, { + autotrigger = true, + convert = function(item) + local signature = vim.tbl_get( + item, + "labelDetails", + "description" + ) or item.detail or "" + return { + abbr = item.label:match("[^(]+") or item.label, + menu = "", + kind = "", + info = signature ~= "" + and fence(vim.bo[buf].filetype, signature) + or " ", + } + end, + }) +end + +function M.setup() + vim.api.nvim_create_autocmd("CompleteChanged", { + group = GROUP, + callback = function(ev) + local cinfo = vim.fn.complete_info({ + "selected", + "preview_winid", + "preview_bufnr", + }) + + local completed = vim.v.event.completed_item or {} + local lsp_item = vim.tbl_get( + completed, + "user_data", + "nvim", + "lsp", + "completion_item" + ) + local client_id = + vim.tbl_get(completed, "user_data", "nvim", "lsp", "client_id") + local client = client_id and vim.lsp.get_client_by_id(client_id) + if + not lsp_item + or not client + or not client:supports_method( + vim.lsp.protocol.Methods.completionItem_resolve + ) + then + style_popup(cinfo.preview_winid, cinfo.preview_bufnr) + return + end + + local selected = cinfo.selected + local word = completed.word + client:request( + vim.lsp.protocol.Methods.completionItem_resolve, + lsp_item, + function(err, result) + if err or not result then + return + end + + local cur = vim.fn.complete_info({ + "selected", + "completed", + "preview_winid", + }) + if + cur.selected ~= selected + or (vim.tbl_get(cur, "completed", "word") or "") + ~= word + then + return + end + + local signature = vim.tbl_get( + result, + "labelDetails", + "description" + ) or result.detail or "" + local doc = result.documentation + if type(doc) == "table" then + doc = doc.value + end + doc = doc or "" + + local ft = vim.bo[ev.buf].filetype + local code_parts = {} + if result.additionalTextEdits then + for _, edit in ipairs(result.additionalTextEdits) do + local text = (edit.newText or ""):gsub("%s+$", "") + if text ~= "" then + table.insert(code_parts, fence(ft, text)) + end + end + end + if signature ~= "" then + table.insert(code_parts, fence(ft, signature)) + end + local sections = {} + if #code_parts > 0 then + table.insert(sections, table.concat(code_parts, "\n\n")) + end + if doc ~= "" then + table.insert(sections, doc) + end + if #sections == 0 then + if + cur.preview_winid + and cur.preview_winid > 0 + and vim.api.nvim_win_is_valid(cur.preview_winid) + then + pcall( + vim.api.nvim_win_close, + cur.preview_winid, + true + ) + end + return + end + + local max_w = 0 + for _, s in ipairs(sections) do + for _, line in + ipairs(vim.split(s, "\n", { plain = true })) + do + max_w = + math.max(max_w, vim.fn.strdisplaywidth(line)) + end + end + local sep = "\n" + .. string.rep("─", math.min(max_w, 80)) + .. "\n" + local combined = table.concat(sections, sep) + + local windata = vim.api.nvim__complete_set(selected, { + info = combined, + }) + if windata then + style_popup(windata.winid, windata.bufnr, max_w) + end + end, + ev.buf + ) + end, + }) +end + +return M diff --git a/lua/lsp/diagnostic.lua b/lua/lsp/diagnostic.lua new file mode 100644 index 0000000..dc7d481 --- /dev/null +++ b/lua/lsp/diagnostic.lua @@ -0,0 +1,35 @@ +local M = {} + +M.signs = { + text = { + [vim.diagnostic.severity.ERROR] = "E", + [vim.diagnostic.severity.WARN] = "W", + [vim.diagnostic.severity.INFO] = "I", + [vim.diagnostic.severity.HINT] = "H", + }, +} + +function M.setup() + vim.diagnostic.config({ + underline = true, + signs = M.signs, + virtual_text = false, + float = { + show_header = false, + source = true, + border = "rounded", + focusable = true, + format = function(diagnostic) + return diagnostic.message + end, + width = 80, + }, + update_in_insert = false, + severity_sort = true, + jump = { + wrap = false, + }, + }) +end + +return M diff --git a/lua/lsp/init.lua b/lua/lsp/init.lua new file mode 100644 index 0000000..22444e5 --- /dev/null +++ b/lua/lsp/init.lua @@ -0,0 +1,109 @@ +local completion = require("lsp.completion") +local diagnostic = require("lsp.diagnostic") +local log = require("log") + +local GROUP = vim.api.nvim_create_augroup("ow.lsp", { clear = true }) + +local M = {} + +--- Load a JSON file and return a parsed table merged with settings +---@param path string +---@param settings? table +---@return table? +local function with_file(path, settings) + local file = io.open(path, "r") + if not file then + return + end + + local json = file:read("*all") + file:close() + local ok, resp = pcall( + vim.json.decode, + json, + { luanil = { object = true, array = true } } + ) + if not ok then + log.warning("Failed to parse json file %s: %s", path, resp) + return + end + + return vim.tbl_deep_extend("force", settings or {}, resp) +end + +local function on_attach(client, buf) + client.settings = with_file( + string.format(".%s.json", client.name), + client.settings + ) or client.settings + + completion.on_attach(client, buf) + + vim.api.nvim_clear_autocmds({ group = GROUP, buffer = buf }) + vim.api.nvim_create_autocmd("LspProgress", { + buffer = buf, + group = GROUP, + callback = function(ev) + local value = ev.data.params.value + vim.api.nvim_echo({ { value.message or "done" } }, false, { + id = "lsp." .. ev.data.params.token, + kind = "progress", + source = "vim.lsp", + title = value.title, + status = value.kind ~= "end" and "running" or "success", + percent = value.percentage, + }) + end, + }) +end + +function M.setup() + diagnostic.setup() + + vim.lsp.enable({ + "bashls", + "clangd", + "cmake", + "gopls", + -- "hyprls", + "intelephense", + -- "jedi_language_server", + "lemminx", + -- "xml_ls", + "lua_ls", + "mesonlsp", + "oxfmt", + "oxlint", + -- "phpactor", + -- "pyrefly", + "pyright", + "ruff", + "rust_analyzer", + "svelte", + "tailwindcss", + "tsgo", + "zls", + }) + + local capabilities = vim.lsp.protocol.make_client_capabilities() + completion.apply_capabilities(capabilities) + vim.lsp.config("*", { + capabilities = capabilities, + }) + + vim.api.nvim_create_autocmd("LspAttach", { + group = GROUP, + callback = function(ev) + local client = vim.lsp.get_client_by_id(ev.data.client_id) + if client then + on_attach(client, ev.buf) + end + end, + }) + + completion.setup() + + vim.lsp.log.set_level(vim.log.levels.WARN) +end + +return M diff --git a/lua/plugins/nvim-tree.lua b/lua/plugins/nvim-tree.lua index c803ff2..33e3c4f 100644 --- a/lua/plugins/nvim-tree.lua +++ b/lua/plugins/nvim-tree.lua @@ -67,7 +67,7 @@ function GitIgnoreDecorator:highlight_group(node) end end -local signs = require("lsp").diagnostic_signs +local signs = require("lsp.diagnostic").signs require("nvim-tree").setup({ on_attach = function(bufnr) local function opts(desc)