Skip to content

Commit 3e745fa

Browse files
committed
[sublime keymap] Fix behavior of swapLineUp from last line
Issue codemirror#2362 Also add origins to the changes generated.
1 parent 00e4259 commit 3e745fa

File tree

2 files changed

+17
-5
lines changed

2 files changed

+17
-5
lines changed

keymap/sublime.js

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -150,8 +150,16 @@
150150
for (var i = 0; i < linesToMove.length; i += 2) {
151151
var from = linesToMove[i], to = linesToMove[i + 1];
152152
var line = cm.getLine(from);
153-
cm.replaceRange("", Pos(from, 0), Pos(from + 1, 0));
154-
cm.replaceRange(line + "\n", Pos(to, 0));
153+
cm.replaceRange("", Pos(from, 0), Pos(from + 1, 0), "+swapLine");
154+
if (to > cm.lastLine()) {
155+
cm.replaceRange("\n" + line, Pos(cm.lastLine()), null, "+swapLine");
156+
var sels = cm.listSelections(), last = sels[sels.length - 1];
157+
var head = last.head.line == to ? Pos(to - 1) : last.head;
158+
var anchor = last.anchor.line == to ? Pos(to - 1) : last.anchor;
159+
cm.setSelections(sels.slice(0, sels.length - 1).concat([{head: head, anchor: anchor}]));
160+
} else {
161+
cm.replaceRange(line + "\n", Pos(to, 0), null, "+swapLine");
162+
}
155163
}
156164
});
157165
};
@@ -169,10 +177,10 @@
169177
var from = linesToMove[i], to = linesToMove[i + 1];
170178
var line = cm.getLine(from);
171179
if (from == cm.lastLine())
172-
cm.replaceRange("", Pos(from - 1), Pos(from));
180+
cm.replaceRange("", Pos(from - 1), Pos(from), "+swapLine");
173181
else
174-
cm.replaceRange("", Pos(from, 0), Pos(from + 1, 0));
175-
cm.replaceRange(line + "\n", Pos(to, 0));
182+
cm.replaceRange("", Pos(from, 0), Pos(from + 1, 0), "+swapLine");
183+
cm.replaceRange(line + "\n", Pos(to, 0), null, "+swapLine");
176184
}
177185
});
178186
};

test/sublime_test.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,10 @@
162162
1, 0, 2, 0,
163163
2, 2, 2, 2));
164164

165+
stTest("swapLineUpFromEnd", "a\nb\nc",
166+
Pos(2, 1), "swapLineUp",
167+
hasSel(1, 1, 1, 1), val("a\nc\nb"));
168+
165169
stTest("joinLines", "abc\ndef\nghi\njkl",
166170
"joinLines", val("abc def\nghi\njkl"), at(0, 4),
167171
"undo",

0 commit comments

Comments
 (0)