Skip to content

Commit 739d845

Browse files
feat: independent code block language padding
## Details Original PR: #131 The idea of this this change is that there is a slight difference in behavior for left aligned vs right aligned language for code blocks. - right: language is put at edge of block, essentially not padded - left: language is indented along with rest of block, essentially padded To make the behavior more consistent the language will no longer be padded for left aligned, unlike before, so it will not be effected by left_pad like the code block body. A new field has been added `code.language_pad` which will add padding specific to the language section. To have the same behavior as before for left aligned this value should be set to the same value as for `code.left_pad`. The padding is applied relative to this position value. - left: language is left padded, moving it to the right - right: language is right padded, moving it to the left This allows more customization for users while allowing for the same behavior as before if that is preferred. Though not technically fully backwards compatible as getting the same behavior as before requires setting an additional option value. Co-authored-by: Biggybi <[email protected]>
1 parent 8b6663b commit 739d845

File tree

8 files changed

+26
-5
lines changed

8 files changed

+26
-5
lines changed

Diff for: README.md

+4
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,8 @@ require('render-markdown').setup({
301301
-- right: right side of code block
302302
-- left: left side of code block
303303
position = 'left',
304+
-- Amount of padding to add around the language
305+
language_pad = 0,
304306
-- An array of language names for which background highlighting will be disabled
305307
-- Likely because that language has background highlights itself
306308
disable_background = { 'diff' },
@@ -619,6 +621,8 @@ require('render-markdown').setup({
619621
-- right: right side of code block
620622
-- left: left side of code block
621623
position = 'left',
624+
-- Amount of padding to add around the language
625+
language_pad = 0,
622626
-- An array of language names for which background highlighting will be disabled
623627
-- Likely because that language has background highlights itself
624628
disable_background = { 'diff' },

Diff for: doc/render-markdown.txt

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
*render-markdown.txt* For 0.10.0 Last change: 2024 August 20
1+
*render-markdown.txt* For 0.10.0 Last change: 2024 August 21
22

33
==============================================================================
44
Table of Contents *render-markdown-table-of-contents*
@@ -331,6 +331,8 @@ Full Default Configuration ~
331331
-- right: right side of code block
332332
-- left: left side of code block
333333
position = 'left',
334+
-- Amount of padding to add around the language
335+
language_pad = 0,
334336
-- An array of language names for which background highlighting will be disabled
335337
-- Likely because that language has background highlights itself
336338
disable_background = { 'diff' },
@@ -651,6 +653,8 @@ Wiki Page
651653
-- right: right side of code block
652654
-- left: left side of code block
653655
position = 'left',
656+
-- Amount of padding to add around the language
657+
language_pad = 0,
654658
-- An array of language names for which background highlighting will be disabled
655659
-- Likely because that language has background highlights itself
656660
disable_background = { 'diff' },

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

+5-2
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,9 @@ function Handler:code(info)
268268
if add_background then
269269
self:code_background(code_block, icon_added)
270270
end
271+
if icon_added then
272+
code_block.start_row = code_block.start_row + 1
273+
end
271274
self:code_left_pad(code_block, add_background)
272275
end
273276

@@ -301,15 +304,15 @@ function Handler:language(code_block, add_background)
301304
-- Code blocks will pick up varying amounts of leading white space depending on the
302305
-- context they are in. This gets lumped into the delimiter node and as a result,
303306
-- after concealing, the extmark will be left shifted. Logic below accounts for this.
304-
icon_text = str.pad(code_block.leading_spaces, icon_text .. info.text)
307+
icon_text = str.pad(code_block.leading_spaces + code.language_pad, icon_text .. info.text)
305308
end
306309
return self:add(true, info.start_row, info.start_col, {
307310
virt_text = { { icon_text, highlight } },
308311
virt_text_pos = 'inline',
309312
})
310313
elseif code.position == 'right' then
311314
local icon_text = icon .. ' ' .. info.text
312-
local win_col = code_block.longest_line
315+
local win_col = code_block.longest_line - code.language_pad
313316
if code.width == 'block' then
314317
win_col = win_col - str.width(icon_text)
315318
end

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ local M = {}
55

66
---@private
77
---@type string
8-
M.version = '6.1.9'
8+
M.version = '6.1.10'
99

1010
function M.check()
1111
vim.health.start('render-markdown.nvim [version]')

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

+3
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ local M = {}
100100
---@field public sign? boolean
101101
---@field public style? render.md.code.Style
102102
---@field public position? render.md.code.Position
103+
---@field public language_pad? integer
103104
---@field public disable_background? string[]
104105
---@field public width? render.md.code.Width
105106
---@field public left_pad? integer
@@ -334,6 +335,8 @@ M.default_config = {
334335
-- right: right side of code block
335336
-- left: left side of code block
336337
position = 'left',
338+
-- Amount of padding to add around the language
339+
language_pad = 0,
337340
-- An array of language names for which background highlighting will be disabled
338341
-- Likely because that language has background highlights itself
339342
disable_background = { 'diff' },

Diff for: lua/render-markdown/parser/code_block.lua

+6-1
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,15 @@ function M.parse(config, context, info)
2626
if info.end_row - info.start_row <= 1 then
2727
return nil
2828
end
29+
30+
-- Account for language padding in first row
2931
local widths = vim.tbl_map(str.width, info:lines())
32+
widths[1] = widths[1] + config.language_pad
33+
local longest_line, width = M.get_width(config, context, widths)
34+
3035
local code_info = info:child('info_string', info.start_row)
3136
local language_info = code_info ~= nil and code_info:child('language', info.start_row) or nil
32-
local longest_line, width = M.get_width(config, context, widths)
37+
3338
---@type render.md.parsed.CodeBlock
3439
return {
3540
col = info.start_col,

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

+1
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,7 @@ function M.validate()
240240
sign = { code.sign, 'boolean', nilable },
241241
style = one_of(code.style, { 'full', 'normal', 'language', 'none' }, {}, nilable),
242242
position = one_of(code.position, { 'left', 'right' }, {}, nilable),
243+
language_pad = { code.language_pad, 'number', nilable },
243244
disable_background = string_array(code.disable_background, nilable),
244245
width = one_of(code.width, { 'full', 'block' }, {}, nilable),
245246
left_pad = { code.left_pad, 'number', nilable },

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

+1
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@
7878
---@field public sign boolean
7979
---@field public style render.md.code.Style
8080
---@field public position render.md.code.Position
81+
---@field public language_pad integer
8182
---@field public disable_background string[]
8283
---@field public width render.md.code.Width
8384
---@field public left_pad integer

0 commit comments

Comments
 (0)