add quasilist_at
[lisp-parkour.git] / walker.lua
blob198e764a35fb146eefa7b17251077b019b1ef0c0
1 local M = {}
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]
12 end
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
18 else
19 return node.word_at(range), node
20 end
21 end
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
28 end
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
33 local keypos = key(t)
34 local start = keypos == startof(t)
35 local finish = keypos == finishof(t)
36 local newpos
37 if finish then
38 newpos = t.finish_before(range.finish)
39 elseif start then
40 newpos = t.start_before(range.start)
41 end
42 if newpos then
43 local word = t.word_at({start = newpos, finish = newpos})
44 if word and pred(word) then
45 return word
46 end
47 end
48 return
49 end
50 local _, nearest_n = t.before(range.start, startof)
51 if nearest_n then
52 for n = nearest_n, 1, -1 do
53 local child = t[n]
54 if child.d and not child.is_empty then
55 local ret = find_before_innermost(child, range, key, pred)
56 if ret then
57 return ret
58 end
59 elseif key(child) < key(range) and pred(child, n, #t) then
60 return child, n, #t
61 end
62 end
63 end
64 if not t.is_root and pred(t) then
65 return t
66 end
67 end
68 end
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
73 local keypos = key(t)
74 local start = keypos == startof(t)
75 local finish = keypos == finishof(t)
76 local newpos
77 if finish then
78 newpos = t.finish_after(math.max(t.start + #t.d, range.finish))
79 elseif start then
80 newpos = range.finish < t.start and t.start + #t.d or t.start_after(range.start)
81 end
82 if newpos then
83 local word = t.word_at({start = newpos, finish = newpos})
84 if word and pred(word) then
85 return word
86 end
87 end
88 return
89 end
90 local _, nearest_n = t.after(range.finish, finishof)
91 if nearest_n then
92 for n = nearest_n, #t do
93 local child = t[n]
94 if child.d and not child.is_empty then
95 local ret = find_after_innermost(child, range, key, pred)
96 if ret then
97 return ret
98 end
99 elseif key(child) >= key(range) and pred(child, n, #t) then
100 return child, n, #t
104 if not t.is_root and pred(t) then
105 return t
110 return {
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)
116 end,
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)
122 end,
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
128 end,
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
134 end,
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)
144 if next_list then
145 local first = next_list.after(range.finish, startof, skip)
146 if first then
147 return first.start
148 else
149 return next_list.start + (next_list.p and #next_list.p or 0) + 1
152 end,
154 start_up_before = function(range)
155 local _, parent = sexp_at(range)
156 return parent.d and parent.start
157 end,
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)
163 if prev_list then
164 local last = prev_list.before(range.start, finishof, skip)
165 if last then
166 return (last.finish or last.p and last.start + #last.p) + 1, prev_list
167 else
168 return prev_list.finish, prev_list
171 end,
173 finish_up_after = function(range)
174 local _, parent = sexp_at(range)
175 return parent.d and parent.finish + 1
176 end,
178 indented_before = function(range)
179 return find_before_innermost(parser.tree, range, startof, function(t) return t.indent end)
180 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
192 end,
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
204 end,
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
216 end,
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
228 end,
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
238 end)
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)
242 end,
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
252 end)
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
257 end,
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)
268 end,
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
274 end)
275 return nxt and nxt.finish + 1 + (next_line and parser.tree[n + 1] and parser.tree[n + 1].indent and 1 or 0)
276 end,
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)
281 end,
283 end_of_quasilist = function(range)
284 local _, parent = sexp_at(range)
285 return parent.finish and parent.finish + 1 - #parser.opposite[parent.d]
286 end,
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
293 end,
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
300 parent = node
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
315 end,
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
322 else
323 local _, nodes = parser.tree.sexp_path(range)
324 local parent
325 local up = #nodes - 1
326 repeat
327 parent = nodes[up]
328 up = up - 1
329 until not parent or (opening == false or not opening or parent.d == opening)
330 return parent
332 end,
334 sexp_at = sexp_at,
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,
344 return M