allow adjoining a lone prefix with a list
[lisp-parkour.git] / edit.lua
blob6c451f1b884a86506709e6170f0f203abef34b31
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 local function is_lone_prefix(node)
80 return node and node.p and (#node.p == node.finish - node.start + 1)
81 end
83 function M.new(parser, walker, fmt, write, delete, eol_at)
85 local function refmt_list(parent, base_indent, padj, deltas, keep_electric_space)
86 local adj = 0
87 local op_adj = 0
88 local indent_adj = 0
89 local pstart = parent.start + (parent.p and #parent.p or 0) + #parent.d
90 if parent.is_empty then
91 normalize_spacing(pstart, parent.finish - pstart, deltas)
92 return deltas
93 end
94 local last_distinguished = fmt:last_distinguished(parent)
95 local first_argument = 1
96 if #parent > 2 and parent[1].is_list and #parent[1] > 1 then
97 local op = parent[1]
98 local nearest_indented = op[#op].indent and op[#op] or walker.indented_before(op[#op])
99 indent_adj = nearest_indented.start - pstart - nearest_indented.indent + 1
101 for i, s in ipairs(parent) do
102 adj = s.indent and 0 or adj
103 if last_distinguished then
104 -- do not let early comments influence the indentation of real expressions:
105 if i <= last_distinguished and s.is_comment then
106 last_distinguished = last_distinguished + 1
108 -- s is the name of a named let. account for it:
109 if i == last_distinguished + 1 and not s.is_list and parent[1].text == "let" then
110 last_distinguished = last_distinguished + 1
112 -- do not let early comments influence the indentation of real expressions:
113 elseif i <= first_argument + 1 and s.is_comment then
114 first_argument = first_argument + 1
116 local has_eol = s.is_line_comment
117 adj = leading_space(s, deltas, adj, parent, i, pstart, has_eol)
118 adj = trailing_space(s, deltas, adj, parent, i, has_eol, keep_electric_space)
119 adj = indentation(s, deltas, adj, parent, i, pstart, base_indent, last_distinguished, first_argument,
120 op_adj, indent_adj)
121 if i == 1 then
122 op_adj = adj
124 if s.is_list then
125 local nearest_indented = s.indent and s or walker.indented_before(s)
126 local parent_column = nearest_indented and
127 nearest_indented.indent + (s.start + (s.p and #s.p or 0) + #s.d) - nearest_indented.start
128 or base_indent + (s.start + (s.p and #s.p or 0) + #s.d) - pstart
129 refmt_list(s, parent_column - adj, padj + adj, deltas, keep_electric_space)
132 return deltas
135 local function path_from(range)
136 local _, parent_at, m = walker.sexp_at(range, true)
137 local path
138 if parent_at[m] then
139 path = walker.sexp_path(parent_at[m])
140 local base = parent_at[m]
141 if range.finish == base.finish + 1 then
142 path.after_finish = 0 -- lists can end up with a different length
143 else
144 path.after_start = range.start - base.start
146 else
147 local s = parent_at[#parent_at]
148 if not s or range.start >= s.finish + 1 + (s.is_line_comment and 1 or 0) then
149 path = walker.sexp_path(parent_at)
150 path.at_pfinish = true
151 else
152 local prev, n = parent_at.before(range.start, finishof)
153 if prev then
154 path = walker.sexp_path(prev)
155 local nxt = parent_at[n + 1]
156 path.after_finish = nxt and not nxt.indent and 1 or range.finish - prev.finish - 1
157 else
158 path = walker.sexp_path(parent_at[1])
159 path.after_start = 0
163 return path
166 local function pos_from(path, range)
167 local sexp, parentng, n = walker.goto_path(path)
168 if sexp then
169 if path.after_finish then
170 local nxt = n and parentng[n + 1]
171 local max = nxt and nxt.start - sexp.finish - 1
172 return sexp.finish + 1 - (range.finish - range.start) +
173 (max and math.min(path.after_finish, max) or path.after_finish)
174 elseif path.at_pfinish then
175 return sexp.finish
176 else
177 return sexp.start + path.after_start
182 local function refmt_at(scope, range, keep_electric_space)
183 if not range or not scope or scope.is_root or (scope.finish - scope.start < 2) then return range.start end
184 local parent = walker.sexp_at(scope, true)
185 if not (parent and parent.is_list) then return range.start end
186 local path = path_from(range)
187 local indented_parent = parent.indent and parent or parent.is_list and walker.indented_before(parent)
188 local parent_column = indented_parent and (indented_parent.indent + parent.start - indented_parent.start +
189 (parent.p and #parent.p or 0) + #parent.d) or
191 local deltas = refmt_list(parent, parent_column, 0, {}, keep_electric_space)
192 table.sort(deltas, function(d1, d2) return d1[1] > d2[1] end)
193 for _, pair in ipairs(deltas) do
194 local offset, delta = unpack(pair)
195 if delta > 0 then
196 write(offset, string.rep(" ", delta))
197 elseif delta < 0 then
198 delete(offset + delta, -delta)
201 parser.tree.rewind(parent.start or 0)
202 return path and pos_from(path, range) or range.start
205 local function splice(pos, sexps, skip, backwards, action)
206 local spliced
207 local sexp = walker.sexp_at(skip, true)
208 local start = sexp.start + (sexp.p and #sexp.p or 0)
209 -- XXX: don't splice empty line comments _yet_. See _join_or_splice
210 local tosplice = action.splice or action.wrap or not sexp.is_line_comment and sexp.is_empty
211 local opening = (sexp.p or "")..sexp.d
212 local closing = parser.opposite[sexp.d]
213 local real_start = tosplice and sexp.start or start + #sexp.d
214 local splice_closing = not sexp.is_line_comment or sexp.is_empty
215 local real_finish = sexp.finish + 1 - (tosplice and splice_closing and 0 or #closing)
216 local first = backwards and
217 {start = real_start, finish = math.max(pos, start + #sexp.d)} or
218 {start = real_start, finish = sexp.is_empty and pos or start + #sexp.d}
219 local second = backwards and
220 {start = sexp.is_empty and pos or sexp.finish + 1 - #closing, finish = real_finish} or
221 {start = math.min(pos, sexp.finish + 1 - #closing), finish = real_finish}
222 action.func(second)
223 action.func(first)
224 spliced = tosplice
225 if action.kill then
226 local ndeleted = first.finish - first.start + second.finish - second.start
227 if ndeleted > 0 then
228 sexps.rewind(sexp.start)
231 return first.finish - first.start, spliced, opening, closing
234 -- If you try to delete some part of indentation, this function joins the current line with
235 -- the previous one, unless the latter is a line comment.
236 -- it does this by
237 -- a) extending or shrinking the range that is to be deleted
238 -- b) returning a truthy value to trick pick_out to glide the cursor, but refmt to restore the deleted spaces
239 -- c) both a) and b)
240 local function delete_indentation(range, pos)
241 local node, parent = walker.sexp_at(range)
242 if node or not parent.is_list then return end
243 local prev = parent.before(pos, finishof)
244 local nxt = parent.after(pos, startof)
245 if prev and prev.finish >= range.start or nxt and nxt.start < range.finish then return end
246 local backwards = pos == range.finish
247 local adj = 0
248 local eol = eol_at(pos)
249 local on_empty_line = not nxt or eol and nxt.start > eol
250 local empty_line_after_comment = prev and prev.is_line_comment
251 and on_empty_line
252 if nxt and nxt.indent then
253 if backwards and prev then
254 range.start = prev.finish + (empty_line_after_comment and 0 or 1)
255 if not prev.d then
256 range.finish = range.start + 1
257 adj = (on_empty_line and 0 or 1)
260 return adj
262 if not nxt and prev then
263 range.start = prev.finish + 1
264 range.finish = parent.finish
265 elseif nxt and nxt.start == range.finish and (not prev or prev.finish + 1 == range.start) then
266 if prev and prev.d or nxt and nxt.d and not is_lone_prefix(prev) then
267 -- don't delete spaces near delimiters, just slide the cursor:
268 if backwards then
269 range.finish = range.start
270 else
271 range.start = range.finish
274 return adj
278 local function big_enough_parent(pos1, pos2)
279 -- since the pos1-pos2 range can cross list boundaries, find which list contains both pos1 and pos2
280 local _, p1, p2
281 _, p1 = walker.sexp_at({start = pos1, finish = pos1}, true)
282 if p1.is_root or not (p1.start < pos1 and p1.finish > pos2) then
283 _, p2 = walker.sexp_at({start = pos2, finish = pos2}, true)
285 return p2 and not p2.is_root and p2 or p1
288 local function extend_overlap(range)
289 local rnode = walker.sexp_at({start = range.finish, finish = range.finish})
290 if rnode and rnode.p and rnode.p:find";"
291 and range.finish > rnode.start and range.finish < rnode.start + #rnode.p then
292 return {start = range.start, finish = rnode.start + #rnode.p}
294 return range
297 local function pick_out(range, pos, action)
298 local ndeleted = 0
299 if range.start == range.finish then return ndeleted end
300 local sexps = parser.tree
301 local skips = sexps.unbalanced_delimiters(range)
302 -- handle splice and kill-splice of forms and strings:
303 if #skips == 1 then
304 local sexp = walker.sexp_at(skips[1], true)
305 local backward_splice = skips[1].opening and pos >= sexp.start + (sexp.p and #sexp.p or 0) + #sexp.d
306 and range.start >= sexp.start
307 local forward_splice = skips[1].closing and pos <= sexp.finish + 1 - #parser.opposite[sexp.d]
308 and range.finish <= sexp.finish + 1
309 if backward_splice or forward_splice then
310 return splice(backward_splice and range.finish or range.start, sexps, sexp, backward_splice, action)
313 local node, parent = walker.sexp_at({start = range.finish, finish = range.finish})
314 -- if the range ends with a line comment, don't delete its closing newline:
315 local drop_eol = action.kill and node and
316 node.finish + 1 == range.finish and node.is_line_comment
317 local par = big_enough_parent(range.start, range.finish)
318 local operator_changed = par[1] and par[1].finish >= range.start
319 local refmt = #skips == 0 and delete_indentation(range, pos) or operator_changed and 0
320 table.sort(skips, function(a, b) return a.start < b.start end)
321 table.insert(skips, {start = range.finish - (drop_eol and 1 or 0)})
322 table.insert(skips, 1, {finish = range.start})
323 ndeleted = ndeleted + (drop_eol and 1 or 0)
324 for i = #skips - 1, 1, -1 do
325 local region = {start = skips[i].finish, finish = skips[i + 1].start}
326 if skips[i].closing and skips[i + 1].opening then
327 -- leave out some of the space between adjacent lists
328 local _, rparent = walker.sexp_at(region)
329 local nxt = rparent.after(region.start, startof)
330 region.start = nxt and nxt.start or region.start
332 if action then
333 action.func(region)
334 ndeleted = ndeleted + (region.finish - region.start)
337 -- if parent[#parent + 1] is nil, we are at EOF
338 if ndeleted > 0 and (not parent.is_root or parent.is_parsed(range.start) or parent[#parent + 1]) then
339 sexps.rewind(range.start)
341 return ndeleted - (refmt or 0), nil, nil, nil, refmt
344 local function raise_sexp(range, pos)
345 local sexp, parent = walker.sexp_at(range, true)
346 if sexp and parent and parent.is_list then
347 delete(sexp.finish + 1, parent.finish - sexp.finish)
348 delete(parent.start, sexp.start - parent.start)
349 parser.tree.rewind(parent.start)
350 range.start = parent.start + pos - sexp.start
351 range.finish = range.start
352 local _, nodes = walker.sexp_path(range)
353 local grandparent = nodes[#nodes - 2]
354 return grandparent and refmt_at(grandparent, range) or range.start
358 local function slurp_sexp(range, forward)
359 local _, parent = walker.sexp_at(range, true)
360 local seeker = forward and walker.finish_after or walker.start_before
361 if not parent or not parent.is_list then return range.start end
362 local r = {start = parent.start, finish = parent.finish + 1}
363 local newpos = seeker(r, is_comment)
364 if not newpos then return range.start end
365 local opening = (parent.p or "")..parent.d
366 local closing = parser.opposite[parent.d]
367 local delimiter = forward and closing or opening
368 if forward then
369 write(newpos, delimiter)
371 delete(forward and parent.finish or parent.start, #delimiter)
372 if not forward then
373 write(newpos, delimiter)
375 parser.tree.rewind(math.min(parent.start, newpos))
376 return refmt_at(big_enough_parent(newpos, range.start), range)
379 local function barf_sexp(range, forward)
380 local _, parent = walker.sexp_at(range, true)
381 local seeker = forward and walker.finish_before or walker.start_after
382 -- TODO: barfing out of strings requires calling the parser on them
383 if not parent or not parent.is_list or parent.is_empty then return range.start end
384 local opening = (parent.p or "")..parent.d
385 local pstart = parent.start + #opening
386 local r = {start = forward and parent.finish - 1 or pstart, finish = forward and parent.finish or pstart + 1}
387 local newpos = seeker(r, is_comment) or forward and pstart or parent.finish
388 local closing = parser.opposite[parent.d]
389 local delimiter = forward and closing or opening
390 if not forward then
391 write(newpos, delimiter)
393 delete(forward and parent.finish or parent.start, #delimiter)
394 if forward then
395 write(newpos, delimiter)
397 parser.tree.rewind(math.min(parent.start, newpos))
398 local drag = forward and (newpos < range.finish) or not forward and (newpos > range.start)
399 local keep_inside = forward and #parent > 1 and range.finish > range.start and 1 or 0
400 newpos = drag and newpos - keep_inside or range.start
401 local rangeng = {start = newpos, finish = newpos}
402 return refmt_at(big_enough_parent(newpos, parent.finish + 1), rangeng)
405 local function splice_sexp(range, _, no_refmt)
406 local _, parent = walker.sexp_at(range)
407 if not parent or not parent.d then return end
408 local opening = (parent.p or "")..parent.d
409 local closing = parser.opposite[parent.d]
410 local finish = parent.finish + 1 - #closing
411 if not parent.is_line_comment then
412 delete(finish, #closing)
414 -- TODO: (un)escape special characters, if necessary
415 delete(parent.start, parent.is_empty and (finish - parent.start) or #opening)
416 parser.tree.rewind(parent.start)
417 range.start = range.start - #opening
418 range.finish = range.start
419 local _, parentng = walker.sexp_at(range, true)
420 return not no_refmt and refmt_at(parentng, range) or range.start
423 local function rewrap(parent, kind)
424 local pstart = parent.start + #((parent.p or "")..parent.d) - 1
425 delete(parent.finish, 1)
426 write(parent.finish, parser.opposite[kind])
427 delete(pstart, #parent.d)
428 write(pstart, kind)
429 parser.tree.rewind(parent.start)
432 local function cycle_wrap(range, pos)
433 local _, parent = walker.sexp_at(range)
434 if not parent or not parent.is_list then return end
435 local next_kind = {["("] = "[", ["["] = "{", ["{"] = "("}
436 rewrap(parent, next_kind[parent.d])
437 return pos
440 local function split_sexp(range)
441 local _, parent = walker.sexp_at(range)
442 if not (parent and parent.d) then return end
443 local new_finish, new_start
444 if parent.is_list then
445 local prev = parent.before(range.start, finishof, is_comment)
446 new_finish = prev and prev.finish + 1
447 -- XXX: do not skip comments here, so they end up in the second list
448 -- and are not separated from their target expression:
449 local nxt = new_finish and parent.after(new_finish, startof)
450 new_start = nxt and nxt.start
451 else
452 new_start = range.start
453 new_finish = range.start
455 if not (new_start and new_finish) then return end
456 local opening = (parent.p or "")..parent.d
457 local closing = parser.opposite[parent.d]
458 write(new_start, opening)
459 local sep = parent.is_line_comment and "" -- line comments already have a separator
460 or new_finish == new_start and " " -- only add a separator if there was none before
461 or ""
462 write(new_finish, closing..sep)
463 parser.tree.rewind(parent.start)
464 range.start = new_start + (parent.is_list and 0 or #opening + #closing)
465 range.finish = range.start
466 local _, nodes = walker.sexp_path(range)
467 local parentng, grandparent = nodes[#nodes - 1], nodes[#nodes - 2]
468 local scope = parentng and not parentng.is_root and parentng or grandparent
469 return refmt_at(scope, range)
472 local function join_sexps(range)
473 local node, parent = walker.sexp_at(range, true)
474 local first = node and node.finish + 1 == range.start and node or parent.before(range.start, finishof)
475 local second = first ~= node and node or parent.after(range.start, startof)
476 if not (first and second and first.d and
477 -- don't join line comments to margin comments:
478 (not first.is_line_comment or first.indent and second.indent) and
479 (first.d == second.d or
480 -- join line comments even when their delimiters differ slightly
481 -- (different number of semicolons, existence/lack of a space after them)
482 parser.opposite[first.d] == parser.opposite[second.d])) then
483 return
485 local opening = (second.p or "")..second.d
486 local closing = parser.opposite[first.d]
487 local pos
488 if not first.is_list then
489 pos = first.finish + 1 - #closing
490 delete(pos, second.start + #opening - pos)
491 else
492 delete(second.start, #opening)
493 delete(first.finish, #closing)
494 pos = second.start - #closing
496 parser.tree.rewind(first.start)
497 range.start = pos
498 range.finish = range.start
499 local _, nodes = walker.sexp_path(range)
500 local parentng, grandparent = nodes[#nodes - 1], nodes[#nodes - 2]
501 local scope = parentng and not parentng.is_root and parentng or grandparent
502 return refmt_at(scope, range)
505 local function delete_splicing(range, pos, splicing, delete_and_yank)
506 local action = {kill = true, wrap = splicing, splice = splicing, func = delete_and_yank}
507 local sexp, parent, n = walker.sexp_at(range, true)
508 range = extend_overlap(range)
509 local ndeleted, spliced = pick_out(range, pos, action)
510 local closing = spliced and parser.opposite[sexp.d]
511 local inner_list_len = spliced and sexp.finish - sexp.start + 1 - #sexp.d - #closing
512 local range_len = range.finish - range.start
513 local backwards = pos == range.finish
514 local whole_object = sexp and
515 (sexp.start == range.start and sexp.finish + 1 == range.finish
516 or spliced and (inner_list_len <= (backwards and range_len + ndeleted or range_len)))
517 local in_head_atom = sexp and not sexp.d and n == 1 and #parent > 1
518 local in_whitespace = not sexp
519 if whole_object or in_whitespace or in_head_atom then
520 local cur = whole_object and sexp.start or range.start
521 -- if parent[#parent + 1] is nil, we are at EOF
522 if not parent.is_root or parent.is_parsed(cur) or parent[#parent + 1] then
523 parser.tree.rewind(cur)
525 local r = {start = cur, finish = cur}
526 local _, parentng = walker.sexp_at(r, true)
527 return refmt_at(parentng, r)
529 return spliced and (backwards and sexp.start or range.start - ndeleted)
530 or not backwards and ndeleted <= 0 and range.finish - ndeleted
531 or backwards and ndeleted <= 0 and range.start + ndeleted
532 or range.start
535 local function transpose(range, first, second)
536 if not (first and second) then return end
537 local copy1 = first.text
538 local copy2 = second.text
539 delete(second.start, second.finish + 1 - second.start)
540 write(second.start, copy1)
541 delete(first.start, first.finish + 1 - first.start)
542 write(first.start, copy2)
543 parser.tree.rewind(first.start)
544 range.start = second.finish + 1
545 range.finish = range.start
546 return refmt_at(big_enough_parent(first.start, second.finish), range)
549 local function transpose_sexps(range)
550 local node, parent = walker.sexp_at(range, true)
551 local first = node and node.finish + 1 == range.start and node or parent.before(range.start, finishof)
552 local second = first ~= node and node or parent.after(range.start, startof)
553 return transpose(range, first, second)
556 local function transpose_words(range)
557 local _, first = walker.start_float_before(range)
558 local _, second = walker.finish_float_after(range)
559 if first and second and first.start == second.start then
560 _, first = walker.start_float_before(first)
562 return transpose(range, first, second)
565 local function transpose_chars(range)
566 local node, parent, i = walker.sexp_at(range)
567 local nxt = i and parent[i + 1]
568 local pstart = not parent.is_root and parent.start + (parent.p and #parent.p or 0) + #parent.d
569 local pfinish = not parent.is_root and parent.finish
570 local npref = node and node.p and node.start + #node.p
571 -- only allow transposing while inside atoms/words and prefixes
572 if node and ((npref and (range.start <= npref and range.start > node.start) or
573 node.d and range.start > node.finish + 1) or not node.d and (not pstart or range.start > pstart)) then
574 local start = range.start -
575 ((range.start == pfinish or range.start == npref or
576 range.start == node.finish + 1 and (parent.is_list or parent.is_root) and (not nxt or nxt.indent)) and 1 or 0)
577 local str_start = start - node.start + 1
578 local char = node.text:sub(str_start, str_start)
579 delete(start, 1)
580 write(start - 1, #char > 0 and char or " ")
581 parser.tree.rewind(start)
582 local in_head_atom = i == 1 and #parent > 1
583 return parent.is_list and in_head_atom and refmt_at(parent, {start = start + 1, finish = start + 1}) or start + 1
587 local function _join_or_splice(parent, n, range, pos)
588 local sexp = parent[n]
589 local nxt = parent[n + 1]
590 local is_last = not nxt or not nxt.is_line_comment
591 local newpos = not (is_last and sexp.is_empty) and join_sexps(range)
592 if not newpos and sexp.is_empty then
593 newpos = splice_sexp({start = pos, finish = pos}, nil, true)
595 return newpos or pos
598 local function delete_nonsplicing(range, pos, delete_maybe_yank)
599 local action = {kill = true, wrap = false, splice = false, func = delete_maybe_yank}
600 range = extend_overlap(range)
601 local ndeleted, spliced, opening, _, refmt = pick_out(range, pos, action)
602 local backwards = pos == range.finish
603 if opening then
604 if spliced then
605 return pos - ndeleted
606 else
607 if ndeleted == 0 then
608 local closing = parser.opposite[opening] -- XXX: why don't I use the pick_out return value?
609 if closing == "\n" then
610 local sexp, parent, n = walker.sexp_at(range)
611 if pos == sexp.start + #sexp.d and backwards then
612 return _join_or_splice(parent, n, range, pos)
613 elseif pos == sexp.finish then
614 local r = {start = pos + #closing, finish = pos + #closing}
615 return _join_or_splice(parent, n, r, pos)
619 return backwards and (range.finish - ndeleted) or range.start
621 else
622 local newpos = backwards and range.start + (ndeleted <= 0 and ndeleted or 0) or (range.finish - ndeleted)
623 if refmt then
624 local r = {start = newpos, finish = newpos}
625 return refmt_at(big_enough_parent(range.start, range.finish - ndeleted), r, true)
627 return newpos
631 local function insert_pair(range, delimiter, shiftless, auto_square)
632 local indices, nodes = walker.sexp_path(range)
633 local sexp = nodes[#nodes]
634 local right_after_prefix = sexp and sexp.p and range.start == sexp.start + #sexp.p
635 -- XXX: here I assume that # is a valid prefix for the dialect
636 local mb_closing = (delimiter == "|") and right_after_prefix and parser.opposite[sexp.p .. delimiter]
637 local closing = mb_closing or parser.opposite[delimiter]
638 local squarewords = fmt.squarewords
639 if squarewords and not right_after_prefix
640 and not (closing == "]" and shiftless and not auto_square)
641 and (auto_square or squarewords.not_optional)
642 and not fmt:adjust_bracket_p(indices, nodes, range) then
643 delimiter, closing = "[", "]"
645 write(range.finish, closing)
646 write(range.start, delimiter)
647 if right_after_prefix or parser.tree.is_parsed(range.start) then
648 parser.tree.rewind(right_after_prefix and sexp.start or range.start)
650 return range.start + #delimiter
653 local function make_wrap(kind)
654 return function(range, pos, shiftless, auto_square)
655 local opening = shiftless and swap[kind] or kind
656 local function _wrap(r)
657 if not (r.finish > r.start) then return end
658 insert_pair(r, opening, false, auto_square)
660 local action = {kill = false, wrap = false, splice = false, func = _wrap}
661 pick_out(range, pos, action)
662 local rangeng = {start = pos + #opening, finish = range.finish}
663 local _, parentng = walker.sexp_at(range) -- TODO: range.finish is wrong. use sexp_path and big_enough_parent
664 return refmt_at(parentng, rangeng)
668 local function newline(parent, range)
669 local line_comment = parent.is_line_comment
670 -- do not autoextend margin comments:
671 if line_comment and range.start < parent.finish + (parent.indent and 1 or 0) then
672 local newpos = split_sexp(range)
673 if newpos then
674 return newpos
677 if not parent.is_list and not line_comment then
678 -- if parent[#parent + 1] is nil, we are at EOF
679 if not parent.is_root or parent.is_parsed(range.start) or parent[#parent + 1] then
680 parser.tree.rewind(parent.start or range.start)
682 local newpos = range.start
683 write(newpos, "\n")
684 return newpos + 1
686 if not parent.is_list then
687 local _
688 _, parent = walker.sexp_at(parent, true)
690 local last_nonblank_in_list = not parent.is_empty and parent[#parent].finish - (line_comment and 1 or 0)
691 local nxt = parent.after(range.start, startof)
692 local last_on_line = range.start == eol_at(range.start)
693 local margin = nxt and not nxt.indent and nxt.is_line_comment and nxt.finish
694 local after_last = last_nonblank_in_list and range.start > last_nonblank_in_list
695 local in_indent = nxt and nxt.indent and range.start <= nxt.start and range.start >= (nxt.start - nxt.indent)
696 local placeholder = "asdf"
697 local newpos = margin or range.start
698 if not parent.is_empty then
699 if in_indent then
700 write(newpos, (placeholder.."\n"))
701 else
702 write(newpos, "\n"..((after_last or last_on_line or margin) and placeholder or ""))
705 parser.tree.rewind(parent.start or newpos)
706 -- move the cursor onto the placeholder, so refmt_at can restore the position:
707 local r = {start = newpos, finish = newpos}
708 newpos = walker.start_after(r) or newpos
709 local rangeng = {start = newpos, finish = newpos}
710 newpos = refmt_at(parent, rangeng)
711 local _, parentng = walker.sexp_at({start = newpos, finish = newpos}, true)
712 if after_last then
713 local autoindent = parentng[#parentng].indent
714 write(parentng.finish, "\n"..string.rep(" ", autoindent or 0))
716 if after_last or last_on_line or margin or in_indent then
717 delete(newpos, #placeholder)
719 parser.tree.rewind(parentng.start or newpos)
720 return newpos
723 local function close_and_newline(range, closing)
724 local opening = closing and parser.opposite[closing]
725 local parent = walker.quasilist_at(range, opening)
726 if parent then
727 local has_eol = parent.is_line_comment
728 local r = {start = parent.finish, finish = parent.finish}
729 local newpos = refmt_at(parent, r)
730 r = {start = newpos + (has_eol and 0 or 1), finish = newpos + (has_eol and 0 or 1)}
731 local list, parentng = walker.sexp_at(r)
732 newpos = list and list.finish + 1 or r.start
733 newpos = newline(parentng, {start = newpos, finish = newpos})
734 if not newpos then
735 newpos = r.finish + 1
736 write(newpos, "\n")
737 parser.tree.rewind(parentng.start or list.start)
739 return newpos
740 elseif closing == "\n" then
741 local eol = eol_at(range.start)
742 local _, parentng = walker.sexp_at({start = eol, finish = eol})
743 local newpos = newline(parentng, {start = eol, finish = eol})
744 return newpos
746 return range.start
749 local function join_line(_, pos)
750 local eol = eol_at(pos)
751 local r = {start = eol, finish = eol + 1}
752 return delete_nonsplicing(r, r.start, delete)
755 local wrap_doublequote = make_wrap'"'
757 local function meta_doublequote(range, pos, auto_square)
758 local escaped = walker.escaped_at(range)
759 if not escaped then
760 return wrap_doublequote(range, pos, nil, auto_square)
761 elseif escaped.is_string then
762 return close_and_newline(range, '"')
764 return pos
767 local block_comment_start
768 for o, c in pairs(parser.opposite) do
769 if #c > 1 then
770 block_comment_start = o
771 break
775 local wrap_comment = make_wrap(block_comment_start)
777 return {
778 delete_splicing = delete_splicing,
779 delete_nonsplicing = delete_nonsplicing,
780 refmt_at = refmt_at,
781 pick_out = pick_out,
782 raise_sexp = raise_sexp,
783 slurp_sexp = slurp_sexp,
784 barf_sexp = barf_sexp,
785 splice_sexp = splice_sexp,
786 wrap_round = make_wrap"(",
787 wrap_square = make_wrap"[",
788 wrap_curly = make_wrap"{",
789 meta_doublequote = meta_doublequote,
790 wrap_comment = wrap_comment,
791 insert_pair = insert_pair,
792 newline = newline,
793 join_line = join_line,
794 close_and_newline = close_and_newline,
795 cycle_wrap = cycle_wrap,
796 split_sexp = split_sexp,
797 join_sexps = join_sexps,
798 transpose_sexps = transpose_sexps,
799 transpose_words = transpose_words,
800 transpose_chars = transpose_chars,
804 return M