Skip to content

Commit b0815e4

Browse files
committed
feat: better extraction of detail from doc
1 parent 3ca68ef commit b0815e4

File tree

1 file changed

+81
-7
lines changed

1 file changed

+81
-7
lines changed

lua/blink/cmp/windows/lib/docs.lua

+81-7
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,7 @@ function docs.render_detail_and_documentation(bufnr, detail, documentation, max_
1717
end
1818
end
1919

20-
-- don't show the detail if it's part of the documentation
21-
local detail_str = table.concat(detail_lines, '\n')
22-
local doc_str = table.concat(doc_lines, '\n')
23-
if doc_str:find(detail_str, 1, true) then
24-
detail_lines = {}
25-
end
20+
detail_lines, doc_lines = docs.extract_detail_from_doc(detail_lines, doc_lines)
2621

2722
local combined_lines = vim.list_extend({}, detail_lines)
2823
-- add a blank line for the --- separator
@@ -126,7 +121,7 @@ end
126121
function docs.combine_markdown_lines(lines)
127122
local combined_lines = {}
128123

129-
local special_starting_chars = { '#', '>', '-', '|' }
124+
local special_starting_chars = { '#', '>', '-', '|', '*', '' }
130125
local in_code_block = false
131126
local prev_is_special = false
132127
for _, line in ipairs(lines) do
@@ -153,6 +148,85 @@ function docs.combine_markdown_lines(lines)
153148
return combined_lines
154149
end
155150

151+
--- Gets the start and end row of the code block for the given row
152+
--- Or returns nil if there's no code block
153+
--- @param lines string[]
154+
--- @param row number
155+
--- @return number?, number?
156+
function docs.get_code_block_range(lines, row)
157+
-- get the start of the code block
158+
local code_block_start = nil
159+
for i = 1, row do
160+
local line = lines[i]
161+
if line:match('^%s*```') then
162+
if code_block_start == nil then
163+
code_block_start = i
164+
else
165+
code_block_start = nil
166+
end
167+
end
168+
end
169+
if code_block_start == nil then return end
170+
171+
-- get the end of the code block
172+
local code_block_end = nil
173+
for i = row, #lines do
174+
local line = lines[i]
175+
if line:match('^%s*```') then
176+
code_block_end = i
177+
break
178+
end
179+
end
180+
if code_block_end == nil then return end
181+
182+
return code_block_start, code_block_end
183+
end
184+
185+
--- Avoids showing the detail if it's part of the documentation
186+
--- or, if the detail is in a code block in the doc,
187+
--- extracts the code block into the detail
188+
---@param detail_lines string[]
189+
---@param doc_lines string[]
190+
---@return string[], string[]
191+
function docs.extract_detail_from_doc(detail_lines, doc_lines)
192+
local detail_str = table.concat(detail_lines, '\n')
193+
local doc_str = table.concat(doc_lines, '\n')
194+
local doc_str_detail_row = doc_str:find(detail_str, 1, true)
195+
196+
-- didn't find the detail in the doc, so return as is
197+
if doc_str_detail_row == nil then
198+
return detail_lines, doc_lines
199+
end
200+
201+
-- get the line of the match
202+
-- hack: surely there's a better way to do this but it's late
203+
-- and I can't be bothered
204+
local offset = 1
205+
local detail_line = 1
206+
for line_num, line in ipairs(doc_lines) do
207+
if #line + offset > doc_str_detail_row then
208+
detail_line = line_num
209+
break
210+
end
211+
offset = offset + #line + 1
212+
end
213+
214+
-- extract the code block, if it exists, and use it as the detail
215+
local code_block_start, code_block_end = docs.get_code_block_range(doc_lines, detail_line)
216+
if code_block_start ~= nil and code_block_end ~= nil then
217+
detail_lines = vim.list_slice(doc_lines, code_block_start + 1, code_block_end - 1)
218+
219+
local doc_lines_start = vim.list_slice(doc_lines, 1, code_block_start - 1)
220+
local doc_lines_end = vim.list_slice(doc_lines, code_block_end + 1, #doc_lines)
221+
vim.list_extend(doc_lines_start, doc_lines_end)
222+
doc_lines = doc_lines_start
223+
else
224+
detail_lines = {}
225+
end
226+
227+
return detail_lines, doc_lines
228+
end
229+
156230
function docs.split_lines(text)
157231
local lines = {}
158232
for s in text:gmatch('[^\r\n]+') do

0 commit comments

Comments
 (0)