Skip to content

Commit d80acb3

Browse files
fix: left padding / margin for code blocks indented with tabs
## Details Issue: #230 When adding inline spaces for padding or margin in code blocks tabs would cause the spacing to not work. For example if a user wants left padding of 2 but has tabstop set to 4 the 2 spaces added would be ignored since they did not occupy enough space to shift the text. To fix this when adding spaces to the left first check if the code block contains any tabs. If it does instead of applying the spacing directly find the next value that is a multiple of the users tabstop and use that. From the previous example we would add 4 spaces now instead of 2. A tabstop of 8 and a padding of 10 would result in 16 spaces.
1 parent bee16b2 commit d80acb3

File tree

6 files changed

+47
-20
lines changed

6 files changed

+47
-20
lines changed

doc/render-markdown.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
*render-markdown.txt* For 0.10.0 Last change: 2024 November 07
1+
*render-markdown.txt* For 0.10.0 Last change: 2024 November 08
22

33
==============================================================================
44
Table of Contents *render-markdown-table-of-contents*

lua/render-markdown/core/context.lua

+5
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,11 @@ function Context:add_checkbox(node, checkbox)
9191
self.checkboxes[node.start_row] = checkbox
9292
end
9393

94+
---@return integer
95+
function Context:tab_size()
96+
return util.get('buf', self.buf, 'tabstop') --[[@as integer]]
97+
end
98+
9499
---@param node? render.md.Node
95100
---@return integer
96101
function Context:width(node)

lua/render-markdown/health.lua

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ local state = require('render-markdown.state')
44
local M = {}
55

66
---@private
7-
M.version = '7.5.1'
7+
M.version = '7.5.2'
88

99
function M.check()
1010
M.start('version')

lua/render-markdown/render/code.lua

+22-4
Original file line numberDiff line numberDiff line change
@@ -46,17 +46,17 @@ function Render:setup()
4646
end
4747

4848
local max_width = vim.fn.max(widths)
49-
local language_padding = self.context:resolve_offset(self.code.language_pad, max_width)
50-
local left_padding = self.context:resolve_offset(self.code.left_pad, max_width)
51-
local right_padding = self.context:resolve_offset(self.code.right_pad, max_width)
49+
local language_padding = self:offset(self.code.language_pad, max_width, self.code.position)
50+
local left_padding = self:offset(self.code.left_pad, max_width, 'left')
51+
local right_padding = self:offset(self.code.right_pad, max_width, 'right')
5252
max_width = math.max(widths[1] + language_padding, left_padding + max_width + right_padding, self.code.min_width)
5353

5454
self.data = {
5555
col = self.node.start_col,
5656
code_node = code_node,
5757
language_node = language_node,
5858
language = (language_node or {}).text,
59-
margin = self.context:resolve_offset(self.code.left_margin, max_width),
59+
margin = self:offset(self.code.left_margin, max_width, 'left'),
6060
language_padding = language_padding,
6161
padding = left_padding,
6262
max_width = max_width,
@@ -67,6 +67,24 @@ function Render:setup()
6767
return true
6868
end
6969

70+
---@private
71+
---@param offset integer
72+
---@param width integer
73+
---@param position render.md.code.Position
74+
---@return integer
75+
function Render:offset(offset, width, position)
76+
if offset <= 0 then
77+
return 0
78+
end
79+
local result = self.context:resolve_offset(offset, width)
80+
if position == 'left' and self.node.text:find('\t') ~= nil then
81+
local tab_size = self.context:tab_size()
82+
-- Rounds to the next multiple of tab_size
83+
result = math.ceil(result / tab_size) * tab_size
84+
end
85+
return result
86+
end
87+
7088
function Render:render()
7189
local disabled_language = self.code.disable_background
7290
if type(disabled_language) == 'table' then

tests/code_spec.lua

+17-13
Original file line numberDiff line numberDiff line change
@@ -83,49 +83,53 @@ describe('code.md', function()
8383

8484
vim.list_extend(expected, util.heading(row:get(), 1))
8585

86-
table.insert(expected, util.code_language(row:increment(2), 0, 'rust', 34))
86+
local width_1 = 34
87+
table.insert(expected, util.code_language(row:increment(2), 0, 'rust', width_1))
8788
for _ = 1, 3 do
8889
vim.list_extend(expected, {
8990
padding(row:increment(), 0, 0, 2, 0),
90-
util.code_hide(row:get(), 0, 34),
91+
util.code_hide(row:get(), 0, width_1),
9192
util.code_row(row:get(), 0),
9293
})
9394
end
94-
table.insert(expected, util.code_border(row:increment(), 0, false, 34))
95+
table.insert(expected, util.code_border(row:increment(), 0, false, width_1))
9596

97+
local width_2 = 20
9698
vim.list_extend(expected, {
9799
util.bullet(row:increment(2), 0, 1),
98-
util.code_language(row:increment(2), 2, 'lua', 20),
100+
util.code_language(row:increment(2), 2, 'lua', width_2),
99101
})
100102
for _ = 1, 2 do
101103
vim.list_extend(expected, {
102104
padding(row:increment(), 2, 0, 2, 1000),
103-
util.code_hide(row:get(), 2, 20),
105+
util.code_hide(row:get(), 2, width_2),
104106
util.code_row(row:get(), 2),
105107
})
106108
end
107-
table.insert(expected, util.code_border(row:increment(), 2, false, 20))
109+
table.insert(expected, util.code_border(row:increment(), 2, false, width_2))
108110

111+
local width_3 = 20
109112
vim.list_extend(expected, {
110113
util.bullet(row:increment(2), 0, 1),
111-
util.code_language(row:increment(2), 2, 'lua', 20),
114+
util.code_language(row:increment(2), 2, 'lua', width_3),
112115
})
113116
for _, col in ipairs({ 2, 0, 2 }) do
114117
vim.list_extend(expected, {
115118
padding(row:increment(), col, 2 - col, 2, 1000),
116-
util.code_hide(row:get(), col, 20),
119+
util.code_hide(row:get(), col, width_3),
117120
util.code_row(row:get(), col),
118121
})
119122
end
120-
table.insert(expected, util.code_border(row:increment(), 2, false, 20))
123+
table.insert(expected, util.code_border(row:increment(), 2, false, width_3))
121124

125+
local width_4 = (2 * vim.opt.tabstop:get()) + 24
122126
vim.list_extend(expected, {
123127
util.bullet(row:increment(2), 0, 1),
124-
util.code_border(row:increment(2), 0, true, 26),
125-
padding(row:increment(), 0, 0, 2, 0),
126-
util.code_hide(row:get(), 0, 26),
128+
util.code_border(row:increment(2), 0, true, width_4),
129+
padding(row:increment(), 0, 0, vim.opt.tabstop:get(), 0),
130+
util.code_hide(row:get(), 0, width_4),
127131
util.code_row(row:get(), 0),
128-
util.code_border(row:increment(), 0, false, 26),
132+
util.code_border(row:increment(), 0, false, width_4),
129133
})
130134

131135
local actual = util.get_actual_marks()

tests/data/code.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,5 @@ fn main() {
2424
- No language
2525

2626
```
27-
print('Hello, World!')
27+
print('Hello, World!')
2828
```

0 commit comments

Comments
 (0)