Skip to content

Commit 430a671

Browse files
fix: handle conceal level 2 entities in tables
## Details Concealed text like > which gets replaced with > at conceal level 2 was not properly accounted for when rendering tables. Add logic to take into account the conceal character width when using conceal level 2.
1 parent 7f0143e commit 430a671

File tree

5 files changed

+53
-32
lines changed

5 files changed

+53
-32
lines changed

doc/render-markdown.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
*render-markdown.txt* For 0.10.0 Last change: 2024 November 17
1+
*render-markdown.txt* For 0.10.0 Last change: 2024 November 18
22

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

lua/render-markdown/core/context.lua

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ local util = require('render-markdown.core.util')
77
---@class render.md.context.Conceal
88
---@field enabled boolean
99
---@field block integer
10-
---@field rows? table<integer, [integer, integer][]>
10+
---@field char boolean
11+
---@field rows? table<integer, [integer, integer, integer][]>
1112

1213
---@class render.md.Context
1314
---@field private buf integer
@@ -50,6 +51,8 @@ function Context.new(buf, win, mode, offset)
5051
enabled = conceal_level > 0,
5152
-- At conceal level 1 each block is replaced with one character
5253
block = conceal_level == 1 and 1 or 0,
54+
-- At conceal level 2 replacement character width is used
55+
char = conceal_level == 2,
5356
rows = nil,
5457
}
5558

@@ -230,7 +233,7 @@ function Context:concealed(node)
230233
if col >= range[1] and col + 1 <= range[2] then
231234
result = result + Str.width(ch)
232235
if col == range[1] then
233-
result = result - self.conceal.block
236+
result = result - self.conceal.block - range[3]
234237
end
235238
end
236239
end
@@ -251,7 +254,7 @@ end
251254

252255
---Cached row level implementation of vim.treesitter.get_captures_at_pos
253256
---@private
254-
---@return table<integer, [integer, integer][]>
257+
---@return table<integer, [integer, integer, integer][]>
255258
function Context:compute_conceal()
256259
if not self.conceal.enabled then
257260
return {}
@@ -262,11 +265,11 @@ function Context:compute_conceal()
262265
parser:for_each_tree(function(tree, language_tree)
263266
local conceal_ranges = self:compute_conceal_ranges(language_tree:lang(), tree:root())
264267
for _, conceal_range in ipairs(conceal_ranges) do
265-
local row, start_col, end_col = unpack(conceal_range)
268+
local row, start_col, end_col, char = unpack(conceal_range)
266269
if ranges[row] == nil then
267270
ranges[row] = {}
268271
end
269-
table.insert(ranges[row], { start_col, end_col })
272+
table.insert(ranges[row], { start_col, end_col, self.conceal.char and Str.width(char) or 0 })
270273
end
271274
end)
272275
return ranges
@@ -275,7 +278,7 @@ end
275278
---@private
276279
---@param language string
277280
---@param root TSNode
278-
---@return [integer, integer, integer][]
281+
---@return [integer, integer, integer, string][]
279282
function Context:compute_conceal_ranges(language, root)
280283
if not self:overlaps_node(root) then
281284
return {}
@@ -300,7 +303,7 @@ function Context:compute_conceal_ranges(language, root)
300303
node_range = { ts_node:range() }
301304
end
302305
local row, start_col, _, end_col = unpack(node_range)
303-
table.insert(result, { row, start_col, end_col })
306+
table.insert(result, { row, start_col, end_col, metadata.conceal })
304307
end
305308
end
306309
end

lua/render-markdown/health.lua

Lines changed: 1 addition & 1 deletion
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.7'
7+
M.version = '7.5.8'
88

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

tests/data/table.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@
66

77
# Table no Inline
88

9-
| Heading 1 | Heading 2 |
9+
| &lt;Heading 1&gt; | Heading 2 |
1010
| --------- | --------- |
1111
| Item 1 | Item 2 |

tests/table_spec.lua

