|
1 | 1 | import * as os from 'os'; |
| 2 | +import * as path from 'path'; |
2 | 3 | import * as vscode from 'vscode'; |
3 | 4 | import * as assert from 'assert'; |
4 | 5 | import * as cp from 'child_process'; |
@@ -76,8 +77,9 @@ export function sortedWorkspaceFolders(): string[] | undefined { |
76 | 77 | * @returns outer most workspace folder |
77 | 78 | */ |
78 | 79 | export function getOuterMostWorkspaceFolder( |
79 | | - folder: vscode.WorkspaceFolder |
80 | | -): vscode.WorkspaceFolder { |
| 80 | + folder: vscode.WorkspaceFolder | undefined |
| 81 | +): vscode.WorkspaceFolder | undefined { |
| 82 | + if (folder === undefined) return undefined; |
81 | 83 | const sorted = sortedWorkspaceFolders(); |
82 | 84 | for (const element of sorted) { |
83 | 85 | let uri = folder.uri.toString(); |
@@ -262,6 +264,22 @@ export function resolveVariables( |
262 | 264 | // return new Promise<string>((resolve) => { resolve(ret) }); |
263 | 265 | } |
264 | 266 |
|
| 267 | +/** |
| 268 | + * Resolves a path relative to the workspace folder, an optional `uri` |
| 269 | + * can be provided to help retrieve the correct workspace folder when working |
| 270 | + * with multi-root workspaces, else the first workspace folder is used. |
| 271 | + * |
| 272 | + * @param relPath relative path to resolve against the workspace folder |
| 273 | + * @param uri optional uri of a file/folder within the workspace folder |
| 274 | + * usefull when using multiroot workspaces to pick the right workspace root |
| 275 | + * @returns absolute path relative to the workspace root |
| 276 | + */ |
| 277 | +export function pathRelToAbs(relPath: string, uri: vscode.Uri): string | undefined { |
| 278 | + const root = getOuterMostWorkspaceFolder(vscode.workspace.getWorkspaceFolder(uri)); |
| 279 | + if (root === undefined) return undefined; |
| 280 | + return path.join(root.uri.fsPath, relPath); |
| 281 | +} |
| 282 | + |
265 | 283 | export function getWholeFileRange(document: vscode.TextDocument): vscode.Range { |
266 | 284 | return new vscode.Range(0, 0, document.lineCount, 0); |
267 | 285 | } |
|
0 commit comments