Skip to content

Commit 6b3b6a6

Browse files
adrianheinemarijnh
authored andcommitted
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.
1 parent a194325 commit 6b3b6a6

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

src/edit/commands.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,9 +106,17 @@ export let commands = {
106106
if (cm.somethingSelected()) cm.indentSelection("add")
107107
else cm.execCommand("insertTab")
108108
},
109+
// Swap the two chars left and right of each selection's head.
110+
// Move cursor behind the two swapped characters afterwards.
111+
//
112+
// Doesn't consider line feeds a character.
113+
// Doesn't scan more than one line above to find a character.
114+
// Doesn't do anything on an empty line.
115+
// Doesn't do anything with non-empty selections.
109116
transposeChars: cm => runInOp(cm, () => {
110117
let ranges = cm.listSelections(), newSel = []
111118
for (let i = 0; i < ranges.length; i++) {
119+
if (!ranges[i].empty()) continue
112120
let cur = ranges[i].head, line = getLine(cm.doc, cur.line).text
113121
if (line) {
114122
if (cur.ch == line.length) cur = new Pos(cur.line, cur.ch - 1)
@@ -118,10 +126,12 @@ export let commands = {
118126
Pos(cur.line, cur.ch - 2), cur, "+transpose")
119127
} else if (cur.line > cm.doc.first) {
120128
let prev = getLine(cm.doc, cur.line - 1).text
121-
if (prev)
129+
if (prev) {
130+
cur = new Pos(cur.line, 1)
122131
cm.replaceRange(line.charAt(0) + cm.doc.lineSeparator() +
123132
prev.charAt(prev.length - 1),
124-
Pos(cur.line - 1, prev.length - 1), Pos(cur.line, 1), "+transpose")
133+
Pos(cur.line - 1, prev.length - 1), cur, "+transpose")
134+
}
125135
}
126136
}
127137
newSel.push(new Range(cur, cur))

test/emacs_test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@
111111
sim("transposeChar", "abcd\ne",
112112
"Ctrl-F", "Ctrl-T", "Ctrl-T", txt("bcad\ne"), at(0, 3),
113113
"Ctrl-F", "Ctrl-T", "Ctrl-T", "Ctrl-T", txt("bcda\ne"), at(0, 4),
114-
"Ctrl-F", "Ctrl-T", txt("bcde\na"), at(1, 0));
114+
"Ctrl-F", "Ctrl-T", txt("bcde\na"), at(1, 1));
115115

116116
sim("manipWordCase", "foo BAR bAZ",
117117
"Alt-C", "Alt-L", "Alt-U", txt("Foo bar BAZ"),

0 commit comments

Comments
 (0)