fix: define update function before usage, fixes #7

This commit is contained in:
amrbashir
2023-02-13 16:30:20 +02:00
parent 601d7f6a2e
commit a7ba30f8d5

View File

@@ -3,6 +3,42 @@ local cfg = {}
local buf, win, prev_win, autocmd
local get_clients
local function update()
if not win or not vim.api.nvim_win_is_valid(win) then
toggle()
end
local clients = get_clients()
local gotHover = false
for i = 1, #clients do
if clients[i].supports_method("textDocument/hover") then
gotHover = true
break
end
end
if not gotHover then
return
end
local l, c = unpack(vim.api.nvim_win_get_cursor(0))
vim.lsp.buf_request(0, "textDocument/hover", {
textDocument = { uri = "file://" .. vim.api.nvim_buf_get_name(0) },
position = { line = l - 1, character = c },
}, function(err, result, ctx, config)
if win and vim.api.nvim_win_is_valid(win) and result and result.contents then
local md_lines = vim.lsp.util.convert_input_to_markdown_lines(result.contents)
md_lines = vim.lsp.util.trim_empty_lines(md_lines)
if vim.tbl_isempty(md_lines) then
return
end
vim.api.nvim_buf_set_option(buf, "modifiable", true)
vim.lsp.util.stylize_markdown(buf, md_lines)
vim.api.nvim_buf_set_option(buf, "modifiable", false)
end
end)
end
local function toggle()
if win and vim.api.nvim_win_is_valid(win) then
vim.api.nvim_win_close(win, false)
@@ -65,42 +101,6 @@ local function toggle()
end
end
local function update()
if not win or not vim.api.nvim_win_is_valid(win) then
toggle()
end
local clients = get_clients()
local gotHover = false
for i = 1, #clients do
if clients[i].supports_method("textDocument/hover") then
gotHover = true
break
end
end
if not gotHover then
return
end
local l, c = unpack(vim.api.nvim_win_get_cursor(0))
vim.lsp.buf_request(0, "textDocument/hover", {
textDocument = { uri = "file://" .. vim.api.nvim_buf_get_name(0) },
position = { line = l - 1, character = c },
}, function(err, result, ctx, config)
if win and vim.api.nvim_win_is_valid(win) and result and result.contents then
local md_lines = vim.lsp.util.convert_input_to_markdown_lines(result.contents)
md_lines = vim.lsp.util.trim_empty_lines(md_lines)
if vim.tbl_isempty(md_lines) then
return
end
vim.api.nvim_buf_set_option(buf, "modifiable", true)
vim.lsp.util.stylize_markdown(buf, md_lines)
vim.api.nvim_buf_set_option(buf, "modifiable", false)
end
end)
end
M.setup = function(user_cfg)
local default_cfg = {
position = "right",