forked from phindle/error-lens
-
Notifications
You must be signed in to change notification settings - Fork 32
/
eventListeners.ts
81 lines (80 loc) · 3.06 KB
/
eventListeners.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import { CustomDelay } from 'src/CustomDelay';
import { updateAllDecorations, updateDecorationsForUri } from 'src/decorations';
import { extensionConfig, Global } from 'src/extension';
import vscode, { window, workspace } from 'vscode';
export function updateChangedActiveTextEditorListener(): void {
if (Global.onDidChangeActiveTextEditor) {
Global.onDidChangeActiveTextEditor.dispose();
}
Global.onDidChangeActiveTextEditor = window.onDidChangeActiveTextEditor(textEditor => {
if (textEditor) {
updateDecorationsForUri(textEditor.document.uri, textEditor);
}
});
}
export function updateChangeVisibleTextEditorsListener(): void {
if (Global.onDidChangeVisibleTextEditors) {
Global.onDidChangeVisibleTextEditors.dispose();
}
Global.onDidChangeVisibleTextEditors = window.onDidChangeVisibleTextEditors(updateAllDecorations);
}
export function updateChangeDiagnosticListener(): void {
if (Global.onDidChangeDiagnosticsDisposable) {
Global.onDidChangeDiagnosticsDisposable.dispose();
}
function onChangedDiagnostics(diagnosticChangeEvent: vscode.DiagnosticChangeEvent): void {
// Many URIs can change - we only need to decorate all visible editors
for (const uri of diagnosticChangeEvent.uris) {
for (const editor of window.visibleTextEditors) {
if (uri.fsPath === editor.document.uri.fsPath) {
updateDecorationsForUri(uri, editor);
}
}
}
}
if (extensionConfig.onSave) {
Global.onDidChangeDiagnosticsDisposable = vscode.languages.onDidChangeDiagnostics(e => {
if (Date.now() - Global.lastSavedTimestamp < 1000) {
onChangedDiagnostics(e);
}
});
return;
}
if (typeof extensionConfig.delay === 'number' && extensionConfig.delay > 0) {
Global.customDelay = new CustomDelay(extensionConfig.delay);
Global.onDidChangeDiagnosticsDisposable = vscode.languages.onDidChangeDiagnostics(Global.customDelay.onDiagnosticChange);
} else {
Global.onDidChangeDiagnosticsDisposable = vscode.languages.onDidChangeDiagnostics(onChangedDiagnostics);
}
}
export function updateCursorChangeListener(): void {
if (Global.onDidCursorChangeDisposable) {
Global.onDidCursorChangeDisposable.dispose();
}
if (extensionConfig.followCursor === 'activeLine' || extensionConfig.followCursor === 'closestProblem' || extensionConfig.statusBarMessageEnabled) {
let lastPositionLine = 999999;// Unlikely line number
Global.onDidCursorChangeDisposable = window.onDidChangeTextEditorSelection(e => {
const selection = e.selections[0];
if (e.selections.length === 1 &&
selection.isEmpty &&
lastPositionLine !== selection.active.line) {
updateDecorationsForUri(e.textEditor.document.uri, e.textEditor, selection);
lastPositionLine = e.selections[0].active.line;
}
});
}
}
export function updateOnSaveListener(): void {
if (Global.onDidSaveTextDocumentDisposable) {
Global.onDidSaveTextDocumentDisposable.dispose();
}
if (!extensionConfig.onSave) {
return;
}
Global.onDidSaveTextDocumentDisposable = workspace.onDidSaveTextDocument(e => {
Global.lastSavedTimestamp = Date.now();
setTimeout(() => {
updateDecorationsForUri(e.uri);
}, 600);
});
}