-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathdocuments.ts
More file actions
101 lines (95 loc) · 4.27 KB
/
documents.ts
File metadata and controls
101 lines (95 loc) · 4.27 KB
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import * as vscode from 'vscode';
import { isConfigFile, isProxyFile } from './utils';
import { updateFileDiagnostics, updateConfigFileDiagnostics } from './diagnostics';
import * as logger from './logger';
export const registerDocumentListeners = (context: vscode.ExtensionContext, collection: vscode.DiagnosticCollection) => {
context.subscriptions.push(
vscode.workspace.onDidOpenTextDocument(document => {
if (document.uri.scheme !== 'file') {
return;
}
try {
if (isProxyFile(document)) {
logger.debug('Proxy file opened', { path: document.uri.fsPath });
updateFileDiagnostics(context, document, collection);
vscode.commands.executeCommand('setContext', 'isDevProxyConfigFile', false);
}
if (!isConfigFile(document)) {
vscode.commands.executeCommand('setContext', 'isDevProxyConfigFile', false);
return;
} else {
logger.debug('Config file opened', { path: document.uri.fsPath });
vscode.commands.executeCommand('setContext', 'isDevProxyConfigFile', true);
updateConfigFileDiagnostics(context, document, collection);
}
} catch (error) {
logger.error('Error handling document open', error);
}
})
);
context.subscriptions.push(
vscode.workspace.onDidChangeTextDocument(event => {
if (event.document.uri.scheme !== 'file') {
return;
}
try {
if (!isConfigFile(event.document) && !isProxyFile(event.document)) {
collection.delete(event.document.uri);
return;
}
if (isConfigFile(event.document)) {
logger.debug('Config file changed', { path: event.document.uri.fsPath });
updateConfigFileDiagnostics(context, event.document, collection);
vscode.commands.executeCommand('setContext', 'isDevProxyConfigFile', true);
return;
}
if (isProxyFile(event.document)) {
logger.debug('Proxy file changed', { path: event.document.uri.fsPath });
updateFileDiagnostics(context, event.document, collection);
vscode.commands.executeCommand('setContext', 'isDevProxyConfigFile', false);
}
} catch (error) {
logger.error('Error handling document change', error);
}
})
);
context.subscriptions.push(
vscode.window.onDidChangeActiveTextEditor(e => {
if (!e) {
vscode.commands.executeCommand('setContext', 'isDevProxyConfigFile', false);
return;
};
isConfigFile(e.document) ?
vscode.commands.executeCommand('setContext', 'isDevProxyConfigFile', true) :
vscode.commands.executeCommand('setContext', 'isDevProxyConfigFile', false);
})
);
context.subscriptions.push(
vscode.workspace.onDidDeleteFiles(e => {
e.files.forEach(file => {
const uri = file;
collection.delete(uri);
});
})
);
// Process already-open documents that were opened before the extension activated
for (const document of vscode.workspace.textDocuments) {
if (document.uri.scheme !== 'file') {
continue;
}
try {
if (isConfigFile(document)) {
updateConfigFileDiagnostics(context, document, collection);
} else if (isProxyFile(document)) {
updateFileDiagnostics(context, document, collection);
}
} catch (error) {
console.error('Error processing already-open document:', document.uri.fsPath, error);
}
}
// Set context for the active editor if it contains a config file
const activeEditor = vscode.window.activeTextEditor;
if (activeEditor && activeEditor.document.uri.scheme === 'file') {
vscode.commands.executeCommand('setContext', 'isDevProxyConfigFile', isConfigFile(activeEditor.document));
}
};