improve wording
[lisp-parkour.git] / edit.lua
blob223ebf5ed9c39a48212d6de9da4e930b2a82e8b8
1 local M = {}
3 -- XXX: in Lua 5.2 unpack() was moved into table
4 local unpack = table.unpack or unpack
6 local function startof(node) return node.start end
7 local function finishof(node) return node.finish end
8 local function is_comment(node) return node.is_comment end
10 local function normalize_spacing(start, delta, list)
11 if delta > 0 then
12 table.insert(list, {start + delta, -delta})
13 elseif delta < 0 then
14 table.insert(list, {start, -delta})
15 end
16 return delta
17 end
19 local function leading_space(s, deltas, adj, parent, i, pstart, has_eol)
20 local prev = parent[i - 1]
21 if not s.indent and not (prev and (s.d == "|" or prev.d == "|")) and not has_eol then
22 local off = prev and prev.finish + 1 or pstart
23 adj = adj + normalize_spacing(off, s.start - off - (prev and 1 or 0), deltas)
24 end
25 return adj
26 end
28 local function trailing_space(s, deltas, adj, parent, i, has_eol, keep_electric_space)
29 local nxt = parent[i + 1]
30 local is_last = not nxt
31 if (is_last and not keep_electric_space) or nxt and nxt.indent then
32 local off = is_last and parent.finish - (has_eol and i < #parent and 1 or 0) or nxt.start - nxt.indent - 1
33 local finish = s.finish + (has_eol and i < #parent and 0 or 1)
34 normalize_spacing(finish, off - finish, deltas)
35 end
36 return adj
37 end
39 local function indentation(s, deltas, adj, parent, i, pstart, base_indent, last_distinguished, first_argument,
40 op_adj, indent_adj)
41 if s.indent then
42 local delta = base_indent - s.indent
43 local firstarg_delta = base_indent + parent[1].finish + 2 - pstart - s.indent - op_adj - (indent_adj or 0)
44 if last_distinguished and i > 1 then
45 delta = delta + 1
46 if i - 1 <= last_distinguished then
47 if parent[first_argument + 1].indent then
48 delta = delta + 2
49 else
50 delta = firstarg_delta
51 end
52 elseif last_distinguished < 0
53 and i > first_argument + 1 and not parent[first_argument + 1].indent then
54 delta = firstarg_delta
55 end
56 elseif i == 1 then
57 -- remove leading space
58 delta = -(s.start - pstart)
59 elseif -- align further arguments below the second one
60 parent.d == "(" -- [] and {} contain data, so no arguments
61 and not parent[1].is_string -- a string can't have arguments
62 --and not parent[1].is_list -- wrong, GNU Emacs compatible behaviour
63 then
64 if i > first_argument + 1 and not parent[first_argument + 1].indent then
65 delta = firstarg_delta
66 end
67 if delta < 0 then delta = math.max(-s.indent, delta) end
68 end
69 if delta ~= 0 then
70 table.insert(deltas, {s.start, delta})
71 end
72 adj = adj - delta
73 end
74 return adj
75 end
77 local function is_lone_prefix(node)
78 return node and node.p and (#node.p == node.finish - node.start + 1)
79 end
81 function M.new(parser, walker, fmt, write, delete, eol_at)
83 local function refmt_list(parent, base_indent, padj, deltas, keep_electric_space)
84 local adj = 0
85 local op_adj = 0
86 local indent_adj = 0
87 local pstart = parent.start + (parent.p and #parent.p or 0) + #parent.d
88 if parent.is_empty then
89 normalize_spacing(pstart, parent.finish - pstart, deltas)
90 return deltas
91 end
92 local last_distinguished = fmt:last_distinguished(parent)
93 local first_argument = 1
94 if #parent > 2 and parent[1].is_list and #parent[1] > 1 then
95 local op = parent[1]
96 local nearest_indented = op[#op].indent and op[#op] or walker.indented_before(op[#op])
97 indent_adj = nearest_indented.start - pstart - nearest_indented.indent + 1
98 end
99 for i, s in ipairs(parent) do
100 adj = s.indent and 0 or adj
101 if last_distinguished then
102 -- do not let early comments influence the indentation of real expressions:
103 if i <= last_distinguished and s.is_comment then
104 last_distinguished = last_distinguished + 1
106 -- s is the name of a named let. account for it:
107 if i == last_distinguished + 1 and not s.is_list and parent[1].text == "let" then
108 last_distinguished = last_distinguished + 1
110 -- do not let early comments influence the indentation of real expressions:
111 elseif i <= first_argument + 1 and s.is_comment then
112 first_argument = first_argument + 1
114 local has_eol = s.is_line_comment
115 adj = leading_space(s, deltas, adj, parent, i, pstart, has_eol)
116 adj = trailing_space(s, deltas, adj, parent, i, has_eol, keep_electric_space)
117 adj = indentation(s, deltas, adj, parent, i, pstart, base_indent, last_distinguished, first_argument,
118 op_adj, indent_adj)
119 if i == 1 then
120 op_adj = adj
122 if s.is_list then
123 local nearest_indented = s.indent and s or walker.indented_before(s)
124 local parent_column = nearest_indented and
125 nearest_indented.indent + (s.start + (s.p and #s.p or 0) + #s.d) - nearest_indented.start
126 or base_indent + (s.start + (s.p and #s.p or 0) + #s.d) - pstart
127 refmt_list(s, parent_column - adj, padj + adj, deltas, keep_electric_space)
130 return deltas
133 local function path_from(range)
134 local _, parent_at, m = walker.sexp_at(range, true)
135 local path
136 if parent_at[m] then
137 path = walker.sexp_path(parent_at[m])
138 local base = parent_at[m]
139 if range.finish == base.finish + 1 then
140 path.after_finish = 0 -- lists can end up with a different length
141 else
142 path.after_start = range.start - base.start
144 else
145 local s = parent_at[#parent_at]
146 if not s or range.start >= s.finish + 1 + (s.is_line_comment and 1 or 0) then
147 path = walker.sexp_path(parent_at)
148 path.at_pfinish = true
149 else
150 local prev, n = parent_at.before(range.start, finishof)
151 if prev then
152 path = walker.sexp_path(prev)
153 local nxt = parent_at[n + 1]
154 path.after_finish = nxt and not nxt.indent and 1 or range.finish - prev.finish - 1
155 else
156 path = walker.sexp_path(parent_at[1])
157 path.after_start = 0
161 return path
164 local function pos_from(path, range)
165 local sexp, parentng, n = walker.goto_path(path)
166 if sexp then
167 if path.after_finish then
168 local nxt = n and parentng[n + 1]
169 local max = nxt and nxt.start - sexp.finish - 1
170 return sexp.finish + 1 - (range.finish - range.start) +
171 (max and math.min(path.after_finish, max) or path.after_finish)
172 elseif path.at_pfinish then
173 return sexp.finish
174 else
175 return sexp.start + path.after_start
180 local function refmt_at(scope, range, keep_electric_space)
181 if not range or not scope or scope.is_root or (scope.finish - scope.start < 2) then return range.start end
182 local parent = walker.sexp_at(scope, true)
183 if not (parent and parent.is_list) then return range.start end
184 local path = path_from(range)
185 local indented_parent = parent.indent and parent or parent.is_list and walker.indented_before(parent)
186 local parent_column = indented_parent and (indented_parent.indent + parent.start - indented_parent.start +
187 (parent.p and #parent.p or 0) + #parent.d) or
189 local deltas = refmt_list(parent, parent_column, 0, {}, keep_electric_space)
190 table.sort(deltas, function(d1, d2) return d1[1] > d2[1] end)
191 for _, pair in ipairs(deltas) do
192 local offset, delta = unpack(pair)
193 if delta > 0 then
194 write(offset, string.rep(" ", delta))
195 elseif delta < 0 then
196 delete(offset + delta, -delta)
199 parser.tree.rewind(parent.start or 0)
200 return path and pos_from(path, range) or range.start
203 local function splice(pos, sexps, skip, backwards, action)
204 local spliced
205 local sexp = walker.sexp_at(skip, true)
206 local start = sexp.start + (sexp.p and #sexp.p or 0)
207 -- XXX: don't splice empty line comments _yet_. See _join_or_splice
208 local tosplice = action.splice or action.wrap or not sexp.is_line_comment and sexp.is_empty
209 local opening = (sexp.p or "")..sexp.d
210 local closing = parser.opposite[sexp.d]
211 local real_start = tosplice and sexp.start or start + #sexp.d
212 local splice_closing = not sexp.is_line_comment or sexp.is_empty
213 local real_finish = sexp.finish + 1 - (tosplice and splice_closing and 0 or #closing)
214 local first = backwards and
215 {start = real_start, finish = math.max(pos, start + #sexp.d)} or
216 {start = real_start, finish = sexp.is_empty and pos or start + #sexp.d}
217 local second = backwards and
218 {start = sexp.is_empty and pos or sexp.finish + 1 - #closing, finish = real_finish} or
219 {start = math.min(pos, sexp.finish + 1 - #closing), finish = real_finish}
220 action.func(second)
221 action.func(first)
222 spliced = tosplice
223 if action.kill then
224 local ndeleted = first.finish - first.start + second.finish - second.start
225 if ndeleted > 0 then
226 sexps.rewind(sexp.start)
229 return first.finish - first.start, spliced, opening, closing
233 -- This function handles some whitespace-only deletion corner cases magically.
234 -- It does so by
235 -- a) extending or shrinking the range that is to be deleted
236 -- b) returning an integer to trick pick_out to glide the cursor, but calling refmt to restore the deleted spaces
237 -- c) both a) and b)
238 local function delete_whitespace_only(range, pos)
239 local node, parent = walker.sexp_at(range)
240 if node or not parent.is_list then return end
241 local prev = parent.before(pos, finishof)
242 local nxt = parent.after(pos, startof)
243 if prev and prev.finish >= range.start or nxt and nxt.start < range.finish then return end
244 local backwards = pos == range.finish
245 local adj = 0
246 local eol = eol_at(pos)
247 local on_empty_line = not nxt or eol and nxt.start > eol
248 local empty_line_after_comment = prev and prev.is_line_comment
249 and on_empty_line
250 if nxt and nxt.indent then
251 if backwards and prev then
252 -- join current line with the previous one, unless the latter is a line comment
253 range.start = prev.finish + (empty_line_after_comment and 0 or 1)
254 if not prev.d then
255 range.finish = range.start + 1
256 adj = (on_empty_line and 0 or 1)
258 else
259 adj = nxt.start - range.finish
261 return adj
263 if not nxt and prev then
264 -- clean up trailing whitespace (e.g. electric RET)
265 range.start = prev.finish + 1
266 range.finish = parent.finish
267 elseif nxt and nxt.start == range.finish and (not prev or prev.finish + 1 == range.start) then
268 if prev and prev.d or nxt and nxt.d and not is_lone_prefix(prev) then
269 -- don't delete spaces near delimiters, just slide the cursor:
270 if backwards then
271 range.finish = range.start
272 else
273 range.start = range.finish
276 return adj
280 local function big_enough_parent(pos1, pos2)
281 -- since the pos1-pos2 range can cross list boundaries, find which list contains both pos1 and pos2
282 local _, p1, p2
283 _, p1 = walker.sexp_at({start = pos1, finish = pos1}, true)
284 if p1.is_root or not (p1.start < pos1 and p1.finish > pos2) then
285 _, p2 = walker.sexp_at({start = pos2, finish = pos2}, true)
287 return p2 and not p2.is_root and p2 or p1
290 local function extend_overlap(range)
291 local rnode = walker.sexp_at({start = range.finish, finish = range.finish})
292 if rnode and rnode.p and rnode.p:find";"
293 and range.finish > rnode.start and range.finish < rnode.start + #rnode.p then
294 return {start = range.start, finish = rnode.start + #rnode.p}
296 return range
299 local function pick_out(range, pos, action)
300 local ndeleted = 0
301 if range.start == range.finish then return ndeleted end
302 local sexps = parser.tree
303 local skips = sexps.unbalanced_delimiters(range)
304 -- handle splice and kill-splice of forms and strings:
305 if #skips == 1 then
306 local sexp = walker.sexp_at(skips[1], true)
307 local backward_splice = skips[1].opening and pos >= sexp.start + (sexp.p and #sexp.p or 0) + #sexp.d
308 and range.start >= sexp.start
309 local forward_splice = skips[1].closing and pos <= sexp.finish + 1 - #parser.opposite[sexp.d]
310 and range.finish <= sexp.finish + 1
311 if backward_splice or forward_splice then
312 return splice(backward_splice and range.finish or range.start, sexps, sexp, backward_splice, action)
315 local node, parent = walker.sexp_at({start = range.finish, finish = range.finish})
316 -- if the range ends with a line comment, don't delete its closing newline:
317 local drop_eol = action.kill and node and
318 node.finish + 1 == range.finish and node.is_line_comment
319 local par = big_enough_parent(range.start, range.finish)
320 local operator_changed = par[1] and par[1].finish >= range.start
321 local refmt = #skips == 0 and delete_whitespace_only(range, pos) or operator_changed and 0
322 table.sort(skips, function(a, b) return a.start < b.start end)
323 table.insert(skips, {start = range.finish - (drop_eol and 1 or 0)})
324 table.insert(skips, 1, {finish = range.start})
325 ndeleted = ndeleted + (drop_eol and 1 or 0)
326 for i = #skips - 1, 1, -1 do
327 local region = {start = skips[i].finish, finish = skips[i + 1].start}
328 if skips[i].closing and skips[i + 1].opening then
329 -- leave out some of the space between adjacent lists
330 local _, rparent = walker.sexp_at(region)
331 local nxt = rparent.after(region.start, startof)
332 region.start = nxt and nxt.start or region.start
334 if action then
335 action.func(region)
336 ndeleted = ndeleted + (region.finish - region.start)
339 -- if parent[#parent + 1] is nil, we are at EOF
340 if ndeleted > 0 and (not parent.is_root or parent.is_parsed(range.start) or parent[#parent + 1]) then
341 sexps.rewind(range.start)
343 return ndeleted - (refmt or 0), nil, nil, nil, refmt
346 local function raise_sexp(range, pos)
347 local sexp, parent = walker.sexp_at(range, true)
348 if sexp and parent and parent.is_list then
349 delete(sexp.finish + 1, parent.finish - sexp.finish)
350 delete(parent.start, sexp.start - parent.start)
351 parser.tree.rewind(parent.start)
352 range.start = parent.start + pos - sexp.start
353 range.finish = range.start
354 local _, nodes = walker.sexp_path(range)
355 local grandparent = nodes[#nodes - 2]
356 return grandparent and refmt_at(grandparent, range) or range.start
360 local function slurp_sexp(range, forward)
361 local _, parent = walker.sexp_at(range, true)
362 local seeker = forward and walker.finish_after or walker.start_before
363 if not parent or not parent.is_list then return range.start end
364 local r = {start = parent.start, finish = parent.finish + 1}
365 local newpos = seeker(r, is_comment)
366 if not newpos then return range.start end
367 local opening = (parent.p or "")..parent.d
368 local closing = parser.opposite[parent.d]
369 local delimiter = forward and closing or opening
370 if forward then
371 write(newpos, delimiter)
373 delete(forward and parent.finish or parent.start, #delimiter)
374 if not forward then
375 write(newpos, delimiter)
377 parser.tree.rewind(math.min(parent.start, newpos))
378 return refmt_at(big_enough_parent(newpos, range.start), range)
381 local function barf_sexp(range, forward)
382 local _, parent = walker.sexp_at(range, true)
383 local seeker = forward and walker.finish_before or walker.start_after
384 -- TODO: barfing out of strings requires calling the parser on them
385 if not parent or not parent.is_list or parent.is_empty then return range.start end
386 local opening = (parent.p or "")..parent.d
387 local pstart = parent.start + #opening
388 local r = {start = forward and parent.finish - 1 or pstart, finish = forward and parent.finish or pstart + 1}
389 local newpos = seeker(r, is_comment) or forward and pstart or parent.finish
390 local closing = parser.opposite[parent.d]
391 local delimiter = forward and closing or opening
392 if not forward then
393 write(newpos, delimiter)
395 delete(forward and parent.finish or parent.start, #delimiter)
396 if forward then
397 write(newpos, delimiter)
399 parser.tree.rewind(math.min(parent.start, newpos))
400 local drag = forward and (newpos < range.finish) or not forward and (newpos > range.start)
401 local keep_inside = forward and #parent > 1 and range.finish > range.start and 1 or 0
402 newpos = drag and newpos - keep_inside or range.start
403 local rangeng = {start = newpos, finish = newpos}
404 return refmt_at(big_enough_parent(newpos, parent.finish + 1), rangeng)
407 local function splice_sexp(range, _, no_refmt)
408 local _, parent = walker.sexp_at(range)
409 if not parent or not parent.d then return end
410 local opening = (parent.p or "")..parent.d
411 local closing = parser.opposite[parent.d]
412 local finish = parent.finish + 1 - #closing
413 if not parent.is_line_comment then
414 delete(finish, #closing)
416 -- TODO: (un)escape special characters, if necessary
417 delete(parent.start, parent.is_empty and (finish - parent.start) or #opening)
418 parser.tree.rewind(parent.start)
419 range.start = range.start - #opening
420 range.finish = range.start
421 local _, parentng = walker.sexp_at(range, true)
422 return not no_refmt and refmt_at(parentng, range) or range.start
425 local function rewrap(parent, kind)
426 local pstart = parent.start + #((parent.p or "")..parent.d) - 1
427 delete(parent.finish, 1)
428 write(parent.finish, parser.opposite[kind])
429 delete(pstart, #parent.d)
430 write(pstart, kind)
431 parser.tree.rewind(parent.start)
434 local function cycle_wrap(range, pos)
435 local _, parent = walker.sexp_at(range)
436 if not parent or not parent.is_list then return end
437 local next_kind = {["("] = "[", ["["] = "{", ["{"] = "("}
438 rewrap(parent, next_kind[parent.d])
439 return pos
442 local function split_sexp(range)
443 local _, parent = walker.sexp_at(range)
444 if not (parent and parent.d) then return end
445 local new_finish, new_start
446 if parent.is_list then
447 local prev = parent.before(range.start, finishof, is_comment)
448 new_finish = prev and prev.finish + 1
449 -- XXX: do not skip comments here, so they end up in the second list
450 -- and are not separated from their target expression:
451 local nxt = new_finish and parent.after(new_finish, startof)
452 new_start = nxt and nxt.start
453 else
454 new_start = range.start
455 new_finish = range.start
457 if not (new_start and new_finish) then return end
458 local opening = (parent.p or "")..parent.d
459 local closing = parser.opposite[parent.d]
460 write(new_start, opening)
461 local sep = parent.is_line_comment and "" -- line comments already have a separator
462 or new_finish == new_start and " " -- only add a separator if there was none before
463 or ""
464 write(new_finish, closing..sep)
465 parser.tree.rewind(parent.start)
466 range.start = new_start + (parent.is_list and 0 or #opening + #closing)
467 range.finish = range.start
468 local _, nodes = walker.sexp_path(range)
469 local parentng, grandparent = nodes[#nodes - 1], nodes[#nodes - 2]
470 local scope = parentng and not parentng.is_root and parentng or grandparent
471 return refmt_at(scope, range)
474 local function join_sexps(range)
475 local node, parent = walker.sexp_at(range, true)
476 local first = node and node.finish + 1 == range.start and node or parent.before(range.start, finishof)
477 local second = first ~= node and node or parent.after(range.start, startof)
478 if not (first and second and first.d and
479 -- don't join line comments to margin comments:
480 (not first.is_line_comment or first.indent and second.indent) and
481 (first.d == second.d or
482 -- join line comments even when their delimiters differ slightly
483 -- (different number of semicolons, existence/lack of a space after them)
484 parser.opposite[first.d] == parser.opposite[second.d])) then
485 return
487 local opening = (second.p or "")..second.d
488 local closing = parser.opposite[first.d]
489 local pos
490 if not first.is_list then
491 pos = first.finish + 1 - #closing
492 delete(pos, second.start + #opening - pos)
493 else
494 delete(second.start, #opening)
495 delete(first.finish, #closing)
496 pos = second.start - #closing
498 parser.tree.rewind(first.start)
499 range.start = pos
500 range.finish = range.start
501 local _, nodes = walker.sexp_path(range)
502 local parentng, grandparent = nodes[#nodes - 1], nodes[#nodes - 2]
503 local scope = parentng and not parentng.is_root and parentng or grandparent
504 return refmt_at(scope, range)
507 local function delete_splicing(range, pos, splicing, delete_and_yank)
508 local action = {kill = true, wrap = splicing, splice = splicing, func = delete_and_yank}
509 local sexp, parent, n = walker.sexp_at(range)
510 local nxt = n and parent[n + 1]
511 local prev = n and parent[n - 1]
512 range = extend_overlap(range)
513 local ndeleted, spliced, opening, closing = pick_out(range, pos, action)
514 local inner_list_len = spliced and sexp.finish - sexp.start + 1 - #opening - #closing
515 local range_len = range.finish - range.start
516 local backwards = pos == range.finish
517 local whole_object = sexp and
518 (sexp.start == range.start and sexp.finish + 1 == range.finish
519 or spliced and inner_list_len <= range_len - (backwards and #opening or #closing))
520 local in_head_atom = sexp and not sexp.d and n == 1 and #parent > 1
521 local in_whitespace = not sexp
522 if whole_object or spliced or in_whitespace or in_head_atom then
523 local cur = (whole_object or spliced) and (prev and prev.finish or sexp.start) or range.start
524 -- if parent[#parent + 1] is nil, we are at EOF
525 if not parent.is_root or parent.is_parsed(cur) or parent[#parent + 1] then
526 parser.tree.rewind(cur)
528 -- make sure the cursor is not left in whitespace:
529 if whole_object and nxt then
530 cur = nxt.start - range_len - (spliced and (backwards and #closing or #opening) or 0)
531 elseif whole_object and prev then
532 cur = prev.finish
533 elseif whole_object and parent.d and not parent.is_list then
534 local _, parentng = walker.sexp_at({start = cur, finish = cur})
535 local trailing = parentng.spaces_after(sexp.start)
536 local leading = not trailing and parentng.spaces_before(sexp.start)
537 if trailing then
538 delete(sexp.start, trailing - sexp.start)
539 parser.tree.rewind(sexp.start)
540 elseif leading then
541 delete(leading, sexp.start - leading)
542 parser.tree.rewind(leading)
543 cur = leading
545 elseif spliced then
546 cur = (backwards and sexp.start or (nxt and nxt.start - range_len or range.start) - ndeleted)
548 local r = {start = cur, finish = cur + 1}
549 local _, parentng = walker.sexp_at(r, true)
550 return refmt_at(parentng, r)
552 return not backwards and ndeleted <= 0 and range.finish - ndeleted
553 or backwards and ndeleted <= 0 and range.start + ndeleted
554 or range.start
557 local function transpose(range, first, second)
558 if not (first and second) then return end
559 local copy1 = first.text
560 local copy2 = second.text
561 delete(second.start, second.finish + 1 - second.start)
562 write(second.start, copy1)
563 delete(first.start, first.finish + 1 - first.start)
564 write(first.start, copy2)
565 parser.tree.rewind(first.start)
566 range.start = second.finish + 1
567 range.finish = range.start
568 return refmt_at(big_enough_parent(first.start, second.finish), range)
571 local function transpose_sexps(range)
572 local node, parent = walker.sexp_at(range, true)
573 local first = node and node.finish + 1 == range.start and node or parent.before(range.start, finishof)
574 local second = first ~= node and node or parent.after(range.start, startof)
575 return transpose(range, first, second)
578 local function transpose_words(range)
579 local first, second, _
580 first = walker.sexp_at(range)
581 if not first or first.d or first.start == range.finish then
582 _, first = walker.start_float_before(range)
584 if first then
585 _, second = walker.finish_float_after({start = first.finish + 1, finish = first.finish + 1})
587 return transpose(range, first, second)
590 local function transpose_chars(range)
591 local node, parent, i = walker.sexp_at(range)
592 local nxt = i and parent[i + 1]
593 local pstart = not parent.is_root and parent.start + (parent.p and #parent.p or 0) + #parent.d
594 local pfinish = not parent.is_root and parent.finish
595 local npref = node and node.p and node.start + #node.p
596 -- only allow transposing while inside atoms/words and prefixes
597 if node and ((npref and (range.start <= npref and range.start > node.start) or
598 node.d and range.start > node.finish + 1) or not node.d and (not pstart or range.start > pstart)) then
599 local start = range.start -
600 ((range.start == pfinish or range.start == npref or
601 range.start == node.finish + 1 and (parent.is_list or parent.is_root) and (not nxt or nxt.indent)) and 1 or 0)
602 local str_start = start - node.start + 1
603 local char = node.text:sub(str_start, str_start)
604 delete(start, 1)
605 write(start - 1, #char > 0 and char or " ")
606 parser.tree.rewind(start)
607 local in_head_atom = i == 1 and #parent > 1
608 return parent.is_list and in_head_atom and refmt_at(parent, {start = start + 1, finish = start + 1}) or start + 1
612 local function _join_or_splice(parent, n, range, pos)
613 local sexp = parent[n]
614 local nxt = parent[n + 1]
615 local is_last = not nxt or not nxt.is_line_comment
616 local newpos = not (is_last and sexp.is_empty) and join_sexps(range)
617 if not newpos and sexp.is_empty then
618 newpos = splice_sexp({start = pos, finish = pos}, nil, true)
620 return newpos or pos
623 local function delete_nonsplicing(range, pos, delete_maybe_yank)
624 local action = {kill = true, wrap = false, splice = false, func = delete_maybe_yank}
625 range = extend_overlap(range)
626 local ndeleted, spliced, opening, _, refmt = pick_out(range, pos, action)
627 local backwards = pos == range.finish
628 if opening then
629 if spliced then
630 return pos - ndeleted
631 else
632 if ndeleted == 0 then
633 local closing = parser.opposite[opening] -- XXX: why don't I use the pick_out return value?
634 if closing == "\n" then
635 local sexp, parent, n = walker.sexp_at(range)
636 if pos == sexp.start + #sexp.d and backwards then
637 return _join_or_splice(parent, n, range, pos)
638 elseif pos == sexp.finish then
639 local r = {start = pos + #closing, finish = pos + #closing}
640 return _join_or_splice(parent, n, r, pos)
644 return backwards and (range.finish - ndeleted) or range.start
646 else
647 local newpos = backwards and range.start + (ndeleted <= 0 and ndeleted or 0) or (range.finish - ndeleted)
648 if refmt then
649 local r = {start = newpos, finish = newpos}
650 return refmt_at(big_enough_parent(range.start, range.finish - ndeleted), r, true)
652 return newpos
656 local function insert_pair(range, delimiter, auto_square)
657 local indices, nodes = walker.sexp_path(range)
658 local sexp = nodes[#nodes]
659 local right_after_prefix = sexp and sexp.p and range.start == sexp.start + #sexp.p
660 -- XXX: here I assume that # is a valid prefix for the dialect
661 local mb_closing = (delimiter == "|") and right_after_prefix and parser.opposite[sexp.p .. delimiter]
662 local closing = mb_closing or parser.opposite[delimiter]
663 local squarewords = fmt.squarewords
664 if squarewords and not right_after_prefix
665 and (auto_square or squarewords.not_optional)
666 and not fmt:adjust_bracket_p(indices, nodes, range) then
667 delimiter, closing = "[", "]"
669 write(range.finish, closing)
670 write(range.start, delimiter)
671 if right_after_prefix or parser.tree.is_parsed(range.start) then
672 parser.tree.rewind(right_after_prefix and sexp.start or range.start)
674 return range.start + #delimiter
677 local function make_wrap(opening)
678 return parser.opposite[opening] and function(range, pos, auto_square)
679 local function _wrap(r)
680 if not (r.finish > r.start) then return end
681 insert_pair(r, opening, false, auto_square)
683 local action = {kill = false, wrap = false, splice = false, func = _wrap}
684 pick_out(range, pos, action)
685 local rangeng = {start = pos + #opening, finish = range.finish}
686 local _, parentng = walker.sexp_at(range) -- TODO: range.finish is wrong. use sexp_path and big_enough_parent
687 return refmt_at(parentng, rangeng)
691 local function newline(parent, range)
692 local line_comment = parent.is_line_comment
693 -- do not autoextend margin comments:
694 if line_comment and range.start < parent.finish + (parent.indent and 1 or 0) then
695 local newpos = split_sexp(range)
696 if newpos then
697 return newpos
700 if not parent.is_list and not line_comment then
701 -- if parent[#parent + 1] is nil, we are at EOF
702 if not parent.is_root or parent.is_parsed(range.start) or parent[#parent + 1] then
703 parser.tree.rewind(parent.start or range.start)
705 local newpos = range.start
706 write(newpos, "\n")
707 return newpos + 1
709 if not parent.is_list then
710 local _
711 _, parent = walker.sexp_at(parent, true)
713 local last_nonblank_in_list = not parent.is_empty and parent[#parent].finish - (line_comment and 1 or 0)
714 local nxt = parent.after(range.start, startof)
715 local last_on_line = range.start == eol_at(range.start)
716 local margin = nxt and not nxt.indent and nxt.is_line_comment and nxt.finish
717 local after_last = last_nonblank_in_list and range.start > last_nonblank_in_list
718 local in_indent = nxt and nxt.indent and range.start <= nxt.start and range.start >= (nxt.start - nxt.indent)
719 local placeholder = "asdf"
720 local newpos = margin or range.start
721 if not parent.is_empty then
722 if in_indent then
723 write(newpos, (placeholder.."\n"))
724 else
725 write(newpos, "\n"..((after_last or last_on_line or margin) and placeholder or ""))
728 parser.tree.rewind(parent.start or newpos)
729 -- move the cursor onto the placeholder, so refmt_at can restore the position:
730 local r = {start = newpos, finish = newpos}
731 newpos = walker.start_after(r) or newpos
732 local rangeng = {start = newpos, finish = newpos}
733 newpos = refmt_at(parent, rangeng)
734 local _, parentng = walker.sexp_at({start = newpos, finish = newpos}, true)
735 if after_last then
736 local autoindent = parentng[#parentng].indent
737 write(parentng.finish, "\n"..string.rep(" ", autoindent or 0))
739 if after_last or last_on_line or margin or in_indent then
740 delete(newpos, #placeholder)
742 parser.tree.rewind(parentng.start or newpos)
743 return newpos
746 local function close_and_newline(range, closing)
747 local opening = closing and parser.opposite[closing]
748 local parent = walker.list_at(range, opening)
749 if parent then
750 local has_eol = parent.is_line_comment
751 local r = {start = parent.finish, finish = parent.finish}
752 local newpos = refmt_at(parent, r)
753 r = {start = newpos + (has_eol and 0 or 1), finish = newpos + (has_eol and 0 or 1)}
754 local list, parentng = walker.sexp_at(r)
755 newpos = list and list.finish + 1 or r.start
756 newpos = newline(parentng, {start = newpos, finish = newpos})
757 if not newpos then
758 newpos = r.finish + 1
759 write(newpos, "\n")
760 parser.tree.rewind(parentng.start or list.start)
762 return newpos
763 elseif closing == "\n" then
764 local eol = eol_at(range.start)
765 local _, parentng = walker.sexp_at({start = eol, finish = eol})
766 local newpos = newline(parentng, {start = eol, finish = eol})
767 return newpos
769 return range.start
772 local function join_line(_, pos)
773 local eol = eol_at(pos)
774 local r = {start = eol, finish = eol + 1}
775 return delete_nonsplicing(r, r.start, delete)
778 local wrap_doublequote = make_wrap'"'
780 local function meta_doublequote(range, pos, auto_square)
781 local escaped = walker.escaped_at(range)
782 if not escaped then
783 return wrap_doublequote(range, pos, nil, auto_square)
784 elseif escaped.is_string then
785 return close_and_newline(range, '"')
787 return pos
790 local block_comment_start
791 for o, c in pairs(parser.opposite) do
792 if #c > 1 then
793 block_comment_start = o
794 break
798 local wrap_comment = make_wrap(block_comment_start)
800 return {
801 delete_splicing = delete_splicing,
802 delete_nonsplicing = delete_nonsplicing,
803 refmt_at = refmt_at,
804 pick_out = pick_out,
805 raise_sexp = raise_sexp,
806 slurp_sexp = slurp_sexp,
807 barf_sexp = barf_sexp,
808 splice_sexp = splice_sexp,
809 wrap_round = make_wrap"(",
810 wrap_square = make_wrap"[",
811 wrap_curly = make_wrap"{",
812 meta_doublequote = meta_doublequote,
813 wrap_comment = wrap_comment,
814 insert_pair = insert_pair,
815 newline = newline,
816 join_line = join_line,
817 close_and_newline = close_and_newline,
818 cycle_wrap = cycle_wrap,
819 split_sexp = split_sexp,
820 join_sexps = join_sexps,
821 transpose_sexps = transpose_sexps,
822 transpose_words = transpose_words,
823 transpose_chars = transpose_chars,
827 return M