3 local function finishof(node
) return node
.finish
end
4 local function startof(node
) return node
.start
end
6 function M
.new(parser
, eol_at
, bol_at
)
8 local function in_quasilist(t
, range
)
9 return t
and t
.d
and not t
.is_list
and
10 range
.start
>= t
.start
+ #t
.d
and range
.finish
<= t
.finish
+ 1 - #parser
.opposite
[t
.d
]
13 local function sexp_at(range
, true_sexp
)
14 local node
, parent
, nth
= parser
.tree
.sexp_at(range
)
15 if not in_quasilist(node
, range
) or true_sexp
then
16 return node
, parent
, nth
18 return node
.word_at(range
), node
22 local function escaped_at(range
)
23 local node
= parser
.escaped
.around(range
)
24 -- TODO: distinguish between doublequoted strings, chars, line comments, and block comments
25 -- and use approprite offset comparisons for each
26 return node
and range
.start
> node
.start
and range
.finish
<= node
.finish
and node
29 local function find_before_innermost(t
, range
, key
, pred
)
30 if t
.is_root
or (t
.d
and t
.start
< range
.start
) then
31 if t
.d
and not t
.is_list
then
33 local start
= keypos
== startof(t
)
34 local finish
= keypos
== finishof(t
)
37 newpos
= t
.finish_before(range
.finish
)
39 newpos
= t
.start_before(range
.start
)
42 local word
= t
.word_at({start
= newpos
, finish
= newpos
})
43 if word
and pred(word
) then
49 local _
, nearest_n
= t
.before(range
.start
, startof
)
51 for n
= nearest_n
, 1, -1 do
53 if child
.d
and not child
.is_empty
then
54 local ret
= find_before_innermost(child
, range
, key
, pred
)
58 elseif key(child
) < key(range
) and pred(child
, n
, #t
) then
63 if not t
.is_root
and pred(t
) then
69 local function find_after_innermost(t
, range
, key
, pred
)
70 if t
.is_root
or (t
.d
and t
.finish
>= range
.finish
) then
71 if t
.d
and not t
.is_list
then
73 local start
= keypos
== startof(t
)
74 local finish
= keypos
== finishof(t
)
77 newpos
= t
.finish_after(math
.max(t
.start
+ #t
.d
, range
.finish
))
79 newpos
= range
.finish
< t
.start
and t
.start
+ #t
.d
or t
.start_after(range
.start
)
82 local word
= t
.word_at({start
= newpos
, finish
= newpos
})
83 if word
and pred(word
) then
89 local _
, nearest_n
= t
.after(range
.finish
, finishof
)
91 for n
= nearest_n
, #t
do
93 if child
.d
and not child
.is_empty
then
94 local ret
= find_after_innermost(child
, range
, key
, pred
)
98 elseif key(child
) >= key(range
) and pred(child
, n
, #t
) then
103 if not t
.is_root
and pred(t
) then
111 start_before
= function(range
, skip
)
112 local _
, parent
= parser
.tree
.sexp_at(range
)
113 local node
= parent
.before(range
.start
, startof
, skip
)
114 return node
and startof(node
)
117 start_after
= function(range
, skip
)
118 local _
, parent
= parser
.tree
.sexp_at(range
)
119 local node
= parent
.after(range
.finish
, startof
, skip
)
120 return node
and startof(node
)
123 finish_before
= function(range
, skip
)
124 local _
, parent
= parser
.tree
.sexp_at(range
)
125 local node
= parent
.before(range
.start
, finishof
, skip
)
126 return node
and finishof(node
) + 1
129 finish_after
= function(range
, skip
)
130 local _
, parent
= parser
.tree
.sexp_at(range
)
131 local node
= parent
.after(range
.finish
, finishof
, skip
)
132 return node
and finishof(node
) + 1
135 start_down_after
= function(range
, skip
)
136 local node
, parent
= parser
.tree
.sexp_at(range
)
137 if in_quasilist(node
, range
) then return end
138 if node
and node
.p
then
139 -- XXX: if we are past a prefix start, this will prevent skipping over its list
140 range
.start
= node
.start
142 local next_list
= parent
.find_after(range
, function(t
) return t
.is_list
end)
144 local first
= next_list
.after(range
.finish
, startof
, skip
)
148 return next_list
.start
+ (next_list
.p
and #next_list
.p
or 0) + 1
153 start_up_before
= function(range
)
154 local _
, parent
= sexp_at(range
)
155 return parent
.d
and parent
.start
158 finish_down_before
= function(range
, skip
)
159 local node
, parent
= parser
.tree
.sexp_at(range
)
160 if in_quasilist(node
, range
) then return end
161 local prev_list
= parent
.find_before(range
, function(t
) return t
.is_list
end)
163 local last
= prev_list
.before(range
.start
, finishof
, skip
)
165 return (last
.finish
or last
.p
and last
.start
+ #last
.p
) + 1, prev_list
167 return prev_list
.finish
, prev_list
172 finish_up_after
= function(range
)
173 local _
, parent
= sexp_at(range
)
174 return parent
.d
and parent
.finish
+ 1
177 indented_before
= function(range
)
178 return find_before_innermost(parser
.tree
, range
, startof
, function(t
) return t
.indent
end)
181 start_float_before
= function(range
, up_empty_list
)
182 if up_empty_list
then
183 local _
, parent
= sexp_at(range
)
184 if parent
.is_empty
then
185 return parent
.start
, parent
188 local node
= find_before_innermost(parser
.tree
, range
, startof
, function(t
, n
, _
)
189 return not t
.d
or t
.is_empty
and n
== 1 end)
190 return node
and node
.start
, node
193 finish_float_after
= function(range
, up_empty_list
)
194 if up_empty_list
then
195 local _
, parent
= sexp_at(range
)
196 if parent
.is_empty
then
197 return parent
.finish
+ 1, parent
200 local node
= find_after_innermost(parser
.tree
, range
, finishof
, function(t
, n
, max_n
)
201 return not t
.d
or t
.is_empty
and n
== max_n
end)
202 return node
and node
.finish
+ 1, node
205 start_word_after
= function(t
, range
)
206 local newpos
= t
.start_after(range
.start
)
208 local word
= t
.word_at({start
= newpos
, finish
= newpos
})
209 return word
and word
.start
211 local _
, parent
, n
= sexp_at(range
, true)
212 local cur
, nxt
= parent
[n
], parent
[n
+ 1]
213 if nxt
and cur
.is_line_comment
and nxt
.is_line_comment
then
214 return nxt
.start_after(range
.start
)
219 finish_word_after
= function(t
, range
)
220 local newpos
= t
.finish_after(math
.max(t
.start
+ #t
.d
, range
.finish
))
222 local word
= t
.word_at({start
= newpos
, finish
= newpos
})
223 return word
and word
.finish
+ 1
225 local _
, parent
, n
= sexp_at(range
, true)
226 local cur
, nxt
= parent
[n
], parent
[n
+ 1]
227 if nxt
and cur
.is_line_comment
and nxt
.is_line_comment
then
228 return nxt
.finish_after(range
.start
)
233 start_word_before
= function(t
, range
)
234 local newpos
= t
.start_before(range
.start
)
236 local word
= t
.word_at({start
= newpos
, finish
= newpos
})
237 return word
and word
.start
239 local _
, parent
, n
= sexp_at(range
, true)
240 local cur
, prev
= parent
[n
], parent
[n
- 1]
241 if prev
and cur
.is_line_comment
and prev
.is_line_comment
then
242 return prev
.start_before(range
.start
)
247 finish_word_before
= function(t
, range
)
248 local newpos
= t
.finish_before(range
.finish
)
250 local word
= t
.word_at({start
= newpos
, finish
= newpos
})
251 return word
and word
.finish
+ 1
253 local _
, parent
, n
= sexp_at(range
, true)
254 local cur
, prev
= parent
[n
], parent
[n
- 1]
255 if prev
and cur
.is_line_comment
and prev
.is_line_comment
then
256 return prev
.finish_before(range
.start
)
261 prev_start_wrapped
= function(range
)
262 local node
, parent
= parser
.tree
.sexp_at(range
)
263 if in_quasilist(node
, range
) then parent
= node
end
264 local pstart
= parent
.start
and (parent
.start
+ (parent
.p
and #parent
.p
or 0) + #parent
.d
)
265 local bol
= bol_at(range
.start
)
266 local prev_wrapped
= (parent
.is_list
or parent
.is_root
) and parent
.find_before(range
,
267 function(t
) return t
.start
< bol
and t
.finish
> bol
end,
268 function(t
) return t
.finish
< bol
270 local prev_start
= prev_wrapped
and prev_wrapped
.start
or bol
271 pstart
= pstart
or prev_start
272 return math
.max(pstart
, prev_start
)
275 next_finish_wrapped
= function(range
)
276 local node
, parent
= parser
.tree
.sexp_at(range
)
277 if in_quasilist(node
, range
) then parent
= node
end
278 local pfinish
= parent
.finish
and parent
.finish
+ 1 - #parser
.opposite
[parent
.d
]
279 local eol
= eol_at(range
.start
)
280 local next_wrapped
= (parent
.is_list
or parent
.is_root
) and parent
.find_after(range
,
281 function(t
) return t
.start
< eol
and t
.finish
> eol
end,
282 function(t
) return t
.start
> eol
284 local next_finish
= next_wrapped
and next_wrapped
.finish
+ 1 or eol
285 -- XXX: second return value, to be used for safe line-commenting:
286 local next_start_on_line
= math
.min(pfinish
or range
.start
, next_wrapped
and next_wrapped
.start
or eol
)
287 return math
.min(pfinish
or next_finish
, next_finish
), next_start_on_line
< eol
and next_start_on_line
or nil
290 beginning_of_quasilist
= function(range
)
291 local _
, parent
= sexp_at(range
)
292 return parent
.start
and (parent
.start
+ (parent
.p
and #parent
.p
or 0) + #parent
.d
)
295 end_of_quasilist
= function(range
)
296 local _
, parent
= sexp_at(range
)
297 return parent
.finish
and parent
.finish
+ 1 - #parser
.opposite
[parent
.d
]
300 repl_line_begin
= function(range
)
301 local _
, parent
= parser
.tree
.sexp_at(range
)
302 if not parent
.is_root
then return end
303 local prev_indent
= parent
.find_before(range
, function(t
) return t
.indent
end)
304 return prev_indent
and prev_indent
.start
307 wider_than
= function(range
)
308 local node
, parent
= parser
.tree
.sexp_at(range
)
309 if not node
and parent
.is_root
then return end
310 local pstart
, pfinish
311 if in_quasilist(node
, range
) then
313 node
= node
.word_at(range
)
315 if not parent
.is_root
then
316 pstart
, pfinish
= parent
.start
+ (parent
.p
and #parent
.p
or 0) + #parent
.d
,
317 parent
.finish
+ 1 - #parser
.opposite
[parent
.d
]
319 local same_as_inner
= range
.start
== pstart
and range
.finish
== pfinish
320 local same_as_node
= node
and range
.start
== node
.start
and range
.finish
- 1 == node
.finish
321 if node
and not same_as_node
then
322 return node
.start
, node
.finish
+ 1
323 elseif not same_as_inner
then
324 return pstart
, pfinish
326 return parent
.start
, parent
.finish
+ 1
329 -- opening: nil - bracketed list, false - any quasilist
330 quasilist_at
= function(range
, opening
)
331 local lcd
= parser
.opposite
["\n"]
332 if opening
and opening
:match('[%' .. lcd
.. '%"]') then
333 local escaped
= escaped_at(range
)
334 return escaped
and escaped
.d
:find("^"..opening
) and escaped
336 local _
, nodes
= parser
.tree
.sexp_path(range
)
338 local up
= #nodes
- 1
342 until not parent
or (opening
== false or not opening
or parent
.d
== opening
)
348 escaped_at
= escaped_at
,
349 paragraph_at
= parser
.tree
.around
,
350 goto_path
= parser
.tree
.goto_path
,
351 sexp_path
= parser
.tree
.sexp_path
,
352 find_after
= parser
.tree
.find_after
,
353 find_before
= parser
.tree
.find_before
,