Skip to content

Commit e9e1e38

Browse files
Fix Add Python Project via context menu to directly add file/folder (#431)
## Issue When right-clicking on a folder or file in the file explorer and selecting "Add Python Project", the extension shows a quick pick menu (Auto Find, Select Existing, Create New) instead of directly adding the selected folder/file to the Python Projects view. This prevents users from easily adding a single file directly. ## Testing Verified that: - Right-clicking on a folder and selecting "Add Python Project" directly adds that folder - Right-clicking on a Python file and selecting "Add Python Project" directly adds that file - Using the "Add Python Project" button in the Python Projects view still shows the quick pick UI Fixes #385. (edit by @eleanorjboyd so it's useful / relevant) --------- Co-authored-by: copilot-swe-agent[bot] <[email protected]> Co-authored-by: eleanorjboyd <[email protected]>
1 parent 6ccbb2f commit e9e1e38

File tree

2 files changed

+42
-11
lines changed

2 files changed

+42
-11
lines changed

src/features/creators/existingProjects.ts

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,25 +15,35 @@ export class ExistingProjects implements PythonProjectCreator {
1515
async create(
1616
_options?: PythonProjectCreatorOptions,
1717
): Promise<PythonProject | PythonProject[] | Uri | Uri[] | undefined> {
18-
const results = await showOpenDialog({
19-
canSelectFiles: true,
20-
canSelectFolders: true,
21-
canSelectMany: true,
22-
filters: {
23-
python: ['py'],
24-
},
25-
title: ProjectCreatorString.selectFilesOrFolders,
26-
});
18+
let existingAddUri: Uri[] | undefined;
19+
if (_options?.rootUri) {
20+
// If rootUri is provided, do not prompt
21+
existingAddUri = [_options.rootUri];
22+
} else if (_options?.quickCreate) {
23+
// If quickCreate is true & no rootUri is provided, we should not prompt for any input
24+
throw new Error('Root URI is required in quickCreate mode.');
25+
} else {
26+
// Prompt the user to select files or folders
27+
existingAddUri = await showOpenDialog({
28+
canSelectFiles: true,
29+
canSelectFolders: true,
30+
canSelectMany: true,
31+
filters: {
32+
python: ['py'],
33+
},
34+
title: ProjectCreatorString.selectFilesOrFolders,
35+
});
36+
}
2737

28-
if (!results || results.length === 0) {
38+
if (!existingAddUri || existingAddUri.length === 0) {
2939
// User cancelled the dialog & doesn't want to add any projects
3040
return;
3141
}
3242

3343
// do we have any limitations that need to be applied here?
3444
// like selected folder not child of workspace folder?
3545

36-
const filtered = results.filter((uri) => {
46+
const filtered = existingAddUri.filter((uri) => {
3747
const p = this.pm.get(uri);
3848
if (p) {
3949
// Skip this project if there's already a project registered with exactly the same path

src/features/envCommands.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,8 +369,29 @@ export async function addPythonProjectCommand(
369369
name: resource.fsPath,
370370
rootUri: resource,
371371
};
372+
373+
// When a URI is provided (right-click in explorer), directly use the existingProjects creator
374+
const existingProjectsCreator = pc.getProjectCreators().find((c) => c.name === 'existingProjects');
375+
if (existingProjectsCreator) {
376+
try {
377+
if (existingProjectsCreator.supportsQuickCreate) {
378+
options = {
379+
...options,
380+
quickCreate: true,
381+
};
382+
}
383+
await existingProjectsCreator.create(options);
384+
return;
385+
} catch (ex) {
386+
if (ex === QuickInputButtons.Back) {
387+
return addPythonProjectCommand(resource, wm, em, pc);
388+
}
389+
throw ex;
390+
}
391+
}
372392
}
373393

394+
// If not a URI or existingProjectsCreator not found, fall back to picker
374395
const creator: PythonProjectCreator | undefined = await pickCreator(pc.getProjectCreators());
375396
if (!creator) {
376397
return;

0 commit comments

Comments
 (0)