Lines changed: 40 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ describe('table.md', function()
3232
util.heading(row:increment(2), 1),
3333
util.table_border(row:increment(2), true, { 11, 11 }),
3434
util.table_pipe(row:get(), 0, true),
35-
util.table_pipe(row:get(), 12, true),
36-
util.table_pipe(row:get(), 24, true),
35+
util.table_pipe(row:get(), 20, true),
36+
util.table_pipe(row:get(), 32, true),
3737
util.table_delimiter(row:increment(), { 11, 11 }),
3838
util.table_pipe(row:increment(), 0, false),
3939
util.table_pipe(row:get(), 12, false),
@@ -88,8 +88,8 @@ describe('table.md', function()
8888
util.heading(row:increment(2), 1),
8989
util.table_border(row:increment(2), true, { 11, 11 }),
9090
util.table_pipe(row:get(), 0, true),
91-
util.table_pipe(row:get(), 12, true),
92-
util.table_pipe(row:get(), 24, true),
91+
util.table_pipe(row:get(), 20, true),
92+
util.table_pipe(row:get(), 32, true),
9393
util.table_delimiter(row:increment(), { 11, 11 }),
9494
util.table_pipe(row:increment(), 0, false),
9595
util.table_pipe(row:get(), 12, false),
@@ -139,8 +139,8 @@ describe('table.md', function()
139139
util.heading(row:increment(2), 1),
140140
util.table_border(row:increment(2), true, { 11, 11 }),
141141
util.table_pipe(row:get(), 0, true),
142-
util.table_pipe(row:get(), 12, true),
143-
util.table_pipe(row:get(), 24, true),
142+
util.table_pipe(row:get(), 20, true),
143+
util.table_pipe(row:get(), 32, true),
144144
util.table_delimiter(row:increment(), { 11, 11 }),
145145
util.table_pipe(row:increment(), 0, false),
146146
util.table_pipe(row:get(), 12, false),
@@ -200,11 +200,9 @@ describe('table.md', function()
200200

201201
vim.list_extend(expected, {
202202
util.heading(row:increment(2), 1),
203-
util.table_border(row:increment(2), true, { 11, 11 }),
204-
table_row(row:get(), 25, '│ Heading 1 │ Heading 2 │', true),
203+
table_row(row:increment(2), 33, '│ &lt;Heading 1&gt; │ Heading 2 │', true),
205204
util.table_delimiter(row:increment(), { 11, 11 }),
206205
table_row(row:increment(), 25, '│ Item 1 │ Item 2 │', false),
207-
util.table_border(row:get(), false, { 11, 11 }),
208206
})
209207

210208
util.assert_view(expected, {
@@ -218,11 +216,9 @@ describe('table.md', function()
218216
' 6',
219217
'󰫎 7 󰲡 Table no Inline',
220218
' 8',
221-
' ┌───────────┬───────────┐',
222-
' 9 │ Heading 1 │ Heading 2 │',
219+
' 9 │ &lt;Heading 1&gt; │ Heading 2 │',
223220
' 10 ├───────────┼───────────┤',
224221
' 11 │ Item 1 │ Item 2 │',
225-
' └───────────┴───────────┘',
226222
})
227223
end)
228224

@@ -240,11 +236,11 @@ describe('table.md', function()
240236
' 6',
241237
'󰫎 7 󰲡 Table no Inline',
242238
' 8',
243-
' ┌───────────┬───────────┐',
244-
' 9 │ Heading 1 │ Heading 2 │',
245-
' 10 ├───────────┼───────────┤',
246-
' 11 │ Item 1 │ Item 2 │',
247-
' └───────────┴───────────┘',
239+
' ┌───────────────────┬───────────┐',
240+
' 9 │ &lt;Heading 1&gt; │ Heading 2 │',
241+
' 10 ├───────────────────┼───────────┤',
242+
' 11 │ Item 1 │ Item 2 │',
243+
' └───────────────────┴───────────┘',
248244
})
249245
end)
250246

@@ -262,11 +258,33 @@ describe('table.md', function()
262258
' 6',
263259
'󰫎 7 󰲡 Table no Inline',
264260
' 8',
265-
' ┌───────────┬───────────┐',
266-
' 9 │ Heading 1 │ Heading 2 │',
267-
' 10 ├───────────┼───────────┤',
268-
' 11 │ Item 1 │ Item 2 │',
269-
' └───────────┴───────────┘',
261+
' ┌─────────────┬───────────┐',
262+
' 9 │ <Heading 1> │ Heading 2 │',
263+
' 10 ├─────────────┼───────────┤',
264+
' 11 │ Item 1 │ Item 2 │',
265+
' └─────────────┴───────────┘',
266+
})
267+
end)
268+
269+
it('conceallevel 2', function()
270+
util.setup('tests/data/table.md', { win_options = { conceallevel = { rendered = 2 } } })
271+
272+
util.assert_screen({
273+
'󰫎 1 󰲡 Table with Inline',
274+
' 2',
275+
' ┌───────────┬────────────────────────┐',
276+
' 3 │ Heading 1 │ Heading 2 │',
277+
' 4 ├───────────┼───────────────────────━┤',
278+
' 5 │ Item 行 │ 󰖟 link │',
279+
' └───────────┴────────────────────────┘',
280+
' 6',
281+
'󰫎 7 󰲡 Table no Inline',
282+
' 8',
283+
' ┌─────────────┬───────────┐',
284+
' 9 │ <Heading 1> │ Heading 2 │',
285+
' 10 ├─────────────┼───────────┤',
286+
' 11 │ Item 1 │ Item 2 │',
287+
' └─────────────┴───────────┘',
270288
})
271289
end)
272290
end)

0 commit comments

Comments
 (0)