From 09b7e177ca40f089ef377fded95b800b3bdb512d Mon Sep 17 00:00:00 2001 From: jinzhongjia Date: Thu, 9 Jan 2025 10:46:59 +0800 Subject: [PATCH] fix: signatureHelp Implementation error (#49) * fix: signatureHelp error on basedpyright * little fix * little fix --- lua/LspUI/lib/util.lua | 16 ++++++++++++ lua/LspUI/signature/util.lua | 50 ++++++++++++++++++++++++++---------- 2 files changed, 52 insertions(+), 14 deletions(-) diff --git a/lua/LspUI/lib/util.lua b/lua/LspUI/lib/util.lua index 46a426e..d25d7ec 100644 --- a/lua/LspUI/lib/util.lua +++ b/lua/LspUI/lib/util.lua @@ -241,4 +241,20 @@ function M.trim_empty_lines(lines) return vim.list_extend({}, lines, start, finish) end +-- 定义一个函数,将消息写入文件 +--- @param message any +---@param file_path string +function M.write_message_to_file(message, file_path) + local fd = uv.fs_open(file_path, "a", 438) -- 438 是文件权限,等同于 0666 + if not fd then + print("无法打开文件: " .. file_path) + return + end + + local stat = uv.fs_fstat(fd) + ---@diagnostic disable-next-line: need-check-nil + uv.fs_write(fd, message .. "\n", stat.size) + uv.fs_close(fd) +end + return M diff --git a/lua/LspUI/signature/util.lua b/lua/LspUI/signature/util.lua index 3392cfc..78efada 100644 --- a/lua/LspUI/signature/util.lua +++ b/lua/LspUI/signature/util.lua @@ -18,8 +18,9 @@ local is_there_virtual_text = false --- @field doc string? --- @param help lsp.SignatureHelp|nil +--- @param client_name string|nil --- @return signature_info? res len will not be zero -local build_signature_info = function(help) +local build_signature_info = function(help, client_name) if not help then return nil end @@ -27,11 +28,22 @@ local build_signature_info = function(help) return nil end - local active_signature = help.activeSignature and help.activeSignature + 1 - or 1 - local active_parameter = help.activeParameter and help.activeParameter + 1 - or 1 - + -- TODO: this is some debug info, maybe remove + -- lib_util.write_message_to_file( + -- vim.inspect(help), + -- "C:\\Users\\jin\\Downloads\\log.txt" + -- ) + + local active_signature, active_parameter + -- this logic is in order to handle certain lsp specification implementations that are not standard + -- if client_name == "basedpyright" then + -- active_signature = help.activeSignature and help.activeSignature + 1 + -- or 1 + -- active_parameter = help.activeParameter and help.activeParameter or 1 + -- else + active_signature = help.activeSignature and help.activeSignature + 1 or 1 + active_parameter = help.activeParameter and help.activeParameter + 1 or 1 + -- end --- @type signature_info ---@diagnostic disable-next-line: missing-fields local res = {} @@ -39,6 +51,8 @@ local build_signature_info = function(help) local signature = help.signatures[active_signature] if signature.activeParameter then active_parameter = signature.activeParameter + 1 + elseif active_parameter > #signature - 1 then + active_parameter = 1 end res.label = signature.label @@ -90,11 +104,11 @@ local signature_group local signature_namespace = api.nvim_create_namespace("LspUI_signature") ---- @type { data: lsp.SignatureHelp?, } +--- @type { data: lsp.SignatureHelp?,client_name:string|nil } local backup = {} --- @param buffer_id number buffer's id ---- @param callback fun(result: lsp.SignatureHelp|nil) callback function +--- @param callback fun(result: lsp.SignatureHelp|nil,client_name:string|nil) callback function M.request = function(buffer_id, callback) -- this buffer id maybe invalid if not api.nvim_buf_is_valid(buffer_id) then @@ -107,6 +121,12 @@ M.request = function(buffer_id, callback) end local params = lsp.util.make_position_params() + -- TODO: this is debug info should be removed + -- lib_util.write_message_to_file( + -- vim.inspect(params), + -- "C:\\Users\\jin\\Downloads\\log1.txt" + -- ) + -- NOTE: we just use one client to get the lsp signature local client = clients[1] -- for _, client in pairs(clients or {}) do @@ -127,7 +147,7 @@ M.request = function(buffer_id, callback) ) return end - callback(result) + callback(result, client.name) end, buffer_id ) @@ -156,8 +176,9 @@ local signature_handle = function() backup.data = nil return end - M.request(current_buffer, function(result) + M.request(current_buffer, function(result, client_name) backup.data = result + backup.client_name = client_name local mode_info = vim.api.nvim_get_mode() local mode = mode_info["mode"] @@ -175,7 +196,7 @@ local signature_handle = function() end local current_window = api.nvim_get_current_win() - M.render(result, current_buffer, current_window) + M.render(result, current_buffer, current_window, client_name) end) end @@ -197,8 +218,9 @@ end --- @param data lsp.SignatureHelp|nil --- @param buffer_id integer --- @param windows_id integer -M.render = function(data, buffer_id, windows_id) - local info = build_signature_info(data) +--- @param client_name string|nil +M.render = function(data, buffer_id, windows_id, client_name) + local info = build_signature_info(data, client_name) if not info then return end @@ -304,7 +326,7 @@ end --- @return signature_info? M.status_line = function() - return build_signature_info(backup.data) + return build_signature_info(backup.data, backup.client_name) end return M