Skip to content

Commit 73c4e24

Browse files
vranamarijnh
authored andcommitted
[sublime bindings] Don't sort last line with no selected chars
Selecting two full lines including line ends makes a selection from line 1 to line 3 (ch: 0). This command used to sort three lines (1 to 3) which is not what I expect. This change makes it sort only two lines if there are no selected characters on the last line. It also fixes a fatal error if there are more than one selection on the same line (the code used 'range' instead of 'ranges'). It also selects the trailing newline after sorting the lines so that the whole lines are selected.
1 parent 6709974 commit 73c4e24

File tree

2 files changed

+7
-6
lines changed

2 files changed

+7
-6
lines changed

keymap/sublime.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,8 @@
310310
if (range.empty()) continue;
311311
var from = range.from().line, to = range.to().line;
312312
while (i < ranges.length - 1 && ranges[i + 1].from().line == to)
313-
to = range[++i].to().line;
313+
to = ranges[++i].to().line;
314+
if (!ranges[i].to().ch) to--;
314315
toSort.push(from, to);
315316
}
316317
if (toSort.length) selected = true;
@@ -331,7 +332,7 @@
331332
return a < b ? -1 : a == b ? 0 : 1;
332333
});
333334
cm.replaceRange(lines, start, end);
334-
if (selected) ranges.push({anchor: start, head: end});
335+
if (selected) ranges.push({anchor: start, head: Pos(to + 1, 0)});
335336
}
336337
if (selected) cm.setSelections(ranges, 0);
337338
});

test/sublime_test.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -249,11 +249,11 @@
249249
"undo",
250250
setSel(0, 0, 2, 0,
251251
3, 0, 5, 0),
252-
"sortLines", val("a\nb\nc\nA\nB\nC"),
253-
hasSel(0, 0, 2, 1,
254-
3, 0, 5, 1),
252+
"sortLines", val("b\nc\na\nB\nC\nA"),
253+
hasSel(0, 0, 2, 0,
254+
3, 0, 5, 0),
255255
"undo",
256-
setSel(1, 0, 4, 0), "sortLinesInsensitive", val("c\na\nB\nb\nC\nA"));
256+
setSel(1, 0, 5, 0), "sortLinesInsensitive", val("c\na\nB\nb\nC\nA"));
257257

258258
stTest("bookmarks", "abc\ndef\nghi\njkl",
259259
Pos(0, 1), "toggleBookmark",

0 commit comments

Comments
 (0)