mirror of
https://github.com/tiyn/nvim-docs-view.git
synced 2025-11-13 05:49:45 +01:00
fix: define update function before usage, fixes #7
This commit is contained in:
@@ -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",
|
||||
|
||||
Reference in New Issue
Block a user