|
| 1 | +local docs = {} |
| 2 | + |
| 3 | +--- @param bufnr number |
| 4 | +--- @param detail? string |
| 5 | +--- @param documentation? lsp.MarkupContent | string |
| 6 | +function docs.render_detail_and_documentation(bufnr, detail, documentation, max_width) |
| 7 | + local detail_lines = {} |
| 8 | + if detail and detail ~= '' then detail_lines = docs.split_lines(detail) end |
| 9 | + |
| 10 | + local doc_lines = {} |
| 11 | + if documentation ~= nil then |
| 12 | + local doc = type(documentation) == 'string' and documentation or documentation.value |
| 13 | + doc_lines = docs.split_lines(doc) |
| 14 | + if type(documentation) ~= 'string' and documentation.kind == 'markdown' then |
| 15 | + -- if the rendering seems bugged, it's likely due to this function |
| 16 | + doc_lines = docs.combine_markdown_lines(doc_lines) |
| 17 | + end |
| 18 | + end |
| 19 | + |
| 20 | + local combined_lines = vim.list_extend({}, detail_lines) |
| 21 | + -- add a blank line for the --- separator |
| 22 | + if #detail_lines > 0 and #doc_lines > 0 then table.insert(combined_lines, '') end |
| 23 | + vim.list_extend(combined_lines, doc_lines) |
| 24 | + |
| 25 | + vim.api.nvim_buf_set_lines(bufnr, 0, -1, true, combined_lines) |
| 26 | + vim.api.nvim_set_option_value('modified', false, { buf = bufnr }) |
| 27 | + |
| 28 | + -- Highlight with treesitter |
| 29 | + vim.api.nvim_buf_clear_namespace(bufnr, require('blink.cmp.config').highlight.ns, 0, -1) |
| 30 | + |
| 31 | + if #detail_lines > 0 then docs.highlight_with_treesitter(bufnr, vim.bo.filetype, 0, #detail_lines) end |
| 32 | + |
| 33 | + -- Only add the separator if there are documentation lines (otherwise only display the detail) |
| 34 | + if #detail_lines > 0 and #doc_lines > 0 then |
| 35 | + vim.api.nvim_buf_set_extmark(bufnr, require('blink.cmp.config').highlight.ns, #detail_lines, 0, { |
| 36 | + virt_text = { { string.rep('─', max_width) } }, |
| 37 | + virt_text_pos = 'overlay', |
| 38 | + hl_eol = true, |
| 39 | + hl_group = 'BlinkCmpDocDetail', |
| 40 | + }) |
| 41 | + end |
| 42 | + |
| 43 | + if #doc_lines > 0 then |
| 44 | + local start = #detail_lines + (#detail_lines > 0 and 1 or 0) |
| 45 | + docs.highlight_with_treesitter(bufnr, 'markdown', start, start + #doc_lines) |
| 46 | + end |
| 47 | +end |
| 48 | + |
| 49 | +--- Highlights the given range with treesitter with the given filetype |
| 50 | +--- @param bufnr number |
| 51 | +--- @param filetype string |
| 52 | +--- @param start_line number |
| 53 | +--- @param end_line number |
| 54 | +--- TODO: fallback to regex highlighting if treesitter fails |
| 55 | +function docs.highlight_with_treesitter(bufnr, filetype, start_line, end_line) |
| 56 | + local Range = require('vim.treesitter._range') |
| 57 | + |
| 58 | + local root_lang = vim.treesitter.language.get_lang(filetype) |
| 59 | + if root_lang == nil then return end |
| 60 | + |
| 61 | + local success, trees = pcall(vim.treesitter.get_parser, bufnr, root_lang) |
| 62 | + if not success or not trees then return end |
| 63 | + |
| 64 | + trees:parse({ start_line, end_line }) |
| 65 | + |
| 66 | + trees:for_each_tree(function(tree, tstree) |
| 67 | + local lang = tstree:lang() |
| 68 | + local highlighter_query = vim.treesitter.query.get(lang, 'highlights') |
| 69 | + if not highlighter_query then return end |
| 70 | + |
| 71 | + local root_node = tree:root() |
| 72 | + local _, _, root_end_row, _ = root_node:range() |
| 73 | + |
| 74 | + local iter = highlighter_query:iter_captures(tree:root(), bufnr, start_line, end_line) |
| 75 | + local line = start_line |
| 76 | + while line < end_line do |
| 77 | + local capture, node, metadata, _ = iter(line) |
| 78 | + if capture == nil then break end |
| 79 | + |
| 80 | + local range = { root_end_row + 1, 0, root_end_row + 1, 0 } |
| 81 | + if node then range = vim.treesitter.get_range(node, bufnr, metadata and metadata[capture]) end |
| 82 | + local start_row, start_col, end_row, end_col = Range.unpack4(range) |
| 83 | + |
| 84 | + if capture then |
| 85 | + local name = highlighter_query.captures[capture] |
| 86 | + local hl = 0 |
| 87 | + if not vim.startswith(name, '_') then hl = vim.api.nvim_get_hl_id_by_name('@' .. name .. '.' .. lang) end |
| 88 | + |
| 89 | + -- The "priority" attribute can be set at the pattern level or on a particular capture |
| 90 | + local priority = ( |
| 91 | + tonumber(metadata.priority or metadata[capture] and metadata[capture].priority) |
| 92 | + or vim.highlight.priorities.treesitter |
| 93 | + ) |
| 94 | + |
| 95 | + -- The "conceal" attribute can be set at the pattern level or on a particular capture |
| 96 | + local conceal = metadata.conceal or metadata[capture] and metadata[capture].conceal |
| 97 | + |
| 98 | + if hl and end_row >= line then |
| 99 | + vim.api.nvim_buf_set_extmark(bufnr, require('blink.cmp.config').highlight.ns, start_row, start_col, { |
| 100 | + end_line = end_row, |
| 101 | + end_col = end_col, |
| 102 | + hl_group = hl, |
| 103 | + priority = priority, |
| 104 | + conceal = conceal, |
| 105 | + }) |
| 106 | + end |
| 107 | + end |
| 108 | + |
| 109 | + if start_row > line then line = start_row end |
| 110 | + end |
| 111 | + end) |
| 112 | +end |
| 113 | + |
| 114 | +--- Combines adjacent paragraph lines together |
| 115 | +--- @param lines string[] |
| 116 | +--- @return string[] |
| 117 | +--- TODO: Likely buggy |
| 118 | +function docs.combine_markdown_lines(lines) |
| 119 | + local combined_lines = {} |
| 120 | + |
| 121 | + local special_starting_chars = { '#', '>', '-', '|' } |
| 122 | + local in_code_block = false |
| 123 | + local prev_is_special = false |
| 124 | + for _, line in ipairs(lines) do |
| 125 | + if line:match('^%s*```') then in_code_block = not in_code_block end |
| 126 | + |
| 127 | + local is_special = line:match('^%s*[' .. table.concat(special_starting_chars) .. ']') or line:match('^%s*%d\\.$') |
| 128 | + local is_empty = line:match('^%s*$') |
| 129 | + local has_linebreak = line:match('%s%s$') |
| 130 | + |
| 131 | + if #combined_lines == 0 or in_code_block or is_special or prev_is_special or is_empty or has_linebreak then |
| 132 | + table.insert(combined_lines, line) |
| 133 | + elseif line:match('^%s*$') then |
| 134 | + table.insert(combined_lines, '') |
| 135 | + else |
| 136 | + combined_lines[#combined_lines] = combined_lines[#combined_lines] .. '' .. line |
| 137 | + end |
| 138 | + |
| 139 | + prev_is_special = is_special |
| 140 | + end |
| 141 | + |
| 142 | + return combined_lines |
| 143 | +end |
| 144 | + |
| 145 | +function docs.split_lines(text) |
| 146 | + local lines = {} |
| 147 | + for s in text:gmatch('[^\r\n]+') do |
| 148 | + table.insert(lines, s) |
| 149 | + end |
| 150 | + return lines |
| 151 | +end |
| 152 | + |
| 153 | +return docs |
0 commit comments