return supported dialects
[lisp-parkour.git] / edit.lua
blobefbad5de854868cdddb98e8913fcea8208964c40
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, if any
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 not scope.start or not scope.finish or
178 (scope.finish - scope.start < 3) or scope.is_root then return range.start end
179 local parent = walker.sexp_at(scope, true)
180 if not (parent and parent.is_list) then return range.start end
181 local path = path_from(range)
182 local indented_parent = parent.indent and parent or parent.is_list and walker.indented_before(parent)
183 local parent_column = indented_parent and (indented_parent.indent + parent.start - indented_parent.start +
184 (parent.p and #parent.p or 0) + #parent.d) or
186 local deltas = refmt_list(parent, parent_column, 0, {}, keep_electric_space)
187 table.sort(deltas, function(d1, d2) return d1[1] > d2[1] end)
188 for _, pair in ipairs(deltas) do
189 local offset, delta = unpack(pair)
190 if delta > 0 then
191 write(offset, string.rep(" ", delta))
192 elseif delta < 0 then
193 delete(offset + delta, -delta)
196 parser.tree.rewind(parent.start or 0)
197 return path and pos_from(path, range) or range.start
200 local function splice(pos, sexps, skip, backwards, action)
201 local spliced
202 local sexp = walker.sexp_at(skip, true)
203 local start = sexp.start + (sexp.p and #sexp.p or 0)
204 -- XXX: don't splice empty line comments _yet_. See _join_or_splice
205 local tosplice = action.splice or action.wrap or not sexp.is_line_comment and sexp.is_empty
206 local opening = (sexp.p or "")..sexp.d
207 local closing = parser.opposite[sexp.d]
208 local real_start = tosplice and sexp.start or start + #sexp.d
209 local splice_closing = not sexp.is_line_comment or sexp.is_empty
210 local real_finish = sexp.finish + 1 - (tosplice and splice_closing and 0 or #closing)
211 local first = backwards and
212 {start = real_start, finish = math.max(pos, start + #sexp.d)} or
213 {start = real_start, finish = sexp.is_empty and pos or start + #sexp.d}
214 local second = backwards and
215 {start = sexp.is_empty and pos or sexp.finish + 1 - #closing, finish = real_finish} or
216 {start = math.min(pos, sexp.finish + 1 - #closing), finish = real_finish}
217 action.func(second)
218 action.func(first)
219 spliced = tosplice
220 if action.kill then
221 local ndeleted = first.finish - first.start + second.finish - second.start
222 if ndeleted > 0 then
223 sexps.rewind(sexp.start)
226 return first.finish - first.start, spliced, opening, closing
229 -- If you try to delete some part of indentation, this function joins the current line with
230 -- the previous one, unless the latter is a line comment.
231 -- it does this by
232 -- a) extending or shrinking the range that is to be deleted
233 -- b) returning a truthy value to trick pick_out to glide the cursor, but refmt to restore the deleted spaces
234 -- c) both a) and b)
235 local function delete_indentation(range, pos)
236 local node, parent = walker.sexp_at(range)
237 if node or not parent.is_list then return end
238 local prev = parent.before(pos, finishof)
239 local nxt = parent.after(pos, startof)
240 if prev and prev.finish >= range.start or nxt and nxt.start < range.finish then return end
241 local backwards = pos == range.finish
242 local adj = 0
243 local eol = eol_at(pos)
244 local on_empty_line = not nxt or eol and nxt.start > eol
245 local empty_line_after_comment = prev and prev.is_line_comment
246 and on_empty_line
247 if nxt and nxt.indent then
248 if backwards and prev then
249 range.start = prev.finish + (empty_line_after_comment and 0 or 1)
250 if not prev.d then
251 range.finish = range.start + 1
252 adj = (on_empty_line and 0 or 1)
255 return adj
257 if not nxt and prev then
258 range.start = prev.finish + 1
259 range.finish = parent.finish
260 elseif nxt and nxt.start == range.finish and (not prev or prev.finish + 1 == range.start) then
261 if prev and prev.d or nxt and nxt.d then
262 -- don't delete spaces near delimiters, just slide the cursor:
263 if backwards then
264 range.finish = range.start
265 else
266 range.start = range.finish
269 return adj
273 local function big_enough_parent(pos1, pos2)
274 -- since the pos1-pos2 range can cross list boundaries, find which list contains both pos1 and pos2
275 local _, p1, p2
276 _, p1 = walker.sexp_at({start = pos1, finish = pos1}, true)
277 if p1.is_root or not (p1.start < pos1 and p1.finish > pos2) then
278 _, p2 = walker.sexp_at({start = pos2, finish = pos2}, true)
280 return p2 and not p2.is_root and p2 or p1
283 local function extend_overlap(range)
284 local rnode = walker.sexp_at({start = range.finish, finish = range.finish})
285 if rnode and rnode.p and rnode.p:find";"
286 and range.finish > rnode.start and range.finish < rnode.start + #rnode.p then
287 return {start = range.start, finish = rnode.start + #rnode.p}
289 return range
292 local function pick_out(range, pos, action)
293 local ndeleted = 0
294 if range.start == range.finish then return ndeleted end
295 local sexps = parser.tree
296 local skips = sexps.unbalanced_delimiters(range)
297 -- handle splice and kill-splice of forms and strings:
298 if #skips == 1 then
299 local sexp = walker.sexp_at(skips[1], true)
300 local backward_splice = skips[1].opening and pos >= sexp.start + (sexp.p and #sexp.p or 0) + #sexp.d
301 and range.start >= sexp.start
302 local forward_splice = skips[1].closing and pos <= sexp.finish + 1 - #parser.opposite[sexp.d]
303 and range.finish <= sexp.finish + 1
304 if backward_splice or forward_splice then
305 return splice(backward_splice and range.finish or range.start, sexps, sexp, backward_splice, action)
308 local node, parent = walker.sexp_at({start = range.finish, finish = range.finish})
309 -- if the range ends with a line comment, don't delete its closing newline:
310 local drop_eol = action.kill and node and
311 node.finish + 1 == range.finish and node.is_line_comment
312 local par = big_enough_parent(range.start, range.finish)
313 local operator_changed = par[1] and par[1].finish >= range.start
314 local refmt = #skips == 0 and delete_indentation(range, pos) or operator_changed and 0
315 table.sort(skips, function(a, b) return a.start < b.start end)
316 table.insert(skips, {start = range.finish - (drop_eol and 1 or 0)})
317 table.insert(skips, 1, {finish = range.start})
318 ndeleted = ndeleted + (drop_eol and 1 or 0)
319 for i = #skips - 1, 1, -1 do
320 local region = {start = skips[i].finish, finish = skips[i + 1].start}
321 if skips[i].closing and skips[i + 1].opening then
322 -- leave out some of the space between adjacent lists
323 local _, rparent = walker.sexp_at(region)
324 local nxt = rparent.after(region.start, startof)
325 region.start = nxt and nxt.start or region.start
327 if action then
328 action.func(region)
329 ndeleted = ndeleted + (region.finish - region.start)
332 -- if parent[#parent + 1] is nil, we are at EOF
333 if ndeleted > 0 and (not parent.is_root or parent.is_parsed(range.start) or parent[#parent + 1]) then
334 sexps.rewind(range.start)
336 return ndeleted - (refmt or 0), nil, nil, nil, refmt
339 local function raise_sexp(range, pos)
340 local sexp, parent = walker.sexp_at(range, true)
341 if sexp and parent and parent.is_list then
342 delete(sexp.finish + 1, parent.finish - sexp.finish)
343 delete(parent.start, sexp.start - parent.start)
344 parser.tree.rewind(parent.start)
345 range.start = parent.start + pos - sexp.start
346 range.finish = range.start
347 local _, nodes = walker.sexp_path(range)
348 local grandparent = nodes[#nodes - 2]
349 return grandparent and refmt_at(grandparent, range) or range.start
353 local function slurp_sexp(range, forward)
354 local _, parent = walker.sexp_at(range, true)
355 local seeker = forward and walker.finish_after or walker.start_before
356 if not parent or not parent.is_list then return range.start end
357 local r = {start = parent.start, finish = parent.finish + 1}
358 local newpos = seeker(r, is_comment)
359 if not newpos then return range.start end
360 local opening = (parent.p or "")..parent.d
361 local closing = parser.opposite[parent.d]
362 local delimiter = forward and closing or opening
363 if forward then
364 write(newpos, delimiter)
366 delete(forward and parent.finish or parent.start, #delimiter)
367 if not forward then
368 write(newpos, delimiter)
370 parser.tree.rewind(math.min(parent.start, newpos))
371 return refmt_at(big_enough_parent(newpos, range.start), range)
374 local function barf_sexp(range, forward)
375 local _, parent, m = walker.sexp_at(range, true)
376 local seeker = forward and walker.finish_before or walker.start_after
377 -- TODO: barfing out of strings requires calling the parser on them
378 if not parent or not parent.is_list then return range.start end
379 local opening = (parent.p or "")..parent.d
380 local pstart = parent.start + #opening
381 local r = {start = forward and parent.finish - 1 or pstart, finish = forward and parent.finish or pstart + 1}
382 local newpos = seeker(r, is_comment) or forward and pstart or parent.finish
383 if not newpos then return range.start end
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 barfed_cursor_backward = m == 1 and not forward
395 local barfed_cursor_forward = m == #parent and forward
396 range.start = range.start + #delimiter * (barfed_cursor_backward and -1 or barfed_cursor_forward and 1 or 0)
397 return refmt_at(big_enough_parent(newpos, parent.finish + 1), range)
400 local function splice_sexp(range, _, no_refmt)
401 local _, parent = walker.sexp_at(range)
402 if not parent or not parent.d then return end
403 local opening = (parent.p or "")..parent.d
404 local closing = parser.opposite[parent.d]
405 local finish = parent.finish + 1 - #closing
406 if not parent.is_line_comment then
407 delete(finish, #closing)
409 -- TODO: (un)escape special characters, if necessary
410 delete(parent.start, parent.is_empty and (finish - parent.start) or #opening)
411 parser.tree.rewind(parent.start)
412 range.start = range.start - #opening
413 range.finish = range.start
414 local _, parentng = walker.sexp_at(range, true)
415 return not no_refmt and refmt_at(parentng, range) or range.start
418 local function rewrap(parent, kind)
419 local pstart = parent.start + #((parent.p or "")..parent.d) - 1
420 delete(parent.finish, 1)
421 write(parent.finish, parser.opposite[kind])
422 delete(pstart, #parent.d)
423 write(pstart, kind)
424 parser.tree.rewind(parent.start)
427 local function cycle_wrap(range, pos)
428 local _, parent = walker.sexp_at(range)
429 if not parent or not parent.is_list then return end
430 local next_kind = {["("] = "[", ["["] = "{", ["{"] = "("}
431 rewrap(parent, next_kind[parent.d])
432 return pos
435 local function split_sexp(range)
436 local _, parent = walker.sexp_at(range)
437 if not (parent and parent.d) then return end
438 local new_finish, new_start
439 if parent.is_list then
440 local prev = parent.before(range.start, finishof, is_comment)
441 new_finish = prev and prev.finish + 1
442 -- XXX: do not skip comments here, so they end up in the second list
443 -- and are not separated from their target expression:
444 local nxt = new_finish and parent.after(new_finish, startof)
445 new_start = nxt and nxt.start
446 else
447 new_start = range.start
448 new_finish = range.start
450 if not (new_start and new_finish) then return end
451 local opening = (parent.p or "")..parent.d
452 local closing = parser.opposite[parent.d]
453 write(new_start, opening)
454 local sep = parent.is_line_comment and "" -- line comments already have a separator
455 or new_finish == new_start and " " -- only add a separator if there was none before
456 or ""
457 write(new_finish, closing..sep)
458 parser.tree.rewind(parent.start)
459 range.start = new_start + (parent.is_list and 0 or #opening + #closing)
460 range.finish = range.start
461 local _, nodes = walker.sexp_path(range)
462 local parentng, grandparent = nodes[#nodes - 1], nodes[#nodes - 2]
463 local scope = parentng and not parentng.is_root and parentng or grandparent
464 return refmt_at(scope, range)
467 local function join_sexps(range)
468 local node, parent = walker.sexp_at(range, true)
469 local first = node and node.finish + 1 == range.start and node or parent.before(range.start, finishof)
470 local second = first ~= node and node or parent.after(range.start, startof)
471 if not (first and second and first.d and
472 -- don't join line comments to margin comments:
473 (not first.is_line_comment or first.indent and second.indent) and
474 (first.d == second.d or
475 -- join line comments even when their delimiters differ slightly
476 -- (different number of semicolons, existence/lack of a space after them)
477 parser.opposite[first.d] == parser.opposite[second.d])) then
478 return
480 local opening = (second.p or "")..second.d
481 local closing = parser.opposite[first.d]
482 local pos
483 if not first.is_list then
484 pos = first.finish + 1 - #closing
485 delete(pos, second.start + #opening - pos)
486 else
487 delete(second.start, #opening)
488 delete(first.finish, #closing)
489 pos = second.start - #closing
491 parser.tree.rewind(first.start)
492 range.start = pos
493 range.finish = range.start
494 local _, nodes = walker.sexp_path(range)
495 local parentng, grandparent = nodes[#nodes - 1], nodes[#nodes - 2]
496 local scope = parentng and not parentng.is_root and parentng or grandparent
497 return refmt_at(scope, range)
500 local function delete_splicing(range, pos, splicing, delete_and_yank)
501 local action = {kill = true, wrap = splicing, splice = splicing, func = delete_and_yank}
502 local sexp, parent, n = walker.sexp_at(range, true)
503 range = extend_overlap(range)
504 local ndeleted, spliced = pick_out(range, pos, action)
505 local closing = spliced and parser.opposite[sexp.d]
506 local inner_list_len = spliced and sexp.finish - sexp.start + 1 - #sexp.d - #closing
507 local range_len = range.finish - range.start
508 local backwards = pos == range.finish
509 local whole_object = sexp and
510 (sexp.start == range.start and sexp.finish + 1 == range.finish
511 or spliced and (inner_list_len <= (backwards and range_len + ndeleted or range_len)))
512 local in_head_atom = sexp and not sexp.d and n == 1 and #parent > 1
513 local in_whitespace = not sexp
514 if whole_object or in_whitespace or in_head_atom then
515 local cur = whole_object and sexp.start or range.start
516 -- if parent[#parent + 1] is nil, we are at EOF
517 if not parent.is_root or parent.is_parsed(cur) or parent[#parent + 1] then
518 parser.tree.rewind(cur)
520 local r = {start = cur, finish = cur}
521 local _, parentng = walker.sexp_at(r, true)
522 return refmt_at(parentng, r)
524 return spliced and (backwards and sexp.start or range.start - ndeleted)
525 or not backwards and ndeleted <= 0 and range.finish - ndeleted
526 or backwards and ndeleted <= 0 and range.start + ndeleted
527 or range.start
530 local function transpose(range, first, second)
531 if not (first and second) then return end
532 local copy1 = first.text
533 local copy2 = second.text
534 delete(second.start, second.finish + 1 - second.start)
535 write(second.start, copy1)
536 delete(first.start, first.finish + 1 - first.start)
537 write(first.start, copy2)
538 parser.tree.rewind(first.start)
539 range.start = second.finish + 1
540 range.finish = range.start
541 return refmt_at(big_enough_parent(first.start, second.finish), range)
544 local function transpose_sexps(range)
545 local node, parent = walker.sexp_at(range, true)
546 local first = node and node.finish + 1 == range.start and node or parent.before(range.start, finishof)
547 local second = first ~= node and node or parent.after(range.start, startof)
548 return transpose(range, first, second)
551 local function transpose_words(range)
552 local _, first = walker.start_float_before(range)
553 local _, second = walker.finish_float_after(range)
554 if first and second and first.start == second.start then
555 _, first = walker.start_float_before(first)
557 return transpose(range, first, second)
560 local function transpose_chars(range)
561 local node, parent, i = walker.sexp_at(range)
562 local nxt = i and parent[i + 1]
563 local pstart = not parent.is_root and parent.start + (parent.p and #parent.p or 0) + #parent.d
564 local pfinish = not parent.is_root and parent.finish
565 local npref = node and node.p and node.start + #node.p
566 -- only allow transposing while inside atoms/words and prefixes
567 if node and ((npref and (range.start <= npref and range.start > node.start) or
568 node.d and range.start > node.finish + 1) or not node.d and (not pstart or range.start > pstart)) then
569 local start = range.start -
570 ((range.start == pfinish or range.start == npref or
571 range.start == node.finish + 1 and (parent.is_list or parent.is_root) and (not nxt or nxt.indent)) and 1 or 0)
572 local str_start = start - node.start + 1
573 local char = node.text:sub(str_start, str_start)
574 delete(start, 1)
575 write(start - 1, #char > 0 and char or " ")
576 parser.tree.rewind(start)
577 local in_head_atom = i == 1 and #parent > 1
578 return parent.is_list and in_head_atom and refmt_at(parent, {start = start + 1, finish = start + 1}) or start + 1
582 local function _join_or_splice(parent, n, range, pos)
583 local sexp = parent[n]
584 local nxt = parent[n + 1]
585 local is_last = not nxt or not nxt.is_line_comment
586 local newpos = not (is_last and sexp.is_empty) and join_sexps(range)
587 if not newpos and sexp.is_empty then
588 newpos = splice_sexp({start = pos, finish = pos}, nil, true)
590 return newpos or pos
593 local function delete_nonsplicing(range, pos, delete_maybe_yank)
594 local action = {kill = true, wrap = false, splice = false, func = delete_maybe_yank}
595 range = extend_overlap(range)
596 local ndeleted, spliced, opening, _, refmt = pick_out(range, pos, action)
597 local backwards = pos == range.finish
598 if opening then
599 if spliced then
600 return pos - ndeleted
601 else
602 if ndeleted == 0 then
603 local closing = parser.opposite[opening] -- XXX: why don't I use the pick_out return value?
604 if closing == "\n" then
605 local sexp, parent, n = walker.sexp_at(range)
606 if pos == sexp.start + #sexp.d and backwards then
607 return _join_or_splice(parent, n, range, pos)
608 elseif pos == sexp.finish then
609 local r = {start = pos + #closing, finish = pos + #closing}
610 return _join_or_splice(parent, n, r, pos)
614 return backwards and (range.finish - ndeleted) or range.start
616 else
617 local newpos = backwards and range.start + (ndeleted <= 0 and ndeleted or 0) or (range.finish - ndeleted)
618 if refmt then
619 local r = {start = newpos, finish = newpos}
620 return refmt_at(big_enough_parent(range.start, range.finish - ndeleted), r, true)
622 return newpos
626 local function insert_pair(range, delimiter, shiftless, auto_square)
627 local indices, nodes = walker.sexp_path(range)
628 local sexp = nodes[#nodes]
629 local right_after_prefix = sexp and sexp.p and range.start == sexp.start + #sexp.p
630 -- XXX: here I assume that # is a valid prefix for the dialect
631 local mb_closing = (delimiter == "|") and right_after_prefix and parser.opposite[sexp.p .. delimiter]
632 local closing = mb_closing or parser.opposite[delimiter]
633 local squarewords = fmt.squarewords
634 if squarewords and not right_after_prefix
635 and not (closing == "]" and shiftless and not auto_square)
636 and (auto_square or squarewords.not_optional)
637 and not fmt:adjust_bracket_p(indices, nodes, range) then
638 delimiter, closing = "[", "]"
640 write(range.finish, closing)
641 write(range.start, delimiter)
642 if right_after_prefix or parser.tree.is_parsed(range.start) then
643 parser.tree.rewind(right_after_prefix and sexp.start or range.start)
645 return range.start + #delimiter
648 local function make_wrap(kind)
649 return function(range, pos, auto_square)
650 local opening = kind
651 local function _wrap(r)
652 if not (r.finish > r.start) then return end
653 insert_pair(r, opening, false, auto_square)
655 local action = {kill = false, wrap = false, splice = false, func = _wrap}
656 pick_out(range, pos, action)
657 local rangeng = {start = pos + #opening, finish = range.finish}
658 local _, parentng = walker.sexp_at(range) -- TODO: range.finish is wrong. use sexp_path and big_enough_parent
659 return refmt_at(parentng, rangeng)
663 local function newline(parent, range)
664 local line_comment = parent.is_line_comment
665 -- do not autoextend margin comments:
666 if line_comment and range.start < parent.finish + (parent.indent and 1 or 0) then
667 local newpos = split_sexp(range)
668 if newpos then
669 return newpos
672 if not parent.is_list and not line_comment then
673 -- if parent[#parent + 1] is nil, we are at EOF
674 if not parent.is_root or parent.is_parsed(range.start) or parent[#parent + 1] then
675 parser.tree.rewind(parent.start or range.start)
677 local newpos = range.start
678 write(newpos, "\n")
679 return newpos + 1
681 if not parent.is_list then
682 local _
683 _, parent = walker.sexp_at(parent, true)
685 local last_nonblank_in_list = not parent.is_empty and parent[#parent].finish - (line_comment and 1 or 0)
686 local nxt = parent.after(range.start, startof)
687 local last_on_line = not nxt or nxt and nxt.indent and nxt.start > range.start
688 local margin = nxt and not nxt.indent and nxt.is_line_comment and nxt.finish
689 local after_last = last_nonblank_in_list and range.start > last_nonblank_in_list
690 local in_indent = nxt and nxt.indent and range.start <= nxt.start and range.start >= (nxt.start - nxt.indent)
691 local placeholder = "asdf"
692 local newpos = margin or range.start
693 if not parent.is_empty then
694 if in_indent then
695 write(newpos, (placeholder.."\n"))
696 else
697 write(newpos, "\n"..((after_last or last_on_line or margin) and placeholder or ""))
700 parser.tree.rewind(parent.start or newpos)
701 -- move the cursor onto the placeholder, so refmt_at can restore the position:
702 local r = {start = newpos, finish = newpos}
703 newpos = walker.start_after(r) or newpos
704 local rangeng = {start = newpos, finish = newpos}
705 newpos = refmt_at(parent, rangeng)
706 local _, parentng = walker.sexp_at({start = newpos, finish = newpos}, true)
707 if after_last then
708 local autoindent = parentng[#parentng].indent
709 write(parentng.finish, "\n"..string.rep(" ", autoindent or 0))
711 if after_last or last_on_line or margin or in_indent then
712 delete(newpos, #placeholder)
714 parser.tree.rewind(parentng.start or newpos)
715 return newpos
718 local function close_and_newline(range, closing)
719 local opening = closing and parser.opposite[closing]
720 local parent = walker.quasilist_at(range, opening)
721 if parent then
722 local has_eol = parent.is_line_comment
723 local r = {start = parent.finish, finish = parent.finish}
724 local newpos = refmt_at(parent, r)
725 r = {start = newpos + (has_eol and 0 or 1), finish = newpos + (has_eol and 0 or 1)}
726 local list, parentng = walker.sexp_at(r)
727 newpos = list and list.finish + 1 or r.start
728 newpos = newline(parentng, {start = newpos, finish = newpos})
729 if not newpos then
730 newpos = r.finish + 1
731 write(newpos, "\n")
732 parser.tree.rewind(parentng.start or list.start)
734 return newpos
735 elseif closing == "\n" then
736 local eol = eol_at(range.start)
737 local _, parentng = walker.sexp_at({start = eol, finish = eol})
738 local newpos = newline(parentng, {start = eol, finish = eol})
739 return newpos
741 return range.start
744 local function join_line(_, pos)
745 local eol = eol_at(pos)
746 local r = {start = eol, finish = eol + 1}
747 return delete_nonsplicing(r, r.start, delete)
750 local wrap_doublequote = make_wrap'"'
752 local function meta_doublequote(range, pos, auto_square)
753 local escaped = walker.escaped_at(range)
754 if not escaped then
755 return wrap_doublequote(range, pos, auto_square)
756 elseif escaped.is_string then
757 return close_and_newline(range, '"')
759 return pos
762 local block_comment_start
763 for o, c in pairs(parser.opposite) do
764 if #c > 1 then
765 block_comment_start = o
766 break
770 local wrap_comment = make_wrap(block_comment_start)
772 return {
773 delete_splicing = delete_splicing,
774 delete_nonsplicing = delete_nonsplicing,
775 refmt_at = refmt_at,
776 pick_out = pick_out,
777 raise_sexp = raise_sexp,
778 slurp_sexp = slurp_sexp,
779 barf_sexp = barf_sexp,
780 splice_sexp = splice_sexp,
781 wrap_round = make_wrap"(",
782 wrap_square = make_wrap"[",
783 wrap_curly = make_wrap"{",
784 meta_doublequote = meta_doublequote,
785 wrap_comment = wrap_comment,
786 insert_pair = insert_pair,
787 newline = newline,
788 join_line = join_line,
789 close_and_newline = close_and_newline,
790 cycle_wrap = cycle_wrap,
791 split_sexp = split_sexp,
792 join_sexps = join_sexps,
793 transpose_sexps = transpose_sexps,
794 transpose_words = transpose_words,
795 transpose_chars = transpose_chars,
799 return M