From: Michael D. Lowis Date: Wed, 26 Sep 2018 20:15:05 +0000 (-0400) Subject: handle reverse selections properly when trimming whitespace X-Git-Url: https://git.mdlowis.com/?a=commitdiff_plain;h=aa993027ac8694ad7646c5364d78942e275b2699;p=projs%2Ftide.git handle reverse selections properly when trimming whitespace --- diff --git a/lib/buf.c b/lib/buf.c index e6d95d5..667dc5e 100644 --- a/lib/buf.c +++ b/lib/buf.c @@ -143,9 +143,17 @@ void buf_reload(Buf* buf) { buf_load(buf, path); } +static Sel selswap(Sel sel) { + size_t off = sel.beg; + sel.beg = sel.end, sel.end = off; + return sel; +} + static void trim_whitespace(Buf* buf) { if (!TrimOnSave || !buf_end(buf)) return; Sel sel = buf->selection; + bool swapped = (sel.beg > sel.end); + if (swapped) sel = selswap(sel); unsigned prev = 1; buf->selection.beg = buf->selection.end = 0; while (buf->selection.end < buf_end(buf) && prev != buf->selection.end) { @@ -166,7 +174,8 @@ static void trim_whitespace(Buf* buf) { /* move to the next character */ prev = buf->selection.end; buf->selection.end = buf_byrune(buf, buf->selection.end, +1); - } + } + if (swapped) sel = selswap(sel); buf->selection = sel; }