Skip to content

Commit a3617d6

Browse files
fix: indented tables and tables with no spaces in cells
## Details Issue: #142 This resolves 2 separate issues. The first is for tables that are indented some amount space over. In such cases the top & bottom full sections as well as the delimitter would be misaligned since they did not take into account any leading spaces and generate full lines rather than changing individual parts which is the case for rows. To fix this add logic to get the amount of leading space and shift these components over accordingly. The second is for tables where the contents of a cell went all the way to the right pipe with no spaces. This would cause the padding to be inserted in the middle of the content rather than at the end. To fix this adjust the column associated with padding to no longer subtract one off. Use a priority value of 0 for the padding marks so the pipe marks pickup the padding as well, otherwise they would end up ignoring it.
1 parent 0a8b941 commit a3617d6

File tree

6 files changed

+39
-20
lines changed

6 files changed

+39
-20
lines changed

Diff for: benches/readme_spec.lua

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ describe('README.md', function()
1111
util.less_than(util.move_down(1), 0.5)
1212
util.num_marks(base_marks + 2)
1313

14-
util.less_than(util.insert_mode(), 5)
14+
util.less_than(util.insert_mode(), 20)
1515
util.num_marks(base_marks + 2)
1616
end)
1717
end)

Diff for: 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 August 21
1+
*render-markdown.txt* For 0.10.0 Last change: 2024 August 22
22

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

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

