From e7d7b9f8492eec3b1f78f9b378c9f8101b7afca2 Mon Sep 17 00:00:00 2001 From: "Niexin.Liu" <383655168@qq.com> Date: Tue, 11 Nov 2025 09:28:41 +0800 Subject: [PATCH 1/2] refactor(lsp-util): Update LSP client and jump utility calls MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Problem Origin: LSP client methods used inconsistent calling syntax internally, potentially leading to inaccurate `self` passing. The jump functionality utilized `vim.lsp.util.jump_to_location`, which is not the latest or recommended interface for the Neovim LSP API. Solution: Standardize all LSP client method calls in `litee.lib.lsp` to Lua's colon syntax (`client:method(...)`) to ensure correct `self` propagation. In `litee.lib.jumps`, replace `vim.lsp.util.jump_to_location` with `vim.lsp.util.show_document` to adopt Neovim LSP's more modern and flexible document display interface. Impact: • For users: No apparent direct functional behavior changes. • For developers: Improves internal code consistency and maintainability, adhering to Neovim LSP API best practices. • Potential risks: Limited risk, mainly internal API call adjustments; should have no functional regression after testing. Key Changes: • Replaced `jump_to_location` with `show_document` in the `jump_invoking` function in `lua/litee/lib/jumps/init.lua`. • Changed `client.supports_method` and `client.request` calls to colon syntax in `M.multi_client_request` in `lua/litee/lib/lsp/init.lua`. • Changed `client.supports_method` and `client.request_sync` calls to colon syntax in `M.symbol_from_node` in `lua/litee/lib/lsp/init.lua`. --- lua/litee/lib/jumps/init.lua | 2 +- lua/litee/lib/lsp/init.lua | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/lua/litee/lib/jumps/init.lua b/lua/litee/lib/jumps/init.lua index 1474e43..71b00c2 100644 --- a/lua/litee/lib/jumps/init.lua +++ b/lua/litee/lib/jumps/init.lua @@ -147,7 +147,7 @@ function M.jump_invoking(location, win, node, offset_encoding) -- if the panel currently has a component "popped-out" -- close it before jumping. lib_panel.close_current_popout() - vim.lsp.util.jump_to_location(location, offset_encoding or "utf-8") + vim.lsp.util.show_document(location, offset_encoding or "utf-8") M.set_jump_hl(true, node) -- cleanup any [No Name] buffers if they exist diff --git a/lua/litee/lib/lsp/init.lua b/lua/litee/lib/lsp/init.lua index 4c29b12..6de000d 100644 --- a/lua/litee/lib/lsp/init.lua +++ b/lua/litee/lib/lsp/init.lua @@ -9,8 +9,8 @@ local M = {} -- but takes a list of clients as the first argument. function M.multi_client_request(clients, method, params, handler, bufnr) for _, client in ipairs(clients) do - if client.supports_method(method) then - client.request(method, params, handler, bufnr or 0) + if client:supports_method(method) then + client:request(method, params, handler, bufnr or 0) return client end end @@ -102,12 +102,12 @@ function M.symbol_from_node(clients, node, bufnr) query = node.name, } for _, client in ipairs(clients) do - if not client.supports_method("workspace/symbol") then + if not client:supports_method("workspace/symbol") then goto continue end -- not all LSPs are optimized, specially ones in early development, set -- this timeout high. - local out = client.request_sync("workspace/symbol", params, 5000, bufnr) + local out = client:request_sync("workspace/symbol", params, 5000, bufnr) if out == nil then goto continue end From 85b99187413359f9ec7ee1fa71e99004f4aea82a Mon Sep 17 00:00:00 2001 From: "Niexin.Liu" <383655168@qq.com> Date: Mon, 24 Nov 2025 17:19:23 +0800 Subject: [PATCH 2/2] refactor(lsp/hover): Optimize LSP hover popup content handling MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Problem Root Cause: The current LSP hover popup content handling logic relies on deprecated Neovim internal APIs `vim.lsp.util.trim_empty_lines` and `vim.lsp.util.stylize_markdown`. These deprecated APIs may lead to future compatibility issues. Solution: Introduce a local `trim_empty_lines` function to replace `vim.lsp.util.trim_empty_lines`. Concurrently, remove the call to `vim.lsp.util.stylize_markdown` and instead use Neovim's native markdown filetype and Treesitter highlighting to render the hover content. Impact: • For Users: The display behavior and content of the LSP hover popup are expected to remain largely unchanged, maintaining functional consistency. • For Developers: Reduces reliance on deprecated Neovim internal APIs, lowering future code maintenance risks and enhancing robustness. • Potential Risks: Removing `stylize_markdown` might lead to subtle differences in specific markdown style highlighting, but given its deprecated status, this risk is acceptable. Key Changes: • Added a local `trim_empty_lines` function to remove leading and trailing empty lines from text. • Used the custom `trim_empty_lines` in `hover_handler` instead of `vim.lsp.util.trim_empty_lines`. • Removed the `vim.lsp.util.stylize_markdown` call, simplifying the highlighting logic. • Explicitly set the `markdown` filetype for the LSP float window buffer and attempted to start `treesitter` highlighting. --- lua/litee/lib/lsp/hover.lua | 78 ++++++++++++++++++++++++++----------- 1 file changed, 55 insertions(+), 23 deletions(-) diff --git a/lua/litee/lib/lsp/hover.lua b/lua/litee/lib/lsp/hover.lua index 007b620..833a8d2 100644 --- a/lua/litee/lib/lsp/hover.lua +++ b/lua/litee/lib/lsp/hover.lua @@ -2,11 +2,32 @@ local M = {} local float_win = nil +-- 本地实现:去掉头尾的空行,替代 vim.lsp.util.trim_empty_lines() +local function trim_empty_lines(lines) + local start_idx = 1 + local end_idx = #lines + + -- 去掉开头空行 + while start_idx <= end_idx and lines[start_idx]:match("^%s*$") do + start_idx = start_idx + 1 + end + + -- 去掉结尾空行 + while end_idx >= start_idx and lines[end_idx]:match("^%s*$") do + end_idx = end_idx - 1 + end + + local new = {} + for i = start_idx, end_idx do + new[#new + 1] = lines[i] + end + return new +end + -- close_hover_popups closes the created popup window -- if it exists. function M.close_hover_popup() - if float_win ~= nil and - vim.api.nvim_win_is_valid(float_win) then + if float_win ~= nil and vim.api.nvim_win_is_valid(float_win) then vim.api.nvim_win_close(float_win, true) float_win = nil end @@ -19,18 +40,22 @@ end -- function conforms to client LSP handler signature. function M.hover_handler(_, result, ctx, config) M.close_hover_popup() - -- get lines from result + config = config or {} config.focus_id = ctx.method + if not (result and result.contents) then - -- return { 'No information available' } - return + -- return { 'No information available' } + return end + + -- 从 LSP 结果生成 markdown 行 local lines = vim.lsp.util.convert_input_to_markdown_lines(result.contents) - lines = vim.lsp.util.trim_empty_lines(lines) + lines = trim_empty_lines(lines) -- 使用我们自己的实现 + if vim.tbl_isempty(lines) then - -- return { 'No information available' } - return + -- return { 'No information available' } + return end -- create buffer for popup @@ -39,12 +64,17 @@ function M.hover_handler(_, result, ctx, config) vim.api.nvim_err_writeln("details_popup: could not create details buffer") return end - vim.api.nvim_buf_set_option(buf, 'bufhidden', 'delete') - vim.api.nvim_buf_set_option(buf, 'syntax', 'markdown') - vim.api.nvim_buf_set_option(buf, 'filetype', 'markdown') - lines = vim.lsp.util.stylize_markdown(buf, lines, {}) + vim.api.nvim_buf_set_option(buf, "bufhidden", "delete") + vim.api.nvim_buf_set_option(buf, "syntax", "markdown") + vim.api.nvim_buf_set_option(buf, "filetype", "markdown") + + -- 之前这里调用的是 vim.lsp.util.stylize_markdown(buf, lines, {}) + -- 该 API 已弃用,我们改成简单方案: + -- 只用 markdown filetype + treesitter 高亮(如果有安装 markdown parser) + pcall(vim.treesitter.start, buf, "markdown") + -- 计算浮窗宽度 local width = 20 for _, line in ipairs(lines) do local line_width = vim.fn.strdisplaywidth(line) @@ -53,22 +83,24 @@ function M.hover_handler(_, result, ctx, config) end end - vim.api.nvim_buf_set_option(buf, 'modifiable', true) + vim.api.nvim_buf_set_option(buf, "modifiable", true) vim.api.nvim_buf_set_lines(buf, 0, #lines, false, lines) - vim.api.nvim_buf_set_option(buf, 'modifiable', false) + vim.api.nvim_buf_set_option(buf, "modifiable", false) + local popup_conf = vim.lsp.util.make_floating_popup_options( - width, - #lines, - { - border= "rounded", - focusable= false, - zindex = 99, - } + width, + #lines, + { + border = "rounded", + focusable = false, + zindex = 99, + } ) + float_win = vim.api.nvim_open_win(buf, false, popup_conf) - return float_win + return float_win end - return M +