Skip to content

Commit 426b135

Browse files
feat: Add heading.width option
## Details Currently headings always extend to the end of the current line. Some users do not like that, to support this add a width attribute with 2 values, 'full' & 'block'. 'full' behaves the same as now and extends the background to the end of the line. 'block' on the other hand ends right after the text of the heading. Original PR: #94 Co-Authored-By: Zeioth <[email protected]>
1 parent f557e5e commit 426b135

File tree

9 files changed

+64
-21
lines changed

9 files changed

+64
-21
lines changed

Diff for: CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@
6464
- Added to LazyVim distribution [#4139](https://github.com/LazyVim/LazyVim/pull/4139)
6565
- Supported by tokyonight.nvim colorscheme [71429c9](https://github.com/folke/tokyonight.nvim/commit/71429c97b7aeafecf333fa825a85eadb21426146)
6666
- Supported by cyberdream.nvim colorscheme [ba25d43](https://github.com/scottmckendry/cyberdream.nvim/commit/ba25d43d68dd34d31bee88286fb6179df2763c31)
67+
- Supported by rose-pine colorscheme [#303](https://github.com/rose-pine/neovim/pull/303)
6768

6869
## 4.1.0 (2024-07-14)
6970

Diff for: README.md

+14-2
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,10 @@ require('render-markdown').setup({
202202
-- Added to the sign column if enabled
203203
-- The 'level' is used to index into the array using a cycle
204204
signs = { '󰫎 ' },
205+
-- Width of the heading background:
206+
-- block: width of the heading text
207+
-- full: full width of the window
208+
width = 'full',
205209
-- The 'level' is used to index into the array using a clamp
206210
-- Highlight for the heading icon and extends through the entire line
207211
backgrounds = {
@@ -260,7 +264,9 @@ require('render-markdown').setup({
260264
-- Replaces '---'|'***'|'___'|'* * *' of 'thematic_break'
261265
-- The icon gets repeated across the window's width
262266
icon = '',
263-
-- Width of the generated line, either the 'full' window width or a hard coded value
267+
-- Width of the generated line:
268+
-- <integer>: a hard coded width value
269+
-- full: full width of the window
264270
width = 'full',
265271
-- Highlight for the whole line generated from the icon
266272
highlight = 'RenderMarkdownDash',
@@ -435,6 +441,10 @@ require('render-markdown').setup({
435441
-- Added to the sign column if enabled
436442
-- The 'level' is used to index into the array using a cycle
437443
signs = { '󰫎 ' },
444+
-- Width of the heading background:
445+
-- block: width of the heading text
446+
-- full: full width of the window
447+
width = 'full',
438448
-- The 'level' is used to index into the array using a clamp
439449
-- Highlight for the heading icon and extends through the entire line
440450
backgrounds = {
@@ -507,7 +517,9 @@ require('render-markdown').setup({
507517
-- Replaces '---'|'***'|'___'|'* * *' of 'thematic_break'
508518
-- The icon gets repeated across the window's width
509519
icon = '',
510-
-- Width of the generated line, either the 'full' window width or a hard coded value
520+
-- Width of the generated line:
521+
-- <integer>: a hard coded width value
522+
-- full: full width of the window
511523
width = 'full',
512524
-- Highlight for the whole line generated from the icon
513525
highlight = 'RenderMarkdownDash',

Diff for: doc/render-markdown.txt

+14-2
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,10 @@ Full Default Configuration ~
243243
-- Added to the sign column if enabled
244244
-- The 'level' is used to index into the array using a cycle
245245
signs = { '󰫎 ' },
246+
-- Width of the heading background:
247+
-- block: width of the heading text
248+
-- full: full width of the window
249+
width = 'full',
246250
-- The 'level' is used to index into the array using a clamp
247251
-- Highlight for the heading icon and extends through the entire line
248252
backgrounds = {
@@ -301,7 +305,9 @@ Full Default Configuration ~
301305
-- Replaces '---'|'***'|'___'|'* * *' of 'thematic_break'
302306
-- The icon gets repeated across the window's width
303307
icon = '─',
304-
-- Width of the generated line, either the 'full' window width or a hard coded value
308+
-- Width of the generated line:
309+
-- <integer>: a hard coded width value
310+
-- full: full width of the window
305311
width = 'full',
306312
-- Highlight for the whole line generated from the icon
307313
highlight = 'RenderMarkdownDash',
@@ -475,6 +481,10 @@ HEADINGS *render-markdown-setup-headings*
475481
-- Added to the sign column if enabled
476482
-- The 'level' is used to index into the array using a cycle
477483
signs = { '󰫎 ' },
484+
-- Width of the heading background:
485+
-- block: width of the heading text
486+
-- full: full width of the window
487+
width = 'full',
478488
-- The 'level' is used to index into the array using a clamp
479489
-- Highlight for the heading icon and extends through the entire line
480490
backgrounds = {
@@ -549,7 +559,9 @@ DASHED LINE *render-markdown-setup-dashed-line*
549559
-- Replaces '---'|'***'|'___'|'* * *' of 'thematic_break'
550560
-- The icon gets repeated across the window's width
551561
icon = '─',
552-
-- Width of the generated line, either the 'full' window width or a hard coded value
562+
-- Width of the generated line:
563+
-- <integer>: a hard coded width value
564+
-- full: full width of the window
553565
width = 'full',
554566
-- Highlight for the whole line generated from the icon
555567
highlight = 'RenderMarkdownDash',

Diff for: doc/todo.md

+1
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@
55
- Add a performance test suite to measure improvements, relative value on
66
same hardware should still be useful.
77
- Look into showing marks when switching buffers, currently remains hidden
8+
- Figure out how to display the many configuration options & impact

Diff for: lua/render-markdown/handler/markdown.lua

+16-12
Original file line numberDiff line numberDiff line change
@@ -80,13 +80,15 @@ function M.render_heading(buf, info)
8080
end_row = info.end_row + 1,
8181
end_col = 0,
8282
hl_group = background,
83-
hl_eol = true,
83+
hl_eol = heading.width == 'full',
8484
},
8585
}
8686
list.add(marks, background_mark)
87+
8788
if heading.sign then
8889
list.add(marks, M.render_sign(buf, info, list.cycle(heading.signs, level), foreground))
8990
end
91+
9092
if icon == nil then
9193
return marks
9294
end
@@ -121,7 +123,7 @@ function M.render_heading(buf, info)
121123
opts = {
122124
end_row = info.end_row,
123125
end_col = info.end_col,
124-
virt_text = { { str.pad(icon, padding), { foreground, background } } },
126+
virt_text = { { str.pad(padding, icon), { foreground, background } } },
125127
virt_text_pos = 'overlay',
126128
},
127129
}
@@ -187,9 +189,11 @@ function M.render_code(buf, info)
187189
return marks
188190
end
189191

190-
local width = util.get_width(buf)
191-
if code.width == 'block' then
192-
local lines = vim.api.nvim_buf_get_lines(buf, start_row, end_row, false)
192+
local width
193+
if code.width == 'full' then
194+
width = util.get_width(buf)
195+
elseif code.width == 'block' then
196+
local lines = vim.api.nvim_buf_get_lines(buf, start_row, end_row, true)
193197
local code_width = vim.fn.max(vim.tbl_map(str.width, lines))
194198
width = code.left_pad + code_width + code.right_pad
195199
end
@@ -242,8 +246,8 @@ function M.render_code(buf, info)
242246
list.add(marks, background_mark)
243247

244248
if code.width == 'block' then
245-
-- overwrite anything beyond block width + right_pad with Normal
246-
local pad = str.pad('', vim.o.columns * 2)
249+
-- Overwrite anything beyond left_pad + block width + right_pad with Normal
250+
local pad = str.pad(vim.o.columns * 2)
247251
for row = start_row, code.border == 'thin' and end_row or end_row - 1 do
248252
---@type render.md.Mark
249253
local block_background_mark = {
@@ -275,7 +279,7 @@ function M.render_code(buf, info)
275279
opts = {
276280
end_row = row + 1,
277281
priority = 0,
278-
virt_text = { { str.pad('', code.left_pad), code.highlight } },
282+
virt_text = { { str.pad(code.left_pad), code.highlight } },
279283
virt_text_pos = 'inline',
280284
},
281285
}
@@ -312,7 +316,7 @@ function M.render_language(buf, info, code_block)
312316
-- context they are in. This gets lumped into the delimiter node and as a result,
313317
-- after concealing, the extmark will be left shifted. Logic below accounts for this.
314318
local padding = str.leading_spaces(code_block.text)
315-
icon_text = str.pad(icon_text .. info.text, padding)
319+
icon_text = str.pad(padding, icon_text .. info.text)
316320
end
317321
local highlight = { icon_highlight }
318322
if code.style == 'full' then
@@ -391,7 +395,7 @@ function M.render_list_marker(buf, info)
391395
opts = {
392396
end_row = info.end_row,
393397
end_col = info.end_col,
394-
virt_text = { { str.pad(icon, leading_spaces), bullet.highlight } },
398+
virt_text = { { str.pad(leading_spaces, icon), bullet.highlight } },
395399
virt_text_pos = 'overlay',
396400
},
397401
}
@@ -404,7 +408,7 @@ function M.render_list_marker(buf, info)
404408
start_row = info.start_row,
405409
start_col = info.end_col - 1,
406410
opts = {
407-
virt_text = { { str.pad('', bullet.right_pad), 'Normal' } },
411+
virt_text = { { str.pad(bullet.right_pad), 'Normal' } },
408412
virt_text_pos = 'inline',
409413
},
410414
}
@@ -594,7 +598,7 @@ function M.render_table_row(buf, row, highlight)
594598
start_row = cell.start_row,
595599
start_col = cell.end_col - 1,
596600
opts = {
597-
virt_text = { { str.pad('', offset), pipe_table.filler } },
601+
virt_text = { { str.pad(offset), pipe_table.filler } },
598602
virt_text_pos = 'inline',
599603
},
600604
}

Diff for: lua/render-markdown/init.lua

+8-1
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ local M = {}
8989
---@field public sign? boolean
9090
---@field public icons? string[]
9191
---@field public signs? string[]
92+
---@field public width? 'full'|'block'
9293
---@field public backgrounds? string[]
9394
---@field public foregrounds? string[]
9495

@@ -228,6 +229,10 @@ M.default_config = {
228229
-- Added to the sign column if enabled
229230
-- The 'level' is used to index into the array using a cycle
230231
signs = { '󰫎 ' },
232+
-- Width of the heading background:
233+
-- block: width of the heading text
234+
-- full: full width of the window
235+
width = 'full',
231236
-- The 'level' is used to index into the array using a clamp
232237
-- Highlight for the heading icon and extends through the entire line
233238
backgrounds = {
@@ -286,7 +291,9 @@ M.default_config = {
286291
-- Replaces '---'|'***'|'___'|'* * *' of 'thematic_break'
287292
-- The icon gets repeated across the window's width
288293
icon = '',
289-
-- Width of the generated line, either the 'full' window width or a hard coded value
294+
-- Width of the generated line:
295+
-- <integer>: a hard coded width value
296+
-- full: full width of the window
290297
width = 'full',
291298
-- Highlight for the whole line generated from the icon
292299
highlight = 'RenderMarkdownDash',

Diff for: lua/render-markdown/state.lua

+1
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ function state.validate()
127127
sign = { heading.sign, 'boolean' },
128128
icons = string_array(heading.icons),
129129
signs = string_array(heading.signs),
130+
width = one_of(heading.width, { 'full', 'block' }),
130131
backgrounds = string_array(heading.backgrounds),
131132
foregrounds = string_array(heading.foregrounds),
132133
})

Diff for: lua/render-markdown/str.lua

+8-4
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,18 @@ end
2222
---@return string
2323
function M.pad_to(value, s)
2424
local padding = M.width(value) - M.width(s)
25-
return M.pad(s, padding)
25+
return M.pad(padding, s)
2626
end
2727

28-
---@param s string
2928
---@param padding integer
29+
---@param s string?
3030
---@return string
31-
function M.pad(s, padding)
32-
return string.rep(' ', padding) .. s
31+
function M.pad(padding, s)
32+
local result = string.rep(' ', padding)
33+
if s ~= nil then
34+
result = result .. s
35+
end
36+
return result
3337
end
3438

3539
return M

Diff for: lua/render-markdown/types.lua

+1
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@
7474
---@field public sign boolean
7575
---@field public icons string[]
7676
---@field public signs string[]
77+
---@field public width 'full'|'block'
7778
---@field public backgrounds string[]
7879
---@field public foregrounds string[]
7980

0 commit comments

Comments
 (0)