Skip to content
This repository was archived by the owner on Oct 13, 2021. It is now read-only.

Commit b434ecd

Browse files
committed
Used stdlib signature_help function
1 parent 3de0996 commit b434ecd

1 file changed

Lines changed: 1 addition & 132 deletions

File tree

lua/completion/signature_help.lua

Lines changed: 1 addition & 132 deletions
Original file line numberDiff line numberDiff line change
@@ -7,123 +7,6 @@ local M = {}
77
----------------------
88
-- signature help --
99
----------------------
10-
local function ok_or_nil(status, ...)
11-
if not status then return end
12-
return ...
13-
end
14-
15-
local function npcall(fn, ...)
16-
return ok_or_nil(pcall(fn, ...))
17-
end
18-
19-
local function find_window_by_var(name, value)
20-
for _, win in ipairs(api.nvim_list_wins()) do
21-
if npcall(api.nvim_win_get_var, win, name) == value then
22-
return win
23-
end
24-
end
25-
end
26-
27-
local function focusable_float(unique_name, fn)
28-
if npcall(api.nvim_win_get_var, 0, unique_name) then
29-
return api.nvim_command("wincmd p")
30-
end
31-
local bufnr = api.nvim_get_current_buf()
32-
do
33-
local win = find_window_by_var(unique_name, bufnr)
34-
if win then
35-
api.nvim_win_close(win, true)
36-
end
37-
end
38-
local pbufnr, pwinnr = fn()
39-
if pbufnr then
40-
api.nvim_win_set_var(pwinnr, unique_name, bufnr)
41-
return pbufnr, pwinnr
42-
end
43-
end
44-
45-
function M.open_floating_preview(contents, filetype, opts)
46-
validate {
47-
contents = { contents, 't' };
48-
filetype = { filetype, 's', true };
49-
opts = { opts, 't', true };
50-
}
51-
opts = opts or {}
52-
53-
-- Trim empty lines from the end.
54-
contents = vim.lsp.util.trim_empty_lines(contents)
55-
56-
local width = opts.width
57-
local height = opts.height or #contents
58-
if not width then
59-
width = 0
60-
for i, line in ipairs(contents) do
61-
-- Clean up the input and add left pad.
62-
line = " "..line:gsub("\r", "")
63-
-- TODO(ashkan) use nvim_strdisplaywidth if/when that is introduced.
64-
local line_width = vim.fn.strdisplaywidth(line)
65-
width = math.max(line_width, width)
66-
contents[i] = line
67-
end
68-
-- Add right padding of 1 each.
69-
width = width + 1
70-
end
71-
72-
local floating_bufnr = api.nvim_create_buf(false, true)
73-
if filetype then
74-
api.nvim_buf_set_option(floating_bufnr, 'filetype', filetype)
75-
end
76-
local float_option = vim.lsp.util.make_floating_popup_options(width, height, opts)
77-
float_option.focusable = false
78-
local floating_winnr = api.nvim_open_win(floating_bufnr, false, float_option)
79-
if filetype == 'markdown' then
80-
api.nvim_win_set_option(floating_winnr, 'conceallevel', 2)
81-
end
82-
api.nvim_buf_set_lines(floating_bufnr, 0, -1, true, contents)
83-
api.nvim_buf_set_option(floating_bufnr, 'modifiable', false)
84-
api.nvim_command("autocmd CursorMovedI,BufHidden,TextChangedI,InsertLeave <buffer> ++once \
85-
lua pcall(vim.api.nvim_win_close, "..floating_winnr..", true)")
86-
return floating_bufnr, floating_winnr
87-
end
88-
89-
local focusable_preview = function(unique_name, fn)
90-
return focusable_float(unique_name, function()
91-
return M.open_floating_preview(fn())
92-
end)
93-
end
94-
95-
local signature_help_to_preview_contents = function(input)
96-
if not input.signatures then
97-
return
98-
end
99-
local contents = {}
100-
local active_signature = input.activeSignature or 0
101-
if active_signature >= #input.signatures then
102-
active_signature = 0
103-
end
104-
local signature = input.signatures[active_signature + 1]
105-
if not signature then
106-
return
107-
end
108-
vim.list_extend(contents, vim.split(signature.label, '\n', true))
109-
if signature.documentation then
110-
vim.lsp.util.convert_input_to_markdown_lines(signature.documentation, contents)
111-
end
112-
if input.parameters then
113-
local active_parameter = input.activeParameter or 0
114-
if active_parameter >= #input.parameters then
115-
active_parameter = 0
116-
end
117-
local parameter = signature.parameters and signature.parameters[active_parameter]
118-
if parameter then
119-
if parameter.documentation then
120-
vim.lsp.util.convert_input_to_markdown_lines(parameter.documentation, contents)
121-
end
122-
end
123-
end
124-
return contents
125-
end
126-
12710
M.autoOpenSignatureHelp = function()
12811
local bufnr = api.nvim_get_current_buf()
12912
local pos = api.nvim_win_get_cursor(0)
@@ -144,21 +27,7 @@ M.autoOpenSignatureHelp = function()
14427
end
14528

14629
if triggered then
147-
local params = vim.lsp.util.make_position_params()
148-
vim.lsp.buf_request(bufnr, 'textDocument/signatureHelp', params, function(_, method, result)
149-
if not (result and result.signatures and result.signatures[1]) then
150-
return
151-
else
152-
focusable_preview(method, function()
153-
local lines = signature_help_to_preview_contents(result)
154-
lines = vim.lsp.util.trim_empty_lines(lines)
155-
if vim.tbl_isempty(lines) then
156-
return { 'No signature available' }
157-
end
158-
return lines, vim.lsp.util.try_trim_markdown_code_blocks(lines)
159-
end)
160-
end
161-
end)
30+
vim.lsp.buf.signature_help()
16231
end
16332
end
16433

0 commit comments

Comments
 (0)