@@ -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
5556end
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
8060function Context :skip (config )
@@ -114,11 +94,6 @@ function Context:add_checkbox(row, checkbox)
11494 self .checkboxes [row ] = checkbox
11595end
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
12499function 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 )
129104end
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
156118end
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
179157end
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
188169end
189170
190171--- @param parser vim.treesitter.LanguageTree
191172function 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
195176end
196177
197178--- @param root TSNode
198179--- @param query vim.treesitter.Query
199180--- @param callback fun ( capture : string , node : render.md.Node )
200181function 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
210191end
211192
212- --- @param callback fun ( range : render.md.Range ): boolean ?
213- --- @return boolean
193+ --- @param callback fun ( range : render.md.Range )
214194function 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
221198end
222199
223200--- @class render.md.context.Cache : { [integer ]: render.md.Context }
@@ -226,17 +203,17 @@ local Cache = {}
226203--- @class render.md.context.Manager
227204local 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 )
240217end
241218
242219--- @param buf integer
0 commit comments