Skip to content

Commit 232a1d1

Browse files
committed
fix(table): indented wrapped table rendering
1 parent 9b938af commit 232a1d1

2 files changed

Lines changed: 56 additions & 7 deletions

File tree

lua/render-markdown/render/markdown/table.lua

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -166,17 +166,18 @@ function Render:compute_layout()
166166
end
167167

168168
local win_width = env.win.width(self.context.win)
169+
local remaining_width = math.max(win_width - self:indent_width(), 1)
169170
local mtw = self.config.max_table_width
170171
local available
171172
if mtw < 0 then
172173
-- Negative: characters from right edge
173-
available = win_width + mtw
174+
available = remaining_width + mtw
174175
elseif mtw <= 1 then
175176
-- Fraction of window width
176-
available = math.floor(win_width * mtw)
177+
available = math.floor(remaining_width * mtw)
177178
else
178-
-- Absolute character width
179-
available = math.floor(mtw)
179+
-- Absolute character width, capped to fit after indentation
180+
available = math.min(math.floor(mtw), remaining_width)
180181
end
181182
local num_cols = #self.data.cols
182183
local padding = self.config.padding
@@ -267,6 +268,13 @@ function Render:compute_layout()
267268
return { wrap = true, col_widths = col_widths, row_heights = row_heights }
268269
end
269270

271+
---@private
272+
---@return integer
273+
function Render:indent_width()
274+
local first = self.data.rows[1].node
275+
return math.max(str.spaces('start', first.text), first.start_col)
276+
end
277+
270278
---@private
271279
---@param node render.md.Node
272280
---@return render.md.table.Col[]?
@@ -537,9 +545,7 @@ function Render:border_line(above)
537545
end)
538546
local text = chars[1] .. table.concat(parts, chars[2]) .. chars[3]
539547
local highlight = above and self.config.head or self.config.row
540-
local first = self.data.rows[1].node
541-
local spaces = math.max(str.spaces('start', first.text), first.start_col)
542-
return self:line():pad(spaces):text(text, highlight)
548+
return self:line():pad(self:indent_width()):text(text, highlight)
543549
end
544550

545551
---@private

tests/table_spec.lua

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,18 @@
33
local util = require('tests.util')
44

55
describe('table', function()
6+
---@param columns integer
7+
---@param callback function
8+
local function with_columns(columns, callback)
9+
local previous = vim.o.columns
10+
vim.o.columns = columns
11+
local ok, err = pcall(callback)
12+
vim.o.columns = previous
13+
if not ok then
14+
error(err, 0)
15+
end
16+
end
17+
618
local lines = {
719
'',
820
'| Heading 1 | `Heading 2` |',
@@ -302,6 +314,37 @@ describe('table', function()
302314
})
303315
end)
304316

317+
it('wrapped indented table', function()
318+
with_columns(60, function()
319+
util.setup.text({
320+
'',
321+
' | Approach | Allocations | Performance |',
322+
' |----------|-------------|-------------|',
323+
' | `format!()` in loop | N | Slow |',
324+
' | `write!()` to reused buffer | 1 | Fast |',
325+
' | `push_str()` + `push()` | 1 | Fastest |',
326+
' | Pre-sized `String::with_capacity()` | 1 (no realloc) | Fast |',
327+
}, {
328+
pipe_table = { max_table_width = -2 },
329+
win_options = { wrap = { default = false, rendered = true } },
330+
})
331+
332+
util.assert_screen({
333+
' ┌────────────────────┬────────────────┬───────────────┐',
334+
' │ Approach │ Allocations │ Performance │',
335+
' ├────────────────────┼────────────────┼───────────────┤',
336+
' │ format!() in loop │ N │ Slow │',
337+
' │ write!() to reused │ 1 │ Fast │',
338+
' │ buffer │ │ │',
339+
' │ push_str() + push( │ 1 │ Fastest │',
340+
' │ ) │ │ │',
341+
' │ Pre-sized String:: │ 1 (no realloc) │ Fast │',
342+
' │ with_capacity() │ │ │',
343+
' └────────────────────┴────────────────┴───────────────┘',
344+
})
345+
end)
346+
end)
347+
305348
it('wrapped long delimiter', function()
306349
util.setup.text({
307350
'',

0 commit comments

Comments
 (0)