From 27da9ee2fdea669b7b8a5b8713101dab5cc2518f Mon Sep 17 00:00:00 2001 From: Georgi Kirilov <> Date: Sat, 31 Oct 2020 11:50:55 +0200 Subject: [PATCH] clean up --- edit.lua | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/edit.lua b/edit.lua index a0becac..70887e1 100644 --- a/edit.lua +++ b/edit.lua @@ -231,13 +231,13 @@ function M.new(parser, walker, fmt, write, delete, eol_at) return first.finish - first.start, spliced, opening, closing end - -- If you try to delete some part of indentation, this function joins the current line with - -- the previous one, unless the latter is a line comment. - -- it does this by + + -- This function handles some whitespace-only deletion corner cases magically. + -- It does so by -- a) extending or shrinking the range that is to be deleted - -- b) returning a truthy value to trick pick_out to glide the cursor, but refmt to restore the deleted spaces + -- b) returning an integer to trick pick_out to glide the cursor, but calling refmt to restore the deleted spaces -- c) both a) and b) - local function delete_indentation(range, pos) + local function delete_whitespace_only(range, pos) local node, parent = walker.sexp_at(range) if node or not parent.is_list then return end local prev = parent.before(pos, finishof) @@ -251,6 +251,7 @@ function M.new(parser, walker, fmt, write, delete, eol_at) and on_empty_line if nxt and nxt.indent then if backwards and prev then + -- join current line with the previous one, unless the latter is a line comment range.start = prev.finish + (empty_line_after_comment and 0 or 1) if not prev.d then range.finish = range.start + 1 @@ -262,6 +263,7 @@ function M.new(parser, walker, fmt, write, delete, eol_at) return adj end if not nxt and prev then + -- clean up trailing whitespace (e.g. electric RET) range.start = prev.finish + 1 range.finish = parent.finish elseif nxt and nxt.start == range.finish and (not prev or prev.finish + 1 == range.start) then @@ -318,7 +320,7 @@ function M.new(parser, walker, fmt, write, delete, eol_at) node.finish + 1 == range.finish and node.is_line_comment local par = big_enough_parent(range.start, range.finish) local operator_changed = par[1] and par[1].finish >= range.start - local refmt = #skips == 0 and delete_indentation(range, pos) or operator_changed and 0 + local refmt = #skips == 0 and delete_whitespace_only(range, pos) or operator_changed and 0 table.sort(skips, function(a, b) return a.start < b.start end) table.insert(skips, {start = range.finish - (drop_eol and 1 or 0)}) table.insert(skips, 1, {finish = range.start}) -- 2.11.4.GIT