Skip to content

Commit 642222a

Browse files
chore(refactor): move window range computation to env module
1 parent 2d2b30f commit 642222a

File tree

7 files changed

+75
-83
lines changed

7 files changed

+75
-83
lines changed

lua/render-markdown/core/conceal.lua

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ end
147147
---@param language string
148148
---@param root TSNode
149149
function Conceal:compute_tree(language, root)
150-
if not self.context:overlaps_node(root) then
150+
if not self.context:overlaps(root) then
151151
return
152152
end
153153
if not vim.tbl_contains({ 'markdown', 'markdown_inline' }, language) then
@@ -158,8 +158,8 @@ function Conceal:compute_tree(language, root)
158158
return
159159
end
160160
self.context:for_each(function(range)
161-
local start, stop = range.top, range.bottom
162-
for id, node, data in query:iter_captures(root, self.buf, start, stop) do
161+
local top, bottom = range.top, range.bottom
162+
for id, node, data in query:iter_captures(root, self.buf, top, bottom) do
163163
if data.conceal_lines ~= nil then
164164
local node_range = self:node_range(id, node, data)
165165
local row = unpack(node_range)

lua/render-markdown/core/context.lua

Lines changed: 45 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ function Context.new(props, offset)
3737

3838
local ranges = {}
3939
for _, window in ipairs(Env.buf.windows(props.buf)) do
40-
ranges[#ranges + 1] = Context.compute_range(props.buf, window, offset)
40+
local top, bottom = Env.range(props.buf, window, offset)
41+
ranges[#ranges + 1] = Range.new(top, bottom)
4142
end
4243
self.ranges = Range.coalesce(ranges)
4344
self.callouts = {}
@@ -54,27 +55,6 @@ function Context.new(props, offset)
5455
return self
5556
end
5657

57-
---@private
58-
---@param buf integer
59-
---@param win integer
60-
---@param offset integer
61-
---@return render.md.Range
62-
function Context.compute_range(buf, win, offset)
63-
local top = math.max(Env.win.view(win).topline - 1 - offset, 0)
64-
65-
local bottom = top
66-
local lines = vim.api.nvim_buf_line_count(buf)
67-
local size = vim.api.nvim_win_get_height(win) + (2 * offset)
68-
while bottom < lines and size > 0 do
69-
bottom = bottom + 1
70-
if Env.row.visible(win, bottom) then
71-
size = size - 1
72-
end
73-
end
74-
75-
return Range.new(top, bottom)
76-
end
77-
7858
---@param config render.md.base.Config
7959
---@return boolean
8060
function Context:skip(config)
@@ -114,11 +94,6 @@ function Context:add_checkbox(row, checkbox)
11494
self.checkboxes[row] = checkbox
11595
end
11696

117-
---@return integer
118-
function Context:tab_size()
119-
return Env.buf.get(self.buf, 'tabstop')
120-
end
121-
12297
---@param node? render.md.Node
12398
---@return integer
12499
function Context:width(node)
@@ -128,19 +103,6 @@ function Context:width(node)
128103
return Str.width(node.text) + self:get_offset(node) - self.conceal:get(node)
129104
end
130105

131-
---@param row integer
132-
---@param offset render.md.context.Offset
133-
function Context:add_offset(row, offset)
134-
if offset.width <= 0 then
135-
return
136-
end
137-
if self.offsets[row] == nil then
138-
self.offsets[row] = {}
139-
end
140-
local offsets = self.offsets[row]
141-
offsets[#offsets + 1] = offset
142-
end
143-
144106
---@private
145107
---@param node render.md.Node
146108
---@return integer
@@ -155,6 +117,19 @@ function Context:get_offset(node)
155117
return result
156118
end
157119

120+
---@param row integer
121+
---@param offset render.md.context.Offset
122+
function Context:add_offset(row, offset)
123+
if offset.width <= 0 then
124+
return
125+
end
126+
if self.offsets[row] == nil then
127+
self.offsets[row] = {}
128+
end
129+
local offsets = self.offsets[row]
130+
offsets[#offsets + 1] = offset
131+
end
132+
158133
---@param value number
159134
---@param used integer
160135
---@return integer
@@ -171,53 +146,55 @@ end
171146

172147
---@param win integer
173148
---@return boolean
174-
function Context:contains_window(win)
175-
local window_range = Context.compute_range(self.buf, win, 0)
176-
return self:for_each(function(range)
177-
return range:contains(window_range.top, window_range.bottom)
178-
end)
149+
function Context:contains(win)
150+
local top, bottom = Env.range(self.buf, win, 0)
151+
for _, range in ipairs(self.ranges) do
152+
if range:contains(top, bottom) then
153+
return true
154+
end
155+
end
156+
return false
179157
end
180158

181159
---@param node TSNode
182160
---@return boolean
183-
function Context:overlaps_node(node)
161+
function Context:overlaps(node)
184162
local top, _, bottom, _ = node:range()
185-
return self:for_each(function(range)
186-
return range:overlaps(top, bottom)
187-
end)
163+
for _, range in ipairs(self.ranges) do
164+
if range:overlaps(top, bottom) then
165+
return true
166+
end
167+
end
168+
return false
188169
end
189170

190171
---@param parser vim.treesitter.LanguageTree
191172
function Context:parse(parser)
192-
self:for_each(function(range)
173+
for _, range in ipairs(self.ranges) do
193174
parser:parse({ range.top, range.bottom })
194-
end)
175+
end
195176
end
196177

197178
---@param root TSNode
198179
---@param query vim.treesitter.Query
199180
---@param callback fun(capture: string, node: render.md.Node)
200181
function Context:query(root, query, callback)
201-
self:for_each(function(range)
202-
local start, stop = range.top, range.bottom
203-
for id, ts_node in query:iter_captures(root, self.buf, start, stop) do
182+
for _, range in ipairs(self.ranges) do
183+
local top, bottom = range.top, range.bottom
184+
for id, ts_node in query:iter_captures(root, self.buf, top, bottom) do
204185
local capture = query.captures[id]
205186
local node = Node.new(self.buf, ts_node)
206187
log.node(capture, node)
207188
callback(capture, node)
208189
end
209-
end)
190+
end
210191
end
211192

212-
---@param callback fun(range: render.md.Range): boolean?
213-
---@return boolean
193+
---@param callback fun(range: render.md.Range)
214194
function Context:for_each(callback)
215195
for _, range in ipairs(self.ranges) do
216-
if callback(range) then
217-
return true
218-
end
196+
callback(range)
219197
end
220-
return false
221198
end
222199

223200
---@class render.md.context.Cache: { [integer]: render.md.Context }
@@ -226,17 +203,17 @@ local Cache = {}
226203
---@class render.md.context.Manager
227204
local M = {}
228205

229-
---@param props render.md.context.Props
230-
function M.reset(props)
231-
Cache[props.buf] = Context.new(props, 10)
232-
end
233-
234206
---@param buf integer
235207
---@param win integer
236208
---@return boolean
237-
function M.contains_range(buf, win)
209+
function M.contains(buf, win)
238210
local context = Cache[buf]
239-
return context ~= nil and context:contains_window(win)
211+
return context ~= nil and context:contains(win)
212+
end
213+
214+
---@param props render.md.context.Props
215+
function M.reset(props)
216+
Cache[props.buf] = Context.new(props, 10)
240217
end
241218

242219
---@param buf integer

lua/render-markdown/core/ui.lua

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -85,13 +85,7 @@ end
8585
---@param event string
8686
---@param change boolean
8787
function M.update(buf, win, event, change)
88-
log.buf(
89-
'info',
90-
'update',
91-
buf,
92-
string.format('event %s', event),
93-
string.format('change %s', change)
94-
)
88+
log.buf('info', 'update', buf, event, string.format('change %s', change))
9589
if not Env.valid(buf, win) then
9690
return
9791
end
@@ -124,7 +118,7 @@ end
124118
---@return boolean
125119
function M.parse(buf, win, change)
126120
-- Need to parse when things change or we have not parsed the visible range yet
127-
return change or not Context.contains_range(buf, win)
121+
return change or not Context.contains(buf, win)
128122
end
129123

130124
---@private
@@ -246,7 +240,7 @@ end
246240
---@return render.md.Mark[]
247241
function M.parse_tree(ctx, language)
248242
log.buf('debug', 'language', ctx.buf, language)
249-
if not Context.get(ctx.buf):overlaps_node(ctx.root) then
243+
if not Context.get(ctx.buf):overlaps(ctx.root) then
250244
return {}
251245
end
252246

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

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

lua/render-markdown/lib/env.lua

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,26 @@ function M.valid(buf, win)
6868
return buf == M.win.buf(win)
6969
end
7070

71+
---@param buf integer
72+
---@param win integer
73+
---@param offset integer
74+
---@return integer, integer
75+
function M.range(buf, win, offset)
76+
local top = math.max(M.win.view(win).topline - 1 - offset, 0)
77+
78+
local bottom = top
79+
local lines = vim.api.nvim_buf_line_count(buf)
80+
local size = vim.api.nvim_win_get_height(win) + (2 * offset)
81+
while bottom < lines and size > 0 do
82+
bottom = bottom + 1
83+
if M.row.visible(win, bottom) then
84+
size = size - 1
85+
end
86+
end
87+
88+
return top, bottom
89+
end
90+
7191
---@class render.md.env.Row
7292
M.row = {}
7393

lua/render-markdown/render/code.lua

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
local Base = require('render-markdown.render.base')
2+
local Env = require('render-markdown.lib.env')
23
local Icons = require('render-markdown.integ.icons')
34
local Str = require('render-markdown.lib.str')
45
local colors = require('render-markdown.colors')
@@ -63,7 +64,7 @@ function Render:offset(value, used)
6364
local result = self.context:percent(value, used)
6465
if self.node.text:find('\t') ~= nil then
6566
-- Rounds to the next multiple of tab size
66-
local tab_size = self.context:tab_size()
67+
local tab_size = Env.buf.get(self.context.buf, 'tabstop')
6768
result = math.ceil(result / tab_size) * tab_size
6869
end
6970
return result

lua/render-markdown/render/table.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ function Render:setup()
6363
self.node:for_each_child(function(row)
6464
if row.type == 'pipe_table_delimiter_row' then
6565
delim = self:parse_delim(row)
66-
elseif self.context:overlaps_node(row:get()) then
66+
elseif self.context:overlaps(row:get()) then
6767
if
6868
vim.tbl_contains(
6969
{ 'pipe_table_header', 'pipe_table_row' },

0 commit comments

Comments
 (0)