Skip to content

Commit cbd8f67

Browse files
committed
feat: add workspace folder selection for project creation
1 parent 07a8cf4 commit cbd8f67

File tree

2 files changed

+47
-3
lines changed

2 files changed

+47
-3
lines changed

src/common/pickers/managers.ts

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { commands, QuickInputButtons, QuickPickItem, QuickPickItemKind } from 'vscode';
1+
import { commands, QuickInputButtons, QuickPickItem, QuickPickItemKind, workspace, WorkspaceFolder } from 'vscode';
22
import { PythonProjectCreator } from '../../api';
33
import { InternalEnvironmentManager, InternalPackageManager } from '../../internal.api';
44
import { Common, Pickers } from '../localize';
@@ -125,6 +125,31 @@ export async function pickPackageManager(
125125
return (item as QuickPickItem & { id: string })?.id;
126126
}
127127

128+
export async function pickWorkspaceFolder(showBackButton = true): Promise<WorkspaceFolder | undefined> {
129+
const folders = workspace.workspaceFolders;
130+
if (!folders || folders.length === 0) {
131+
return undefined;
132+
}
133+
if (folders.length === 1) {
134+
return folders[0];
135+
}
136+
const items = folders.map((f) => ({
137+
label: f.name,
138+
description: f.uri.fsPath,
139+
folder: f,
140+
}));
141+
142+
const selected = await showQuickPickWithButtons(items, {
143+
placeHolder: 'Select a workspace folder',
144+
ignoreFocusOut: true,
145+
showBackButton,
146+
});
147+
if (!selected) {
148+
return undefined;
149+
}
150+
const selectedItem = Array.isArray(selected) ? selected[0] : selected;
151+
return selectedItem?.folder;
152+
}
128153
export async function pickCreator(creators: PythonProjectCreator[]): Promise<PythonProjectCreator | undefined> {
129154
if (creators.length === 0) {
130155
return;

src/features/envCommands.ts

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { commands, QuickInputButtons, TaskExecution, TaskRevealKind, Terminal, Uri } from 'vscode';
1+
import { commands, QuickInputButtons, TaskExecution, TaskRevealKind, Terminal, Uri, workspace } from 'vscode';
22
import {
33
CreateEnvironmentOptions,
44
PythonEnvironment,
@@ -20,7 +20,12 @@ import { removePythonProjectSetting, setEnvironmentManager, setPackageManager }
2020
import { clipboardWriteText } from '../common/env.apis';
2121
import {} from '../common/errors/utils';
2222
import { pickEnvironment } from '../common/pickers/environments';
23-
import { pickCreator, pickEnvironmentManager, pickPackageManager } from '../common/pickers/managers';
23+
import {
24+
pickCreator,
25+
pickEnvironmentManager,
26+
pickPackageManager,
27+
pickWorkspaceFolder,
28+
} from '../common/pickers/managers';
2429
import { pickProject, pickProjectMany } from '../common/pickers/projects';
2530
import { activeTextEditor, showErrorMessage, showInformationMessage } from '../common/window.apis';
2631
import { quoteArgs } from './execution/execUtils';
@@ -451,6 +456,20 @@ export async function addPythonProjectCommand(
451456
return;
452457
}
453458

459+
// if multiroot, prompt the user to select which workspace to create the project in
460+
const workspaceFolders = workspace.workspaceFolders;
461+
if (!resource && workspaceFolders && workspaceFolders.length > 1) {
462+
try {
463+
const workspace = await pickWorkspaceFolder(true);
464+
resource = workspace?.uri;
465+
} catch (ex) {
466+
if (ex === QuickInputButtons.Back) {
467+
return addPythonProjectCommand(resource, wm, em, pc);
468+
}
469+
throw ex;
470+
}
471+
}
472+
454473
try {
455474
await creator.create(options);
456475
} catch (ex) {

0 commit comments

Comments
 (0)