3 local function finishof(node
) return node
.finish
end
4 local function startof(node
) return node
.start
end
5 local function is_comment(node
) return node
.is_comment
end
7 function M
.new(parser
, eol_at
, bol_at
)
9 local function in_quasilist(t
, range
)
10 return t
and t
.d
and not t
.is_list
and
11 range
.start
>= t
.start
+ #t
.d
and range
.finish
<= t
.finish
+ 1 - #parser
.opposite
[t
.d
]
14 local function sexp_at(range
, true_sexp
)
15 local node
, parent
, nth
= parser
.tree
.sexp_at(range
)
16 if not in_quasilist(node
, range
) or true_sexp
then
17 return node
, parent
, nth
19 return node
.word_at(range
), node
23 local function escaped_at(range
)
24 local node
= parser
.escaped
.around(range
)
25 -- TODO: distinguish between doublequoted strings, chars, line comments, and block comments
26 -- and use approprite offset comparisons for each
27 return node
and range
.start
> node
.start
and range
.finish
<= node
.finish
and node
30 local function find_before_innermost(t
, range
, key
, pred
)
31 if t
.is_root
or (t
.d
and t
.start
< range
.start
) then
32 if t
.d
and not t
.is_list
then
34 local start
= keypos
== startof(t
)
35 local finish
= keypos
== finishof(t
)
38 newpos
= t
.finish_before(range
.finish
)
40 newpos
= t
.start_before(range
.start
)
43 local word
= t
.word_at({start
= newpos
, finish
= newpos
})
44 if word
and pred(word
) then
50 local _
, nearest_n
= t
.before(range
.start
, startof
)
52 for n
= nearest_n
, 1, -1 do
54 if child
.d
and not child
.is_empty
then
55 local ret
= find_before_innermost(child
, range
, key
, pred
)
59 elseif key(child
) < key(range
) and pred(child
, n
, #t
) then
64 if not t
.is_root
and pred(t
) then
70 local function find_after_innermost(t
, range
, key
, pred
)
71 if t
.is_root
or (t
.d
and t
.finish
>= range
.finish
) then
72 if t
.d
and not t
.is_list
then
74 local start
= keypos
== startof(t
)
75 local finish
= keypos
== finishof(t
)
78 newpos
= t
.finish_after(math
.max(t
.start
+ #t
.d
, range
.finish
))
80 newpos
= range
.finish
< t
.start
and t
.start
+ #t
.d
or t
.start_after(range
.start
)
83 local word
= t
.word_at({start
= newpos
, finish
= newpos
})
84 if word
and pred(word
) then
90 local _
, nearest_n
= t
.after(range
.finish
, finishof
)
92 for n
= nearest_n
, #t
do
94 if child
.d
and not child
.is_empty
then
95 local ret
= find_after_innermost(child
, range
, key
, pred
)
99 elseif key(child
) >= key(range
) and pred(child
, n
, #t
) then
104 if not t
.is_root
and pred(t
) then
112 start_before
= function(range
, skip
)
113 local _
, parent
= parser
.tree
.sexp_at(range
)
114 local node
= parent
.before(range
.start
, startof
, skip
)
115 return node
and startof(node
)
118 start_after
= function(range
, skip
)
119 local _
, parent
= parser
.tree
.sexp_at(range
)
120 local node
= parent
.after(range
.finish
, startof
, skip
)
121 return node
and startof(node
)
124 finish_before
= function(range
, skip
)
125 local _
, parent
= parser
.tree
.sexp_at(range
)
126 local node
= parent
.before(range
.start
, finishof
, skip
)
127 return node
and finishof(node
) + 1
130 finish_after
= function(range
, skip
)
131 local _
, parent
= parser
.tree
.sexp_at(range
)
132 local node
= parent
.after(range
.finish
, finishof
, skip
)
133 return node
and finishof(node
) + 1
136 start_down_after
= function(range
, skip
)
137 local node
, parent
= parser
.tree
.sexp_at(range
)
138 if in_quasilist(node
, range
) then return end
139 if node
and node
.p
then
140 -- XXX: if we are past a prefix start, this will prevent skipping over its list
141 range
.start
= node
.start
143 local next_list
= parent
.find_after(range
, function(t
) return t
.is_list
end)
145 local first
= next_list
.after(range
.finish
, startof
, skip
)
149 return next_list
.start
+ (next_list
.p
and #next_list
.p
or 0) + 1
154 start_up_before
= function(range
)
155 local _
, parent
= sexp_at(range
)
156 return parent
.d
and parent
.start
159 finish_down_before
= function(range
, skip
)
160 local node
, parent
= parser
.tree
.sexp_at(range
)
161 if in_quasilist(node
, range
) then return end
162 local prev_list
= parent
.find_before(range
, function(t
) return t
.is_list
end)
164 local last
= prev_list
.before(range
.start
, finishof
, skip
)
166 return (last
.finish
or last
.p
and last
.start
+ #last
.p
) + 1, prev_list
168 return prev_list
.finish
, prev_list
173 finish_up_after
= function(range
)
174 local _
, parent
= sexp_at(range
)
175 return parent
.d
and parent
.finish
+ 1
178 indented_before
= function(range
)
179 return find_before_innermost(parser
.tree
, range
, startof
, function(t
) return t
.indent
end)
182 start_float_before
= function(range
, up_empty_list
)
183 if up_empty_list
then
184 local _
, parent
= sexp_at(range
)
185 if parent
.is_empty
then
186 return parent
.start
, parent
189 local node
= find_before_innermost(parser
.tree
, range
, startof
, function(t
, n
, _
)
190 return not t
.d
or t
.is_empty
and n
== 1 end)
191 return node
and node
.start
, node
194 start_float_after
= function(range
, up_empty_list
)
195 if up_empty_list
then
196 local _
, parent
= sexp_at(range
)
197 if parent
.is_empty
then
198 return parent
.finish
+ 1, parent
201 local node
= find_after_innermost(parser
.tree
, range
, startof
, function(t
, n
, max_n
)
202 return not t
.d
or t
.is_empty
and n
== max_n
end)
203 return node
and node
.start
, node
206 finish_float_before
= function(range
, up_empty_list
)
207 if up_empty_list
then
208 local _
, parent
= sexp_at(range
)
209 if parent
.is_empty
then
210 return parent
.start
, parent
213 local node
= find_before_innermost(parser
.tree
, range
, finishof
, function(t
, n
, _
)
214 return not t
.d
or t
.is_empty
and n
== 1 end)
215 return node
and node
.finish
+ 1, node
218 finish_float_after
= function(range
, up_empty_list
)
219 if up_empty_list
then
220 local _
, parent
= sexp_at(range
)
221 if parent
.is_empty
then
222 return parent
.finish
+ 1, parent
225 local node
= find_after_innermost(parser
.tree
, range
, finishof
, function(t
, n
, max_n
)
226 return not t
.d
or t
.is_empty
and n
== max_n
end)
227 return node
and node
.finish
+ 1, node
230 prev_start_wrapped
= function(range
)
231 local node
, parent
= parser
.tree
.sexp_at(range
)
232 if in_quasilist(node
, range
) then parent
= node
end
233 local pstart
= parent
.start
and (parent
.start
+ (parent
.p
and #parent
.p
or 0) + #parent
.d
)
234 local bol
= bol_at(range
.start
)
235 local prev_wrapped
= (parent
.is_list
or parent
.is_root
) and parent
.find_before(range
,
236 function(t
) return t
.start
< bol
and t
.finish
> bol
end,
237 function(t
) return t
.finish
< bol
239 local prev_start
= prev_wrapped
and prev_wrapped
.start
or bol
240 pstart
= pstart
or prev_start
241 return math
.max(pstart
, prev_start
)
244 next_finish_wrapped
= function(range
)
245 local node
, parent
= parser
.tree
.sexp_at(range
)
246 if in_quasilist(node
, range
) then parent
= node
end
247 local pfinish
= parent
.finish
and parent
.finish
+ 1 - #parser
.opposite
[parent
.d
]
248 local eol
= eol_at(range
.start
)
249 local next_wrapped
= (parent
.is_list
or parent
.is_root
) and parent
.find_after(range
,
250 function(t
) return t
.start
< eol
and t
.finish
> eol
end,
251 function(t
) return t
.start
> eol
253 local next_finish
= next_wrapped
and next_wrapped
.finish
+ 1 or eol
254 -- XXX: second return value, to be used for safe line-commenting:
255 local next_start_on_line
= math
.min(pfinish
or range
.start
, next_wrapped
and next_wrapped
.start
or eol
)
256 return math
.min(pfinish
or next_finish
, next_finish
), next_start_on_line
< eol
and next_start_on_line
or nil
259 paragraph_start_before
= function(range
, prev_line
)
260 local prev
, n
= parser
.tree
.before(range
.start
, startof
, is_comment
)
261 if not prev_line
then
262 return prev
and prev
.start
264 local pprev
= n
and parser
.tree
[n
- 1]
265 local has_eol
= pprev
and pprev
.is_comment
and parser
.opposite
[pprev
.d
] == "\n"
266 local adjacent
= pprev
and prev
and prev
.start
- (pprev
.finish
+ (has_eol
and 0 or 1)) <= 1
267 return prev
and prev
.start
- ((adjacent
or prev
.start
== 0) and 0 or 1)
270 paragraph_finish_after
= function(range
, next_line
)
271 local node
= parser
.tree
.around(range
)
272 local nxt
, n
= parser
.tree
.find_after(node
or range
, function(t
)
273 return not t
.is_comment
and t
.finish
> range
.finish
275 return nxt
and nxt
.finish
+ 1 + (next_line
and parser
.tree
[n
+ 1] and parser
.tree
[n
+ 1].indent
and 1 or 0)
278 beginning_of_quasilist
= function(range
)
279 local _
, parent
= sexp_at(range
)
280 return parent
.start
and (parent
.start
+ (parent
.p
and #parent
.p
or 0) + #parent
.d
)
283 end_of_quasilist
= function(range
)
284 local _
, parent
= sexp_at(range
)
285 return parent
.finish
and parent
.finish
+ 1 - #parser
.opposite
[parent
.d
]
288 repl_line_begin
= function(range
)
289 local _
, parent
= parser
.tree
.sexp_at(range
)
290 if not parent
.is_root
then return end
291 local prev_indent
= parent
.find_before(range
, function(t
) return t
.indent
end)
292 return prev_indent
and prev_indent
.start
295 wider_than
= function(range
)
296 local node
, parent
= parser
.tree
.sexp_at(range
)
297 if not node
and parent
.is_root
then return end
298 local pstart
, pfinish
299 if in_quasilist(node
, range
) then
301 node
= node
.word_at(range
)
303 if not parent
.is_root
then
304 pstart
, pfinish
= parent
.start
+ (parent
.p
and #parent
.p
or 0) + #parent
.d
,
305 parent
.finish
+ 1 - #parser
.opposite
[parent
.d
]
307 local same_as_inner
= range
.start
== pstart
and range
.finish
== pfinish
308 local same_as_node
= node
and range
.start
== node
.start
and range
.finish
- 1 == node
.finish
309 if node
and not same_as_node
then
310 return node
.start
, node
.finish
+ 1
311 elseif not same_as_inner
then
312 return pstart
, pfinish
314 return parent
.start
, parent
.finish
+ 1
317 -- opening: nil - bracketed list; false - any quasilist;
318 quasilist_at
= function(range
, opening
)
319 if opening
and opening
:match
'[%;%"]' then
320 local escaped
= escaped_at(range
)
321 return escaped
and escaped
.d
:find("^"..opening
) and escaped
323 local _
, nodes
= parser
.tree
.sexp_path(range
)
325 local up
= #nodes
- 1
329 until not parent
or (opening
== false or not opening
or parent
.d
== opening
)
335 escaped_at
= escaped_at
,
336 paragraph_at
= parser
.tree
.around
,
337 goto_path
= parser
.tree
.goto_path
,
338 sexp_path
= parser
.tree
.sexp_path
,
339 find_after
= parser
.tree
.find_after
,
340 find_before
= parser
.tree
.find_before
,