Skip to content

Commit 75a0a95

Browse files
feat: setext headers pad all lines
## Details Rather than padding setext style headings first line only with the icon, pad all the other lines by the width of the icon to improve text alignment.
1 parent f187721 commit 75a0a95

File tree

5 files changed

+43
-34
lines changed

5 files changed

+43
-34
lines changed

Diff for: CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@
99
- configurable padding highlight [#176](https://github.com/MeanderingProgrammer/render-markdown.nvim/issues/176)
1010
[095078d](https://github.com/MeanderingProgrammer/render-markdown.nvim/commit/095078d931ce23b544face8ca7b845adf7fad7e9)
1111

12+
### Bug Fixes
13+
14+
- window options on alternate buffer switch [#177](https://github.com/MeanderingProgrammer/render-markdown.nvim/issues/177)
15+
[f187721](https://github.com/MeanderingProgrammer/render-markdown.nvim/commit/f187721a5381f4443ef97ad1a7c0681a65511d28)
16+
1217
## 7.1.0 (2024-09-19)
1318

1419
### Features

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 September 22
1+
*render-markdown.txt* For 0.10.0 Last change: 2024 September 23
22

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

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

+1-1
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.1.4'
7+
M.version = '7.1.5'
88

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

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

+23-20
Original file line numberDiff line numberDiff line change
@@ -75,42 +75,48 @@ end
7575
---@private
7676
---@return integer
7777
function Render:icon()
78+
local icon, highlight = self.data.icon, { self.data.foreground, self.data.background }
79+
7880
if not self.data.atx then
79-
if self.data.icon == nil then
81+
if icon == nil then
8082
return 0
8183
end
82-
local added = self.marks:add(true, self.info.start_row, self.info.start_col, {
83-
end_row = self.info.end_row,
84-
end_col = self.info.end_col,
85-
virt_text = { { self.data.icon, { self.data.foreground, self.data.background } } },
86-
virt_text_pos = 'inline',
87-
})
88-
return added and str.width(self.data.icon) or 0
84+
local added = true
85+
for row = self.info.start_row, self.data.end_row - 1 do
86+
added = added
87+
and self.marks:add(true, row, self.info.start_col, {
88+
end_row = row,
89+
end_col = self.info.end_col,
90+
virt_text = { { row == self.info.start_row and icon or str.pad(str.width(icon)), highlight } },
91+
virt_text_pos = 'inline',
92+
})
93+
end
94+
return added and str.width(icon) or 0
8995
end
9096

9197
-- For atx headings available width is level + 1 - concealed, where level = number of
9298
-- `#` characters, one is added to account for the space after the last `#` but before
9399
-- the heading title, and concealed text is subtracted since that space is not usable
94100
local width = self.data.level + 1 - self.context:concealed(self.info)
95-
if self.data.icon == nil then
101+
if icon == nil then
96102
return width
97103
end
98104

99-
local padding = width - str.width(self.data.icon)
105+
local padding = width - str.width(icon)
100106
if self.heading.position == 'inline' or padding < 0 then
101107
local added = self.marks:add(true, self.info.start_row, self.info.start_col, {
102108
end_row = self.info.end_row,
103109
end_col = self.info.end_col,
104-
virt_text = { { self.data.icon, { self.data.foreground, self.data.background } } },
110+
virt_text = { { icon, highlight } },
105111
virt_text_pos = 'inline',
106112
conceal = '',
107113
})
108-
return added and str.width(self.data.icon) or width
114+
return added and str.width(icon) or width
109115
else
110116
self.marks:add(true, self.info.start_row, self.info.start_col, {
111117
end_row = self.info.end_row,
112118
end_col = self.info.end_col,
113-
virt_text = { { str.pad(padding) .. self.data.icon, { self.data.foreground, self.data.background } } },
119+
virt_text = { { str.pad(padding) .. icon, highlight } },
114120
virt_text_pos = 'overlay',
115121
})
116122
return width
@@ -122,16 +128,13 @@ end
122128
---@return integer
123129
function Render:width(icon_width)
124130
if self.data.heading_width == 'block' then
125-
local width = nil
131+
local content_width = nil
126132
if self.data.atx then
127-
width = icon_width + self.context:width(self.info:sibling('inline'))
133+
content_width = self.context:width(self.info:sibling('inline'))
128134
else
129-
-- Account for icon in first row
130-
local widths = vim.tbl_map(str.width, self.info:lines())
131-
widths[1] = widths[1] + icon_width
132-
width = vim.fn.max(widths)
135+
content_width = vim.fn.max(vim.tbl_map(str.width, self.info:lines()))
133136
end
134-
width = self.heading.left_pad + width + self.heading.right_pad
137+
local width = self.heading.left_pad + icon_width + content_width + self.heading.right_pad
135138
return math.max(width, self.heading.min_width)
136139
else
137140
return self.context:get_width()

Diff for: tests/ad_hoc_spec.lua

+13-12
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,25 @@ local util = require('tests.util')
77
---@param level integer
88
---@return render.md.MarkInfo[]
99
local function setext_heading(start_row, end_row, level)
10-
local result = util.heading(start_row, level)
11-
local background_mark = table.remove(result, #result)
12-
local icon_mark = table.remove(result, #result)
10+
local sign_mark, icon_mark, background_mark = unpack(util.heading(start_row, level))
11+
local icon, highlight = unpack(icon_mark.virt_text[1])
1312

13+
local result = {}
1414
for row = start_row, end_row do
1515
local row_background_mark = vim.deepcopy(background_mark)
1616
row_background_mark.row = { row, row + 1 }
17-
table.insert(result, row_background_mark)
17+
vim.list_extend(result, {
18+
{
19+
row = { row, row },
20+
col = { 0, 0 },
21+
virt_text = { { row == start_row and vim.trim(icon) .. ' ' or ' ', highlight } },
22+
virt_text_pos = 'inline',
23+
},
24+
row_background_mark,
25+
})
1826
end
19-
20-
icon_mark.row = { start_row, end_row + 1 }
21-
icon_mark.col = { 0, 0 }
22-
icon_mark.virt_text[1][1] = vim.trim(icon_mark.virt_text[1][1]) .. ' '
23-
icon_mark.virt_text_pos = 'inline'
24-
table.insert(result, 3, icon_mark)
25-
27+
table.insert(result, 2, sign_mark)
2628
table.insert(result, #result, util.conceal(end_row, 0, 3))
27-
2829
return result
2930
end
3031

0 commit comments

Comments
 (0)