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/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 + 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