From 01f5a2854087d08271961ea4aa7f12c108a5818c Mon Sep 17 00:00:00 2001 From: Sheng Chen Date: Wed, 2 Oct 2019 12:26:56 +0800 Subject: [PATCH] refactor: Stop using the 'vscode.workspace.rootPath' API (#443) --- src/commands/test.ts | 2 +- src/utils/uiUtils.ts | 19 +++++++++++++++---- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/src/commands/test.ts b/src/commands/test.ts index 262f7339..d070c9aa 100644 --- a/src/commands/test.ts +++ b/src/commands/test.ts @@ -65,7 +65,7 @@ export async function testSolution(uri?: vscode.Uri): Promise { } break; case ":file": - const testFile: vscode.Uri[] | undefined = await showFileSelectDialog(); + const testFile: vscode.Uri[] | undefined = await showFileSelectDialog(filePath); if (testFile && testFile.length) { const input: string = (await fse.readFile(testFile[0].fsPath, "utf-8")).trim(); if (input) { diff --git a/src/utils/uiUtils.ts b/src/utils/uiUtils.ts index 037b2371..53275fa0 100644 --- a/src/utils/uiUtils.ts +++ b/src/utils/uiUtils.ts @@ -80,8 +80,8 @@ export async function openKeybindingsEditor(query?: string): Promise { await vscode.commands.executeCommand("workbench.action.openGlobalKeybindings", query); } -export async function showFileSelectDialog(): Promise { - const defaultUri: vscode.Uri | undefined = vscode.workspace.rootPath ? vscode.Uri.file(vscode.workspace.rootPath) : undefined; +export async function showFileSelectDialog(fsPath?: string): Promise { + const defaultUri: vscode.Uri | undefined = getBelongingWorkspaceFolderUri(fsPath); const options: vscode.OpenDialogOptions = { defaultUri, canSelectFiles: true, @@ -92,8 +92,19 @@ export async function showFileSelectDialog(): Promise return await vscode.window.showOpenDialog(options); } -export async function showDirectorySelectDialog(): Promise { - const defaultUri: vscode.Uri | undefined = vscode.workspace.rootPath ? vscode.Uri.file(vscode.workspace.rootPath) : undefined; +function getBelongingWorkspaceFolderUri(fsPath: string | undefined): vscode.Uri | undefined { + let defaultUri: vscode.Uri | undefined; + if (fsPath) { + const workspaceFolder: vscode.WorkspaceFolder | undefined = vscode.workspace.getWorkspaceFolder(vscode.Uri.file(fsPath)); + if (workspaceFolder) { + defaultUri = workspaceFolder.uri; + } + } + return defaultUri; +} + +export async function showDirectorySelectDialog(fsPath?: string): Promise { + const defaultUri: vscode.Uri | undefined = getBelongingWorkspaceFolderUri(fsPath); const options: vscode.OpenDialogOptions = { defaultUri, canSelectFiles: false,