fix electric RET with one-char long first element
[lisp-parkour.git] / edit.lua
blob91a64d9975d9691ed0b33c4f785abd6fc278a69e
1 local M = {}
3 -- XXX: in Lua 5.2 unpack() was moved into table
4 local unpack = unpack or table.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 function M.new(parser, walker, fmt, write, delete, eol_at)
79 local function refmt_list(parent, base_indent, padj, deltas, keep_electric_space)
80 local adj = 0
81 local op_adj = 0
82 local indent_adj = 0
83 local pstart = parent.start + (parent.p and #parent.p or 0) + #parent.d
84 if parent.is_empty then
85 normalize_spacing(pstart, parent.finish - pstart, deltas)
86 return deltas
87 end
88 local last_distinguished = fmt:last_distinguished(parent)
89 local first_argument = 1
90 if #parent > 2 and parent[1].is_list and #parent[1] > 1 then
91 local op = parent[1]
92 local nearest_indented = op[#op].indent and op[#op] or walker.indented_before(op[#op])
93 indent_adj = nearest_indented.start - pstart - nearest_indented.indent + 1
94 end
95 for i, s in ipairs(parent) do
96 adj = s.indent and 0 or adj
97 if last_distinguished then
98 -- do not let early comments influence the indentation of real expressions:
99 if i <= last_distinguished and s.is_comment then
100 last_distinguished = last_distinguished + 1
102 -- s is the name of a named let. account for it:
103 if i == last_distinguished + 1 and not s.is_list and parent[1].text == "let" then
104 last_distinguished = last_distinguished + 1
106 -- do not let early comments influence the indentation of real expressions:
107 elseif i <= first_argument + 1 and s.is_comment then
108 first_argument = first_argument + 1
110 local has_eol = s.is_line_comment
111 adj = leading_space(s, deltas, adj, parent, i, pstart, has_eol)
112 adj = trailing_space(s, deltas, adj, parent, i, has_eol, keep_electric_space)
113 adj = indentation(s, deltas, adj, parent, i, pstart, base_indent, last_distinguished, first_argument,
114 op_adj, indent_adj)
115 if i == 1 then
116 op_adj = adj
118 if s.is_list then
119 local nearest_indented = s.indent and s or walker.indented_before(s)
120 local parent_column = nearest_indented and
121 nearest_indented.indent + (s.start + (s.p and #s.p or 0) + #s.d) - nearest_indented.start
122 or base_indent + (s.start + (s.p and #s.p or 0) + #s.d) - pstart
123 refmt_list(s, parent_column - adj, padj + adj, deltas, keep_electric_space)
126 return deltas
129 local function path_from(range)
130 local _, parent_at, m = walker.sexp_at(range, true)
131 local path
132 if parent_at[m] then
133 path = walker.sexp_path(parent_at[m])
134 local base = parent_at[m]
135 if range.finish == base.finish + 1 then
136 path.after_finish = 0 -- lists can end up with a different length
137 else
138 path.after_start = range.start - base.start
140 else
141 local s = parent_at[#parent_at]
142 if not s or range.start >= s.finish + 1 + (s.is_line_comment and 1 or 0) then
143 path = walker.sexp_path(parent_at)
144 path.at_pfinish = true
145 else
146 local prev, n = parent_at.before(range.start, finishof)
147 if prev then
148 path = walker.sexp_path(prev)
149 local nxt = parent_at[n + 1]
150 path.after_finish = nxt and not nxt.indent and 1 or range.finish - prev.finish - 1
151 else
152 path = walker.sexp_path(parent_at[1])
153 path.after_start = 0
157 return path
160 local function pos_from(path, range)
161 local sexp, parentng, n = walker.goto_path(path)
162 if sexp then
163 if path.after_finish then
164 local nxt = n and parentng[n + 1]
165 local max = nxt and nxt.start - sexp.finish - 1
166 return sexp.finish + 1 - (range.finish - range.start) +
167 (max and math.min(path.after_finish, max) or path.after_finish)
168 elseif path.at_pfinish then
169 return sexp.finish
170 else
171 return sexp.start + path.after_start
176 local function refmt_at(scope, range, keep_electric_space)
177 if not range or not scope or scope.is_root or (scope.finish - scope.start < 2) then return range.start end
178 local parent = walker.sexp_at(scope, true)
179 if not (parent and parent.is_list) then return range.start end
180 local path = path_from(range)
181 local indented_parent = parent.indent and parent or parent.is_list and walker.indented_before(parent)
182 local parent_column = indented_parent and (indented_parent.indent + parent.start - indented_parent.start +
183 (parent.p and #parent.p or 0) + #parent.d) or
185 local deltas = refmt_list(parent, parent_column, 0, {}, keep_electric_space)
186 table.sort(deltas, function(d1, d2) return d1[1] > d2[1] end)
187 for _, pair in ipairs(deltas) do
188 local offset, delta = unpack(pair)
189 if delta > 0 then
190 write(offset, string.rep(" ", delta))
191 elseif delta < 0 then
192 delete(offset + delta, -delta)
195 parser.tree.rewind(parent.start or 0)
196 return path and pos_from(path, range) or range.start
199 local function splice(pos, sexps, skip, backwards, action)
200 local spliced
201 local sexp = walker.sexp_at(skip, true)
202 local start = sexp.start + (sexp.p and #sexp.p or 0)
203 -- XXX: don't splice empty line comments _yet_. See _join_or_splice
204 local tosplice = action.splice or action.wrap or not sexp.is_line_comment and sexp.is_empty
205 local opening = (sexp.p or "")..sexp.d
206 local closing = parser.opposite[sexp.d]
207 local real_start = tosplice and sexp.start or start + #sexp.d
208 local splice_closing = not sexp.is_line_comment or sexp.is_empty
209 local real_finish = sexp.finish + 1 - (tosplice and splice_closing and 0 or #closing)
210 local first = backwards and
211 {start = real_start, finish = math.max(pos, start + #sexp.d)} or
212 {start = real_start, finish = sexp.is_empty and pos or start + #sexp.d}
213 local second = backwards and
214 {start = sexp.is_empty and pos or sexp.finish + 1 - #closing, finish = real_finish} or
215 {start = math.min(pos, sexp.finish + 1 - #closing), finish = real_finish}
216 action.func(second)
217 action.func(first)
218 spliced = tosplice
219 if action.kill then
220 local ndeleted = first.finish - first.start + second.finish - second.start
221 if ndeleted > 0 then
222 sexps.rewind(sexp.start)
225 return first.finish - first.start, spliced, opening, closing
228 -- If you try to delete some part of indentation, this function joins the current line with
229 -- the previous one, unless the latter is a line comment.
230 -- it does this by
231 -- a) extending or shrinking the range that is to be deleted
232 -- b) returning a truthy value to trick pick_out to glide the cursor, but refmt to restore the deleted spaces
233 -- c) both a) and b)
234 local function delete_indentation(range, pos)
235 local node, parent = walker.sexp_at(range)
236 if node or not parent.is_list then return end
237 local prev = parent.before(pos, finishof)
238 local nxt = parent.after(pos, startof)
239 if prev and prev.finish >= range.start or nxt and nxt.start < range.finish then return end
240 local backwards = pos == range.finish
241 local adj = 0
242 local eol = eol_at(pos)
243 local on_empty_line = not nxt or eol and nxt.start > eol
244 local empty_line_after_comment = prev and prev.is_line_comment
245 and on_empty_line
246 if nxt and nxt.indent then
247 if backwards and prev then
248 range.start = prev.finish + (empty_line_after_comment and 0 or 1)
249 if not prev.d then
250 range.finish = range.start + 1
251 adj = (on_empty_line and 0 or 1)
254 return adj
256 if not nxt and prev then
257 range.start = prev.finish + 1
258 range.finish = parent.finish
259 elseif nxt and nxt.start == range.finish and (not prev or prev.finish + 1 == range.start) then
260 if prev and prev.d or nxt and nxt.d then
261 -- don't delete spaces near delimiters, just slide the cursor:
262 if backwards then
263 range.finish = range.start
264 else
265 range.start = range.finish
268 return adj
272 local function big_enough_parent(pos1, pos2)
273 -- since the pos1-pos2 range can cross list boundaries, find which list contains both pos1 and pos2
274 local _, p1, p2
275 _, p1 = walker.sexp_at({start = pos1, finish = pos1}, true)
276 if p1.is_root or not (p1.start < pos1 and p1.finish > pos2) then
277 _, p2 = walker.sexp_at({start = pos2, finish = pos2}, true)
279 return p2 and not p2.is_root and p2 or p1
282 local function extend_overlap(range)
283 local rnode = walker.sexp_at({start = range.finish, finish = range.finish})
284 if rnode and rnode.p and rnode.p:find";"
285 and range.finish > rnode.start and range.finish < rnode.start + #rnode.p then
286 return {start = range.start, finish = rnode.start + #rnode.p}
288 return range
291 local function pick_out(range, pos, action)
292 local ndeleted = 0
293 if range.start == range.finish then return ndeleted end
294 local sexps = parser.tree
295 local skips = sexps.unbalanced_delimiters(range)
296 -- handle splice and kill-splice of forms and strings:
297 if #skips == 1 then
298 local sexp = walker.sexp_at(skips[1], true)
299 local backward_splice = skips[1].opening and pos >= sexp.start + (sexp.p and #sexp.p or 0) + #sexp.d
300 and range.start >= sexp.start
301 local forward_splice = skips[1].closing and pos <= sexp.finish + 1 - #parser.opposite[sexp.d]
302 and range.finish <= sexp.finish + 1
303 if backward_splice or forward_splice then
304 return splice(backward_splice and range.finish or range.start, sexps, sexp, backward_splice, action)
307 local node, parent = walker.sexp_at({start = range.finish, finish = range.finish})
308 -- if the range ends with a line comment, don't delete its closing newline:
309 local drop_eol = action.kill and node and
310 node.finish + 1 == range.finish and node.is_line_comment
311 local par = big_enough_parent(range.start, range.finish)
312 local operator_changed = par[1] and par[1].finish >= range.start
313 local refmt = #skips == 0 and delete_indentation(range, pos) or operator_changed and 0
314 table.sort(skips, function(a, b) return a.start < b.start end)
315 table.insert(skips, {start = range.finish - (drop_eol and 1 or 0)})
316 table.insert(skips, 1, {finish = range.start})
317 ndeleted = ndeleted + (drop_eol and 1 or 0)
318 for i = #skips - 1, 1, -1 do
319 local region = {start = skips[i].finish, finish = skips[i + 1].start}
320 if skips[i].closing and skips[i + 1].opening then
321 -- leave out some of the space between adjacent lists
322 local _, rparent = walker.sexp_at(region)
323 local nxt = rparent.after(region.start, startof)
324 region.start = nxt and nxt.start or region.start
326 if action then
327 action.func(region)
328 ndeleted = ndeleted + (region.finish - region.start)
331 -- if parent[#parent + 1] is nil, we are at EOF
332 if ndeleted > 0 and (not parent.is_root or parent.is_parsed(range.start) or parent[#parent + 1]) then
333 sexps.rewind(range.start)
335 return ndeleted - (refmt or 0), nil, nil, nil, refmt
338 local function raise_sexp(range, pos)
339 local sexp, parent = walker.sexp_at(range, true)
340 if sexp and parent and parent.is_list then
341 delete(sexp.finish + 1, parent.finish - sexp.finish)
342 delete(parent.start, sexp.start - parent.start)
343 parser.tree.rewind(parent.start)
344 range.start = parent.start + pos - sexp.start
345 range.finish = range.start
346 local _, nodes = walker.sexp_path(range)
347 local grandparent = nodes[#nodes - 2]
348 return grandparent and refmt_at(grandparent, range) or range.start
352 local function slurp_sexp(range, forward)
353 local _, parent = walker.sexp_at(range, true)
354 local seeker = forward and walker.finish_after or walker.start_before
355 if not parent or not parent.is_list then return range.start end
356 local r = {start = parent.start, finish = parent.finish + 1}
357 local newpos = seeker(r, is_comment)
358 if not newpos then return range.start end
359 local opening = (parent.p or "")..parent.d
360 local closing = parser.opposite[parent.d]
361 local delimiter = forward and closing or opening
362 if forward then
363 write(newpos, delimiter)
365 delete(forward and parent.finish or parent.start, #delimiter)
366 if not forward then
367 write(newpos, delimiter)
369 parser.tree.rewind(math.min(parent.start, newpos))
370 return refmt_at(big_enough_parent(newpos, range.start), range)
373 local function barf_sexp(range, forward)
374 local _, parent, m = walker.sexp_at(range, true)
375 local seeker = forward and walker.finish_before or walker.start_after
376 -- TODO: barfing out of strings requires calling the parser on them
377 if not parent or not parent.is_list then return range.start end
378 local opening = (parent.p or "")..parent.d
379 local pstart = parent.start + #opening
380 local r = {start = forward and parent.finish - 1 or pstart, finish = forward and parent.finish or pstart + 1}
381 local newpos = seeker(r, is_comment) or forward and pstart or parent.finish
382 if not newpos then return range.start end
383 local closing = parser.opposite[parent.d]
384 local delimiter = forward and closing or opening
385 if not forward then
386 write(newpos, delimiter)
388 delete(forward and parent.finish or parent.start, #delimiter)
389 if forward then
390 write(newpos, delimiter)
392 parser.tree.rewind(math.min(parent.start, newpos))
393 local barfed_cursor_backward = m == 1 and not forward
394 local barfed_cursor_forward = m == #parent and forward
395 range.start = range.start + #delimiter * (barfed_cursor_backward and -1 or barfed_cursor_forward and 1 or 0)
396 return refmt_at(big_enough_parent(newpos, parent.finish + 1), range)
399 local function splice_sexp(range, _, no_refmt)
400 local _, parent = walker.sexp_at(range)
401 if not parent or not parent.d then return end
402 local opening = (parent.p or "")..parent.d
403 local closing = parser.opposite[parent.d]
404 local finish = parent.finish + 1 - #closing
405 if not parent.is_line_comment then
406 delete(finish, #closing)
408 -- TODO: (un)escape special characters, if necessary
409 delete(parent.start, parent.is_empty and (finish - parent.start) or #opening)
410 parser.tree.rewind(parent.start)
411 range.start = range.start - #opening
412 range.finish = range.start
413 local _, parentng = walker.sexp_at(range, true)
414 return not no_refmt and refmt_at(parentng, range) or range.start
417 local function rewrap(parent, kind)
418 local pstart = parent.start + #((parent.p or "")..parent.d) - 1
419 delete(parent.finish, 1)
420 write(parent.finish, parser.opposite[kind])
421 delete(pstart, #parent.d)
422 write(pstart, kind)
423 parser.tree.rewind(parent.start)
426 local function cycle_wrap(range, pos)
427 local _, parent = walker.sexp_at(range)
428 if not parent or not parent.is_list then return end
429 local next_kind = {["("] = "[", ["["] = "{", ["{"] = "("}
430 rewrap(parent, next_kind[parent.d])
431 return pos
434 local function split_sexp(range)
435 local _, parent = walker.sexp_at(range)
436 if not (parent and parent.d) then return end
437 local new_finish, new_start
438 if parent.is_list then
439 local prev = parent.before(range.start, finishof, is_comment)
440 new_finish = prev and prev.finish + 1
441 -- XXX: do not skip comments here, so they end up in the second list
442 -- and are not separated from their target expression:
443 local nxt = new_finish and parent.after(new_finish, startof)
444 new_start = nxt and nxt.start
445 else
446 new_start = range.start
447 new_finish = range.start
449 if not (new_start and new_finish) then return end
450 local opening = (parent.p or "")..parent.d
451 local closing = parser.opposite[parent.d]
452 write(new_start, opening)
453 local sep = parent.is_line_comment and "" -- line comments already have a separator
454 or new_finish == new_start and " " -- only add a separator if there was none before
455 or ""
456 write(new_finish, closing..sep)
457 parser.tree.rewind(parent.start)
458 range.start = new_start + (parent.is_list and 0 or #opening + #closing)
459 range.finish = range.start
460 local _, nodes = walker.sexp_path(range)
461 local parentng, grandparent = nodes[#nodes - 1], nodes[#nodes - 2]
462 local scope = parentng and not parentng.is_root and parentng or grandparent
463 return refmt_at(scope, range)
466 local function join_sexps(range)
467 local node, parent = walker.sexp_at(range, true)
468 local first = node and node.finish + 1 == range.start and node or parent.before(range.start, finishof)
469 local second = first ~= node and node or parent.after(range.start, startof)
470 if not (first and second and first.d and
471 -- don't join line comments to margin comments:
472 (not first.is_line_comment or first.indent and second.indent) and
473 (first.d == second.d or
474 -- join line comments even when their delimiters differ slightly
475 -- (different number of semicolons, existence/lack of a space after them)
476 parser.opposite[first.d] == parser.opposite[second.d])) then
477 return
479 local opening = (second.p or "")..second.d
480 local closing = parser.opposite[first.d]
481 local pos
482 if not first.is_list then
483 pos = first.finish + 1 - #closing
484 delete(pos, second.start + #opening - pos)
485 else
486 delete(second.start, #opening)
487 delete(first.finish, #closing)
488 pos = second.start - #closing
490 parser.tree.rewind(first.start)
491 range.start = pos
492 range.finish = range.start
493 local _, nodes = walker.sexp_path(range)
494 local parentng, grandparent = nodes[#nodes - 1], nodes[#nodes - 2]
495 local scope = parentng and not parentng.is_root and parentng or grandparent
496 return refmt_at(scope, range)
499 local function delete_splicing(range, pos, splicing, delete_and_yank)
500 local action = {kill = true, wrap = splicing, splice = splicing, func = delete_and_yank}
501 local sexp, parent, n = walker.sexp_at(range, true)
502 range = extend_overlap(range)
503 local ndeleted, spliced = pick_out(range, pos, action)
504 local closing = spliced and parser.opposite[sexp.d]
505 local inner_list_len = spliced and sexp.finish - sexp.start + 1 - #sexp.d - #closing
506 local range_len = range.finish - range.start
507 local backwards = pos == range.finish
508 local whole_object = sexp and
509 (sexp.start == range.start and sexp.finish + 1 == range.finish
510 or spliced and (inner_list_len <= (backwards and range_len + ndeleted or range_len)))
511 local in_head_atom = sexp and not sexp.d and n == 1 and #parent > 1
512 local in_whitespace = not sexp
513 if whole_object or in_whitespace or in_head_atom then
514 local cur = whole_object and sexp.start or range.start
515 -- if parent[#parent + 1] is nil, we are at EOF
516 if not parent.is_root or parent.is_parsed(cur) or parent[#parent + 1] then
517 parser.tree.rewind(cur)
519 local r = {start = cur, finish = cur}
520 local _, parentng = walker.sexp_at(r, true)
521 return refmt_at(parentng, r)
523 return spliced and (backwards and sexp.start or range.start - ndeleted)
524 or not backwards and ndeleted <= 0 and range.finish - ndeleted
525 or backwards and ndeleted <= 0 and range.start + ndeleted
526 or range.start
529 local function transpose(range, first, second)
530 if not (first and second) then return end
531 local copy1 = first.text
532 local copy2 = second.text
533 delete(second.start, second.finish + 1 - second.start)
534 write(second.start, copy1)
535 delete(first.start, first.finish + 1 - first.start)
536 write(first.start, copy2)
537 parser.tree.rewind(first.start)
538 range.start = second.finish + 1
539 range.finish = range.start
540 return refmt_at(big_enough_parent(first.start, second.finish), range)
543 local function transpose_sexps(range)
544 local node, parent = walker.sexp_at(range, true)
545 local first = node and node.finish + 1 == range.start and node or parent.before(range.start, finishof)
546 local second = first ~= node and node or parent.after(range.start, startof)
547 return transpose(range, first, second)
550 local function transpose_words(range)
551 local _, first = walker.start_float_before(range)
552 local _, second = walker.finish_float_after(range)
553 if first and second and first.start == second.start then
554 _, first = walker.start_float_before(first)
556 return transpose(range, first, second)
559 local function transpose_chars(range)
560 local node, parent, i = walker.sexp_at(range)
561 local nxt = i and parent[i + 1]
562 local pstart = not parent.is_root and parent.start + (parent.p and #parent.p or 0) + #parent.d
563 local pfinish = not parent.is_root and parent.finish
564 local npref = node and node.p and node.start + #node.p
565 -- only allow transposing while inside atoms/words and prefixes
566 if node and ((npref and (range.start <= npref and range.start > node.start) or
567 node.d and range.start > node.finish + 1) or not node.d and (not pstart or range.start > pstart)) then
568 local start = range.start -
569 ((range.start == pfinish or range.start == npref or
570 range.start == node.finish + 1 and (parent.is_list or parent.is_root) and (not nxt or nxt.indent)) and 1 or 0)
571 local str_start = start - node.start + 1
572 local char = node.text:sub(str_start, str_start)
573 delete(start, 1)
574 write(start - 1, #char > 0 and char or " ")
575 parser.tree.rewind(start)
576 local in_head_atom = i == 1 and #parent > 1
577 return parent.is_list and in_head_atom and refmt_at(parent, {start = start + 1, finish = start + 1}) or start + 1
581 local function _join_or_splice(parent, n, range, pos)
582 local sexp = parent[n]
583 local nxt = parent[n + 1]
584 local is_last = not nxt or not nxt.is_line_comment
585 local newpos = not (is_last and sexp.is_empty) and join_sexps(range)
586 if not newpos and sexp.is_empty then
587 newpos = splice_sexp({start = pos, finish = pos}, nil, true)
589 return newpos or pos
592 local function delete_nonsplicing(range, pos, delete_maybe_yank)
593 local action = {kill = true, wrap = false, splice = false, func = delete_maybe_yank}
594 range = extend_overlap(range)
595 local ndeleted, spliced, opening, _, refmt = pick_out(range, pos, action)
596 local backwards = pos == range.finish
597 if opening then
598 if spliced then
599 return pos - ndeleted
600 else
601 if ndeleted == 0 then
602 local closing = parser.opposite[opening] -- XXX: why don't I use the pick_out return value?
603 if closing == "\n" then
604 local sexp, parent, n = walker.sexp_at(range)
605 if pos == sexp.start + #sexp.d and backwards then
606 return _join_or_splice(parent, n, range, pos)
607 elseif pos == sexp.finish then
608 local r = {start = pos + #closing, finish = pos + #closing}
609 return _join_or_splice(parent, n, r, pos)
613 return backwards and (range.finish - ndeleted) or range.start
615 else
616 local newpos = backwards and range.start + (ndeleted <= 0 and ndeleted or 0) or (range.finish - ndeleted)
617 if refmt then
618 local r = {start = newpos, finish = newpos}
619 return refmt_at(big_enough_parent(range.start, range.finish - ndeleted), r, true)
621 return newpos
625 local function insert_pair(range, delimiter, shiftless, auto_square)
626 local indices, nodes = walker.sexp_path(range)
627 local sexp = nodes[#nodes]
628 local right_after_prefix = sexp and sexp.p and range.start == sexp.start + #sexp.p
629 -- XXX: here I assume that # is a valid prefix for the dialect
630 local mb_closing = (delimiter == "|") and right_after_prefix and parser.opposite[sexp.p .. delimiter]
631 local closing = mb_closing or parser.opposite[delimiter]
632 local squarewords = fmt.squarewords
633 if squarewords and not right_after_prefix
634 and not (closing == "]" and shiftless and not auto_square)
635 and (auto_square or squarewords.not_optional)
636 and not fmt:adjust_bracket_p(indices, nodes, range) then
637 delimiter, closing = "[", "]"
639 write(range.finish, closing)
640 write(range.start, delimiter)
641 if right_after_prefix or parser.tree.is_parsed(range.start) then
642 parser.tree.rewind(right_after_prefix and sexp.start or range.start)
644 return range.start + #delimiter
647 local function make_wrap(kind)
648 return function(range, pos, auto_square)
649 local opening = kind
650 local function _wrap(r)
651 if not (r.finish > r.start) then return end
652 insert_pair(r, opening, false, auto_square)
654 local action = {kill = false, wrap = false, splice = false, func = _wrap}
655 pick_out(range, pos, action)
656 local rangeng = {start = pos + #opening, finish = range.finish}
657 local _, parentng = walker.sexp_at(range) -- TODO: range.finish is wrong. use sexp_path and big_enough_parent
658 return refmt_at(parentng, rangeng)
662 local function newline(parent, range)
663 local line_comment = parent.is_line_comment
664 -- do not autoextend margin comments:
665 if line_comment and range.start < parent.finish + (parent.indent and 1 or 0) then
666 local newpos = split_sexp(range)
667 if newpos then
668 return newpos
671 if not parent.is_list and not line_comment then
672 -- if parent[#parent + 1] is nil, we are at EOF
673 if not parent.is_root or parent.is_parsed(range.start) or parent[#parent + 1] then
674 parser.tree.rewind(parent.start or range.start)
676 local newpos = range.start
677 write(newpos, "\n")
678 return newpos + 1
680 if not parent.is_list then
681 local _
682 _, parent = walker.sexp_at(parent, true)
684 local last_nonblank_in_list = not parent.is_empty and parent[#parent].finish - (line_comment and 1 or 0)
685 local nxt = parent.after(range.start, startof)
686 local last_on_line = range.start == eol_at(range.start)
687 local margin = nxt and not nxt.indent and nxt.is_line_comment and nxt.finish
688 local after_last = last_nonblank_in_list and range.start > last_nonblank_in_list
689 local in_indent = nxt and nxt.indent and range.start <= nxt.start and range.start >= (nxt.start - nxt.indent)
690 local placeholder = "asdf"
691 local newpos = margin or range.start
692 if not parent.is_empty then
693 if in_indent then
694 write(newpos, (placeholder.."\n"))
695 else
696 write(newpos, "\n"..((after_last or last_on_line or margin) and placeholder or ""))
699 parser.tree.rewind(parent.start or newpos)
700 -- move the cursor onto the placeholder, so refmt_at can restore the position:
701 local r = {start = newpos, finish = newpos}
702 newpos = walker.start_after(r) or newpos
703 local rangeng = {start = newpos, finish = newpos}
704 newpos = refmt_at(parent, rangeng)
705 local _, parentng = walker.sexp_at({start = newpos, finish = newpos}, true)
706 if after_last then
707 local autoindent = parentng[#parentng].indent
708 write(parentng.finish, "\n"..string.rep(" ", autoindent or 0))
710 if after_last or last_on_line or margin or in_indent then
711 delete(newpos, #placeholder)
713 parser.tree.rewind(parentng.start or newpos)
714 return newpos
717 local function close_and_newline(range, closing)
718 local opening = closing and parser.opposite[closing]
719 local parent = walker.quasilist_at(range, opening)
720 if parent then
721 local has_eol = parent.is_line_comment
722 local r = {start = parent.finish, finish = parent.finish}
723 local newpos = refmt_at(parent, r)
724 r = {start = newpos + (has_eol and 0 or 1), finish = newpos + (has_eol and 0 or 1)}
725 local list, parentng = walker.sexp_at(r)
726 newpos = list and list.finish + 1 or r.start
727 newpos = newline(parentng, {start = newpos, finish = newpos})
728 if not newpos then
729 newpos = r.finish + 1
730 write(newpos, "\n")
731 parser.tree.rewind(parentng.start or list.start)
733 return newpos
734 elseif closing == "\n" then
735 local eol = eol_at(range.start)
736 local _, parentng = walker.sexp_at({start = eol, finish = eol})
737 local newpos = newline(parentng, {start = eol, finish = eol})
738 return newpos
740 return range.start
743 local function join_line(_, pos)
744 local eol = eol_at(pos)
745 local r = {start = eol, finish = eol + 1}
746 return delete_nonsplicing(r, r.start, delete)
749 local wrap_doublequote = make_wrap'"'
751 local function meta_doublequote(range, pos, auto_square)
752 local escaped = walker.escaped_at(range)
753 if not escaped then
754 return wrap_doublequote(range, pos, auto_square)
755 elseif escaped.is_string then
756 return close_and_newline(range, '"')
758 return pos
761 local block_comment_start
762 for o, c in pairs(parser.opposite) do
763 if #c > 1 then
764 block_comment_start = o
765 break
769 local wrap_comment = make_wrap(block_comment_start)
771 return {
772 delete_splicing = delete_splicing,
773 delete_nonsplicing = delete_nonsplicing,
774 refmt_at = refmt_at,
775 pick_out = pick_out,
776 raise_sexp = raise_sexp,
777 slurp_sexp = slurp_sexp,
778 barf_sexp = barf_sexp,
779 splice_sexp = splice_sexp,
780 wrap_round = make_wrap"(",
781 wrap_square = make_wrap"[",
782 wrap_curly = make_wrap"{",
783 meta_doublequote = meta_doublequote,
784 wrap_comment = wrap_comment,
785 insert_pair = insert_pair,
786 newline = newline,
787 join_line = join_line,
788 close_and_newline = close_and_newline,
789 cycle_wrap = cycle_wrap,
790 split_sexp = split_sexp,
791 join_sexps = join_sexps,
792 transpose_sexps = transpose_sexps,
793 transpose_words = transpose_words,
794 transpose_chars = transpose_chars,
798 return M