Skip to content

Commit

Permalink
feat: stylize documentation window
Browse files Browse the repository at this point in the history
  • Loading branch information
telebart committed Oct 9, 2024
1 parent 8632278 commit 2bfbc25
Showing 1 changed file with 33 additions and 9 deletions.
42 changes: 33 additions & 9 deletions lua/blink/cmp/windows/documentation.lua
Original file line number Diff line number Diff line change
Expand Up @@ -54,19 +54,43 @@ function docs.show_item(item)
return
end

local doc = type(item.documentation) == 'string' and item.documentation or item.documentation.value
local doc_lines = {}
for s in doc:gmatch('[^\r\n]+') do
table.insert(doc_lines, s)
if item.detail and item.detail ~= '' then
table.insert(doc_lines, {
kind = "markdown",
value = string.format("```%s\n%s\n```", vim.bo.filetype, item.detail),
})
end

if type(item.documentation) == 'table' then
table.insert(doc_lines, item.documentation)
elseif type(item.documentation) == "string" then
table.insert(doc_lines, {
kind = "plaintext",
value = item.documentation
})
end
vim.api.nvim_buf_set_lines(docs.win:get_buf(), 0, -1, true, doc_lines)
vim.api.nvim_set_option_value('modified', false, { buf = docs.win:get_buf() })

local filetype = item.documentation.kind == 'markdown' and 'markdown' or 'plaintext'
if filetype ~= vim.api.nvim_get_option_value('filetype', { buf = docs.win:get_buf() }) then
vim.api.nvim_set_option_value('filetype', filetype, { buf = docs.win:get_buf() })
local success = pcall(vim.lsp.util.stylize_markdown, docs.win:get_buf(), vim.lsp.util.convert_input_to_markdown_lines(doc_lines), {})
if not success then
-- fallback to simple formatting
for i, line in ipairs(doc_lines) do
if not line.value then
doc_lines[i] = ""
goto continue
end
if i == 1 then
doc_lines[i] = line.value:gsub("```"..vim.bo.filetype, "```"):gsub("[\r\n]", "")
else
doc_lines[i] = line.value:gsub("[\r\n]", "")
end
::continue::
end
vim.api.nvim_buf_set_lines(docs.win:get_buf(), 0, -1, false, doc_lines)
end

vim.api.nvim_set_option_value('modified', false, { buf = docs.win:get_buf() })

if autocomplete.win:get_win() then
docs.win:open()
docs.update_position()
Expand Down Expand Up @@ -110,7 +134,7 @@ function docs.update_position()

local autocomplete_win_is_up = autocomplete_win_config.row - cursor_screen_row < 0
local direction_priority = autocomplete_win_is_up and config.direction_priority.autocomplete_north
or config.direction_priority.autocomplete_south
or config.direction_priority.autocomplete_south

local height = docs.win:get_height()
local width = docs.win:get_width()
Expand Down

0 comments on commit 2bfbc25

Please sign in to comment.