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

Commit 7e65695

Browse files
committed
fix: avoid overwriting custom callback by checking client object(#63)
1 parent 6b96613 commit 7e65695

File tree

2 files changed

+55
-51
lines changed

2 files changed

+55
-51
lines changed

after/plugin/completion.vim

Lines changed: 0 additions & 1 deletion
This file was deleted.

lua/completion/hover.lua

Lines changed: 55 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -242,62 +242,67 @@ local fancy_floating_markdown = function(contents, opts)
242242
return bufnr, winnr
243243
end
244244

245-
-- Modify hover callback
246-
function M.modifyCallback()
247-
local callback = 'textDocument/hover'
248-
local default_callback = vim.lsp.callbacks[callback]
249-
vim.lsp.callbacks[callback] = function(_, method, result)
250-
-- if M.winnr ~= nil and api.nvim_win_is_valid(M.winnr) then
251-
-- api.nvim_win_close(M.winnr, true)
252-
-- end
253-
if vim.fn.pumvisible() == 1 then
254-
M.focusable_float(method, function()
255-
if not (result and result.contents) then
256-
-- return { 'No information available' }
257-
return
258-
end
259-
local markdown_lines = vim.lsp.util.convert_input_to_markdown_lines(result.contents)
260-
markdown_lines = vim.lsp.util.trim_empty_lines(markdown_lines)
261-
if vim.tbl_isempty(markdown_lines) then
262-
-- return { 'No information available' }
263-
return
264-
end
265-
local bufnr, winnr
266-
-- modified to open hover window align to popupmenu
245+
local callback = 'textDocument/hover'
246+
local default_callback = vim.lsp.callbacks[callback]
267247

268-
local position = vim.fn.pum_getpos()
269-
-- Set max width option to avoid overlapping with popup menu
270-
local total_column = api.nvim_get_option('columns')
271-
local align
272-
if position['col'] < total_column/2 then
273-
align = 'right'
274-
else
275-
align = 'left'
276-
end
277-
bufnr, winnr = fancy_floating_markdown(markdown_lines, {
278-
pad_left = 1; pad_right = 1;
279-
col = position['col']; width = position['width']; row = position['row']-1;
280-
align = align
281-
})
282-
M.winnr = winnr
248+
function callback_function(_, method, result)
249+
-- if M.winnr ~= nil and api.nvim_win_is_valid(M.winnr) then
250+
-- api.nvim_win_close(M.winnr, true)
251+
-- end
252+
if vim.fn.pumvisible() == 1 then
253+
M.focusable_float(method, function()
254+
if not (result and result.contents) then
255+
-- return { 'No information available' }
256+
return
257+
end
258+
local markdown_lines = vim.lsp.util.convert_input_to_markdown_lines(result.contents)
259+
markdown_lines = vim.lsp.util.trim_empty_lines(markdown_lines)
260+
if vim.tbl_isempty(markdown_lines) then
261+
-- return { 'No information available' }
262+
return
263+
end
264+
local bufnr, winnr
265+
-- modified to open hover window align to popupmenu
283266

284-
vim.lsp.util.close_preview_autocmd({"CursorMoved", "BufHidden", "InsertCharPre"}, winnr)
285-
local hover_len = #vim.api.nvim_buf_get_lines(bufnr,0,-1,false)[1]
286-
local win_width = vim.api.nvim_win_get_width(0)
287-
if hover_len > win_width then
288-
vim.api.nvim_win_set_width(winnr,math.min(hover_len,win_width))
289-
vim.api.nvim_win_set_height(winnr,math.ceil(hover_len/win_width))
290-
vim.wo[winnr].wrap = true
291-
end
292-
return bufnr, winnr
293-
end)
294-
else
295-
default_callback(_, method, result, _)
296-
end
267+
local position = vim.fn.pum_getpos()
268+
-- Set max width option to avoid overlapping with popup menu
269+
local total_column = api.nvim_get_option('columns')
270+
local align
271+
if position['col'] < total_column/2 then
272+
align = 'right'
273+
else
274+
align = 'left'
275+
end
276+
bufnr, winnr = fancy_floating_markdown(markdown_lines, {
277+
pad_left = 1; pad_right = 1;
278+
col = position['col']; width = position['width']; row = position['row']-1;
279+
align = align
280+
})
281+
M.winnr = winnr
282+
283+
vim.lsp.util.close_preview_autocmd({"CursorMoved", "BufHidden", "InsertCharPre"}, winnr)
284+
local hover_len = #vim.api.nvim_buf_get_lines(bufnr,0,-1,false)[1]
285+
local win_width = vim.api.nvim_win_get_width(0)
286+
if hover_len > win_width then
287+
vim.api.nvim_win_set_width(winnr,math.min(hover_len,win_width))
288+
vim.api.nvim_win_set_height(winnr,math.ceil(hover_len/win_width))
289+
vim.wo[winnr].wrap = true
290+
end
291+
return bufnr, winnr
292+
end)
293+
else
294+
default_callback(_, method, result, _)
297295
end
298296
end
299297

300298
M.autoOpenHoverInPopup = function(manager)
299+
for _, client in pairs(vim.lsp.buf_get_clients(0)) do
300+
default_callback = client.config.callbacks['textDocument/hover'] or vim.lsp.callbacks['textDocument/hover']
301+
if default_callback ~= callback_function then
302+
client.config.callbacks['textDocument/hover'] = callback_function
303+
end
304+
end
305+
301306
local bufnr = api.nvim_get_current_buf()
302307
if api.nvim_call_function('pumvisible', {}) == 1 then
303308
-- Auto open hover

0 commit comments

Comments
 (0)