Skip to content

Commit

Permalink
refactor: misc cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Saghen committed Dec 18, 2024
1 parent b3215b6 commit 46eb5f3
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 45 deletions.
50 changes: 6 additions & 44 deletions lua/blink/cmp/lib/window/docs.lua
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,6 @@ function docs.render_detail_and_documentation(bufnr, detail, documentation, max_
if documentation ~= nil then
local doc = type(documentation) == 'string' and documentation or documentation.value
doc_lines = docs.split_lines(doc)
-- if type(documentation) ~= 'string' and documentation.kind == 'markdown' then
-- -- if the rendering seems bugged, it's likely due to this function
-- doc_lines = docs.combine_markdown_lines(doc_lines)
-- end
end

detail_lines, doc_lines = docs.extract_detail_from_doc(detail_lines, doc_lines)
Expand All @@ -27,7 +23,7 @@ function docs.render_detail_and_documentation(bufnr, detail, documentation, max_

-- add a blank line for the --- separator
local doc_already_has_separator = #doc_lines > 1 and (doc_lines[1] == '---' or doc_lines[1] == '***')
if #detail_lines > 0 and #doc_lines > 0 then table.insert(combined_lines, '') end
if #detail_lines > 0 and #doc_lines > 0 then table.insert(combined_lines, '') end
-- skip original separator in doc_lines, so we can highlight it later
vim.list_extend(combined_lines, doc_lines, doc_already_has_separator and 2 or 1)

Expand All @@ -37,7 +33,9 @@ function docs.render_detail_and_documentation(bufnr, detail, documentation, max_
-- Highlight with treesitter
vim.api.nvim_buf_clear_namespace(bufnr, highlight_ns, 0, -1)

if #detail_lines > 0 and use_treesitter_highlighting then docs.highlight_with_treesitter(bufnr, vim.bo.filetype, 0, #detail_lines) end
if #detail_lines > 0 and use_treesitter_highlighting then
docs.highlight_with_treesitter(bufnr, vim.bo.filetype, 0, #detail_lines)
end

-- Only add the separator if there are documentation lines (otherwise only display the detail)
if #detail_lines > 0 and #doc_lines > 0 then
Expand Down Expand Up @@ -119,40 +117,6 @@ function docs.highlight_with_treesitter(bufnr, filetype, start_line, end_line)
end)
end

--- Combines adjacent paragraph lines together
--- @param lines string[]
--- @return string[]
--- TODO: Likely buggy
function docs.combine_markdown_lines(lines)
local combined_lines = {}

local special_starting_chars = { '#', '>', '-', '|', '*', '' }
local in_code_block = false
local prev_is_special = false
for _, line in ipairs(lines) do
if line:match('^%s*```') then in_code_block = not in_code_block end
-- skip separators
if line:match('^[%s\\-]+$') then goto continue end

local is_special = line:match('^%s*[' .. table.concat(special_starting_chars) .. ']') or line:match('^%s*%d\\.$')
local is_empty = line:match('^%s*$')
local has_linebreak = line:match('%s%s$')

if #combined_lines == 0 or in_code_block or is_special or prev_is_special or is_empty or has_linebreak then
table.insert(combined_lines, line)
elseif line:match('^%s*$') then
if combined_lines[#combined_lines] ~= '' then table.insert(combined_lines, '') end
else
combined_lines[#combined_lines] = combined_lines[#combined_lines] .. '' .. line
end

prev_is_special = is_special
::continue::
end

return combined_lines
end

--- Gets the start and end row of the code block for the given row
--- Or returns nil if there's no code block
--- @param lines string[]
Expand Down Expand Up @@ -202,12 +166,10 @@ function docs.extract_detail_from_doc(detail_lines, doc_lines)
local doc_str_detail_row = doc_str:find(detail_str, 1, true)

-- didn't find the detail in the doc, so return as is
if doc_str_detail_row == nil or #detail_str == 0 or #doc_str == 0 then
return detail_lines, doc_lines
end
if doc_str_detail_row == nil or #detail_str == 0 or #doc_str == 0 then return detail_lines, doc_lines end

-- get the line of the match
-- hack: surely there's a better way to do this but it's late
-- hack: surely there's a better way to do this but it's late
-- and I can't be bothered
local offset = 1
local detail_line = 1
Expand Down
2 changes: 1 addition & 1 deletion lua/blink/cmp/sources/luasnip.lua
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ function source:execute(_, item)
local luasnip = require('luasnip')
local snip = luasnip.get_id_snippet(item.data.snip_id)

-- if trigger is a pattern, expand "pattern" instead of actual snippet.
-- if trigger is a pattern, expand "pattern" instead of actual snippet
if snip.regTrig then snip = snip:get_pattern_expand_helper() end

-- get (0, 0) indexed cursor position
Expand Down

0 comments on commit 46eb5f3

Please sign in to comment.