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

Commit 4c239c9

Browse files
committed
fix: avoid overwrite user defined hover function(#63)
1 parent 8caf29d commit 4c239c9

3 files changed

Lines changed: 36 additions & 37 deletions

File tree

after/plugin/completion.vim

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
lua require'completion.hover'.modifyCallback()

lua/completion.lua

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,6 @@ M.customize_buf_label = function(label)
166166
end
167167

168168
M.on_attach = function()
169-
hover.modifyCallback()
170169
api.nvim_command [[augroup CompletionCommand]]
171170
api.nvim_command("autocmd! * <buffer>")
172171
api.nvim_command("autocmd InsertEnter <buffer> lua require'completion'.on_InsertEnter()")

lua/completion/hover.lua

Lines changed: 35 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -245,48 +245,47 @@ end
245245
-- Modify hover callback
246246
function M.modifyCallback()
247247
local callback = 'textDocument/hover'
248+
local default_callback = vim.lsp.callbacks[callback]
248249
vim.lsp.callbacks[callback] = function(_, method, result)
249250
-- if M.winnr ~= nil and api.nvim_win_is_valid(M.winnr) then
250251
-- api.nvim_win_close(M.winnr, true)
251252
-- end
252-
M.focusable_float(method, function()
253-
if not (result and result.contents) then
254-
-- return { 'No information available' }
255-
return
256-
end
257-
local markdown_lines = vim.lsp.util.convert_input_to_markdown_lines(result.contents)
258-
markdown_lines = vim.lsp.util.trim_empty_lines(markdown_lines)
259-
if vim.tbl_isempty(markdown_lines) then
260-
-- return { 'No information available' }
261-
return
262-
end
263-
local bufnr, winnr
264-
-- modified to open hover window align to popupmenu
265-
if vim.fn.pumvisible() == 1 then
266-
local position = vim.fn.pum_getpos()
267-
-- Set max width option to avoid overlapping with popup menu
268-
local total_column = api.nvim_get_option('columns')
269-
local align
270-
if position['col'] < total_column/2 then
271-
align = 'right'
272-
else
273-
align = 'left'
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
274258
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
275267

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-
else
283-
bufnr, winnr = vim.lsp.util.fancy_floating_markdown(markdown_lines, {
284-
pad_left = 1; pad_right = 1;
285-
})
286-
end
287-
vim.lsp.util.close_preview_autocmd({"CursorMoved", "BufHidden", "InsertCharPre"}, winnr)
288-
return bufnr, winnr
289-
end)
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
283+
vim.lsp.util.close_preview_autocmd({"CursorMoved", "BufHidden", "InsertCharPre"}, winnr)
284+
return bufnr, winnr
285+
end)
286+
else
287+
default_callback(_, method, result, _)
288+
end
290289
end
291290
end
292291

0 commit comments

Comments
 (0)