Skip to content

Commit 8aff425

Browse files
authored
feat(code): width = block (#88)
* merge * refactor: cleanup * fix: no need to subtract width from block pad * fix: add left_pad to block width * fix: right_pad = 0 by default * fix: extmark priority
1 parent 5879827 commit 8aff425

File tree

3 files changed

+45
-2
lines changed

3 files changed

+45
-2
lines changed

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

+34-2
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,17 @@ M.render_code = function(buf, info)
177177
if start_row == end_row - 1 then
178178
return marks
179179
end
180+
181+
local width = util.get_width(buf)
182+
if code.width == 'block' then
183+
width = 0
184+
local lines = vim.api.nvim_buf_get_lines(buf, start_row, end_row, false)
185+
for _, line in ipairs(lines) do
186+
width = math.max(width, str.width(line))
187+
end
188+
end
189+
width = width + code.right_pad + code.left_pad
190+
180191
if code.border == 'thin' then
181192
local code_start = ts.child(buf, info, 'fenced_code_block_delimiter', info.start_row)
182193
if #marks == 0 and ts.hidden(buf, code_info) and ts.hidden(buf, code_start) then
@@ -187,7 +198,7 @@ M.render_code = function(buf, info)
187198
start_row = info.start_row,
188199
start_col = info.start_col,
189200
opts = {
190-
virt_text = { { code.above:rep(util.get_width(buf)), colors.inverse(code.highlight) } },
201+
virt_text = { { code.above:rep(width), colors.inverse(code.highlight) } },
191202
virt_text_pos = 'overlay',
192203
},
193204
}
@@ -202,7 +213,7 @@ M.render_code = function(buf, info)
202213
start_row = info.end_row - 1,
203214
start_col = info.start_col,
204215
opts = {
205-
virt_text = { { code.below:rep(util.get_width(buf)), colors.inverse(code.highlight) } },
216+
virt_text = { { code.below:rep(width), colors.inverse(code.highlight) } },
206217
virt_text_pos = 'overlay',
207218
},
208219
}
@@ -222,6 +233,27 @@ M.render_code = function(buf, info)
222233
},
223234
}
224235
list.add(marks, background_mark)
236+
237+
if code.width == 'block' then
238+
-- overwrite anything beyond block width + right_pad with Normal
239+
local pad = str.pad('', vim.o.columns * 2)
240+
for row = start_row, code.border == 'thin' and end_row or end_row - 1 do
241+
---@type render.md.Mark
242+
local block_background_mark = {
243+
conceal = false,
244+
start_row = row,
245+
start_col = 0,
246+
opts = {
247+
priority = 0,
248+
hl_mode = 'combine',
249+
virt_text = { { pad, 'Normal' } },
250+
virt_text_win_col = width,
251+
},
252+
}
253+
list.add(marks, block_background_mark)
254+
end
255+
end
256+
225257
-- Requires inline extmarks
226258
if not util.has_10 or code.left_pad <= 0 then
227259
return marks

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

+9
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ local M = {}
6969
---@field public sign? boolean
7070
---@field public style? 'full'|'normal'|'language'|'none'
7171
---@field public left_pad? integer
72+
---@field public right_pad? integer
73+
---@field public width? 'full'|'block'
7274
---@field public border? 'thin'|'thick'
7375
---@field public above? string
7476
---@field public below? string
@@ -244,6 +246,9 @@ M.default_config = {
244246
style = 'full',
245247
-- Amount of padding to add to the left of code blocks
246248
left_pad = 0,
249+
-- Amount of padding to add to the right of code blocks
250+
-- when width is 'block'
251+
right_pad = 0,
247252
-- Determins how the top / bottom of code block are rendered:
248253
-- thick: use the same highlight as the code body
249254
-- thin: when lines are empty overlay the above & below icons
@@ -255,6 +260,10 @@ M.default_config = {
255260
-- Highlight for code blocks & inline code
256261
highlight = 'RenderMarkdownCode',
257262
highlight_inline = 'RenderMarkdownCodeInline',
263+
-- Width of the code block background:
264+
-- * full: full width of the window
265+
-- * block: width of the code block
266+
width = 'full',
258267
},
259268
dash = {
260269
-- Turn on / off thematic break rendering

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

+2
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,9 @@
5353
---@field public enabled boolean
5454
---@field public sign boolean
5555
---@field public style 'full'|'normal'|'language'|'none'
56+
---@field public width 'full'|'block'
5657
---@field public left_pad integer
58+
---@field public right_pad integer
5759
---@field public border 'thin'|'thick'
5860
---@field public above string
5961
---@field public below string

0 commit comments

Comments
 (0)