-
Notifications
You must be signed in to change notification settings - Fork 97
/
gotoTest.ts
89 lines (64 loc) · 3.04 KB
/
gotoTest.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
import * as vscode from "vscode";
import { AppInsightsClient } from "./appInsightsClient";
import { TestNode } from "./testNode";
export class GotoTest {
public go(test: TestNode): void {
AppInsightsClient.sendEvent("gotoTest");
const testname = this.getTestName(test.name);
const symbolInformation = vscode.commands.executeCommand<vscode.SymbolInformation[]>(
"vscode.executeWorkspaceSymbolProvider",
testname,
).then((symbols) => {
let symbol: vscode.SymbolInformation;
try {
symbol = this.findTestLocation(symbols, test);
vscode.workspace.openTextDocument(symbol.location.uri).then((doc) => {
vscode.window.showTextDocument(doc).then((editor) => {
const loc = symbol.location.range;
const selection = new vscode.Selection(loc.start.line, loc.start.character, loc.start.line, loc.end.character);
vscode.window.activeTextEditor.selection = selection;
vscode.window.activeTextEditor.revealRange(selection, vscode.TextEditorRevealType.InCenter);
});
});
} catch (r) {
vscode.window.showWarningMessage(r.message);
}
});
}
public findTestLocation(symbols: vscode.SymbolInformation[], testNode: TestNode): vscode.SymbolInformation {
if (symbols.length === 0) {
throw new Error("Could not find test (no symbols found)");
}
const testName = this.getTestName(testNode.name);
symbols = symbols.filter( (s) => s.kind === vscode.SymbolKind.Method && s.name.replace("()", "") === testName);
if (symbols.length === 0) {
throw Error("Could not find test (no symbols matching)");
}
// If multiple results are found, try to match the uri of the match to the parent path of the test
if (symbols.length > 1) {
const testNamespace = this.getTestNamespace(testNode);
symbols = symbols.filter((x) => x.location.uri.toString().replace(/\//g, ".").toLowerCase().indexOf(testNamespace.toLowerCase() + ".") > -1);
if (symbols.length === 0) {
throw Error("Could not find test (namespace not matching uri)");
}
if (symbols.length > 1) {
throw Error("Could not find test (found multiple matching symbols)");
}
}
return symbols[0];
}
public getTestName(testName: string): string {
const lastDotIndex = testName.lastIndexOf(".");
if (lastDotIndex > -1) {
testName = testName.substring(lastDotIndex + 1);
}
return testName;
}
public getTestNamespace(testNode: TestNode): string {
if (testNode.parentPath.length === 0) {
const testName = this.getTestName(testNode.name);
return testNode.name.substring(0, testNode.name.indexOf(testName) - 1);
}
return testNode.parentPath;
}
}