@@ -37,7 +37,8 @@ function Context.new(props, offset)
37
37
38
38
local ranges = {}
39
39
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 )
41
42
end
42
43
self .ranges = Range .coalesce (ranges )
43
44
self .callouts = {}
@@ -54,27 +55,6 @@ function Context.new(props, offset)
54
55
return self
55
56
end
56
57
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
-
78
58
--- @param config render.md.base.Config
79
59
--- @return boolean
80
60
function Context :skip (config )
@@ -114,11 +94,6 @@ function Context:add_checkbox(row, checkbox)
114
94
self .checkboxes [row ] = checkbox
115
95
end
116
96
117
- --- @return integer
118
- function Context :tab_size ()
119
- return Env .buf .get (self .buf , ' tabstop' )
120
- end
121
-
122
97
--- @param node ? render.md.Node
123
98
--- @return integer
124
99
function Context :width (node )
@@ -128,19 +103,6 @@ function Context:width(node)
128
103
return Str .width (node .text ) + self :get_offset (node ) - self .conceal :get (node )
129
104
end
130
105
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
-
144
106
--- @private
145
107
--- @param node render.md.Node
146
108
--- @return integer
@@ -155,6 +117,19 @@ function Context:get_offset(node)
155
117
return result
156
118
end
157
119
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
+
158
133
--- @param value number
159
134
--- @param used integer
160
135
--- @return integer
@@ -171,53 +146,55 @@ end
171
146
172
147
--- @param win integer
173
148
--- @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
179
157
end
180
158
181
159
--- @param node TSNode
182
160
--- @return boolean
183
- function Context :overlaps_node (node )
161
+ function Context :overlaps (node )
184
162
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
188
169
end
189
170
190
171
--- @param parser vim.treesitter.LanguageTree
191
172
function Context :parse (parser )
192
- self : for_each ( function ( range )
173
+ for _ , range in ipairs ( self . ranges ) do
193
174
parser :parse ({ range .top , range .bottom })
194
- end )
175
+ end
195
176
end
196
177
197
178
--- @param root TSNode
198
179
--- @param query vim.treesitter.Query
199
180
--- @param callback fun ( capture : string , node : render.md.Node )
200
181
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
204
185
local capture = query .captures [id ]
205
186
local node = Node .new (self .buf , ts_node )
206
187
log .node (capture , node )
207
188
callback (capture , node )
208
189
end
209
- end )
190
+ end
210
191
end
211
192
212
- --- @param callback fun ( range : render.md.Range ): boolean ?
213
- --- @return boolean
193
+ --- @param callback fun ( range : render.md.Range )
214
194
function Context :for_each (callback )
215
195
for _ , range in ipairs (self .ranges ) do
216
- if callback (range ) then
217
- return true
218
- end
196
+ callback (range )
219
197
end
220
- return false
221
198
end
222
199
223
200
--- @class render.md.context.Cache : { [integer ]: render.md.Context }
@@ -226,17 +203,17 @@ local Cache = {}
226
203
--- @class render.md.context.Manager
227
204
local M = {}
228
205
229
- --- @param props render.md.context.Props
230
- function M .reset (props )
231
- Cache [props .buf ] = Context .new (props , 10 )
232
- end
233
-
234
206
--- @param buf integer
235
207
--- @param win integer
236
208
--- @return boolean
237
- function M .contains_range (buf , win )
209
+ function M .contains (buf , win )
238
210
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 )
240
217
end
241
218
242
219
--- @param buf integer
0 commit comments