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
)
12 table.insert(list
, {start
+ delta
, -delta
})
14 table.insert(list
, {start
, -delta
})
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
)
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
)
39 local function indentation(s
, deltas
, adj
, parent
, i
, pstart
, base_indent
, last_distinguished
, first_argument
,
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
46 if i
- 1 <= last_distinguished
then
47 if parent
[first_argument
+ 1].indent
then
50 delta
= firstarg_delta
52 elseif last_distinguished
< 0
53 and i
> first_argument
+ 1 and not parent
[first_argument
+ 1].indent
then
54 delta
= firstarg_delta
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
64 if i
> first_argument
+ 1 and not parent
[first_argument
+ 1].indent
then
65 delta
= firstarg_delta
67 if delta
< 0 then delta
= math
.max(-s
.indent
, delta
) end
70 table.insert(deltas
, {s
.start
, delta
})
77 function M
.new(parser
, walker
, fmt
, write, delete
, eol_at
)
79 local function refmt_list(parent
, base_indent
, padj
, deltas
, keep_electric_space
)
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
)
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
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
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
,
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
)
129 local function path_from(range
)
130 local _
, parent_at
, m
= walker
.sexp_at(range
, true)
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
138 path
.after_start
= range
.start
- base
.start
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
146 local prev
, n
= parent_at
.before(range
.start
, finishof
)
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
152 path
= walker
.sexp_path(parent_at
[1])
160 local function pos_from(path
, range
)
161 local sexp
, parentng
, n
= walker
.goto_path(path
)
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
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
)
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
)
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
}
221 local ndeleted
= first
.finish
- first
.start
+ second
.finish
- second
.start
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.
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
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
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
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)
251 range
.finish
= range
.start
+ 1
252 adj
= (on_empty_line
and 0 or 1)
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:
264 range
.finish
= range
.start
266 range
.start
= range
.finish
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
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
}
292 local function pick_out(range
, pos
, action
)
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:
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
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
364 write(newpos
, delimiter
)
366 delete(forward
and parent
.finish
or parent
.start
, #delimiter
)
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
387 write(newpos
, delimiter
)
389 delete(forward
and parent
.finish
or parent
.start
, #delimiter
)
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
)
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
])
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
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
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
480 local opening
= (second
.p
or "")..second
.d
481 local closing
= parser
.opposite
[first
.d
]
483 if not first
.is_list
then
484 pos
= first
.finish
+ 1 - #closing
485 delete(pos
, second
.start
+ #opening
- pos
)
487 delete(second
.start
, #opening
)
488 delete(first
.finish
, #closing
)
489 pos
= second
.start
- #closing
491 parser
.tree
.rewind(first
.start
)
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
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
)
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)
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
599 return pos
- ndeleted
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
616 local newpos
= backwards
and range
.start
or (range
.finish
- ndeleted
)
618 local r
= {start
= newpos
, finish
= newpos
}
619 return refmt_at(big_enough_parent(range
.start
, range
.finish
- ndeleted
), r
, true)
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
)
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
)
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
680 if not parent
.is_list
then
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
= not nxt
or nxt
and nxt
.indent
and nxt
.start
> 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
694 write(newpos
, (placeholder
.."\n"))
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)
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
)
717 local function close_and_newline(range
, closing
)
718 local opening
= closing
and parser
.opposite
[closing
]
719 local parent
= walker
.quasilist_at(range
, opening
)
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
})
729 newpos
= r
.finish
+ 1
731 parser
.tree
.rewind(parentng
.start
or list
.start
)
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
})
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
)
754 return wrap_doublequote(range
, pos
, auto_square
)
755 elseif escaped
.is_string
then
756 return close_and_newline(range
, '"')
761 local block_comment_start
762 for o
, c
in pairs(parser
.opposite
) do
764 block_comment_start
= o
769 local wrap_comment
= make_wrap(block_comment_start
)
772 delete_splicing
= delete_splicing
,
773 delete_nonsplicing
= delete_nonsplicing
,
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
,
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
,