remove paragraph motions
[lisp-parkour.git] / walker.lua
blob5630ab459908dfa19d80a4655fca337e7e207155
1 local M = {}
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]
11 end
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
17 else
18 return node.word_at(range), node
19 end
20 end
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
27 end
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
32 local keypos = key(t)
33 local start = keypos == startof(t)
34 local finish = keypos == finishof(t)
35 local newpos
36 if finish then
37 newpos = t.finish_before(range.finish)
38 elseif start then
39 newpos = t.start_before(range.start)
40 end
41 if newpos then
42 local word = t.word_at({start = newpos, finish = newpos})
43 if word and pred(word) then
44 return word
45 end
46 end
47 return
48 end
49 local _, nearest_n = t.before(range.start, startof)
50 if nearest_n then
51 for n = nearest_n, 1, -1 do
52 local child = t[n]
53 if child.d and not child.is_empty then
54 local ret = find_before_innermost(child, range, key, pred)
55 if ret then
56 return ret
57 end
58 elseif key(child) < key(range) and pred(child, n, #t) then
59 return child, n, #t
60 end
61 end
62 end
63 if not t.is_root and pred(t) then
64 return t
65 end
66 end
67 end
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
72 local keypos = key(t)
73 local start = keypos == startof(t)
74 local finish = keypos == finishof(t)
75 local newpos
76 if finish then
77 newpos = t.finish_after(math.max(t.start + #t.d, range.finish))
78 elseif start then
79 newpos = range.finish < t.start and t.start + #t.d or t.start_after(range.start)
80 end
81 if newpos then
82 local word = t.word_at({start = newpos, finish = newpos})
83 if word and pred(word) then
84 return word
85 end
86 end
87 return
88 end
89 local _, nearest_n = t.after(range.finish, finishof)
90 if nearest_n then
91 for n = nearest_n, #t do
92 local child = t[n]
93 if child.d and not child.is_empty then
94 local ret = find_after_innermost(child, range, key, pred)
95 if ret then
96 return ret
97 end
98 elseif key(child) >= key(range) and pred(child, n, #t) then
99 return child, n, #t
103 if not t.is_root and pred(t) then
104 return t
109 return {
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)
115 end,
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)
121 end,
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
127 end,
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
133 end,
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)
143 if next_list then
144 local first = next_list.after(range.finish, startof, skip)
145 if first then
146 return first.start
147 else
148 return next_list.start + (next_list.p and #next_list.p or 0) + 1
151 end,
153 start_up_before = function(range)
154 local _, parent = sexp_at(range)
155 return parent.d and parent.start
156 end,
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)
162 if prev_list then
163 local last = prev_list.before(range.start, finishof, skip)
164 if last then
165 return (last.finish or last.p and last.start + #last.p) + 1, prev_list
166 else
167 return prev_list.finish, prev_list
170 end,
172 finish_up_after = function(range)
173 local _, parent = sexp_at(range)
174 return parent.d and parent.finish + 1
175 end,
177 indented_before = function(range)
178 return find_before_innermost(parser.tree, range, startof, function(t) return t.indent end)
179 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
191 end,
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
203 end,
205 start_word_after = function(t, range)
206 local newpos = t.start_after(range.start)
207 if newpos then
208 local word = t.word_at({start = newpos, finish = newpos})
209 return word and word.start
210 else
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)
217 end,
219 finish_word_after = function(t, range)
220 local newpos = t.finish_after(math.max(t.start + #t.d, range.finish))
221 if newpos then
222 local word = t.word_at({start = newpos, finish = newpos})
223 return word and word.finish + 1
224 else
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)
231 end,
233 start_word_before = function(t, range)
234 local newpos = t.start_before(range.start)
235 if newpos then
236 local word = t.word_at({start = newpos, finish = newpos})
237 return word and word.start
238 else
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)
245 end,
247 finish_word_before = function(t, range)
248 local newpos = t.finish_before(range.finish)
249 if newpos then
250 local word = t.word_at({start = newpos, finish = newpos})
251 return word and word.finish + 1
252 else
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)
259 end,
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
269 end)
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)
273 end,
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
283 end)
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
288 end,
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)
293 end,
295 end_of_quasilist = function(range)
296 local _, parent = sexp_at(range)
297 return parent.finish and parent.finish + 1 - #parser.opposite[parent.d]
298 end,
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
305 end,
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
312 parent = node
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
327 end,
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
335 else
336 local _, nodes = parser.tree.sexp_path(range)
337 local parent
338 local up = #nodes - 1
339 repeat
340 parent = nodes[up]
341 up = up - 1
342 until not parent or (opening == false or not opening or parent.d == opening)
343 return parent
345 end,
347 sexp_at = sexp_at,
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,
357 return M