@@ -17,12 +17,7 @@ function docs.render_detail_and_documentation(bufnr, detail, documentation, max_
17
17
end
18
18
end
19
19
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 )
26
21
27
22
local combined_lines = vim .list_extend ({}, detail_lines )
28
23
-- add a blank line for the --- separator
126
121
function docs .combine_markdown_lines (lines )
127
122
local combined_lines = {}
128
123
129
- local special_starting_chars = { ' #' , ' >' , ' -' , ' |' }
124
+ local special_starting_chars = { ' #' , ' >' , ' -' , ' |' , ' * ' , ' • ' }
130
125
local in_code_block = false
131
126
local prev_is_special = false
132
127
for _ , line in ipairs (lines ) do
@@ -153,6 +148,85 @@ function docs.combine_markdown_lines(lines)
153
148
return combined_lines
154
149
end
155
150
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
+
156
230
function docs .split_lines (text )
157
231
local lines = {}
158
232
for s in text :gmatch (' [^\r\n ]+' ) do
0 commit comments