-
Notifications
You must be signed in to change notification settings - Fork 3.5k
/
Copy pathextension.ts
127 lines (99 loc) · 3.69 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
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import * as vscode from 'vscode';
import { getContentFromFilesystem, MarkdownTestData, TestCase, testData, TestFile } from './testTree';
export async function activate(context: vscode.ExtensionContext) {
const ctrl = vscode.tests.createTestController('mathTestController', 'Markdown Math');
context.subscriptions.push(ctrl);
const fileChangedEmitter = new vscode.EventEmitter<vscode.Uri>();
const runHandler = (request: vscode.TestRunRequest, cancellation: vscode.CancellationToken) => {
if (!request.continuous) {
return startSingleTestRun(request);
}
return startContinuousTestRun(request);
};
const startSingleTestRun = (request: vscode.TestRunRequest) => {
vscode.window.showInformationMessage('startSingleTestRun : '+request);
};
const startContinuousTestRun = (request: vscode.TestRunRequest) => {
vscode.window.showInformationMessage('startContinuousTestRun : '+request);
};
ctrl.createRunProfile('Run Tests', vscode.TestRunProfileKind.Run, runHandler, true, undefined, true);
ctrl.resolveHandler = async item => {
if (!item) {
context.subscriptions.push(...startWatchingWorkspace(ctrl, fileChangedEmitter));
return;
}
const data = testData.get(item);
if (data instanceof TestFile) {
await data.updateFromDisk(ctrl, item);
}
};
function updateNodeForDocument(e: vscode.TextDocument) {
if (e.uri.scheme !== 'file') {
return;
}
if (!e.uri.path.endsWith('.md')) {
return;
}
const { file, data } = getOrCreateFile(ctrl, e.uri);
data.updateFromContents(ctrl, e.getText(), file);
console.log('test-provider-sample: update md');
}
for (const document of vscode.workspace.textDocuments) {
updateNodeForDocument(document);
}
context.subscriptions.push(
vscode.workspace.onDidOpenTextDocument(updateNodeForDocument),
vscode.workspace.onDidChangeTextDocument(e => updateNodeForDocument(e.document)),
);
console.log('test-provider-sample activated');
}
function getOrCreateFile(controller: vscode.TestController, uri: vscode.Uri) {
const existing = controller.items.get(uri.toString());
if (existing) {
return { file: existing, data: testData.get(existing) as TestFile };
}
const file = controller.createTestItem(uri.toString(), uri.path.split('/').pop()!, uri);
controller.items.add(file);
const data = new TestFile();
testData.set(file, data);
file.canResolveChildren = true;
return { file, data };
}
function gatherTestItems(collection: vscode.TestItemCollection) {
const items: vscode.TestItem[] = [];
collection.forEach(item => items.push(item));
return items;
}
function getWorkspaceTestPatterns() {
if (!vscode.workspace.workspaceFolders) {
return [];
}
return vscode.workspace.workspaceFolders.map(workspaceFolder => ({
workspaceFolder,
pattern: new vscode.RelativePattern(workspaceFolder, '**/*.md'),
}));
}
async function findInitialFiles(controller: vscode.TestController, pattern: vscode.GlobPattern) {
for (const file of await vscode.workspace.findFiles(pattern)) {
getOrCreateFile(controller, file);
}
}
function startWatchingWorkspace(controller: vscode.TestController, fileChangedEmitter: vscode.EventEmitter<vscode.Uri> ) {
return getWorkspaceTestPatterns().map(({ workspaceFolder, pattern }) => {
const watcher = vscode.workspace.createFileSystemWatcher(pattern);
watcher.onDidCreate(uri => {
getOrCreateFile(controller, uri);
fileChangedEmitter.fire(uri);
});
watcher.onDidChange(async uri => {
const { file, data } = getOrCreateFile(controller, uri);
if (data.didResolve) {
await data.updateFromDisk(controller, file);
}
fileChangedEmitter.fire(uri);
});
watcher.onDidDelete(uri => controller.items.delete(uri.toString()));
findInitialFiles(controller, pattern);
return watcher;
});
}