Skip to content

Commit

Permalink
Document transposeChars, fix some inconsistencies
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
adrianheine authored and marijnh committed Oct 4, 2016
1 parent a194325 commit 6b3b6a6
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
14 changes: 12 additions & 2 deletions src/edit/commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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))
Expand Down
2 changes: 1 addition & 1 deletion test/emacs_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down

0 comments on commit 6b3b6a6

Please sign in to comment.