This repository has been archived by the owner on Oct 12, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 19
/
extension.ts
52 lines (38 loc) · 1.76 KB
/
extension.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import * as vscode from 'vscode';
export function activate(context: vscode.ExtensionContext) {
context.subscriptions.push(vscode.commands.registerCommand('jrieken.backspaceLeft', backspace));
}
function backspace() {
if (vscode.window.activeTextEditor) {
const { document, selections, options: { tabSize } } = vscode.window.activeTextEditor;
if (typeof tabSize === 'number') {
let hasNewSelections = false;
const newSelections = selections.map(selection => {
if (!selection.isEmpty) {
return selection;
}
const { start } = selection; // since it's empty start is the same as 'active'
if (start.character === 0) {
return selection;
}
const line = document.lineAt(start);
if (line.firstNonWhitespaceCharacterIndex < start.character) {
return selection;
}
// check for n-space characters preceeding the caret
let match = /^\t*((?: )+)$/.exec(line.text.substr(0, start.character));
if (match) {
hasNewSelections = true;
let toRemove = (match[1].length % tabSize) || tabSize;
return new vscode.Selection(start.with(void 0, start.character - toRemove), start);
}
return selection;
});
// set the new (expanded) selections and run 'deleteLeft'
if (hasNewSelections) {
vscode.window.activeTextEditor.selections = newSelections;
}
}
}
return vscode.commands.executeCommand('deleteLeft');
}