Skip to content

Commit 62252c7

Browse files
Use an early return for heading icon check
1 parent f9d54a8 commit 62252c7

File tree

3 files changed

+59
-64
lines changed

3 files changed

+59
-64
lines changed

lua/render-markdown/handler/markdown.lua

Lines changed: 48 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -67,49 +67,10 @@ M.render_heading = function(buf, info)
6767
local marks = {}
6868

6969
local level = str.width(info.text)
70-
local foreground = list.clamp(heading.foregrounds, level)
7170
local icon = list.cycle(heading.icons, level)
71+
local foreground = list.clamp(heading.foregrounds, level)
7272
local background = list.clamp(heading.backgrounds, level)
7373

74-
if icon then
75-
-- Available width is level + 1 - concealed, where level = number of `#` characters, one
76-
-- is added to account for the space after the last `#` but before the heading title,
77-
-- and concealed text is subtracted since that space is not usable
78-
local padding = level + 1 - ts.concealed(buf, info) - str.width(icon)
79-
if padding < 0 then
80-
-- Requires inline extmarks to place when there is not enough space available
81-
if util.has_10 then
82-
---@type render.md.Mark
83-
local icon_mark = {
84-
conceal = true,
85-
start_row = info.start_row,
86-
start_col = info.start_col,
87-
opts = {
88-
end_row = info.end_row,
89-
end_col = info.end_col,
90-
virt_text = { { icon, { foreground, background } } },
91-
virt_text_pos = 'inline',
92-
conceal = '',
93-
},
94-
}
95-
list.add(marks, icon_mark)
96-
end
97-
else
98-
---@type render.md.Mark
99-
local icon_mark = {
100-
conceal = true,
101-
start_row = info.start_row,
102-
start_col = info.start_col,
103-
opts = {
104-
end_row = info.end_row,
105-
end_col = info.end_col,
106-
virt_text = { { str.pad(icon, padding), { foreground, background } } },
107-
virt_text_pos = 'overlay',
108-
},
109-
}
110-
list.add(marks, icon_mark)
111-
end
112-
end
11374
---@type render.md.Mark
11475
local background_mark = {
11576
conceal = true,
@@ -126,7 +87,46 @@ M.render_heading = function(buf, info)
12687
if heading.sign then
12788
list.add(marks, M.render_sign(buf, info, list.cycle(heading.signs, level), foreground))
12889
end
129-
90+
if icon == nil then
91+
return marks
92+
end
93+
-- Available width is level + 1 - concealed, where level = number of `#` characters, one
94+
-- is added to account for the space after the last `#` but before the heading title,
95+
-- and concealed text is subtracted since that space is not usable
96+
local padding = level + 1 - ts.concealed(buf, info) - str.width(icon)
97+
if padding < 0 then
98+
-- Requires inline extmarks to place when there is not enough space available
99+
if util.has_10 then
100+
---@type render.md.Mark
101+
local icon_mark = {
102+
conceal = true,
103+
start_row = info.start_row,
104+
start_col = info.start_col,
105+
opts = {
106+
end_row = info.end_row,
107+
end_col = info.end_col,
108+
virt_text = { { icon, { foreground, background } } },
109+
virt_text_pos = 'inline',
110+
conceal = '',
111+
},
112+
}
113+
list.add(marks, icon_mark)
114+
end
115+
else
116+
---@type render.md.Mark
117+
local icon_mark = {
118+
conceal = true,
119+
start_row = info.start_row,
120+
start_col = info.start_col,
121+
opts = {
122+
end_row = info.end_row,
123+
end_col = info.end_col,
124+
virt_text = { { str.pad(icon, padding), { foreground, background } } },
125+
virt_text_pos = 'overlay',
126+
},
127+
}
128+
list.add(marks, icon_mark)
129+
end
130130
return marks
131131
end
132132

@@ -333,12 +333,15 @@ M.render_list_marker = function(buf, info)
333333
if not bullet.enabled then
334334
return nil
335335
end
336+
local level = ts.level_in_section(info, 'list')
337+
local icon = list.cycle(bullet.icons, level)
338+
if icon == nil then
339+
return nil
340+
end
336341
-- List markers from tree-sitter should have leading spaces removed, however there are known
337342
-- edge cases in the parser: https://github.com/tree-sitter-grammars/tree-sitter-markdown/issues/127
338343
-- As a result we handle leading spaces here, can remove if this gets fixed upstream
339344
local leading_spaces = str.leading_spaces(info.text)
340-
local level = ts.level_in_section(info, 'list')
341-
local icon = list.cycle(bullet.icons, level)
342345
---@type render.md.Mark
343346
return {
344347
conceal = true,
@@ -408,12 +411,12 @@ end
408411
---@private
409412
---@param buf integer
410413
---@param info render.md.NodeInfo
411-
---@param text string
414+
---@param text string?
412415
---@param highlight string
413416
---@return render.md.Mark?
414417
M.render_sign = function(buf, info, text, highlight)
415418
local sign = state.config.sign
416-
if not sign.enabled then
419+
if not sign.enabled or text == nil then
417420
return nil
418421
end
419422
if vim.tbl_contains(sign.exclude.buftypes, util.get_buf(buf, 'buftype')) then

lua/render-markdown/list.lua

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,28 +11,20 @@ end
1111

1212
---@param values string[]
1313
---@param index integer
14-
---@return string
14+
---@return string?
1515
function M.cycle(values, index)
16+
if #values == 0 then
17+
return nil
18+
end
1619
return values[((index - 1) % #values) + 1]
1720
end
1821

1922
---@param values string[]
2023
---@param index integer
2124
---@return string
2225
function M.clamp(values, index)
26+
assert(#values >= 1, 'Must have at least one value')
2327
return values[math.min(index, #values)]
2428
end
2529

26-
---@param values string[]
27-
---@return string
28-
function M.first(values)
29-
return values[1]
30-
end
31-
32-
---@param values string[]
33-
---@return string
34-
function M.last(values)
35-
return values[#values]
36-
end
37-
3830
return M

tests/util.lua

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,19 +56,19 @@ M.heading = function(row, level)
5656
return { sign_mark }
5757
else
5858
return {
59+
{
60+
row = { row, row + 1 },
61+
col = { 0, 0 },
62+
hl_group = background,
63+
hl_eol = true,
64+
},
5965
{
6066
row = { row, row },
6167
col = { 0, level },
6268
virt_text = { { icons[level], { foreground, background } } },
6369
virt_text_pos = 'overlay',
6470
},
6571
sign_mark,
66-
{
67-
row = { row, row + 1 },
68-
col = { 0, 0 },
69-
hl_group = background,
70-
hl_eol = true,
71-
},
7272
}
7373
end
7474
end

0 commit comments

Comments
 (0)