nvim/lua/util/open-docs-in-split.lua

45 lines
1.3 KiB
Lua
Raw Normal View History

2025-02-16 20:11:07 -08:00
function open_doc_in_vsplit()
local word = vim.fn.expand("<cword>")
if word == "" then
vim.notify("No word under cursor", vim.log.levels.INFO)
return
end
-- Try to get LSP hover documentation
local params = vim.lsp.util.make_position_params()
local results = vim.lsp.buf_request_sync(0, "textDocument/hover", params, 1000) or {}
local doc_lines = nil
for _, res in pairs(results) do
local contents = res.result and res.result.contents
if contents then
doc_lines = vim.lsp.util.convert_input_to_markdown_lines(contents)
doc_lines = vim.lsp.util.trim_empty_lines(doc_lines)
if #doc_lines > 0 then
break
end
end
end
if doc_lines and #doc_lines > 0 then
-- Open a new vertical split for LSP hover documentation
vim.cmd("vnew")
local bufnr = vim.api.nvim_get_current_buf()
-- Set the buffer to unmodifiable scratch buffer
vim.api.nvim_buf_set_lines(bufnr, 0, -1, false, doc_lines)
vim.bo[bufnr].modifiable = false
vim.bo[bufnr].bufhidden = "wipe"
vim.bo[bufnr].filetype = "markdown"
return
end
-- Fallback to vim help command
local help_cmd = "vertical help " .. word
local ok, _ = pcall(vim.cmd, help_cmd)
if not ok then
vim.notify("No documentation available for '" .. word .. "'", vim.log.levels.INFO)
end
end
return open_doc_in_vsplit