+28-9
Original file line numberDiff line numberDiff line change
@@ -570,6 +570,7 @@ function Handler:table_delimiter(delim)
570570
local pipe_table = self.config.pipe_table
571571
local indicator = pipe_table.alignment_indicator
572572
local border = pipe_table.border
573+
573574
local sections = vim.tbl_map(function(column)
574575
-- If column is small there's no good place to put the alignment indicator
575576
-- Alignment indicator must be exactly one character wide
@@ -588,11 +589,18 @@ function Handler:table_delimiter(delim)
588589
return left .. indicator .. right
589590
end
590591
end, delim.columns)
591-
local delimiter = border[4] .. table.concat(sections, border[5]) .. border[6]
592+
593+
local virt_text = {}
594+
local leading_spaces = str.leading_spaces(delim.info.text)
595+
if leading_spaces > 0 then
596+
table.insert(virt_text, { str.spaces(leading_spaces), 'Normal' })
597+
end
598+
table.insert(virt_text, { border[4] .. table.concat(sections, border[5]) .. border[6], pipe_table.head })
599+
592600
self:add(true, delim.info.start_row, delim.info.start_col, {
593601
end_row = delim.info.end_row,
594602
end_col = delim.info.end_col,
595-
virt_text = { { delimiter, pipe_table.head } },
603+
virt_text = virt_text,
596604
virt_text_pos = 'overlay',
597605
})
598606
end
@@ -615,7 +623,9 @@ function Handler:table_row(delim, row)
615623
for i, column in ipairs(row.columns) do
616624
local offset = delim.columns[i].width - column.width
617625
if offset > 0 then
618-
self:add(true, column.info.start_row, column.info.end_col - 1, {
626+
-- Use low priority to include pipe marks in padding
627+
self:add(true, column.info.start_row, column.info.end_col, {
628+
priority = 0,
619629
virt_text = { { str.spaces(offset), pipe_table.filler } },
620630
virt_text_pos = 'inline',
621631
})
@@ -669,28 +679,37 @@ function Handler:table_full(parsed_table)
669679
end
670680
end
671681

682+
---@param row render.md.parsed.table.Row
683+
---@return integer
684+
local function get_spaces(row)
685+
return math.max(str.leading_spaces(row.info.text), row.info.start_col)
686+
end
687+
672688
local delim = parsed_table.delim
673689
local first = parsed_table.rows[1]
674690
local last = parsed_table.rows[#parsed_table.rows]
675691
if not width_equal(first, delim) or not width_equal(last, delim) then
676692
return
677693
end
678694

695+
local spaces = get_spaces(first)
696+
if spaces ~= get_spaces(last) then
697+
return
698+
end
699+
679700
local sections = vim.tbl_map(function(column)
680701
return border[11]:rep(column.width)
681702
end, delim.columns)
682703

683-
local line_above = {
684-
{ border[1] .. table.concat(sections, border[2]) .. border[3], pipe_table.head },
685-
}
704+
local line_above = spaces > 0 and { { str.spaces(spaces), 'Normal' } } or {}
705+
table.insert(line_above, { border[1] .. table.concat(sections, border[2]) .. border[3], pipe_table.head })
686706
self:add(false, first.info.start_row, first.info.start_col, {
687707
virt_lines_above = true,
688708
virt_lines = { self:indent_virt_line(parsed_table.info, line_above) },
689709
})
690710

691-
local line_below = {
692-
{ border[7] .. table.concat(sections, border[8]) .. border[9], pipe_table.row },
693-
}
711+
local line_below = spaces > 0 and { { str.spaces(spaces), 'Normal' } } or {}
712+
table.insert(line_below, { border[7] .. table.concat(sections, border[8]) .. border[9], pipe_table.row })
694713
self:add(false, last.info.start_row, last.info.start_col, {
695714
virt_lines_above = false,
696715
virt_lines = { self:indent_virt_line(parsed_table.info, line_below) },

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.2.0'
8+
M.version = '6.2.1'
99

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

Diff for: tests/list_table_spec.lua

+5-5
Original file line numberDiff line numberDiff line change
@@ -55,21 +55,21 @@ describe('list_table.md', function()
5555
util.table_pipe(row:increment(2), 0, true),
5656
util.table_border(row:get(), 'above', { 8, 15, 7, 6 }),
5757
util.inline_code(row:get(), 2, 8),
58-
util.table_padding(row:get(), 8, 2),
5958
util.table_pipe(row:get(), 9, true),
60-
util.table_padding(row:get(), 24, 2),
59+
util.table_padding(row:get(), 9, 2),
6160
util.table_pipe(row:get(), 25, true),
61+
util.table_padding(row:get(), 25, 2),
6262
util.table_pipe(row:get(), 33, true),
6363
util.table_pipe(row:get(), 40, true),
6464
})
6565
table.insert(expected, delimiter(row:increment(), { { 1, 7 }, { 7, 1, 7 }, { 6, 1 }, { 6 } }))
6666
vim.list_extend(expected, {
6767
util.table_pipe(row:increment(), 0, false),
6868
util.inline_code(row:get(), 2, 8),
69-
util.table_padding(row:get(), 8, 2),
7069
util.table_pipe(row:get(), 9, false),
71-
util.table_padding(row:get(), 24, 4),
70+
util.table_padding(row:get(), 9, 2),
7271
util.table_pipe(row:get(), 25, false),
72+
util.table_padding(row:get(), 25, 4),
7373
util.table_pipe(row:get(), 33, false),
7474
util.table_pipe(row:get(), 40, false),
7575
})
@@ -78,8 +78,8 @@ describe('list_table.md', function()
7878
util.table_border(row:get(), 'below', { 8, 15, 7, 6 }),
7979
util.table_pipe(row:get(), 9, false),
8080
util.link(row:get(), 11, 24, 'link'),
81-
util.table_padding(row:get(), 24, 7),
8281
util.table_pipe(row:get(), 25, false),
82+
util.table_padding(row:get(), 25, 7),
8383
util.table_pipe(row:get(), 33, false),
8484
util.table_pipe(row:get(), 40, false),
8585
})

Diff for: tests/table_spec.lua

+3-3
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,17 @@ describe('table.md', function()
1414
util.table_border(row:get(), 'above', { 11, 24 }),
1515
util.table_pipe(row:get(), 12, true),
1616
util.inline_code(row:get(), 14, 25),
17-
util.table_padding(row:get(), 36, 2),
1817
util.table_pipe(row:get(), 37, true),
18+
util.table_padding(row:get(), 37, 2),
1919
util.table_border(row:increment(), 'delimiter', { 11, 24 }),
2020
util.table_pipe(row:increment(), 0, false),
2121
util.table_border(row:get(), 'below', { 11, 24 }),
2222
util.inline_code(row:get(), 2, 12),
23-
util.table_padding(row:get(), 12, 2),
2423
util.table_pipe(row:get(), 13, false),
24+
util.table_padding(row:get(), 13, 2),
2525
util.link(row:get(), 15, 38, 'web'),
26-
util.table_padding(row:get(), 38, 16),
2726
util.table_pipe(row:get(), 39, false),
27+
util.table_padding(row:get(), 39, 16),
2828
})
2929

3030
vim.list_extend(expected, {

0 commit comments

Comments
 (0)