Skip to content

Commit de1a0b7

Browse files
committed
fix comments folding into 3 lines
1 parent fe886d0 commit de1a0b7

File tree

2 files changed

+52
-36
lines changed

2 files changed

+52
-36
lines changed

lua/xdoc/renderer.lua

+47-36
Original file line numberDiff line numberDiff line change
@@ -113,45 +113,56 @@ end
113113

114114
--- Main rendering function
115115
function M.render()
116-
if not enabled then return end
117-
118-
local bufnr = vim.api.nvim_get_current_buf()
119-
vim.api.nvim_buf_clear_namespace(bufnr, ns, 0, -1)
120-
121-
local comment_lines = parser.parse_buffer()
122-
local blocks = group_blocks(comment_lines)
123-
124-
for _, block in ipairs(blocks) do
125-
local is_empty_box = (#block >= 3 and util.is_empty_comment(block[1].text) and util.is_empty_comment(block[#block].text))
126-
local is_doc = is_doc_block(block)
127-
128-
if is_empty_box or is_doc then
129-
local content
130-
if is_empty_box then
131-
content = vim.list_slice(block, 2, #block - 1)
132-
else
133-
content = block
134-
end
135-
136-
local box = build_box(content)
137-
138-
local virt_opts = {
139-
virt_lines = box,
140-
hl_mode = "combine",
141-
virt_lines_above = block[1].lnum ~= 0, -- show below if first line
142-
}
143-
144-
vim.api.nvim_buf_set_extmark(bufnr, ns, block[1].lnum, 0, virt_opts)
116+
if not enabled then
117+
return
118+
end
119+
120+
local bufnr = vim.api.nvim_get_current_buf()
121+
vim.api.nvim_buf_clear_namespace(bufnr, ns, 0, -1)
122+
123+
local comment_lines = parser.parse_buffer()
124+
local blocks = group_blocks(comment_lines)
125+
126+
for _, block in ipairs(blocks) do
127+
local is_empty_box = (#block >= 3
128+
and util.is_empty_comment(block[1].text)
129+
and util.is_empty_comment(block[#block].text))
130+
local is_doc = is_doc_block(block)
131+
132+
if is_empty_box or is_doc then
133+
local content
134+
if is_empty_box then
135+
content = vim.list_slice(block, 2, #block - 1)
136+
else
137+
content = block
138+
end
139+
140+
local box = build_box(content)
141+
142+
if is_empty_box then
143+
-- Attach the virtual box immediately after the folded block,
144+
-- but render it immediately above the marker line.
145+
local mark_line = block[#block].lnum + 1
146+
vim.api.nvim_buf_set_extmark(bufnr, ns, mark_line, 0, {
147+
virt_lines = box,
148+
hl_mode = "combine",
149+
virt_lines_above = true, -- Changed to true to remove gap.
150+
})
145151

146-
if is_empty_box then
147-
local fold_start = block[2].lnum + 1
148-
local fold_end = block[#block - 1].lnum + 1
149-
if fold_end >= fold_start then
150-
vim.cmd(fold_start .. "," .. fold_end .. "fold")
151-
end
152-
end
152+
local fold_start = block[1].lnum + 1
153+
local fold_end = block[#block].lnum + 1
154+
if fold_end >= fold_start then
155+
vim.cmd(fold_start .. "," .. fold_end .. "fold")
153156
end
157+
else
158+
vim.api.nvim_buf_set_extmark(bufnr, ns, block[1].lnum, 0, {
159+
virt_lines = box,
160+
hl_mode = "combine",
161+
virt_lines_above = block[1].lnum ~= 0,
162+
})
163+
end
154164
end
165+
end
155166
end
156167

157168
--- Clears all virtual highlights and folds created by xdoc

lua/xdoc/util.lua

+5
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,10 @@ function M.is_empty_comment(text)
2121
return vim.trim(text) == ""
2222
end
2323

24+
--
25+
-- # meow
26+
-- wiwiwiwi
27+
--
28+
2429

2530
return M

0 commit comments

Comments
 (0)