Skip to content

Commit e5c3c50

Browse files
feat: add cell_offset function to pipe_table configuration
## Details Adds `pipe_table.cell_offset`, which can be used to adjust the computed width of table cells based on more user specific behavior. This will be added directly to the width (can be negative to subtract). The computed value is not fact checked in any way so if the offset provided results in distorted rendering it is up to the user to fix it. Existing logic takes care of treesitter highlighting behavior and offsets caused by `extmark`s from this plugin. Other plugins are not accounted for as the behavior is not consistent across all plugins, this is likely the primary usage of this feature.
1 parent ca86b59 commit e5c3c50

File tree

6 files changed

+47
-12
lines changed

6 files changed

+47
-12
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -674,6 +674,10 @@ require('render-markdown').setup({
674674
-- | padded | raw + cells are padded to maximum visual width for each column |
675675
-- | trimmed | padded except empty space is subtracted from visual width calculation |
676676
cell = 'padded',
677+
-- Adjust the computed width of table cells using custom logic.
678+
cell_offset = function()
679+
return 0
680+
end,
677681
-- Amount of space to put between cell contents and border.
678682
padding = 1,
679683
-- Minimum column width to use for padded or trimmed cell.
@@ -1373,6 +1377,10 @@ require('render-markdown').setup({
13731377
-- | padded | raw + cells are padded to maximum visual width for each column |
13741378
-- | trimmed | padded except empty space is subtracted from visual width calculation |
13751379
cell = 'padded',
1380+
-- Adjust the computed width of table cells using custom logic.
1381+
cell_offset = function()
1382+
return 0
1383+
end,
13761384
-- Amount of space to put between cell contents and border.
13771385
padding = 1,
13781386
-- Minimum column width to use for padded or trimmed cell.

doc/render-markdown.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -740,6 +740,10 @@ Default Configuration ~
740740
-- | padded | raw + cells are padded to maximum visual width for each column |
741741
-- | trimmed | padded except empty space is subtracted from visual width calculation |
742742
cell = 'padded',
743+
-- Adjust the computed width of table cells using custom logic.
744+
cell_offset = function()
745+
return 0
746+
end,
743747
-- Amount of space to put between cell contents and border.
744748
padding = 1,
745749
-- Minimum column width to use for padded or trimmed cell.
@@ -1423,6 +1427,10 @@ Table Configuration ~
14231427
-- | padded | raw + cells are padded to maximum visual width for each column |
14241428
-- | trimmed | padded except empty space is subtracted from visual width calculation |
14251429
cell = 'padded',
1430+
-- Adjust the computed width of table cells using custom logic.
1431+
cell_offset = function()
1432+
return 0
1433+
end,
14261434
-- Amount of space to put between cell contents and border.
14271435
padding = 1,
14281436
-- Minimum column width to use for padded or trimmed cell.

lua/render-markdown/config/pipe_table.lua

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
---@class (exact) render.md.table.Config: render.md.base.Config
22
---@field preset render.md.table.Preset
33
---@field cell render.md.table.Cell
4+
---@field cell_offset fun(ctx: render.md.table.cell.Context): integer
45
---@field padding integer
56
---@field min_width integer
67
---@field border string[]
@@ -12,6 +13,9 @@
1213
---@field filler string
1314
---@field style render.md.table.Style
1415

16+
---@class (exact) render.md.table.cell.Context
17+
---@field node TSNode
18+
1519
---@enum render.md.table.Preset
1620
local Preset = {
1721
none = 'none',
@@ -56,6 +60,10 @@ M.default = {
5660
-- | padded | raw + cells are padded to maximum visual width for each column |
5761
-- | trimmed | padded except empty space is subtracted from visual width calculation |
5862
cell = 'padded',
63+
-- Adjust the computed width of table cells using custom logic.
64+
cell_offset = function()
65+
return 0
66+
end,
5967
-- Amount of space to put between cell contents and border.
6068
padding = 1,
6169
-- Minimum column width to use for padded or trimmed cell.
@@ -94,6 +102,7 @@ function M.validate(spec)
94102
require('render-markdown.config.base').validate(spec)
95103
spec:one_of('preset', vim.tbl_values(Preset))
96104
spec:one_of('cell', vim.tbl_values(Cell))
105+
spec:type('cell_offset', 'function')
97106
spec:type('padding', 'number')
98107
spec:type('min_width', 'number')
99108
spec:list('border', 'string')

lua/render-markdown/health.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ local state = require('render-markdown.state')
55
local M = {}
66

77
---@private
8-
M.version = '8.7.6'
8+
M.version = '8.7.7'
99

1010
function M.check()
1111
M.start('versions')

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

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -171,10 +171,10 @@ function Render:parse_row(node, num_cols)
171171
for i, cell in ipairs(cells) do
172172
-- account for double width glyphs by replacing cell range with width
173173
local start_col, end_col = pipes[i].end_col, pipes[i + 1].start_col
174-
local width = end_col - start_col
175-
width = width
174+
local width = (end_col - start_col)
176175
- (cell.end_col - cell.start_col)
177176
+ self.context:width(cell)
177+
+ self.config.cell_offset({ node = cell:get() })
178178
assert(width >= 0, 'invalid table layout')
179179
cols[#cols + 1] = {
180180
row = cell.start_row,
@@ -281,23 +281,32 @@ function Render:row(row)
281281

282282
if vim.tbl_contains({ 'trimmed', 'padded' }, self.config.cell) then
283283
for i, col in ipairs(row.cols) do
284-
local delim_col = self.data.delim.cols[i]
285-
-- amount of space needed to get column to target width
286-
local fill = delim_col.width - col.width
284+
local delim = self.data.delim.cols[i]
285+
local space = col.space
286+
local fill = delim.width - col.width
287+
-- delim(20) : --------------------
288+
-- col(4,7,2): ----XXXXXXX--
289+
-- fill(7) : _______
287290
if not self.context.conceal:enabled() then
291+
-- result: ----XXXXXXX--_______
288292
-- without concealing it is impossible to do full alignment
289293
self:shift(col, 'right', fill)
290-
elseif delim_col.alignment == Alignment.center then
291-
local shift =
292-
math.floor((fill + col.space.right - col.space.left) / 2)
294+
elseif delim.alignment == Alignment.center then
295+
-- (7 + 2 - 4) // 2 = 5 // 2 = 2 -> move two spaces to the right
296+
-- result: __----XXXXXXX--_____
297+
local shift = math.floor((fill + space.right - space.left) / 2)
293298
self:shift(col, 'left', shift)
294299
self:shift(col, 'right', fill - shift)
295-
elseif delim_col.alignment == Alignment.right then
296-
local shift = col.space.right - self.config.padding
300+
elseif delim.alignment == Alignment.right then
301+
-- 2 - 1 = 1 -> conceal one space on right side
302+
-- result: -_______----XXXXXXX-
303+
local shift = space.right - self.config.padding
297304
self:shift(col, 'left', fill + shift)
298305
self:shift(col, 'right', -shift)
299306
else
300-
local shift = col.space.left - self.config.padding
307+
-- 4 - 1 = 3 -> conceal three spaces on left side
308+
-- result: -XXXXXXX--_______---
309+
local shift = space.left - self.config.padding
301310
self:shift(col, 'left', -shift)
302311
self:shift(col, 'right', fill + shift)
303312
end

lua/render-markdown/types.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,7 @@
257257
---@class (exact) render.md.table.UserConfig: render.md.base.UserConfig
258258
---@field preset? render.md.table.Preset
259259
---@field cell? render.md.table.Cell
260+
---@field cell_offset? fun(ctx: render.md.table.cell.Context): integer
260261
---@field padding? integer
261262
---@field min_width? integer
262263
---@field border? string[]

0 commit comments

Comments
 (0)