Skip to content
Snippets Groups Projects
Commit 6b3b6a60 authored by Adrian Heine's avatar Adrian Heine Committed by Marijn Haverbeke
Browse files

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.
parent a194325b
No related branches found
No related tags found
No related merge requests found
...@@ -106,9 +106,17 @@ export let commands = { ...@@ -106,9 +106,17 @@ export let commands = {
if (cm.somethingSelected()) cm.indentSelection("add") if (cm.somethingSelected()) cm.indentSelection("add")
else cm.execCommand("insertTab") 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, () => { transposeChars: cm => runInOp(cm, () => {
let ranges = cm.listSelections(), newSel = [] let ranges = cm.listSelections(), newSel = []
for (let i = 0; i < ranges.length; i++) { 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 let cur = ranges[i].head, line = getLine(cm.doc, cur.line).text
if (line) { if (line) {
if (cur.ch == line.length) cur = new Pos(cur.line, cur.ch - 1) if (cur.ch == line.length) cur = new Pos(cur.line, cur.ch - 1)
...@@ -118,10 +126,12 @@ export let commands = { ...@@ -118,10 +126,12 @@ export let commands = {
Pos(cur.line, cur.ch - 2), cur, "+transpose") Pos(cur.line, cur.ch - 2), cur, "+transpose")
} else if (cur.line > cm.doc.first) { } else if (cur.line > cm.doc.first) {
let prev = getLine(cm.doc, cur.line - 1).text 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() + cm.replaceRange(line.charAt(0) + cm.doc.lineSeparator() +
prev.charAt(prev.length - 1), 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)) newSel.push(new Range(cur, cur))
......
...@@ -111,7 +111,7 @@ ...@@ -111,7 +111,7 @@
sim("transposeChar", "abcd\ne", sim("transposeChar", "abcd\ne",
"Ctrl-F", "Ctrl-T", "Ctrl-T", txt("bcad\ne"), at(0, 3), "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", "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", sim("manipWordCase", "foo BAR bAZ",
"Alt-C", "Alt-L", "Alt-U", txt("Foo bar BAZ"), "Alt-C", "Alt-L", "Alt-U", txt("Foo bar BAZ"),
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment