Skip to content

Commit 969b742

Browse files
committed
fix(table): align cells in wrapped tables
Both the wrapped and regular cells.
1 parent 24a7b97 commit 969b742

2 files changed

Lines changed: 61 additions & 3 deletions

File tree

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

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -712,9 +712,22 @@ function Render:row_wrapped_lines(row, row_index)
712712
local col_width = self.wrap_layout.col_widths[i]
713713
line:text(border_icon, highlight)
714714
line:pad(padding, filler)
715-
local chunk = cell_lines[i][visual_line + 1] or Line.new(filler)
716-
line:extend(chunk)
717-
line:pad(col_width - chunk:width(), filler)
715+
local chunks = cell_lines[i]
716+
local chunk = chunks[visual_line + 1] or Line.new(filler)
717+
local fill = col_width - chunk:width()
718+
local alignment = self.data.cols[i].alignment
719+
if alignment == Alignment.center then
720+
local left = math.floor(fill / 2)
721+
line:pad(left, filler)
722+
line:extend(chunk)
723+
line:pad(fill - left, filler)
724+
elseif alignment == Alignment.right then
725+
line:pad(fill, filler)
726+
line:extend(chunk)
727+
else
728+
line:extend(chunk)
729+
line:pad(fill, filler)
730+
end
718731
line:pad(padding, filler)
719732
end
720733
line:text(border_icon, highlight)

tests/table_spec.lua

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,51 @@ describe('table', function()
377377
end)
378378
end)
379379

380+
it('wrapped aligns unwrapped cells', function()
381+
util.setup.text({
382+
'',
383+
'|Left|Right|Center|Long|',
384+
'|:---|----:|:----:|----|',
385+
'|a|2|mid|one two three four five six seven|',
386+
}, {
387+
pipe_table = { max_table_width = 40 },
388+
win_options = { wrap = { default = false, rendered = true } },
389+
})
390+
391+
util.assert_screen({
392+
'┌──────┬───────┬────────┬──────────────┐',
393+
'│ Left │ Right │ Center │ Long │',
394+
'├━─────┼──────━┼━──────━┼──────────────┤',
395+
'│ a │ 2 │ mid │ one two │',
396+
'│ │ │ │ three four │',
397+
'│ │ │ │ five six │',
398+
'│ │ │ │ seven │',
399+
'└──────┴───────┴────────┴──────────────┘',
400+
})
401+
end)
402+
403+
it('wrapped aligns wrapped cells', function()
404+
util.setup.text({
405+
'',
406+
'| L | R | C |',
407+
'|:--|--:|:-:|',
408+
'| a | one two three four five six | one two three four five six |',
409+
}, {
410+
pipe_table = { max_table_width = 40 },
411+
win_options = { wrap = { default = false, rendered = true } },
412+
})
413+
414+
util.assert_screen({
415+
'┌─────┬───────────────┬───────────────┐',
416+
'│ L │ R │ C │',
417+
'├━────┼──────────────━┼━─────────────━┤',
418+
'│ a │ one two │ one two │',
419+
'│ │ three four │ three four │',
420+
'│ │ five six │ five six │',
421+
'└─────┴───────────────┴───────────────┘',
422+
})
423+
end)
424+
380425
it('wrapped long delimiter', function()
381426
util.setup.text({
382427
'',

0 commit comments

Comments
 (0)