Skip to content

Commit

Permalink
refactor: Stop using the 'vscode.workspace.rootPath' API (#443)
Browse files Browse the repository at this point in the history
  • Loading branch information
jdneo authored Oct 2, 2019
1 parent 8d50a2f commit 01f5a28
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/commands/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ export async function testSolution(uri?: vscode.Uri): Promise<void> {
}
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) {
Expand Down
19 changes: 15 additions & 4 deletions src/utils/uiUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ export async function openKeybindingsEditor(query?: string): Promise<void> {
await vscode.commands.executeCommand("workbench.action.openGlobalKeybindings", query);
}

export async function showFileSelectDialog(): Promise<vscode.Uri[] | undefined> {
const defaultUri: vscode.Uri | undefined = vscode.workspace.rootPath ? vscode.Uri.file(vscode.workspace.rootPath) : undefined;
export async function showFileSelectDialog(fsPath?: string): Promise<vscode.Uri[] | undefined> {
const defaultUri: vscode.Uri | undefined = getBelongingWorkspaceFolderUri(fsPath);
const options: vscode.OpenDialogOptions = {
defaultUri,
canSelectFiles: true,
Expand All @@ -92,8 +92,19 @@ export async function showFileSelectDialog(): Promise<vscode.Uri[] | undefined>
return await vscode.window.showOpenDialog(options);
}

export async function showDirectorySelectDialog(): Promise<vscode.Uri[] | undefined> {
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<vscode.Uri[] | undefined> {
const defaultUri: vscode.Uri | undefined = getBelongingWorkspaceFolderUri(fsPath);
const options: vscode.OpenDialogOptions = {
defaultUri,
canSelectFiles: false,
Expand Down

0 comments on commit 01f5a28

Please sign in to comment.