-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathutils.ts
More file actions
51 lines (44 loc) · 1.43 KB
/
utils.ts
File metadata and controls
51 lines (44 loc) · 1.43 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
import * as vscode from 'vscode';
import * as path from 'path';
export function findProjectDir() {
const docUri = vscode.window.activeTextEditor?.document.uri;
if (!docUri) {
throw new Error("No active editor found.");
}
const workspace = vscode.workspace.getWorkspaceFolder(docUri);
if (workspace) {
const folder = workspace.uri.fsPath;
console.log(`tox workspace folder: ${folder}`);
return folder;
}
const docPath = docUri.fsPath;
const docDir = path.dirname(docPath);
console.log(`tox doc path: ${docPath} -> ${docDir}`);
return docDir;
}
/**
* Get a new terminal or use an existing one with the same name.
* @param projDir The directory of the project.
* @param name The name of the terminal
* @returns The terminal to run commands on.
*/
export function getTerminal(projDir : string = findProjectDir(), name : string = "tox") : vscode.Terminal {
for (const terminal of vscode.window.terminals) {
if (terminal.name === name){
return terminal;
}
}
return vscode.window.createTerminal({"cwd": projDir, "name": name});
}
/**
* Get the top-most parent label (+ description) for terminal name
* @param test The test to start from.
* @returns The label and description of the root test item.
*/
export function getRootParentLabelDesc(test: vscode.TestItem) : string {
let root = test;
while (root.parent !== undefined){
root = root.parent;
}
return root.label + " " + root.description; // FIXME: return as tuple?
}