Skip to content

Commit

Permalink
fix: signatureHelp Implementation error (#49)
Browse files Browse the repository at this point in the history
* fix: signatureHelp error on basedpyright

* little fix

* little fix
  • Loading branch information
jinzhongjia authored Jan 9, 2025
1 parent a783d90 commit 09b7e17
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 14 deletions.
16 changes: 16 additions & 0 deletions lua/LspUI/lib/util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
50 changes: 36 additions & 14 deletions lua/LspUI/signature/util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -18,27 +18,41 @@ 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
if #help.signatures == 0 then
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 = {}

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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -127,7 +147,7 @@ M.request = function(buffer_id, callback)
)
return
end
callback(result)
callback(result, client.name)
end,
buffer_id
)
Expand Down Expand Up @@ -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"]
Expand All @@ -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

Expand All @@ -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
Expand Down Expand Up @@ -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

0 comments on commit 09b7e17

Please sign in to comment.