-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathextension.ts
47 lines (41 loc) · 1.52 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
import * as vscode from 'vscode';
export function activate(context: vscode.ExtensionContext) {
context.subscriptions.push(new LineTrimmer());
}
export function deactivate() {
}
class LineTrimmer {
private _disposable: vscode.Disposable;
private _lines = new WeakMap<any, Set<number>>()
constructor() {
this._disposable = vscode.window.onDidChangeTextEditorSelection(this.onChangeSelection, this);
}
onChangeSelection(e: vscode.TextEditorSelectionChangeEvent) {
const editor = vscode.window.activeTextEditor;
const doc = editor.document;
const lines = new Set<number>(e.selections.map(sel => sel.active.line));
const previousLines = this._lines.get(doc);
if(previousLines) {
previousLines.forEach(lineNum => {
if(!lines.has(lineNum) && doc.lineCount > lineNum) {
const line = doc.lineAt(lineNum);
if(!line) {
return;
}
if(doc.languageId === 'markdown' && line.text.match(/[^ ] $/)) {
return;
}
const text = line.isEmptyOrWhitespace ? '' :
line.text.replace(/[ \t]+$/, '');
if(line.text !== text) {
editor.edit(ed => ed.replace(line.range, text));
}
}
});
}
this._lines.set(doc, lines);
}
dispose() {
this._disposable.dispose();
}
}