Skip to content

Commit 79bec10

Browse files
vincentwoomarijnh
authored andcommitted
[sublime] Implement smart backspace with multi-cursor support
1 parent 440395d commit 79bec10

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

keymap/sublime.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -420,6 +420,34 @@
420420

421421
map[cK + ctrl + "Backspace"] = "delLineLeft";
422422

423+
cmds[map["Backspace"] = "smartBackspace"] = function(cm) {
424+
if (cm.somethingSelected()) return CodeMirror.Pass;
425+
426+
cm.operation(function() {
427+
var cursors = cm.listSelections();
428+
var indentUnit = cm.getOption("indentUnit");
429+
430+
for (var i = cursors.length - 1; i >= 0; i--) {
431+
var cursor = cursors[i].head;
432+
var toStartOfLine = cm.getRange({line: cursor.line, ch: 0}, cursor);
433+
var column = CodeMirror.countColumn(toStartOfLine, null, cm.getOption("tabSize"));
434+
435+
// Delete by one character by default
436+
var deletePos = cm.findPosH(cursor, -1, "char", false);
437+
438+
if (toStartOfLine && !/\S/.test(toStartOfLine) && column % indentUnit == 0) {
439+
var prevIndent = new Pos(cursor.line,
440+
CodeMirror.findColumn(toStartOfLine, column - indentUnit, indentUnit));
441+
442+
// Smart delete only if we found a valid prevIndent location
443+
if (prevIndent.ch != cursor.ch) deletePos = prevIndent;
444+
}
445+
446+
cm.replaceRange("", deletePos, cursor, "+delete");
447+
}
448+
});
449+
};
450+
423451
cmds[map[cK + ctrl + "K"] = "delLineRight"] = function(cm) {
424452
cm.operation(function() {
425453
var ranges = cm.listSelections();

0 commit comments

Comments
 (0)