From 6b3b6a600101eb29a53a45f6117ba3db14ba17c2 Mon Sep 17 00:00:00 2001 From: Adrian Heine <mail@adrianheine.de> Date: Sun, 2 Oct 2016 13:44:45 +0200 Subject: [PATCH] Document transposeChars, fix some inconsistencies With non-empty selections, transposeChar would only work on the chars next to head and ignore the selected chars. Also, a cursor at line start was not moved on successful swapping. --- src/edit/commands.js | 14 ++++++++++++-- test/emacs_test.js | 2 +- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/src/edit/commands.js b/src/edit/commands.js index fe39ed727..0b0125d1e 100644 --- a/src/edit/commands.js +++ b/src/edit/commands.js @@ -106,9 +106,17 @@ export let commands = { if (cm.somethingSelected()) cm.indentSelection("add") else cm.execCommand("insertTab") }, + // Swap the two chars left and right of each selection's head. + // Move cursor behind the two swapped characters afterwards. + // + // Doesn't consider line feeds a character. + // Doesn't scan more than one line above to find a character. + // Doesn't do anything on an empty line. + // Doesn't do anything with non-empty selections. transposeChars: cm => runInOp(cm, () => { let ranges = cm.listSelections(), newSel = [] for (let i = 0; i < ranges.length; i++) { + if (!ranges[i].empty()) continue let cur = ranges[i].head, line = getLine(cm.doc, cur.line).text if (line) { if (cur.ch == line.length) cur = new Pos(cur.line, cur.ch - 1) @@ -118,10 +126,12 @@ export let commands = { Pos(cur.line, cur.ch - 2), cur, "+transpose") } else if (cur.line > cm.doc.first) { let prev = getLine(cm.doc, cur.line - 1).text - if (prev) + if (prev) { + cur = new Pos(cur.line, 1) cm.replaceRange(line.charAt(0) + cm.doc.lineSeparator() + prev.charAt(prev.length - 1), - Pos(cur.line - 1, prev.length - 1), Pos(cur.line, 1), "+transpose") + Pos(cur.line - 1, prev.length - 1), cur, "+transpose") + } } } newSel.push(new Range(cur, cur)) diff --git a/test/emacs_test.js b/test/emacs_test.js index 124575c72..628651c78 100644 --- a/test/emacs_test.js +++ b/test/emacs_test.js @@ -111,7 +111,7 @@ sim("transposeChar", "abcd\ne", "Ctrl-F", "Ctrl-T", "Ctrl-T", txt("bcad\ne"), at(0, 3), "Ctrl-F", "Ctrl-T", "Ctrl-T", "Ctrl-T", txt("bcda\ne"), at(0, 4), - "Ctrl-F", "Ctrl-T", txt("bcde\na"), at(1, 0)); + "Ctrl-F", "Ctrl-T", txt("bcde\na"), at(1, 1)); sim("manipWordCase", "foo BAR bAZ", "Alt-C", "Alt-L", "Alt-U", txt("Foo bar BAZ"), -- GitLab