Skip to content

Commit 65b263d

Browse files
committedJan 2, 2025
feat: padding for inline code
## Details Request: #274 Adds `inline_pad` option to `code` component which is used to add spaces on both sides of inline code blocks. In order to make offset calculations work correctly needed to make some minor adjustments to the logic: - Marks that do not specify an end use the start as the end rather than being ignored when adding offset to context - A node overlaps with a range if its start is <= the end of the range, not strictly <, this is because ends are inclusive for these ranges
1 parent 6fbd149 commit 65b263d

File tree

11 files changed

+59
-31
lines changed

11 files changed

+59
-31
lines changed
 

Diff for: ‎README.md

+8-4
Original file line numberDiff line numberDiff line change
@@ -381,10 +381,12 @@ require('render-markdown').setup({
381381
below = '',
382382
-- Highlight for code blocks
383383
highlight = 'RenderMarkdownCode',
384-
-- Highlight for inline code
385-
highlight_inline = 'RenderMarkdownCodeInline',
386384
-- Highlight for language, overrides icon provider value
387385
highlight_language = nil,
386+
-- Padding to add to the left & right of inline code
387+
inline_pad = 0,
388+
-- Highlight for inline code
389+
highlight_inline = 'RenderMarkdownCodeInline',
388390
},
389391
dash = {
390392
-- Turn on / off thematic break rendering
@@ -860,10 +862,12 @@ require('render-markdown').setup({
860862
below = '',
861863
-- Highlight for code blocks
862864
highlight = 'RenderMarkdownCode',
863-
-- Highlight for inline code
864-
highlight_inline = 'RenderMarkdownCodeInline',
865865
-- Highlight for language, overrides icon provider value
866866
highlight_language = nil,
867+
-- Padding to add to the left & right of inline code
868+
inline_pad = 0,
869+
-- Highlight for inline code
870+
highlight_inline = 'RenderMarkdownCodeInline',
867871
},
868872
})
869873
```

Diff for: ‎doc/render-markdown.txt

+9-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
*render-markdown.txt* For 0.10.0 Last change: 2024 December 29
1+
*render-markdown.txt* For 0.10.0 Last change: 2025 January 02
22

33
==============================================================================
44
Table of Contents *render-markdown-table-of-contents*
@@ -437,10 +437,12 @@ Default Configuration ~
437437
below = '▀',
438438
-- Highlight for code blocks
439439
highlight = 'RenderMarkdownCode',
440-
-- Highlight for inline code
441-
highlight_inline = 'RenderMarkdownCodeInline',
442440
-- Highlight for language, overrides icon provider value
443441
highlight_language = nil,
442+
-- Padding to add to the left & right of inline code
443+
inline_pad = 0,
444+
-- Highlight for inline code
445+
highlight_inline = 'RenderMarkdownCodeInline',
444446
},
445447
dash = {
446448
-- Turn on / off thematic break rendering
@@ -910,10 +912,12 @@ Code Block Configuration ~
910912
below = '▀',
911913
-- Highlight for code blocks
912914
highlight = 'RenderMarkdownCode',
913-
-- Highlight for inline code
914-
highlight_inline = 'RenderMarkdownCodeInline',
915915
-- Highlight for language, overrides icon provider value
916916
highlight_language = nil,
917+
-- Padding to add to the left & right of inline code
918+
inline_pad = 0,
919+
-- Highlight for inline code
920+
highlight_inline = 'RenderMarkdownCodeInline',
917921
},
918922
})
919923
<

Diff for: ‎lua/render-markdown/core/context.lua

+1-1
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ function Context:get_offset(node)
127127
local result = 0
128128
local ranges = self.offsets[node.start_row] or {}
129129
for _, range in ipairs(ranges) do
130-
if node.start_col < range[2] and node.end_col > range[1] then
130+
if node.start_col <= range[2] and node.end_col > range[1] then
131131
result = result + range[3]
132132
end
133133
end

Diff for: ‎lua/render-markdown/health.lua

+1-1
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 = '7.7.7'
8+
M.version = '7.7.8'
99

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

Diff for: ‎lua/render-markdown/init.lua

+6-3
Original file line numberDiff line numberDiff line change
@@ -161,8 +161,9 @@ local M = {}
161161
---@field public above? string
162162
---@field public below? string
163163
---@field public highlight? string
164-
---@field public highlight_inline? string
165164
---@field public highlight_language? string
165+
---@field public inline_pad? integer
166+
---@field public highlight_inline? string
166167

167168
---@class (exact) render.md.UserParagraph: render.md.UserBaseComponent
168169
---@field public left_margin? number
@@ -470,10 +471,12 @@ M.default_config = {
470471
below = '',
471472
-- Highlight for code blocks
472473
highlight = 'RenderMarkdownCode',
473-
-- Highlight for inline code
474-
highlight_inline = 'RenderMarkdownCodeInline',
475474
-- Highlight for language, overrides icon provider value
476475
highlight_language = nil,
476+
-- Padding to add to the left & right of inline code
477+
inline_pad = 0,
478+
-- Highlight for inline code
479+
highlight_inline = 'RenderMarkdownCodeInline',
477480
},
478481
dash = {
479482
-- Turn on / off thematic break rendering

Diff for: ‎lua/render-markdown/lib/list.lua

+1-4
Original file line numberDiff line numberDiff line change
@@ -90,11 +90,8 @@ end
9090
---@private
9191
---@param mark render.md.Mark
9292
function Marks:update_context(mark)
93-
local end_col = mark.opts.end_col
94-
if end_col == nil then
95-
return
96-
end
9793
local row, start_col = mark.start_row, mark.start_col
94+
local end_col = mark.opts.end_col or start_col
9895
if mark.opts.conceal ~= nil then
9996
self.context.conceal:add(row, start_col, end_col, end_col - start_col)
10097
end

Diff for: ‎lua/render-markdown/manager.lua

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ function M.setup()
2121
M.attach(args.buf)
2222
end,
2323
})
24-
-- Window resizing is not buffer specific so is managed more globablly
24+
-- Window resizing is not buffer specific so is managed more globally
2525
vim.api.nvim_create_autocmd('WinResized', {
2626
group = M.group,
2727
callback = function(args)

Diff for: ‎lua/render-markdown/render/code_inline.lua

+16
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,22 @@ function Render:render()
1818
self.marks:add_over('code_background', self.node, {
1919
hl_group = self.code.highlight_inline,
2020
})
21+
self:side_padding(self.node.start_row, self.node.start_col)
22+
self:side_padding(self.node.end_row, self.node.end_col)
23+
end
24+
25+
---@private
26+
---@param row integer
27+
---@param col integer
28+
function Render:side_padding(row, col)
29+
local padding = self.code.inline_pad
30+
if padding > 0 then
31+
self.marks:add(true, row, col, {
32+
priority = 0,
33+
virt_text = { self:padding_text(padding) },
34+
virt_text_pos = 'inline',
35+
})
36+
end
2137
end
2238

2339
return Render

Diff for: ‎lua/render-markdown/state.lua

+1
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,7 @@ function M.validate()
176176
component_rules(code)
177177
:type({ 'sign', 'language_name' }, 'boolean')
178178
:type({ 'language_pad', 'left_margin', 'left_pad', 'right_pad', 'min_width' }, 'number')
179+
:type('inline_pad', 'number')
179180
:type({ 'above', 'below', 'highlight', 'highlight_inline' }, 'string')
180181
:type('highlight_language', { 'string', 'nil' })
181182
:list('disable_background', 'string', 'boolean')

Diff for: ‎lua/render-markdown/types.lua

+2-1
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,9 @@
132132
---@field public above string
133133
---@field public below string
134134
---@field public highlight string
135-
---@field public highlight_inline string
136135
---@field public highlight_language? string
136+
---@field public inline_pad integer
137+
---@field public highlight_inline string
137138

138139
---@class (exact) render.md.Paragraph: render.md.BaseComponent
139140
---@field public left_margin number

Diff for: ‎tests/util.lua

+13-11
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,17 @@ end
3838
---@field sign_text? string
3939
---@field sign_hl_group? string
4040
---@field priority? integer
41-
local MarkInfo = {}
42-
MarkInfo.__index = MarkInfo
41+
42+
---@class render.md.MarkDetails: render.md.MarkInfo
43+
local MarkDetails = {}
44+
MarkDetails.__index = MarkDetails
4345

4446
---@param row integer
4547
---@param col integer
4648
---@param details vim.api.keyset.extmark_details
47-
---@return render.md.MarkInfo
48-
function MarkInfo.new(row, col, details)
49-
local self = setmetatable({}, MarkInfo)
49+
---@return render.md.MarkDetails
50+
function MarkDetails.new(row, col, details)
51+
local self = setmetatable({}, MarkDetails)
5052
self.row = { row, details.end_row }
5153
self.col = { col, details.end_col }
5254
self.hl_eol = details.hl_eol
@@ -67,7 +69,7 @@ function MarkInfo.new(row, col, details)
6769
end
6870

6971
---@return integer[]
70-
function MarkInfo:priorities()
72+
function MarkDetails:priorities()
7173
local result = {}
7274

7375
local row_offset = 0
@@ -87,10 +89,10 @@ function MarkInfo:priorities()
8789
return result
8890
end
8991

90-
---@param a render.md.MarkInfo
91-
---@param b render.md.MarkInfo
92+
---@param a render.md.MarkDetails
93+
---@param b render.md.MarkDetails
9294
---@return boolean
93-
function MarkInfo.__lt(a, b)
95+
function MarkDetails.__lt(a, b)
9496
local as, bs = a:priorities(), b:priorities()
9597
for i = 1, math.max(#as, #bs) do
9698
if as[i] ~= bs[i] then
@@ -467,11 +469,11 @@ end
467469
function M.actual_marks()
468470
local namespace = require('render-markdown.core.ui').namespace
469471
local marks = vim.api.nvim_buf_get_extmarks(0, namespace, 0, -1, { details = true })
470-
---@type render.md.MarkInfo[]
472+
---@type render.md.MarkDetails[]
471473
local actual = {}
472474
for _, mark in ipairs(marks) do
473475
local _, row, col, details = unpack(mark)
474-
table.insert(actual, MarkInfo.new(row, col, details))
476+
table.insert(actual, MarkDetails.new(row, col, details))
475477
end
476478
table.sort(actual)
477479
return actual

0 commit comments

Comments
 (0)
Please sign in to comment.