fix barfing
[lisp-parkour.git] / edit.lua
blob094e15b9e5fcc3dbd57729ed9ded3ff943db8bf6
1 local M = {}
3 -- XXX: in Lua 5.2 unpack() was moved into table
4 local unpack = unpack or table.unpack
6 local swap = {["["] = "(", ["("] = "["}
8 local function startof(node) return node.start end
9 local function finishof(node) return node.finish end
10 local function is_comment(node) return node.is_comment end
12 local function normalize_spacing(start, delta, list)
13 if delta > 0 then
14 table.insert(list, {start + delta, -delta})
15 elseif delta < 0 then
16 table.insert(list, {start, -delta})
17 end
18 return delta
19 end
21 local function leading_space(s, deltas, adj, parent, i, pstart, has_eol)
22 local prev = parent[i - 1]
23 if not s.indent and not (prev and (s.d == "|" or prev.d == "|")) and not has_eol then
24 local off = prev and prev.finish + 1 or pstart
25 adj = adj + normalize_spacing(off, s.start - off - (prev and 1 or 0), deltas)
26 end
27 return adj
28 end
30 local function trailing_space(s, deltas, adj, parent, i, has_eol, keep_electric_space)
31 local nxt = parent[i + 1]
32 local is_last = not nxt
33 if (is_last and not keep_electric_space) or nxt and nxt.indent then
34 local off = is_last and parent.finish - (has_eol and i < #parent and 1 or 0) or nxt.start - nxt.indent - 1
35 local finish = s.finish + (has_eol and i < #parent and 0 or 1)
36 normalize_spacing(finish, off - finish, deltas)
37 end
38 return adj
39 end
41 local function indentation(s, deltas, adj, parent, i, pstart, base_indent, last_distinguished, first_argument,
42 op_adj, indent_adj)
43 if s.indent then
44 local delta = base_indent - s.indent
45 local firstarg_delta = base_indent + parent[1].finish + 2 - pstart - s.indent - op_adj - (indent_adj or 0)
46 if last_distinguished and i > 1 then
47 delta = delta + 1
48 if i - 1 <= last_distinguished then
49 if parent[first_argument + 1].indent then
50 delta = delta + 2
51 else
52 delta = firstarg_delta
53 end
54 elseif last_distinguished < 0
55 and i > first_argument + 1 and not parent[first_argument + 1].indent then
56 delta = firstarg_delta
57 end
58 elseif i == 1 then
59 -- remove leading space
60 delta = -(s.start - pstart)
61 elseif -- align further arguments below the second one
62 parent.d == "(" -- [] and {} contain data, so no arguments
63 and not parent[1].is_string -- a string can't have arguments
64 --and not parent[1].is_list -- wrong, GNU Emacs compatible behaviour
65 then
66 if i > first_argument + 1 and not parent[first_argument + 1].indent then
67 delta = firstarg_delta
68 end
69 if delta < 0 then delta = math.max(-s.indent, delta) end
70 end
71 if delta ~= 0 then
72 table.insert(deltas, {s.start, delta})
73 end
74 adj = adj - delta
75 end
76 return adj
77 end
79 function M.new(parser, walker, fmt, write, delete, eol_at)
81 local function refmt_list(parent, base_indent, padj, deltas, keep_electric_space)
82 local adj = 0
83 local op_adj = 0
84 local indent_adj = 0
85 local pstart = parent.start + (parent.p and #parent.p or 0) + #parent.d
86 if parent.is_empty then
87 normalize_spacing(pstart, parent.finish - pstart, deltas)
88 return deltas
89 end
90 local last_distinguished = fmt:last_distinguished(parent)
91 local first_argument = 1
92 if #parent > 2 and parent[1].is_list and #parent[1] > 1 then
93 local op = parent[1]
94 local nearest_indented = op[#op].indent and op[#op] or walker.indented_before(op[#op])
95 indent_adj = nearest_indented.start - pstart - nearest_indented.indent + 1
96 end
97 for i, s in ipairs(parent) do
98 adj = s.indent and 0 or adj
99 if last_distinguished then
100 -- do not let early comments influence the indentation of real expressions:
101 if i <= last_distinguished and s.is_comment then
102 last_distinguished = last_distinguished + 1
104 -- s is the name of a named let. account for it:
105 if i == last_distinguished + 1 and not s.is_list and parent[1].text == "let" then
106 last_distinguished = last_distinguished + 1
108 -- do not let early comments influence the indentation of real expressions:
109 elseif i <= first_argument + 1 and s.is_comment then
110 first_argument = first_argument + 1
112 local has_eol = s.is_line_comment
113 adj = leading_space(s, deltas, adj, parent, i, pstart, has_eol)
114 adj = trailing_space(s, deltas, adj, parent, i, has_eol, keep_electric_space)
115 adj = indentation(s, deltas, adj, parent, i, pstart, base_indent, last_distinguished, first_argument,
116 op_adj, indent_adj)
117 if i == 1 then
118 op_adj = adj
120 if s.is_list then
121 local nearest_indented = s.indent and s or walker.indented_before(s)
122 local parent_column = nearest_indented and
123 nearest_indented.indent + (s.start + (s.p and #s.p or 0) + #s.d) - nearest_indented.start
124 or base_indent + (s.start + (s.p and #s.p or 0) + #s.d) - pstart
125 refmt_list(s, parent_column - adj, padj + adj, deltas, keep_electric_space)
128 return deltas
131 local function path_from(range)
132 local _, parent_at, m = walker.sexp_at(range, true)
133 local path
134 if parent_at[m] then
135 path = walker.sexp_path(parent_at[m])
136 local base = parent_at[m]
137 if range.finish == base.finish + 1 then
138 path.after_finish = 0 -- lists can end up with a different length
139 else
140 path.after_start = range.start - base.start
142 else
143 local s = parent_at[#parent_at]
144 if not s or range.start >= s.finish + 1 + (s.is_line_comment and 1 or 0) then
145 path = walker.sexp_path(parent_at)
146 path.at_pfinish = true
147 else
148 local prev, n = parent_at.before(range.start, finishof)
149 if prev then
150 path = walker.sexp_path(prev)
151 local nxt = parent_at[n + 1]
152 path.after_finish = nxt and not nxt.indent and 1 or range.finish - prev.finish - 1
153 else
154 path = walker.sexp_path(parent_at[1])
155 path.after_start = 0
159 return path
162 local function pos_from(path, range)
163 local sexp, parentng, n = walker.goto_path(path)
164 if sexp then
165 if path.after_finish then
166 local nxt = n and parentng[n + 1]
167 local max = nxt and nxt.start - sexp.finish - 1
168 return sexp.finish + 1 - (range.finish - range.start) +
169 (max and math.min(path.after_finish, max) or path.after_finish)
170 elseif path.at_pfinish then
171 return sexp.finish
172 else
173 return sexp.start + path.after_start
178 local function refmt_at(scope, range, keep_electric_space)
179 if not range or not scope or scope.is_root or (scope.finish - scope.start < 2) then return range.start end
180 local parent = walker.sexp_at(scope, true)
181 if not (parent and parent.is_list) then return range.start end
182 local path = path_from(range)
183 local indented_parent = parent.indent and parent or parent.is_list and walker.indented_before(parent)
184 local parent_column = indented_parent and (indented_parent.indent + parent.start - indented_parent.start +
185 (parent.p and #parent.p or 0) + #parent.d) or
187 local deltas = refmt_list(parent, parent_column, 0, {}, keep_electric_space)
188 table.sort(deltas, function(d1, d2) return d1[1] > d2[1] end)
189 for _, pair in ipairs(deltas) do
190 local offset, delta = unpack(pair)
191 if delta > 0 then
192 write(offset, string.rep(" ", delta))
193 elseif delta < 0 then
194 delete(offset + delta, -delta)
197 parser.tree.rewind(parent.start or 0)
198 return path and pos_from(path, range) or range.start
201 local function splice(pos, sexps, skip, backwards, action)
202 local spliced
203 local sexp = walker.sexp_at(skip, true)
204 local start = sexp.start + (sexp.p and #sexp.p or 0)
205 -- XXX: don't splice empty line comments _yet_. See _join_or_splice
206 local tosplice = action.splice or action.wrap or not sexp.is_line_comment and sexp.is_empty
207 local opening = (sexp.p or "")..sexp.d
208 local closing = parser.opposite[sexp.d]
209 local real_start = tosplice and sexp.start or start + #sexp.d
210 local splice_closing = not sexp.is_line_comment or sexp.is_empty
211 local real_finish = sexp.finish + 1 - (tosplice and splice_closing and 0 or #closing)
212 local first = backwards and
213 {start = real_start, finish = math.max(pos, start + #sexp.d)} or
214 {start = real_start, finish = sexp.is_empty and pos or start + #sexp.d}
215 local second = backwards and
216 {start = sexp.is_empty and pos or sexp.finish + 1 - #closing, finish = real_finish} or
217 {start = math.min(pos, sexp.finish + 1 - #closing), finish = real_finish}
218 action.func(second)
219 action.func(first)
220 spliced = tosplice
221 if action.kill then
222 local ndeleted = first.finish - first.start + second.finish - second.start
223 if ndeleted > 0 then
224 sexps.rewind(sexp.start)
227 return first.finish - first.start, spliced, opening, closing
230 -- If you try to delete some part of indentation, this function joins the current line with
231 -- the previous one, unless the latter is a line comment.
232 -- it does this by
233 -- a) extending or shrinking the range that is to be deleted
234 -- b) returning a truthy value to trick pick_out to glide the cursor, but refmt to restore the deleted spaces
235 -- c) both a) and b)
236 local function delete_indentation(range, pos)
237 local node, parent = walker.sexp_at(range)
238 if node or not parent.is_list then return end
239 local prev = parent.before(pos, finishof)
240 local nxt = parent.after(pos, startof)
241 if prev and prev.finish >= range.start or nxt and nxt.start < range.finish then return end
242 local backwards = pos == range.finish
243 local adj = 0
244 local eol = eol_at(pos)
245 local on_empty_line = not nxt or eol and nxt.start > eol
246 local empty_line_after_comment = prev and prev.is_line_comment
247 and on_empty_line
248 if nxt and nxt.indent then
249 if backwards and prev then
250 range.start = prev.finish + (empty_line_after_comment and 0 or 1)
251 if not prev.d then
252 range.finish = range.start + 1
253 adj = (on_empty_line and 0 or 1)
256 return adj
258 if not nxt and prev then
259 range.start = prev.finish + 1
260 range.finish = parent.finish
261 elseif nxt and nxt.start == range.finish and (not prev or prev.finish + 1 == range.start) then
262 if prev and prev.d or nxt and nxt.d then
263 -- don't delete spaces near delimiters, just slide the cursor:
264 if backwards then
265 range.finish = range.start
266 else
267 range.start = range.finish
270 return adj
274 local function big_enough_parent(pos1, pos2)
275 -- since the pos1-pos2 range can cross list boundaries, find which list contains both pos1 and pos2
276 local _, p1, p2
277 _, p1 = walker.sexp_at({start = pos1, finish = pos1}, true)
278 if p1.is_root or not (p1.start < pos1 and p1.finish > pos2) then
279 _, p2 = walker.sexp_at({start = pos2, finish = pos2}, true)
281 return p2 and not p2.is_root and p2 or p1
284 local function extend_overlap(range)
285 local rnode = walker.sexp_at({start = range.finish, finish = range.finish})
286 if rnode and rnode.p and rnode.p:find";"
287 and range.finish > rnode.start and range.finish < rnode.start + #rnode.p then
288 return {start = range.start, finish = rnode.start + #rnode.p}
290 return range
293 local function pick_out(range, pos, action)
294 local ndeleted = 0
295 if range.start == range.finish then return ndeleted end
296 local sexps = parser.tree
297 local skips = sexps.unbalanced_delimiters(range)
298 -- handle splice and kill-splice of forms and strings:
299 if #skips == 1 then
300 local sexp = walker.sexp_at(skips[1], true)
301 local backward_splice = skips[1].opening and pos >= sexp.start + (sexp.p and #sexp.p or 0) + #sexp.d
302 and range.start >= sexp.start
303 local forward_splice = skips[1].closing and pos <= sexp.finish + 1 - #parser.opposite[sexp.d]
304 and range.finish <= sexp.finish + 1
305 if backward_splice or forward_splice then
306 return splice(backward_splice and range.finish or range.start, sexps, sexp, backward_splice, action)
309 local node, parent = walker.sexp_at({start = range.finish, finish = range.finish})
310 -- if the range ends with a line comment, don't delete its closing newline:
311 local drop_eol = action.kill and node and
312 node.finish + 1 == range.finish and node.is_line_comment
313 local par = big_enough_parent(range.start, range.finish)
314 local operator_changed = par[1] and par[1].finish >= range.start
315 local refmt = #skips == 0 and delete_indentation(range, pos) or operator_changed and 0
316 table.sort(skips, function(a, b) return a.start < b.start end)
317 table.insert(skips, {start = range.finish - (drop_eol and 1 or 0)})
318 table.insert(skips, 1, {finish = range.start})
319 ndeleted = ndeleted + (drop_eol and 1 or 0)
320 for i = #skips - 1, 1, -1 do
321 local region = {start = skips[i].finish, finish = skips[i + 1].start}
322 if skips[i].closing and skips[i + 1].opening then
323 -- leave out some of the space between adjacent lists
324 local _, rparent = walker.sexp_at(region)
325 local nxt = rparent.after(region.start, startof)
326 region.start = nxt and nxt.start or region.start
328 if action then
329 action.func(region)
330 ndeleted = ndeleted + (region.finish - region.start)
333 -- if parent[#parent + 1] is nil, we are at EOF
334 if ndeleted > 0 and (not parent.is_root or parent.is_parsed(range.start) or parent[#parent + 1]) then
335 sexps.rewind(range.start)
337 return ndeleted - (refmt or 0), nil, nil, nil, refmt
340 local function raise_sexp(range, pos)
341 local sexp, parent = walker.sexp_at(range, true)
342 if sexp and parent and parent.is_list then
343 delete(sexp.finish + 1, parent.finish - sexp.finish)
344 delete(parent.start, sexp.start - parent.start)
345 parser.tree.rewind(parent.start)
346 range.start = parent.start + pos - sexp.start
347 range.finish = range.start
348 local _, nodes = walker.sexp_path(range)
349 local grandparent = nodes[#nodes - 2]
350 return grandparent and refmt_at(grandparent, range) or range.start
354 local function slurp_sexp(range, forward)
355 local _, parent = walker.sexp_at(range, true)
356 local seeker = forward and walker.finish_after or walker.start_before
357 if not parent or not parent.is_list then return range.start end
358 local r = {start = parent.start, finish = parent.finish + 1}
359 local newpos = seeker(r, is_comment)
360 if not newpos then return range.start end
361 local opening = (parent.p or "")..parent.d
362 local closing = parser.opposite[parent.d]
363 local delimiter = forward and closing or opening
364 if forward then
365 write(newpos, delimiter)
367 delete(forward and parent.finish or parent.start, #delimiter)
368 if not forward then
369 write(newpos, delimiter)
371 parser.tree.rewind(math.min(parent.start, newpos))
372 return refmt_at(big_enough_parent(newpos, range.start), range)
375 local function barf_sexp(range, forward)
376 local _, parent, m = walker.sexp_at(range, true)
377 local seeker = forward and walker.finish_before or walker.start_after
378 -- TODO: barfing out of strings requires calling the parser on them
379 if not parent or not parent.is_list or parent.is_empty then return range.start end
380 local opening = (parent.p or "")..parent.d
381 local pstart = parent.start + #opening
382 local r = {start = forward and parent.finish - 1 or pstart, finish = forward and parent.finish or pstart + 1}
383 local newpos = seeker(r, is_comment) or forward and pstart or parent.finish
384 local closing = parser.opposite[parent.d]
385 local delimiter = forward and closing or opening
386 if not forward then
387 write(newpos, delimiter)
389 delete(forward and parent.finish or parent.start, #delimiter)
390 if forward then
391 write(newpos, delimiter)
393 parser.tree.rewind(math.min(parent.start, newpos))
394 local drag = forward and (newpos < range.finish) or not forward and (newpos > range.start)
395 local keep_inside = forward and m > 1 and range.finish > range.start and 1 or 0
396 newpos = drag and newpos - keep_inside or range.start
397 local rangeng = {start = newpos, finish = newpos}
398 return refmt_at(big_enough_parent(newpos, parent.finish + 1), rangeng)
401 local function splice_sexp(range, _, no_refmt)
402 local _, parent = walker.sexp_at(range)
403 if not parent or not parent.d then return end
404 local opening = (parent.p or "")..parent.d
405 local closing = parser.opposite[parent.d]
406 local finish = parent.finish + 1 - #closing
407 if not parent.is_line_comment then
408 delete(finish, #closing)
410 -- TODO: (un)escape special characters, if necessary
411 delete(parent.start, parent.is_empty and (finish - parent.start) or #opening)
412 parser.tree.rewind(parent.start)
413 range.start = range.start - #opening
414 range.finish = range.start
415 local _, parentng = walker.sexp_at(range, true)
416 return not no_refmt and refmt_at(parentng, range) or range.start
419 local function rewrap(parent, kind)
420 local pstart = parent.start + #((parent.p or "")..parent.d) - 1
421 delete(parent.finish, 1)
422 write(parent.finish, parser.opposite[kind])
423 delete(pstart, #parent.d)
424 write(pstart, kind)
425 parser.tree.rewind(parent.start)
428 local function cycle_wrap(range, pos)
429 local _, parent = walker.sexp_at(range)
430 if not parent or not parent.is_list then return end
431 local next_kind = {["("] = "[", ["["] = "{", ["{"] = "("}
432 rewrap(parent, next_kind[parent.d])
433 return pos
436 local function split_sexp(range)
437 local _, parent = walker.sexp_at(range)
438 if not (parent and parent.d) then return end
439 local new_finish, new_start
440 if parent.is_list then
441 local prev = parent.before(range.start, finishof, is_comment)
442 new_finish = prev and prev.finish + 1
443 -- XXX: do not skip comments here, so they end up in the second list
444 -- and are not separated from their target expression:
445 local nxt = new_finish and parent.after(new_finish, startof)
446 new_start = nxt and nxt.start
447 else
448 new_start = range.start
449 new_finish = range.start
451 if not (new_start and new_finish) then return end
452 local opening = (parent.p or "")..parent.d
453 local closing = parser.opposite[parent.d]
454 write(new_start, opening)
455 local sep = parent.is_line_comment and "" -- line comments already have a separator
456 or new_finish == new_start and " " -- only add a separator if there was none before
457 or ""
458 write(new_finish, closing..sep)
459 parser.tree.rewind(parent.start)
460 range.start = new_start + (parent.is_list and 0 or #opening + #closing)
461 range.finish = range.start
462 local _, nodes = walker.sexp_path(range)
463 local parentng, grandparent = nodes[#nodes - 1], nodes[#nodes - 2]
464 local scope = parentng and not parentng.is_root and parentng or grandparent
465 return refmt_at(scope, range)
468 local function join_sexps(range)
469 local node, parent = walker.sexp_at(range, true)
470 local first = node and node.finish + 1 == range.start and node or parent.before(range.start, finishof)
471 local second = first ~= node and node or parent.after(range.start, startof)
472 if not (first and second and first.d and
473 -- don't join line comments to margin comments:
474 (not first.is_line_comment or first.indent and second.indent) and
475 (first.d == second.d or
476 -- join line comments even when their delimiters differ slightly
477 -- (different number of semicolons, existence/lack of a space after them)
478 parser.opposite[first.d] == parser.opposite[second.d])) then
479 return
481 local opening = (second.p or "")..second.d
482 local closing = parser.opposite[first.d]
483 local pos
484 if not first.is_list then
485 pos = first.finish + 1 - #closing
486 delete(pos, second.start + #opening - pos)
487 else
488 delete(second.start, #opening)
489 delete(first.finish, #closing)
490 pos = second.start - #closing
492 parser.tree.rewind(first.start)
493 range.start = pos
494 range.finish = range.start
495 local _, nodes = walker.sexp_path(range)
496 local parentng, grandparent = nodes[#nodes - 1], nodes[#nodes - 2]
497 local scope = parentng and not parentng.is_root and parentng or grandparent
498 return refmt_at(scope, range)
501 local function delete_splicing(range, pos, splicing, delete_and_yank)
502 local action = {kill = true, wrap = splicing, splice = splicing, func = delete_and_yank}
503 local sexp, parent, n = walker.sexp_at(range, true)
504 range = extend_overlap(range)
505 local ndeleted, spliced = pick_out(range, pos, action)
506 local closing = spliced and parser.opposite[sexp.d]
507 local inner_list_len = spliced and sexp.finish - sexp.start + 1 - #sexp.d - #closing
508 local range_len = range.finish - range.start
509 local backwards = pos == range.finish
510 local whole_object = sexp and
511 (sexp.start == range.start and sexp.finish + 1 == range.finish
512 or spliced and (inner_list_len <= (backwards and range_len + ndeleted or range_len)))
513 local in_head_atom = sexp and not sexp.d and n == 1 and #parent > 1
514 local in_whitespace = not sexp
515 if whole_object or in_whitespace or in_head_atom then
516 local cur = whole_object and sexp.start or range.start
517 -- if parent[#parent + 1] is nil, we are at EOF
518 if not parent.is_root or parent.is_parsed(cur) or parent[#parent + 1] then
519 parser.tree.rewind(cur)
521 local r = {start = cur, finish = cur}
522 local _, parentng = walker.sexp_at(r, true)
523 return refmt_at(parentng, r)
525 return spliced and (backwards and sexp.start or range.start - ndeleted)
526 or not backwards and ndeleted <= 0 and range.finish - ndeleted
527 or backwards and ndeleted <= 0 and range.start + ndeleted
528 or range.start
531 local function transpose(range, first, second)
532 if not (first and second) then return end
533 local copy1 = first.text
534 local copy2 = second.text
535 delete(second.start, second.finish + 1 - second.start)
536 write(second.start, copy1)
537 delete(first.start, first.finish + 1 - first.start)
538 write(first.start, copy2)
539 parser.tree.rewind(first.start)
540 range.start = second.finish + 1
541 range.finish = range.start
542 return refmt_at(big_enough_parent(first.start, second.finish), range)
545 local function transpose_sexps(range)
546 local node, parent = walker.sexp_at(range, true)
547 local first = node and node.finish + 1 == range.start and node or parent.before(range.start, finishof)
548 local second = first ~= node and node or parent.after(range.start, startof)
549 return transpose(range, first, second)
552 local function transpose_words(range)
553 local _, first = walker.start_float_before(range)
554 local _, second = walker.finish_float_after(range)
555 if first and second and first.start == second.start then
556 _, first = walker.start_float_before(first)
558 return transpose(range, first, second)
561 local function transpose_chars(range)
562 local node, parent, i = walker.sexp_at(range)
563 local nxt = i and parent[i + 1]
564 local pstart = not parent.is_root and parent.start + (parent.p and #parent.p or 0) + #parent.d
565 local pfinish = not parent.is_root and parent.finish
566 local npref = node and node.p and node.start + #node.p
567 -- only allow transposing while inside atoms/words and prefixes
568 if node and ((npref and (range.start <= npref and range.start > node.start) or
569 node.d and range.start > node.finish + 1) or not node.d and (not pstart or range.start > pstart)) then
570 local start = range.start -
571 ((range.start == pfinish or range.start == npref or
572 range.start == node.finish + 1 and (parent.is_list or parent.is_root) and (not nxt or nxt.indent)) and 1 or 0)
573 local str_start = start - node.start + 1
574 local char = node.text:sub(str_start, str_start)
575 delete(start, 1)
576 write(start - 1, #char > 0 and char or " ")
577 parser.tree.rewind(start)
578 local in_head_atom = i == 1 and #parent > 1
579 return parent.is_list and in_head_atom and refmt_at(parent, {start = start + 1, finish = start + 1}) or start + 1
583 local function _join_or_splice(parent, n, range, pos)
584 local sexp = parent[n]
585 local nxt = parent[n + 1]
586 local is_last = not nxt or not nxt.is_line_comment
587 local newpos = not (is_last and sexp.is_empty) and join_sexps(range)
588 if not newpos and sexp.is_empty then
589 newpos = splice_sexp({start = pos, finish = pos}, nil, true)
591 return newpos or pos
594 local function delete_nonsplicing(range, pos, delete_maybe_yank)
595 local action = {kill = true, wrap = false, splice = false, func = delete_maybe_yank}
596 range = extend_overlap(range)
597 local ndeleted, spliced, opening, _, refmt = pick_out(range, pos, action)
598 local backwards = pos == range.finish
599 if opening then
600 if spliced then
601 return pos - ndeleted
602 else
603 if ndeleted == 0 then
604 local closing = parser.opposite[opening] -- XXX: why don't I use the pick_out return value?
605 if closing == "\n" then
606 local sexp, parent, n = walker.sexp_at(range)
607 if pos == sexp.start + #sexp.d and backwards then
608 return _join_or_splice(parent, n, range, pos)
609 elseif pos == sexp.finish then
610 local r = {start = pos + #closing, finish = pos + #closing}
611 return _join_or_splice(parent, n, r, pos)
615 return backwards and (range.finish - ndeleted) or range.start
617 else
618 local newpos = backwards and range.start + (ndeleted <= 0 and ndeleted or 0) or (range.finish - ndeleted)
619 if refmt then
620 local r = {start = newpos, finish = newpos}
621 return refmt_at(big_enough_parent(range.start, range.finish - ndeleted), r, true)
623 return newpos
627 local function insert_pair(range, delimiter, shiftless, auto_square)
628 local indices, nodes = walker.sexp_path(range)
629 local sexp = nodes[#nodes]
630 local right_after_prefix = sexp and sexp.p and range.start == sexp.start + #sexp.p
631 -- XXX: here I assume that # is a valid prefix for the dialect
632 local mb_closing = (delimiter == "|") and right_after_prefix and parser.opposite[sexp.p .. delimiter]
633 local closing = mb_closing or parser.opposite[delimiter]
634 local squarewords = fmt.squarewords
635 if squarewords and not right_after_prefix
636 and not (closing == "]" and shiftless and not auto_square)
637 and (auto_square or squarewords.not_optional)
638 and not fmt:adjust_bracket_p(indices, nodes, range) then
639 delimiter, closing = "[", "]"
641 write(range.finish, closing)
642 write(range.start, delimiter)
643 if right_after_prefix or parser.tree.is_parsed(range.start) then
644 parser.tree.rewind(right_after_prefix and sexp.start or range.start)
646 return range.start + #delimiter
649 local function make_wrap(kind)
650 return function(range, pos, shiftless, auto_square)
651 local opening = shiftless and swap[kind] or kind
652 local function _wrap(r)
653 if not (r.finish > r.start) then return end
654 insert_pair(r, opening, false, auto_square)
656 local action = {kill = false, wrap = false, splice = false, func = _wrap}
657 pick_out(range, pos, action)
658 local rangeng = {start = pos + #opening, finish = range.finish}
659 local _, parentng = walker.sexp_at(range) -- TODO: range.finish is wrong. use sexp_path and big_enough_parent
660 return refmt_at(parentng, rangeng)
664 local function newline(parent, range)
665 local line_comment = parent.is_line_comment
666 -- do not autoextend margin comments:
667 if line_comment and range.start < parent.finish + (parent.indent and 1 or 0) then
668 local newpos = split_sexp(range)
669 if newpos then
670 return newpos
673 if not parent.is_list and not line_comment then
674 -- if parent[#parent + 1] is nil, we are at EOF
675 if not parent.is_root or parent.is_parsed(range.start) or parent[#parent + 1] then
676 parser.tree.rewind(parent.start or range.start)
678 local newpos = range.start
679 write(newpos, "\n")
680 return newpos + 1
682 if not parent.is_list then
683 local _
684 _, parent = walker.sexp_at(parent, true)
686 local last_nonblank_in_list = not parent.is_empty and parent[#parent].finish - (line_comment and 1 or 0)
687 local nxt = parent.after(range.start, startof)
688 local last_on_line = range.start == eol_at(range.start)
689 local margin = nxt and not nxt.indent and nxt.is_line_comment and nxt.finish
690 local after_last = last_nonblank_in_list and range.start > last_nonblank_in_list
691 local in_indent = nxt and nxt.indent and range.start <= nxt.start and range.start >= (nxt.start - nxt.indent)
692 local placeholder = "asdf"
693 local newpos = margin or range.start
694 if not parent.is_empty then
695 if in_indent then
696 write(newpos, (placeholder.."\n"))
697 else
698 write(newpos, "\n"..((after_last or last_on_line or margin) and placeholder or ""))
701 parser.tree.rewind(parent.start or newpos)
702 -- move the cursor onto the placeholder, so refmt_at can restore the position:
703 local r = {start = newpos, finish = newpos}
704 newpos = walker.start_after(r) or newpos
705 local rangeng = {start = newpos, finish = newpos}
706 newpos = refmt_at(parent, rangeng)
707 local _, parentng = walker.sexp_at({start = newpos, finish = newpos}, true)
708 if after_last then
709 local autoindent = parentng[#parentng].indent
710 write(parentng.finish, "\n"..string.rep(" ", autoindent or 0))
712 if after_last or last_on_line or margin or in_indent then
713 delete(newpos, #placeholder)
715 parser.tree.rewind(parentng.start or newpos)
716 return newpos
719 local function close_and_newline(range, closing)
720 local opening = closing and parser.opposite[closing]
721 local parent = walker.quasilist_at(range, opening)
722 if parent then
723 local has_eol = parent.is_line_comment
724 local r = {start = parent.finish, finish = parent.finish}
725 local newpos = refmt_at(parent, r)
726 r = {start = newpos + (has_eol and 0 or 1), finish = newpos + (has_eol and 0 or 1)}
727 local list, parentng = walker.sexp_at(r)
728 newpos = list and list.finish + 1 or r.start
729 newpos = newline(parentng, {start = newpos, finish = newpos})
730 if not newpos then
731 newpos = r.finish + 1
732 write(newpos, "\n")
733 parser.tree.rewind(parentng.start or list.start)
735 return newpos
736 elseif closing == "\n" then
737 local eol = eol_at(range.start)
738 local _, parentng = walker.sexp_at({start = eol, finish = eol})
739 local newpos = newline(parentng, {start = eol, finish = eol})
740 return newpos
742 return range.start
745 local function join_line(_, pos)
746 local eol = eol_at(pos)
747 local r = {start = eol, finish = eol + 1}
748 return delete_nonsplicing(r, r.start, delete)
751 local wrap_doublequote = make_wrap'"'
753 local function meta_doublequote(range, pos, auto_square)
754 local escaped = walker.escaped_at(range)
755 if not escaped then
756 return wrap_doublequote(range, pos, nil, auto_square)
757 elseif escaped.is_string then
758 return close_and_newline(range, '"')
760 return pos
763 local block_comment_start
764 for o, c in pairs(parser.opposite) do
765 if #c > 1 then
766 block_comment_start = o
767 break
771 local wrap_comment = make_wrap(block_comment_start)
773 return {
774 delete_splicing = delete_splicing,
775 delete_nonsplicing = delete_nonsplicing,
776 refmt_at = refmt_at,
777 pick_out = pick_out,
778 raise_sexp = raise_sexp,
779 slurp_sexp = slurp_sexp,
780 barf_sexp = barf_sexp,
781 splice_sexp = splice_sexp,
782 wrap_round = make_wrap"(",
783 wrap_square = make_wrap"[",
784 wrap_curly = make_wrap"{",
785 meta_doublequote = meta_doublequote,
786 wrap_comment = wrap_comment,
787 insert_pair = insert_pair,
788 newline = newline,
789 join_line = join_line,
790 close_and_newline = close_and_newline,
791 cycle_wrap = cycle_wrap,
792 split_sexp = split_sexp,
793 join_sexps = join_sexps,
794 transpose_sexps = transpose_sexps,
795 transpose_words = transpose_words,
796 transpose_chars = transpose_chars,
800 return M