Skip to content

Commit

Permalink
[sublime] Implement smart backspace with multi-cursor support
Browse files Browse the repository at this point in the history
  • Loading branch information
vincentwoo authored and marijnh committed Jun 9, 2016
1 parent 440395d commit 79bec10
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions keymap/sublime.js
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,34 @@

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

cmds[map["Backspace"] = "smartBackspace"] = function(cm) {
if (cm.somethingSelected()) return CodeMirror.Pass;

cm.operation(function() {
var cursors = cm.listSelections();
var indentUnit = cm.getOption("indentUnit");

for (var i = cursors.length - 1; i >= 0; i--) {
var cursor = cursors[i].head;
var toStartOfLine = cm.getRange({line: cursor.line, ch: 0}, cursor);
var column = CodeMirror.countColumn(toStartOfLine, null, cm.getOption("tabSize"));

// Delete by one character by default
var deletePos = cm.findPosH(cursor, -1, "char", false);

if (toStartOfLine && !/\S/.test(toStartOfLine) && column % indentUnit == 0) {
var prevIndent = new Pos(cursor.line,
CodeMirror.findColumn(toStartOfLine, column - indentUnit, indentUnit));

// Smart delete only if we found a valid prevIndent location
if (prevIndent.ch != cursor.ch) deletePos = prevIndent;
}

cm.replaceRange("", deletePos, cursor, "+delete");
}
});
};

cmds[map[cK + ctrl + "K"] = "delLineRight"] = function(cm) {
cm.operation(function() {
var ranges = cm.listSelections();
Expand Down

0 comments on commit 79bec10

Please sign in to comment.