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