3 -- XXX: in Lua 5.2 unpack() was moved into table
4 local unpack
= table.unpack
or 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
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 local function is_lone_prefix(node
)
78 return node
and node
.p
and (#node
.p
== node
.finish
- node
.start
+ 1)
81 function M
.new(parser
, walker
, fmt
, write, delete
, eol_at
)
83 local function refmt_list(parent
, base_indent
, padj
, deltas
, keep_electric_space
)
87 local pstart
= parent
.start
+ (parent
.p
and #parent
.p
or 0) + #parent
.d
88 if parent
.is_empty
then
89 normalize_spacing(pstart
, parent
.finish
- pstart
, deltas
)
92 local last_distinguished
= fmt
:last_distinguished(parent
)
93 local first_argument
= 1
94 if #parent
> 2 and parent
[1].is_list
and #parent
[1] > 1 then
96 local nearest_indented
= op
[#op
].indent
and op
[#op
] or walker
.indented_before(op
[#op
])
97 indent_adj
= nearest_indented
.start
- pstart
- nearest_indented
.indent
+ 1
99 for i
, s
in ipairs(parent
) do
100 adj
= s
.indent
and 0 or adj
101 if last_distinguished
then
102 -- do not let early comments influence the indentation of real expressions:
103 if i
<= last_distinguished
and s
.is_comment
then
104 last_distinguished
= last_distinguished
+ 1
106 -- s is the name of a named let. account for it:
107 if i
== last_distinguished
+ 1 and not s
.is_list
and parent
[1].text
== "let" then
108 last_distinguished
= last_distinguished
+ 1
110 -- do not let early comments influence the indentation of real expressions:
111 elseif i
<= first_argument
+ 1 and s
.is_comment
then
112 first_argument
= first_argument
+ 1
114 local has_eol
= s
.is_line_comment
115 adj
= leading_space(s
, deltas
, adj
, parent
, i
, pstart
, has_eol
)
116 adj
= trailing_space(s
, deltas
, adj
, parent
, i
, has_eol
, keep_electric_space
)
117 adj
= indentation(s
, deltas
, adj
, parent
, i
, pstart
, base_indent
, last_distinguished
, first_argument
,
123 local nearest_indented
= s
.indent
and s
or walker
.indented_before(s
)
124 local parent_column
= nearest_indented
and
125 nearest_indented
.indent
+ (s
.start
+ (s
.p
and #s
.p
or 0) + #s
.d
) - nearest_indented
.start
126 or base_indent
+ (s
.start
+ (s
.p
and #s
.p
or 0) + #s
.d
) - pstart
127 refmt_list(s
, parent_column
- adj
, padj
+ adj
, deltas
, keep_electric_space
)
133 local function path_from(range
)
134 local _
, parent_at
, m
= walker
.sexp_at(range
, true)
137 path
= walker
.sexp_path(parent_at
[m
])
138 local base
= parent_at
[m
]
139 if range
.finish
== base
.finish
+ 1 then
140 path
.after_finish
= 0 -- lists can end up with a different length
142 path
.after_start
= range
.start
- base
.start
145 local s
= parent_at
[#parent_at
]
146 if not s
or range
.start
>= s
.finish
+ 1 + (s
.is_line_comment
and 1 or 0) then
147 path
= walker
.sexp_path(parent_at
)
148 path
.at_pfinish
= true
150 local prev
, n
= parent_at
.before(range
.start
, finishof
)
152 path
= walker
.sexp_path(prev
)
153 local nxt
= parent_at
[n
+ 1]
154 path
.after_finish
= nxt
and not nxt
.indent
and 1 or range
.finish
- prev
.finish
- 1
156 path
= walker
.sexp_path(parent_at
[1])
164 local function pos_from(path
, range
)
165 local sexp
, parentng
, n
= walker
.goto_path(path
)
167 if path
.after_finish
then
168 local nxt
= n
and parentng
[n
+ 1]
169 local max = nxt
and nxt
.start
- sexp
.finish
- 1
170 return sexp
.finish
+ 1 - (range
.finish
- range
.start
) +
171 (max and math
.min(path
.after_finish
, max) or path
.after_finish
)
172 elseif path
.at_pfinish
then
175 return sexp
.start
+ path
.after_start
180 local function refmt_at(scope
, range
, keep_electric_space
)
181 if not range
or not scope
or scope
.is_root
or (scope
.finish
- scope
.start
< 2) then return range
.start
end
182 local parent
= walker
.sexp_at(scope
, true)
183 if not (parent
and parent
.is_list
) then return range
.start
end
184 local path
= path_from(range
)
185 local indented_parent
= parent
.indent
and parent
or parent
.is_list
and walker
.indented_before(parent
)
186 local parent_column
= indented_parent
and (indented_parent
.indent
+ parent
.start
- indented_parent
.start
+
187 (parent
.p
and #parent
.p
or 0) + #parent
.d
) or
189 local deltas
= refmt_list(parent
, parent_column
, 0, {}, keep_electric_space
)
190 table.sort(deltas
, function(d1
, d2
) return d1
[1] > d2
[1] end)
191 for _
, pair
in ipairs(deltas
) do
192 local offset
, delta
= unpack(pair
)
194 write(offset
, string.rep(" ", delta
))
195 elseif delta
< 0 then
196 delete(offset
+ delta
, -delta
)
199 parser
.tree
.rewind(parent
.start
or 0)
200 return path
and pos_from(path
, range
) or range
.start
203 local function splice(pos
, sexps
, skip
, backwards
, action
)
205 local sexp
= walker
.sexp_at(skip
, true)
206 local start
= sexp
.start
+ (sexp
.p
and #sexp
.p
or 0)
207 -- XXX: don't splice empty line comments _yet_. See _join_or_splice
208 local tosplice
= action
.splice
or action
.wrap
or not sexp
.is_line_comment
and sexp
.is_empty
209 local opening
= (sexp
.p
or "")..sexp
.d
210 local closing
= parser
.opposite
[sexp
.d
]
211 local real_start
= tosplice
and sexp
.start
or start
+ #sexp
.d
212 local splice_closing
= not sexp
.is_line_comment
or sexp
.is_empty
213 local real_finish
= sexp
.finish
+ 1 - (tosplice
and splice_closing
and 0 or #closing
)
214 local first
= backwards
and
215 {start
= real_start
, finish
= math
.max(pos
, start
+ #sexp
.d
)} or
216 {start
= real_start
, finish
= sexp
.is_empty
and pos
or start
+ #sexp
.d
}
217 local second
= backwards
and
218 {start
= sexp
.is_empty
and pos
or sexp
.finish
+ 1 - #closing
, finish
= real_finish
} or
219 {start
= math
.min(pos
, sexp
.finish
+ 1 - #closing
), finish
= real_finish
}
224 local ndeleted
= first
.finish
- first
.start
+ second
.finish
- second
.start
226 sexps
.rewind(sexp
.start
)
229 return first
.finish
- first
.start
, spliced
, opening
, closing
233 -- This function handles some whitespace-only deletion corner cases magically.
235 -- a) extending or shrinking the range that is to be deleted
236 -- b) returning an integer to trick pick_out to glide the cursor, but calling refmt to restore the deleted spaces
238 local function delete_whitespace_only(range
, pos
)
239 local node
, parent
= walker
.sexp_at(range
)
240 if node
or not parent
.is_list
then return end
241 local prev
= parent
.before(pos
, finishof
)
242 local nxt
= parent
.after(pos
, startof
)
243 if prev
and prev
.finish
>= range
.start
or nxt
and nxt
.start
< range
.finish
then return end
244 local backwards
= pos
== range
.finish
246 local eol
= eol_at(pos
)
247 local on_empty_line
= not nxt
or eol
and nxt
.start
> eol
248 local empty_line_after_comment
= prev
and prev
.is_line_comment
250 if nxt
and nxt
.indent
then
251 if backwards
and prev
then
252 -- join current line with the previous one, unless the latter is a line comment
253 range
.start
= prev
.finish
+ (empty_line_after_comment
and 0 or 1)
255 range
.finish
= range
.start
+ 1
256 adj
= (on_empty_line
and 0 or 1)
259 adj
= nxt
.start
- range
.finish
263 if not nxt
and prev
then
264 -- clean up trailing whitespace (e.g. electric RET)
265 range
.start
= prev
.finish
+ 1
266 range
.finish
= parent
.finish
267 elseif nxt
and nxt
.start
== range
.finish
and (not prev
or prev
.finish
+ 1 == range
.start
) then
268 if prev
and prev
.d
or nxt
and nxt
.d
and not is_lone_prefix(prev
) then
269 -- don't delete spaces near delimiters, just slide the cursor:
271 range
.finish
= range
.start
273 range
.start
= range
.finish
280 local function big_enough_parent(pos1
, pos2
)
281 -- since the pos1-pos2 range can cross list boundaries, find which list contains both pos1 and pos2
283 _
, p1
= walker
.sexp_at({start
= pos1
, finish
= pos1
}, true)
284 if p1
.is_root
or not (p1
.start
< pos1
and p1
.finish
> pos2
) then
285 _
, p2
= walker
.sexp_at({start
= pos2
, finish
= pos2
}, true)
287 return p2
and not p2
.is_root
and p2
or p1
290 local function extend_overlap(range
)
291 local rnode
= walker
.sexp_at({start
= range
.finish
, finish
= range
.finish
})
292 if rnode
and rnode
.p
and rnode
.p
:find
";"
293 and range
.finish
> rnode
.start
and range
.finish
< rnode
.start
+ #rnode
.p
then
294 return {start
= range
.start
, finish
= rnode
.start
+ #rnode
.p
}
299 local function pick_out(range
, pos
, action
)
301 if range
.start
== range
.finish
then return ndeleted
end
302 local sexps
= parser
.tree
303 local skips
= sexps
.unbalanced_delimiters(range
)
304 -- handle splice and kill-splice of forms and strings:
306 local sexp
= walker
.sexp_at(skips
[1], true)
307 local backward_splice
= skips
[1].opening
and pos
>= sexp
.start
+ (sexp
.p
and #sexp
.p
or 0) + #sexp
.d
308 and range
.start
>= sexp
.start
309 local forward_splice
= skips
[1].closing
and pos
<= sexp
.finish
+ 1 - #parser
.opposite
[sexp
.d
]
310 and range
.finish
<= sexp
.finish
+ 1
311 if backward_splice
or forward_splice
then
312 return splice(backward_splice
and range
.finish
or range
.start
, sexps
, sexp
, backward_splice
, action
)
315 local node
, parent
= walker
.sexp_at({start
= range
.finish
, finish
= range
.finish
})
316 -- if the range ends with a line comment, don't delete its closing newline:
317 local drop_eol
= action
.kill
and node
and
318 node
.finish
+ 1 == range
.finish
and node
.is_line_comment
319 local par
= big_enough_parent(range
.start
, range
.finish
)
320 local operator_changed
= par
[1] and par
[1].finish
>= range
.start
321 local refmt
= #skips
== 0 and delete_whitespace_only(range
, pos
) or operator_changed
and 0
322 table.sort(skips
, function(a
, b
) return a
.start
< b
.start
end)
323 table.insert(skips
, {start
= range
.finish
- (drop_eol
and 1 or 0)})
324 table.insert(skips
, 1, {finish
= range
.start
})
325 ndeleted
= ndeleted
+ (drop_eol
and 1 or 0)
326 for i
= #skips
- 1, 1, -1 do
327 local region
= {start
= skips
[i
].finish
, finish
= skips
[i
+ 1].start
}
328 if skips
[i
].closing
and skips
[i
+ 1].opening
then
329 -- leave out some of the space between adjacent lists
330 local _
, rparent
= walker
.sexp_at(region
)
331 local nxt
= rparent
.after(region
.start
, startof
)
332 region
.start
= nxt
and nxt
.start
or region
.start
336 ndeleted
= ndeleted
+ (region
.finish
- region
.start
)
339 -- if parent[#parent + 1] is nil, we are at EOF
340 if ndeleted
> 0 and (not parent
.is_root
or parent
.is_parsed(range
.start
) or parent
[#parent
+ 1]) then
341 sexps
.rewind(range
.start
)
343 return ndeleted
- (refmt
or 0), nil, nil, nil, refmt
346 local function raise_sexp(range
, pos
)
347 local sexp
, parent
= walker
.sexp_at(range
, true)
348 if sexp
and parent
and parent
.is_list
then
349 delete(sexp
.finish
+ 1, parent
.finish
- sexp
.finish
)
350 delete(parent
.start
, sexp
.start
- parent
.start
)
351 parser
.tree
.rewind(parent
.start
)
352 range
.start
= parent
.start
+ pos
- sexp
.start
353 range
.finish
= range
.start
354 local _
, nodes
= walker
.sexp_path(range
)
355 local grandparent
= nodes
[#nodes
- 2]
356 return grandparent
and refmt_at(grandparent
, range
) or range
.start
360 local function slurp_sexp(range
, forward
)
361 local _
, parent
= walker
.sexp_at(range
, true)
362 local seeker
= forward
and walker
.finish_after
or walker
.start_before
363 if not parent
or not parent
.is_list
then return range
.start
end
364 local r
= {start
= parent
.start
, finish
= parent
.finish
+ 1}
365 local newpos
= seeker(r
, is_comment
)
366 if not newpos
then return range
.start
end
367 local opening
= (parent
.p
or "")..parent
.d
368 local closing
= parser
.opposite
[parent
.d
]
369 local delimiter
= forward
and closing
or opening
371 write(newpos
, delimiter
)
373 delete(forward
and parent
.finish
or parent
.start
, #delimiter
)
375 write(newpos
, delimiter
)
377 parser
.tree
.rewind(math
.min(parent
.start
, newpos
))
378 return refmt_at(big_enough_parent(newpos
, range
.start
), range
)
381 local function barf_sexp(range
, forward
)
382 local _
, parent
= walker
.sexp_at(range
, true)
383 local seeker
= forward
and walker
.finish_before
or walker
.start_after
384 -- TODO: barfing out of strings requires calling the parser on them
385 if not parent
or not parent
.is_list
or parent
.is_empty
then return range
.start
end
386 local opening
= (parent
.p
or "")..parent
.d
387 local pstart
= parent
.start
+ #opening
388 local r
= {start
= forward
and parent
.finish
- 1 or pstart
, finish
= forward
and parent
.finish
or pstart
+ 1}
389 local newpos
= seeker(r
, is_comment
) or forward
and pstart
or parent
.finish
390 local closing
= parser
.opposite
[parent
.d
]
391 local delimiter
= forward
and closing
or opening
393 write(newpos
, delimiter
)
395 delete(forward
and parent
.finish
or parent
.start
, #delimiter
)
397 write(newpos
, delimiter
)
399 parser
.tree
.rewind(math
.min(parent
.start
, newpos
))
400 local drag
= forward
and (newpos
< range
.finish
) or not forward
and (newpos
> range
.start
)
401 local keep_inside
= forward
and #parent
> 1 and range
.finish
> range
.start
and 1 or 0
402 newpos
= drag
and newpos
- keep_inside
or range
.start
403 local rangeng
= {start
= newpos
, finish
= newpos
}
404 return refmt_at(big_enough_parent(newpos
, parent
.finish
+ 1), rangeng
)
407 local function splice_sexp(range
, _
, no_refmt
)
408 local _
, parent
= walker
.sexp_at(range
)
409 if not parent
or not parent
.d
then return end
410 local opening
= (parent
.p
or "")..parent
.d
411 local closing
= parser
.opposite
[parent
.d
]
412 local finish
= parent
.finish
+ 1 - #closing
413 if not parent
.is_line_comment
then
414 delete(finish
, #closing
)
416 -- TODO: (un)escape special characters, if necessary
417 delete(parent
.start
, parent
.is_empty
and (finish
- parent
.start
) or #opening
)
418 parser
.tree
.rewind(parent
.start
)
419 range
.start
= range
.start
- #opening
420 range
.finish
= range
.start
421 local _
, parentng
= walker
.sexp_at(range
, true)
422 return not no_refmt
and refmt_at(parentng
, range
) or range
.start
425 local function rewrap(parent
, kind
)
426 local pstart
= parent
.start
+ #((parent
.p
or "")..parent
.d
) - 1
427 delete(parent
.finish
, 1)
428 write(parent
.finish
, parser
.opposite
[kind
])
429 delete(pstart
, #parent
.d
)
431 parser
.tree
.rewind(parent
.start
)
434 local function cycle_wrap(range
, pos
)
435 local _
, parent
= walker
.sexp_at(range
)
436 if not parent
or not parent
.is_list
then return end
437 local next_kind
= {["("] = "[", ["["] = "{", ["{"] = "("}
438 rewrap(parent
, next_kind
[parent
.d
])
442 local function split_sexp(range
)
443 local _
, parent
= walker
.sexp_at(range
)
444 if not (parent
and parent
.d
) then return end
445 local new_finish
, new_start
446 if parent
.is_list
then
447 local prev
= parent
.before(range
.start
, finishof
, is_comment
)
448 new_finish
= prev
and prev
.finish
+ 1
449 -- XXX: do not skip comments here, so they end up in the second list
450 -- and are not separated from their target expression:
451 local nxt
= new_finish
and parent
.after(new_finish
, startof
)
452 new_start
= nxt
and nxt
.start
454 new_start
= range
.start
455 new_finish
= range
.start
457 if not (new_start
and new_finish
) then return end
458 local opening
= (parent
.p
or "")..parent
.d
459 local closing
= parser
.opposite
[parent
.d
]
460 write(new_start
, opening
)
461 local sep
= parent
.is_line_comment
and "" -- line comments already have a separator
462 or new_finish
== new_start
and " " -- only add a separator if there was none before
464 write(new_finish
, closing
..sep
)
465 parser
.tree
.rewind(parent
.start
)
466 range
.start
= new_start
+ (parent
.is_list
and 0 or #opening
+ #closing
)
467 range
.finish
= range
.start
468 local _
, nodes
= walker
.sexp_path(range
)
469 local parentng
, grandparent
= nodes
[#nodes
- 1], nodes
[#nodes
- 2]
470 local scope
= parentng
and not parentng
.is_root
and parentng
or grandparent
471 return refmt_at(scope
, range
)
474 local function join_sexps(range
)
475 local node
, parent
= walker
.sexp_at(range
, true)
476 local first
= node
and node
.finish
+ 1 == range
.start
and node
or parent
.before(range
.start
, finishof
)
477 local second
= first
~= node
and node
or parent
.after(range
.start
, startof
)
478 if not (first
and second
and first
.d
and
479 -- don't join line comments to margin comments:
480 (not first
.is_line_comment
or first
.indent
and second
.indent
) and
481 (first
.d
== second
.d
or
482 -- join line comments even when their delimiters differ slightly
483 -- (different number of semicolons, existence/lack of a space after them)
484 parser
.opposite
[first
.d
] == parser
.opposite
[second
.d
])) then
487 local opening
= (second
.p
or "")..second
.d
488 local closing
= parser
.opposite
[first
.d
]
490 if not first
.is_list
then
491 pos
= first
.finish
+ 1 - #closing
492 delete(pos
, second
.start
+ #opening
- pos
)
494 delete(second
.start
, #opening
)
495 delete(first
.finish
, #closing
)
496 pos
= second
.start
- #closing
498 parser
.tree
.rewind(first
.start
)
500 range
.finish
= range
.start
501 local _
, nodes
= walker
.sexp_path(range
)
502 local parentng
, grandparent
= nodes
[#nodes
- 1], nodes
[#nodes
- 2]
503 local scope
= parentng
and not parentng
.is_root
and parentng
or grandparent
504 return refmt_at(scope
, range
)
507 local function delete_splicing(range
, pos
, splicing
, delete_and_yank
)
508 local action
= {kill
= true, wrap
= splicing
, splice
= splicing
, func
= delete_and_yank
}
509 local sexp
, parent
, n
= walker
.sexp_at(range
, true)
510 local nxt
= n
and parent
[n
+ 1]
511 local prev
= n
and parent
[n
- 1]
512 range
= extend_overlap(range
)
513 local ndeleted
, spliced
, opening
, closing
= pick_out(range
, pos
, action
)
514 local inner_list_len
= spliced
and sexp
.finish
- sexp
.start
+ 1 - #opening
- #closing
515 local range_len
= range
.finish
- range
.start
516 local backwards
= pos
== range
.finish
517 local whole_object
= sexp
and
518 (sexp
.start
== range
.start
and sexp
.finish
+ 1 == range
.finish
519 or spliced
and inner_list_len
<= range_len
- (backwards
and #opening
or #closing
))
520 local in_head_atom
= sexp
and not sexp
.d
and n
== 1 and #parent
> 1
521 local in_whitespace
= not sexp
522 if whole_object
or spliced
or in_whitespace
or in_head_atom
then
523 local cur
= (whole_object
or spliced
) and (prev
and prev
.finish
or sexp
.start
) or range
.start
524 -- if parent[#parent + 1] is nil, we are at EOF
525 if not parent
.is_root
or parent
.is_parsed(cur
) or parent
[#parent
+ 1] then
526 parser
.tree
.rewind(cur
)
528 -- make sure the cursor is not left in whitespace:
529 if whole_object
and nxt
then
530 cur
= nxt
.start
- range_len
- (spliced
and (backwards
and #closing
or #opening
) or 0)
531 elseif whole_object
and prev
then
534 cur
= (backwards
and sexp
.start
or (nxt
and nxt
.start
- range_len
or range
.start
) - ndeleted
)
536 local r
= {start
= cur
, finish
= cur
+ 1}
537 local _
, parentng
= walker
.sexp_at(r
, true)
538 return refmt_at(parentng
, r
)
540 return not backwards
and ndeleted
<= 0 and range
.finish
- ndeleted
541 or backwards
and ndeleted
<= 0 and range
.start
+ ndeleted
545 local function transpose(range
, first
, second
)
546 if not (first
and second
) then return end
547 local copy1
= first
.text
548 local copy2
= second
.text
549 delete(second
.start
, second
.finish
+ 1 - second
.start
)
550 write(second
.start
, copy1
)
551 delete(first
.start
, first
.finish
+ 1 - first
.start
)
552 write(first
.start
, copy2
)
553 parser
.tree
.rewind(first
.start
)
554 range
.start
= second
.finish
+ 1
555 range
.finish
= range
.start
556 return refmt_at(big_enough_parent(first
.start
, second
.finish
), range
)
559 local function transpose_sexps(range
)
560 local node
, parent
= walker
.sexp_at(range
, true)
561 local first
= node
and node
.finish
+ 1 == range
.start
and node
or parent
.before(range
.start
, finishof
)
562 local second
= first
~= node
and node
or parent
.after(range
.start
, startof
)
563 return transpose(range
, first
, second
)
566 local function transpose_words(range
)
567 local _
, first
= walker
.start_float_before(range
)
568 local _
, second
= walker
.finish_float_after(range
)
569 if first
and second
and first
.start
== second
.start
then
570 _
, first
= walker
.start_float_before(first
)
572 return transpose(range
, first
, second
)
575 local function transpose_chars(range
)
576 local node
, parent
, i
= walker
.sexp_at(range
)
577 local nxt
= i
and parent
[i
+ 1]
578 local pstart
= not parent
.is_root
and parent
.start
+ (parent
.p
and #parent
.p
or 0) + #parent
.d
579 local pfinish
= not parent
.is_root
and parent
.finish
580 local npref
= node
and node
.p
and node
.start
+ #node
.p
581 -- only allow transposing while inside atoms/words and prefixes
582 if node
and ((npref
and (range
.start
<= npref
and range
.start
> node
.start
) or
583 node
.d
and range
.start
> node
.finish
+ 1) or not node
.d
and (not pstart
or range
.start
> pstart
)) then
584 local start
= range
.start
-
585 ((range
.start
== pfinish
or range
.start
== npref
or
586 range
.start
== node
.finish
+ 1 and (parent
.is_list
or parent
.is_root
) and (not nxt
or nxt
.indent
)) and 1 or 0)
587 local str_start
= start
- node
.start
+ 1
588 local char
= node
.text
:sub(str_start
, str_start
)
590 write(start
- 1, #char
> 0 and char
or " ")
591 parser
.tree
.rewind(start
)
592 local in_head_atom
= i
== 1 and #parent
> 1
593 return parent
.is_list
and in_head_atom
and refmt_at(parent
, {start
= start
+ 1, finish
= start
+ 1}) or start
+ 1
597 local function _join_or_splice(parent
, n
, range
, pos
)
598 local sexp
= parent
[n
]
599 local nxt
= parent
[n
+ 1]
600 local is_last
= not nxt
or not nxt
.is_line_comment
601 local newpos
= not (is_last
and sexp
.is_empty
) and join_sexps(range
)
602 if not newpos
and sexp
.is_empty
then
603 newpos
= splice_sexp({start
= pos
, finish
= pos
}, nil, true)
608 local function delete_nonsplicing(range
, pos
, delete_maybe_yank
)
609 local action
= {kill
= true, wrap
= false, splice
= false, func
= delete_maybe_yank
}
610 range
= extend_overlap(range
)
611 local ndeleted
, spliced
, opening
, _
, refmt
= pick_out(range
, pos
, action
)
612 local backwards
= pos
== range
.finish
615 return pos
- ndeleted
617 if ndeleted
== 0 then
618 local closing
= parser
.opposite
[opening
] -- XXX: why don't I use the pick_out return value?
619 if closing
== "\n" then
620 local sexp
, parent
, n
= walker
.sexp_at(range
)
621 if pos
== sexp
.start
+ #sexp
.d
and backwards
then
622 return _join_or_splice(parent
, n
, range
, pos
)
623 elseif pos
== sexp
.finish
then
624 local r
= {start
= pos
+ #closing
, finish
= pos
+ #closing
}
625 return _join_or_splice(parent
, n
, r
, pos
)
629 return backwards
and (range
.finish
- ndeleted
) or range
.start
632 local newpos
= backwards
and range
.start
+ (ndeleted
<= 0 and ndeleted
or 0) or (range
.finish
- ndeleted
)
634 local r
= {start
= newpos
, finish
= newpos
}
635 return refmt_at(big_enough_parent(range
.start
, range
.finish
- ndeleted
), r
, true)
641 local function insert_pair(range
, delimiter
, auto_square
)
642 local indices
, nodes
= walker
.sexp_path(range
)
643 local sexp
= nodes
[#nodes
]
644 local right_after_prefix
= sexp
and sexp
.p
and range
.start
== sexp
.start
+ #sexp
.p
645 -- XXX: here I assume that # is a valid prefix for the dialect
646 local mb_closing
= (delimiter
== "|") and right_after_prefix
and parser
.opposite
[sexp
.p
.. delimiter
]
647 local closing
= mb_closing
or parser
.opposite
[delimiter
]
648 local squarewords
= fmt
.squarewords
649 if squarewords
and not right_after_prefix
650 and (auto_square
or squarewords
.not_optional
)
651 and not fmt
:adjust_bracket_p(indices
, nodes
, range
) then
652 delimiter
, closing
= "[", "]"
654 write(range
.finish
, closing
)
655 write(range
.start
, delimiter
)
656 if right_after_prefix
or parser
.tree
.is_parsed(range
.start
) then
657 parser
.tree
.rewind(right_after_prefix
and sexp
.start
or range
.start
)
659 return range
.start
+ #delimiter
662 local function make_wrap(opening
)
663 return parser
.opposite
[opening
] and function(range
, pos
, auto_square
)
664 local function _wrap(r
)
665 if not (r
.finish
> r
.start
) then return end
666 insert_pair(r
, opening
, false, auto_square
)
668 local action
= {kill
= false, wrap
= false, splice
= false, func
= _wrap
}
669 pick_out(range
, pos
, action
)
670 local rangeng
= {start
= pos
+ #opening
, finish
= range
.finish
}
671 local _
, parentng
= walker
.sexp_at(range
) -- TODO: range.finish is wrong. use sexp_path and big_enough_parent
672 return refmt_at(parentng
, rangeng
)
676 local function newline(parent
, range
)
677 local line_comment
= parent
.is_line_comment
678 -- do not autoextend margin comments:
679 if line_comment
and range
.start
< parent
.finish
+ (parent
.indent
and 1 or 0) then
680 local newpos
= split_sexp(range
)
685 if not parent
.is_list
and not line_comment
then
686 -- if parent[#parent + 1] is nil, we are at EOF
687 if not parent
.is_root
or parent
.is_parsed(range
.start
) or parent
[#parent
+ 1] then
688 parser
.tree
.rewind(parent
.start
or range
.start
)
690 local newpos
= range
.start
694 if not parent
.is_list
then
696 _
, parent
= walker
.sexp_at(parent
, true)
698 local last_nonblank_in_list
= not parent
.is_empty
and parent
[#parent
].finish
- (line_comment
and 1 or 0)
699 local nxt
= parent
.after(range
.start
, startof
)
700 local last_on_line
= range
.start
== eol_at(range
.start
)
701 local margin
= nxt
and not nxt
.indent
and nxt
.is_line_comment
and nxt
.finish
702 local after_last
= last_nonblank_in_list
and range
.start
> last_nonblank_in_list
703 local in_indent
= nxt
and nxt
.indent
and range
.start
<= nxt
.start
and range
.start
>= (nxt
.start
- nxt
.indent
)
704 local placeholder
= "asdf"
705 local newpos
= margin
or range
.start
706 if not parent
.is_empty
then
708 write(newpos
, (placeholder
.."\n"))
710 write(newpos
, "\n"..((after_last
or last_on_line
or margin
) and placeholder
or ""))
713 parser
.tree
.rewind(parent
.start
or newpos
)
714 -- move the cursor onto the placeholder, so refmt_at can restore the position:
715 local r
= {start
= newpos
, finish
= newpos
}
716 newpos
= walker
.start_after(r
) or newpos
717 local rangeng
= {start
= newpos
, finish
= newpos
}
718 newpos
= refmt_at(parent
, rangeng
)
719 local _
, parentng
= walker
.sexp_at({start
= newpos
, finish
= newpos
}, true)
721 local autoindent
= parentng
[#parentng
].indent
722 write(parentng
.finish
, "\n"..string.rep(" ", autoindent
or 0))
724 if after_last
or last_on_line
or margin
or in_indent
then
725 delete(newpos
, #placeholder
)
727 parser
.tree
.rewind(parentng
.start
or newpos
)
731 local function close_and_newline(range
, closing
)
732 local opening
= closing
and parser
.opposite
[closing
]
733 local parent
= walker
.quasilist_at(range
, opening
)
735 local has_eol
= parent
.is_line_comment
736 local r
= {start
= parent
.finish
, finish
= parent
.finish
}
737 local newpos
= refmt_at(parent
, r
)
738 r
= {start
= newpos
+ (has_eol
and 0 or 1), finish
= newpos
+ (has_eol
and 0 or 1)}
739 local list
, parentng
= walker
.sexp_at(r
)
740 newpos
= list
and list
.finish
+ 1 or r
.start
741 newpos
= newline(parentng
, {start
= newpos
, finish
= newpos
})
743 newpos
= r
.finish
+ 1
745 parser
.tree
.rewind(parentng
.start
or list
.start
)
748 elseif closing
== "\n" then
749 local eol
= eol_at(range
.start
)
750 local _
, parentng
= walker
.sexp_at({start
= eol
, finish
= eol
})
751 local newpos
= newline(parentng
, {start
= eol
, finish
= eol
})
757 local function join_line(_
, pos
)
758 local eol
= eol_at(pos
)
759 local r
= {start
= eol
, finish
= eol
+ 1}
760 return delete_nonsplicing(r
, r
.start
, delete
)
763 local wrap_doublequote
= make_wrap
'"'
765 local function meta_doublequote(range
, pos
, auto_square
)
766 local escaped
= walker
.escaped_at(range
)
768 return wrap_doublequote(range
, pos
, nil, auto_square
)
769 elseif escaped
.is_string
then
770 return close_and_newline(range
, '"')
775 local block_comment_start
776 for o
, c
in pairs(parser
.opposite
) do
778 block_comment_start
= o
783 local wrap_comment
= make_wrap(block_comment_start
)
786 delete_splicing
= delete_splicing
,
787 delete_nonsplicing
= delete_nonsplicing
,
790 raise_sexp
= raise_sexp
,
791 slurp_sexp
= slurp_sexp
,
792 barf_sexp
= barf_sexp
,
793 splice_sexp
= splice_sexp
,
794 wrap_round
= make_wrap
"(",
795 wrap_square
= make_wrap
"[",
796 wrap_curly
= make_wrap
"{",
797 meta_doublequote
= meta_doublequote
,
798 wrap_comment
= wrap_comment
,
799 insert_pair
= insert_pair
,
801 join_line
= join_line
,
802 close_and_newline
= close_and_newline
,
803 cycle_wrap
= cycle_wrap
,
804 split_sexp
= split_sexp
,
805 join_sexps
= join_sexps
,
806 transpose_sexps
= transpose_sexps
,
807 transpose_words
= transpose_words
,
808 transpose_chars
= transpose_chars
,