Skip to content

Commit f57ffbe

Browse files
authored
Merge branch 'main' into copilot/fix-330
2 parents 69f688a + e9e1e38 commit f57ffbe

File tree

8 files changed

+73
-25
lines changed

8 files changed

+73
-25
lines changed

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -457,12 +457,12 @@
457457
{
458458
"command": "python-envs.addPythonProject",
459459
"group": "inline",
460-
"when": "explorerViewletVisible && explorerResourceIsFolder"
460+
"when": "explorerViewletVisible && explorerResourceIsFolder && !python-envs:isExistingProject"
461461
},
462462
{
463463
"command": "python-envs.addPythonProject",
464464
"group": "inline",
465-
"when": "explorerViewletVisible && resourceExtname == .py"
465+
"when": "explorerViewletVisible && resourceExtname == .py && !python-envs:isExistingProject"
466466
}
467467
],
468468
"editor/title/run": [

package.nls.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
"python-envs.terminal.revertStartupScriptChanges.title": "Revert Shell Startup Script Changes",
1414
"python-envs.setEnvManager.title": "Set Environment Manager",
1515
"python-envs.setPkgManager.title": "Set Package Manager",
16-
"python-envs.addPythonProject.title": "Add Python Project",
16+
"python-envs.addPythonProject.title": "Add as Python Project",
1717
"python-envs.removePythonProject.title": "Remove Python Project",
1818
"python-envs.copyEnvPath.title": "Copy Environment Path",
1919
"python-envs.copyProjectPath.title": "Copy Project Path",
@@ -37,4 +37,4 @@
3737
"python-envs.uninstallPackage.title": "Uninstall Package",
3838
"python.languageModelTools.python_environment.userDescription": "Get Python environment info for a file or path, including version, packages, and the command to run it.",
3939
"python.languageModelTools.python_install_package.userDescription": "Installs Python packages in the given workspace."
40-
}
40+
}

src/extension.ts

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { commands, ExtensionContext, LogOutputChannel, Terminal, Uri } from 'vscode';
1+
import { commands, ExtensionContext, LogOutputChannel, Terminal, Uri, window } from 'vscode';
22
import { PythonEnvironment, PythonEnvironmentApi } from './api';
33
import { ensureCorrectVersion } from './common/extVersion';
44
import { registerTools } from './common/lm.apis';
@@ -13,7 +13,6 @@ import {
1313
activeTerminal,
1414
createLogOutputChannel,
1515
onDidChangeActiveTerminal,
16-
onDidChangeActiveTextEditor,
1716
onDidChangeTerminalShellIntegration,
1817
} from './common/window.apis';
1918
import { createManagerReady } from './features/common/managerReady';
@@ -88,6 +87,14 @@ export async function activate(context: ExtensionContext): Promise<PythonEnviron
8887
const projectManager: PythonProjectManager = new PythonProjectManagerImpl();
8988
context.subscriptions.push(projectManager);
9089

90+
// Helper function to check if a resource is an existing Python project
91+
const isExistingProject = (uri: Uri | undefined): boolean => {
92+
if (!uri) {
93+
return false;
94+
}
95+
return projectManager.get(uri) !== undefined;
96+
};
97+
9198
const envVarManager: EnvVarManager = new PythonEnvVariableManager(projectManager);
9299
context.subscriptions.push(envVarManager);
93100

@@ -194,6 +201,10 @@ export async function activate(context: ExtensionContext): Promise<PythonEnviron
194201
await setPackageManagerCommand(envManagers, projectManager);
195202
}),
196203
commands.registerCommand('python-envs.addPythonProject', async (resource) => {
204+
// Set context to show/hide menu item depending on whether the resource is already a Python project
205+
if (resource instanceof Uri) {
206+
commands.executeCommand('setContext', 'python-envs:isExistingProject', isExistingProject(resource));
207+
}
197208
await addPythonProjectCommand(resource, projectManager, envManagers, projectCreators);
198209
}),
199210
commands.registerCommand('python-envs.removePythonProject', async (item) => {
@@ -254,7 +265,7 @@ export async function activate(context: ExtensionContext): Promise<PythonEnviron
254265
}
255266
}
256267
}),
257-
onDidChangeActiveTextEditor(async () => {
268+
window.onDidChangeActiveTextEditor(async () => {
258269
updateViewsAndStatus(statusBar, workspaceView, managerView, api);
259270
}),
260271
envManagers.onDidChangeEnvironment(async () => {

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
@@ -378,8 +378,29 @@ export async function addPythonProjectCommand(
378378
name: resource.fsPath,
379379
rootUri: resource,
380380
};
381+
382+
// When a URI is provided (right-click in explorer), directly use the existingProjects creator
383+
const existingProjectsCreator = pc.getProjectCreators().find((c) => c.name === 'existingProjects');
384+
if (existingProjectsCreator) {
385+
try {
386+
if (existingProjectsCreator.supportsQuickCreate) {
387+
options = {
388+
...options,
389+
quickCreate: true,
390+
};
391+
}
392+
await existingProjectsCreator.create(options);
393+
return;
394+
} catch (ex) {
395+
if (ex === QuickInputButtons.Back) {
396+
return addPythonProjectCommand(resource, wm, em, pc);
397+
}
398+
throw ex;
399+
}
400+
}
381401
}
382402

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

src/managers/builtin/sysPythonManager.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,6 @@ export class SysPythonManager implements EnvironmentManager {
199199
// This environment is unknown. Resolve it.
200200
const resolved = await resolveSystemPythonEnvironmentPath(context.fsPath, this.nativeFinder, this.api, this);
201201
if (resolved) {
202-
// HERE IT GOT TOO MANY
203202
// This is just like finding a new environment or creating a new one.
204203
// Add it to collection, and trigger the added event.
205204

src/managers/pyenv/main.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,14 @@ export async function registerPyenvFeatures(
1313
const api: PythonEnvironmentApi = await getPythonApi();
1414

1515
try {
16-
await getPyenv(nativeFinder);
17-
18-
const mgr = new PyEnvManager(nativeFinder, api);
19-
disposables.push(mgr, api.registerEnvironmentManager(mgr));
16+
const pyenv = await getPyenv(nativeFinder);
17+
18+
if (pyenv) {
19+
const mgr = new PyEnvManager(nativeFinder, api);
20+
disposables.push(mgr, api.registerEnvironmentManager(mgr));
21+
} else {
22+
traceInfo('Pyenv not found, turning off pyenv features.');
23+
}
2024
} catch (ex) {
2125
traceInfo('Pyenv not found, turning off pyenv features.', ex);
2226
}

src/managers/pyenv/pyenvUtils.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -225,8 +225,11 @@ export async function refreshPyenv(
225225
.filter((e) => !isNativeEnvInfo(e))
226226
.map((e) => e as NativeEnvManagerInfo)
227227
.filter((e) => e.tool.toLowerCase() === 'pyenv');
228-
pyenv = managers[0].executable;
229-
await setPyenv(pyenv);
228+
229+
if (managers.length > 0) {
230+
pyenv = managers[0].executable;
231+
await setPyenv(pyenv);
232+
}
230233
}
231234

232235
const envs = data

0 commit comments

Comments
 (0)