Skip to content

Commit 6c5081c

Browse files
chore(refactor): pass context to conceal module constructor
## Details Rather than passing `context` to individual methods in the `conceal` module, pass it to the constructor and store it in a private field. Remove `hidden` method wrapper in `context` and access it from `conceal` directly where needed, provides little additional utility. Rename method `resolve_offset` -> `to_width`.
1 parent e724a49 commit 6c5081c

File tree

8 files changed

+48
-50
lines changed

8 files changed

+48
-50
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
- `code.language_icon` option [#376](https://github.com/MeanderingProgrammer/render-markdown.nvim/issues/376)
1717
[8ee2701](https://github.com/MeanderingProgrammer/render-markdown.nvim/commit/8ee2701a6c4cdaef7ea0b27c13c26971ae3c9761)
1818
[7bf951b](https://github.com/MeanderingProgrammer/render-markdown.nvim/commit/7bf951b8ad93d47b90be290be6fc60da5788ddaa)
19+
- code border for different conceal settings [e724a49](https://github.com/MeanderingProgrammer/render-markdown.nvim/commit/e724a49dee315744d6f5d3c651ddd604cc7afc52)
1920

2021
### Bug Fixes
2122

lua/render-markdown/core/conceal.lua

Lines changed: 33 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,23 @@ local Str = require('render-markdown.lib.str')
1212
---@field sections render.md.conceal.Section[]
1313

1414
---@class render.md.Conceal
15+
---@field private context render.md.Context
1516
---@field private buf integer
1617
---@field private level integer
1718
---@field private computed boolean
1819
---@field private lines table<integer, render.md.conceal.Line>
1920
local Conceal = {}
2021
Conceal.__index = Conceal
2122

23+
---@param context render.md.Context
2224
---@param buf integer
23-
---@param level integer
25+
---@param win integer
2426
---@return render.md.Conceal
25-
function Conceal.new(buf, level)
27+
function Conceal.new(context, buf, win)
2628
local self = setmetatable({}, Conceal)
29+
self.context = context
2730
self.buf = buf
28-
self.level = level
31+
self.level = Env.win.get(win, 'conceallevel')
2932
self.computed = false
3033
self.lines = {}
3134
return self
@@ -49,17 +52,24 @@ function Conceal:add(row, entry)
4952
if type(entry) == 'boolean' then
5053
line.hidden = entry
5154
else
52-
if entry.width <= 0 then
53-
return
54-
end
5555
-- If the section is covered by an existing one don't add it
56-
for _, section in ipairs(line.sections) do
57-
if section.start_col <= entry.start_col and section.end_col >= entry.end_col then
58-
return
59-
end
56+
if entry.width > 0 and not self:has(line, entry) then
57+
table.insert(line.sections, entry)
58+
end
59+
end
60+
end
61+
62+
---@private
63+
---@param line render.md.conceal.Line
64+
---@param entry render.md.conceal.Section
65+
---@return boolean
66+
function Conceal:has(line, entry)
67+
for _, section in ipairs(line.sections) do
68+
if section.start_col <= entry.start_col and section.end_col >= entry.end_col then
69+
return true
6070
end
61-
table.insert(line.sections, entry)
6271
end
72+
return false
6373
end
6474

6575
---@param width integer
@@ -77,20 +87,18 @@ function Conceal:adjust(width, character)
7787
end
7888
end
7989

80-
---@param context render.md.Context
8190
---@param node render.md.Node
8291
---@return boolean
83-
function Conceal:hidden(context, node)
92+
function Conceal:hidden(node)
8493
-- conceal lines metadata require neovim >= 0.11.0 to function
85-
return Env.has_11 and self:line(context, node).hidden
94+
return Env.has_11 and self:line(node).hidden
8695
end
8796

88-
---@param context render.md.Context
8997
---@param node render.md.Node
9098
---@return integer
91-
function Conceal:get(context, node)
99+
function Conceal:get(node)
92100
local result = 0
93-
for _, section in ipairs(self:line(context, node).sections) do
101+
for _, section in ipairs(self:line(node).sections) do
94102
if node.start_col < section.end_col and node.end_col > section.start_col then
95103
local amount = self:adjust(section.width, section.character)
96104
result = result + amount
@@ -100,12 +108,11 @@ function Conceal:get(context, node)
100108
end
101109

102110
---@private
103-
---@param context render.md.Context
104111
---@param node render.md.Node
105-
function Conceal:line(context, node)
112+
function Conceal:line(node)
106113
if not self.computed then
107-
self:compute(context)
108114
self.computed = true
115+
self:compute()
109116
end
110117
local line = self.lines[node.start_row]
111118
if line == nil then
@@ -116,8 +123,7 @@ end
116123

117124
---Cached row level implementation of vim.treesitter.get_captures_at_pos
118125
---@private
119-
---@param context render.md.Context
120-
function Conceal:compute(context)
126+
function Conceal:compute()
121127
if not self:enabled() then
122128
return
123129
end
@@ -128,18 +134,17 @@ function Conceal:compute(context)
128134
if parser == nil then
129135
return
130136
end
131-
context:parse(parser)
137+
self.context:parse(parser)
132138
parser:for_each_tree(function(tree, language_tree)
133-
self:compute_tree(context, language_tree:lang(), tree:root())
139+
self:compute_tree(language_tree:lang(), tree:root())
134140
end)
135141
end
136142

137143
---@private
138-
---@param context render.md.Context
139144
---@param language string
140145
---@param root TSNode
141-
function Conceal:compute_tree(context, language, root)
142-
if not context:overlaps_node(root) then
146+
function Conceal:compute_tree(language, root)
147+
if not self.context:overlaps_node(root) then
143148
return
144149
end
145150
if not vim.tbl_contains({ 'markdown', 'markdown_inline' }, language) then
@@ -149,7 +154,7 @@ function Conceal:compute_tree(context, language, root)
149154
if query == nil then
150155
return
151156
end
152-
context:for_each(function(range)
157+
self.context:for_each(function(range)
153158
for id, node, metadata in query:iter_captures(root, self.buf, range.top, range.bottom) do
154159
if metadata.conceal_lines ~= nil then
155160
local node_range = self:node_range(id, node, metadata)

lua/render-markdown/core/context.lua

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ function Context.new(props, offset)
4747

4848
self.mode = props.mode
4949
self.top_level_mode = props.top_level_mode
50-
self.conceal = Conceal.new(self.buf, Env.win.get(self.win, 'conceallevel'))
50+
self.conceal = Conceal.new(self, self.buf, self.win)
5151
self.last_heading = nil
5252

5353
return self
@@ -123,22 +123,13 @@ function Context:tab_size()
123123
return Env.buf.get(self.buf, 'tabstop')
124124
end
125125

126-
---@param node? render.md.Node
127-
---@return boolean
128-
function Context:hidden(node)
129-
if node == nil then
130-
return false
131-
end
132-
return self.conceal:hidden(self, node)
133-
end
134-
135126
---@param node? render.md.Node
136127
---@return integer
137128
function Context:width(node)
138129
if node == nil then
139130
return 0
140131
end
141-
return Str.width(node.text) + self:get_offset(node) - self.conceal:get(self, node)
132+
return Str.width(node.text) + self:get_offset(node) - self.conceal:get(node)
142133
end
143134

144135
---@param row integer
@@ -172,7 +163,7 @@ end
172163
---@param offset number
173164
---@param width integer
174165
---@return integer
175-
function Context:resolve_offset(offset, width)
166+
function Context:to_width(offset, width)
176167
if offset <= 0 then
177168
return 0
178169
elseif offset < 1 then

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.1.15'
8+
M.version = '8.1.16'
99

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

lua/render-markdown/render/code.lua

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ function Render:offset(offset, width, position)
8181
if offset <= 0 then
8282
return 0
8383
end
84-
local result = self.context:resolve_offset(offset, width)
84+
local result = self.context:to_width(offset, width)
8585
if position == 'left' and self.node.text:find('\t') ~= nil then
8686
local tab_size = self.context:tab_size()
8787
-- Rounds to the next multiple of tab_size
@@ -186,7 +186,8 @@ function Render:border(row, above, empty)
186186
if self.code.border == 'none' then
187187
return
188188
end
189-
if self.node:child('fenced_code_block_delimiter', row) == nil then
189+
local delim = self.node:child('fenced_code_block_delimiter', row)
190+
if delim == nil then
190191
return
191192
end
192193
local width, highlight = self.data.width - self.data.col, self.code.highlight

lua/render-markdown/render/dash.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ end
1616

1717
function Render:render()
1818
local width = self.dash.width
19-
width = type(width) == 'number' and self.context:resolve_offset(width, 0) or vim.o.columns
20-
local margin = self.context:resolve_offset(self.dash.left_margin, width)
19+
width = type(width) == 'number' and self.context:to_width(width, 0) or vim.o.columns
20+
local margin = self.context:to_width(self.dash.left_margin, width)
2121

2222
local line = self:append({}, margin)
2323
self:append(line, self.dash.icon:rep(width), self.dash.highlight)

lua/render-markdown/render/heading.lua

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ function Render:setup()
3636
if self.context:skip(self.heading) then
3737
return false
3838
end
39-
if self.context:hidden(self.node) then
39+
if self.context.conceal:hidden(self.node) then
4040
return false
4141
end
4242

@@ -190,12 +190,12 @@ function Render:width(icon_width)
190190
else
191191
width = width + vim.fn.max(Iter.list.map(self.node:lines(), Str.width))
192192
end
193-
local left_padding = self.context:resolve_offset(self.data.left_pad, width)
194-
local right_padding = self.context:resolve_offset(self.data.right_pad, width)
193+
local left_padding = self.context:to_width(self.data.left_pad, width)
194+
local right_padding = self.context:to_width(self.data.right_pad, width)
195195
width = math.max(left_padding + width + right_padding, self.data.min_width)
196196
---@type render.md.width.Heading
197197
return {
198-
margin = self.context:resolve_offset(self.data.left_margin, width),
198+
margin = self.context:to_width(self.data.left_margin, width),
199199
padding = left_padding,
200200
content = width,
201201
}

lua/render-markdown/render/paragraph.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ end
2222
function Render:render()
2323
local width = vim.fn.max(Iter.list.map(self.node:lines(), Str.width))
2424
width = math.max(width, self.paragraph.min_width)
25-
local margin = self.context:resolve_offset(self.paragraph.left_margin, width)
25+
local margin = self.context:to_width(self.paragraph.left_margin, width)
2626
local line = self:append({}, margin)
2727
if #line == 0 then
2828
return

0 commit comments

Comments
 (0)