Skip to content

Commit

Permalink
[feat]: LSP Signature help support, but default is disabled
Browse files Browse the repository at this point in the history
  • Loading branch information
jinzhongjia committed Jan 31, 2024
1 parent 687814f commit 88be236
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 18 deletions.
2 changes: 2 additions & 0 deletions lua/LspUI/_meta.lua
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@
--- @class LspUI_signature
--- @field enable boolean? whether enable `signature` module
--- @field command_enable boolean? whether enable command for `signature`
--- @field icon string? the icon for float signature
--- @field color {fg: string?, bg: string?}? the color for signature
--- @field debounce (integer|boolean)? whether enable debounce for signature ? defalt is 250 milliseconds, this will reduce calculations when you move the cursor frequently, but it will cause the delay of signature, false will diable it

--- @class LspUI_config config for LspUI
Expand Down
11 changes: 7 additions & 4 deletions lua/LspUI/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ local default_pos_config = {
transparency = default_transparency,
}

-- TODO: now, this is noe avaiable
-- TODO: now, this is not avaiable
--
--- @type LspUI_call_hierarchy_config
local default_call_hierarchy_config = {
Expand All @@ -149,10 +149,13 @@ local default_call_hierarchy_config = {

--- @type LspUI_signature
local default_signature_config = {
--- NOTE: this is disabled!
--- TODO: this should be false
enable = true,
enable = false,
command_enable = true,
icon = "",
color = {
fg = "#FF8C00",
bg = nil,
},
debounce = 300,
}

Expand Down
6 changes: 3 additions & 3 deletions lua/LspUI/pos_abstract.lua
Original file line number Diff line number Diff line change
Expand Up @@ -432,10 +432,10 @@ local secondary_view_autocmd = function()
buffer = M.secondary_view_buffer(),
callback = function()
-- get current cursor position
local cursor_position =
api.nvim_win_get_cursor(M.secondary_view_window())

local lnum = cursor_position[1]
--- @type integer
---@diagnostic disable-next-line: assign-type-mismatch
local lnum = fn.line(".")

local uri, range = get_lsp_position_by_lnum(lnum)
if not uri then
Expand Down
13 changes: 12 additions & 1 deletion lua/LspUI/signature/init.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
local api, fn = vim.api, vim.fn
local command = require("LspUI.command")
local config = require("LspUI.config")
local lib_notify = require("LspUI.lib.notify")
local util = require("LspUI.signature.util")
Expand All @@ -19,6 +18,18 @@ M.init = function()

is_initialized = true

local hl_val = {
fg = config.options.signature.color.fg,
italic = true,
-- standout = true,
undercurl = true,
}

if config.options.signature.color.bg then
hl_val.fg = config.options.signature.color.bg
end
api.nvim_set_hl(0, "LspUI_Signature", hl_val)

-- init autocmd
util.autocmd()
end
Expand Down
103 changes: 93 additions & 10 deletions lua/LspUI/signature/util.lua
Original file line number Diff line number Diff line change
@@ -1,21 +1,24 @@
local api, lsp = vim.api, vim.lsp
local api, lsp, fn = vim.api, vim.lsp, vim.fn
local signature_feature = lsp.protocol.Methods.textDocument_signatureHelp

local config = require("LspUI.config")
local lib_debug = require("LspUI.lib.debug")
local lib_notify = require("LspUI.lib.notify")
local lib_util = require("LspUI.lib.util")

local M = {}

-- this variable records whether there is a virtual_text
--- @type boolean
local is_there_virtual_text = false

--- @class signature_info
--- @field label string
--- @field hint integer?
--- @field parameters {label: string, doc: (string|lsp.MarkupContent)?}[]?
--- @field doc string?

--- @param help lsp.SignatureHelp
--- @return signature_info?
--- @param help lsp.SignatureHelp|nil
--- @return signature_info? res len will not be zero
local build_signature_info = function(help)
if not help then
return nil
Expand All @@ -34,6 +37,9 @@ local build_signature_info = function(help)
local res = {}

local signature = help.signatures[active_signature]
if signature.activeParameter then
active_parameter = signature.activeParameter + 1
end

res.label = signature.label
---@diagnostic disable-next-line: assign-type-mismatch
Expand Down Expand Up @@ -79,8 +85,10 @@ end
--- @type {[number]: boolean}
local buffer_list = {}

local signature_group =
api.nvim_create_augroup("Lspui_signature", { clear = true })
--- @type integer
local signature_group

local signature_namespace = api.nvim_create_namespace("LspUI_signature")

--- @type { data: lsp.SignatureHelp?, }
local backup = {}
Expand Down Expand Up @@ -147,7 +155,24 @@ local signature_handle = function()
end
M.request(current_buffer, function(result)
backup.data = result
-- TODO: add render to here

local mode_info = vim.api.nvim_get_mode()
local mode = mode_info["mode"]
local is_insert = mode:find("i") ~= nil or mode:find("ic") ~= nil
if not is_insert then
return
end

M.clean_render(current_buffer)

local callback_current_buffer = api.nvim_get_current_buf()
-- when call current buffer is not equal to current buffer, return
if callback_current_buffer ~= current_buffer then
return
end

local current_window = api.nvim_get_current_win()
M.render(result, current_buffer, current_window)
end)
end

Expand All @@ -166,12 +191,58 @@ local build_func = function()
func = lib_util.debounce(signature_handle, time)
end

M.render = function() 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)
if not info then
return
end

if not info.hint then
return
end

--- @type integer
---@diagnostic disable-next-line: assign-type-mismatch
local row = fn.line(".") == 1 and 1 or fn.line(".") - 2
--- @type integer
local col = fn.virtcol(".") - 1

api.nvim_buf_set_extmark(buffer_id, signature_namespace, row, 0, {
virt_text = {
{
string.format(
"%s %s",
config.options.signature.icon,
info.parameters[info.hint].label
),
"LspUI_Signature",
},
},
virt_text_win_col = col,
hl_mode = "blend",
})
is_there_virtual_text = true
end

-- clean signature virtual text
--- @param buffer_id integer
M.clean_render = function(buffer_id)
if not is_there_virtual_text then
return
end

M.clean_render = function() end
api.nvim_buf_clear_namespace(buffer_id, signature_namespace, 0, -1)
is_there_virtual_text = false
end

-- this is autocmd init for signature
M.autocmd = function()
signature_group =
api.nvim_create_augroup("Lspui_signature", { clear = true })

-- build debounce function
build_func()

Expand All @@ -193,7 +264,7 @@ M.autocmd = function()
})

-- maybe this can also use CurosrHold
api.nvim_create_autocmd({ "CursorMovedI", "CursorMoved" }, {
api.nvim_create_autocmd({ "CursorMovedI", "InsertEnter" }, {
group = signature_group,
callback = vim.schedule_wrap(func),
desc = lib_util.command_desc("Signature update when CursorHoldI"),
Expand All @@ -204,12 +275,24 @@ M.autocmd = function()
group = signature_group,
callback = function()
local current_buffer = api.nvim_get_current_buf()
M.clean_render(current_buffer)
if buffer_list[current_buffer] then
buffer_list[current_buffer] = false
end
end,
desc = lib_util.command_desc("Exec signature clean cmd when QuitPre"),
})

api.nvim_create_autocmd({ "InsertLeave", "WinLeave" }, {
group = signature_group,
callback = function()
local current_buffer = api.nvim_get_current_buf()
M.clean_render(current_buffer)
end,
desc = lib_util.command_desc(
"Exec signature virtual text clean cmd when InsertLeave or WinLeave"
),
})
end

M.deautocmd = function()
Expand Down

0 comments on commit 88be236

Please sign in to comment.