дзеркало
https://github.com/l0ng-ai/openidea.git
синхронізовано 2026-11-09 19:25:54 +02:00
feat(interpreters): add an existing Python virtual environment as an interpreter (#2076)
Refs #1907 Add Python Interpreter → Existing environment: Browse picks the environment folder, which is resolved right away (pyvenv.cfg, bin/python or Scripts\python.exe) and added to the interpreter list as "Python X.Y (<env name>)", then remembered. Picking the folder rather than the executable keeps the environment's identity, since the native file picker resolves the venv's python symlink to the base interpreter. Works for `python -m venv` and uv environments; a broken environment shows an error instead of being added. Probing runs off the command thread. Co-authored-by: Inf1mum <[email protected]>
Цей коміт міститься в:
1 джерело
9673ccc687
коміт
32a778d28d
13 files changed
+430
-25
No files matched your search
@@ -5,7 +5,7 @@
|
||||
import { isCancelled } from "@/platform/ipc";
|
||||
import { useId, useMemo, useState } from "react";
|
||||
import { Dialog, IconButton, Input, Select, Spinner, type SelectOption, T } from "@/ui";
|
||||
import { choosePath, createVenv, rememberSdk, type VenvTool } from "./api";
|
||||
import { choosePath, createVenv, pythonInterpreter, rememberSdk, type Sdk, type VenvTool } from "./api";
|
||||
import { tildify, useDetected } from "./components";
|
||||
import styles from "./Sdk.module.css";
|
||||
import { tx } from "@/platform/i18n";
|
||||
@@ -36,6 +36,7 @@ export function AddInterpreterDialog({
|
||||
const [base, setBase] = useState<string | null>(null);
|
||||
const [location, setLocation] = useState(`${root}/.venv`);
|
||||
const [existing, setExisting] = useState<string | null>(null);
|
||||
const [browsed, setBrowsed] = useState<Sdk[]>([]);
|
||||
const [busy, setBusy] = useState<{ cancel: () => void } | null>(null);
|
||||
const [log, setLog] = useState<string[]>([]);
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
@@ -48,17 +49,46 @@ export function AddInterpreterDialog({
|
||||
detail: tildify(s.path),
|
||||
});
|
||||
const baseOptions = bases.map(toOption);
|
||||
const existingOptions = useMemo(() => {
|
||||
const opts = list.map(toOption);
|
||||
if (existing && !opts.some((o) => o.value === existing))
|
||||
opts.push({ value: existing, label: tildify(existing), detail: "custom" });
|
||||
return opts;
|
||||
}, [list, existing]);
|
||||
const existingOptions = [...browsed, ...list.filter((s) => !browsed.some((b) => b.path === s.path))].map((s) => ({
|
||||
value: s.path,
|
||||
label: `${s.name} — ${tildify(s.path)}`,
|
||||
}));
|
||||
|
||||
const checkExisting = (path: string, onResolved: (sdk: Sdk) => void) => {
|
||||
setError(null);
|
||||
let cancelled = false;
|
||||
setBusy({
|
||||
cancel: () => {
|
||||
cancelled = true;
|
||||
setBusy(null);
|
||||
},
|
||||
});
|
||||
void pythonInterpreter(path)
|
||||
.then((sdk) => {
|
||||
if (cancelled) return;
|
||||
if (!sdk) {
|
||||
setError(tx("Cannot run this Python interpreter. Select a working Python executable or virtualenv folder."));
|
||||
return;
|
||||
}
|
||||
onResolved(sdk);
|
||||
})
|
||||
.catch((e: unknown) => {
|
||||
if (!cancelled) setError(String((e as Error)?.message ?? e));
|
||||
})
|
||||
.finally(() => {
|
||||
if (!cancelled) setBusy(null);
|
||||
});
|
||||
};
|
||||
|
||||
const ok = () => {
|
||||
if (busy) return;
|
||||
setError(null);
|
||||
if (mode === "existing") {
|
||||
if (existing) onAdded(existing);
|
||||
if (existing)
|
||||
checkExisting(existing, (sdk) => {
|
||||
rememberSdk("python", sdk);
|
||||
onAdded(sdk.path);
|
||||
});
|
||||
return;
|
||||
}
|
||||
if (!baseValue || !location.trim()) return;
|
||||
@@ -74,7 +104,7 @@ export function AddInterpreterDialog({
|
||||
.finally(() => setBusy(null));
|
||||
};
|
||||
|
||||
const canOk = mode === "new" ? !!baseValue && !!location.trim() && !busy : !!existing;
|
||||
const canOk = !busy && (mode === "new" ? !!baseValue && !!location.trim() : !!existing?.trim());
|
||||
const note = {
|
||||
venv: tx("Runs “<base> -m venv <location>”; the new interpreter becomes the project interpreter."),
|
||||
conda: tx("Runs “conda create -p <location>”; the new interpreter becomes the project interpreter."),
|
||||
@@ -91,7 +121,11 @@ export function AddInterpreterDialog({
|
||||
title="Add Python Interpreter"
|
||||
width={680}
|
||||
aria-label="Add Python Interpreter"
|
||||
footerStart={busy && <Spinner size={14} label="Creating environment" />}
|
||||
footerStart={
|
||||
busy && (
|
||||
<Spinner size={14} label={mode === "existing" ? tx("Checking interpreter") : tx("Creating environment")} />
|
||||
)
|
||||
}
|
||||
buttons={[
|
||||
{ label: "OK", role: "default", disabled: !canOk, onClick: ok },
|
||||
{ label: "Cancel", role: "cancel" },
|
||||
@@ -103,6 +137,7 @@ export function AddInterpreterDialog({
|
||||
<li key={k.tool}>
|
||||
<button
|
||||
type="button"
|
||||
disabled={!!busy}
|
||||
className={`${styles.kind} ${kind === k.tool ? styles.kindOn : ""}`}
|
||||
aria-current={kind === k.tool || undefined}
|
||||
onClick={() => {
|
||||
@@ -118,11 +153,23 @@ export function AddInterpreterDialog({
|
||||
<div className={styles.form}>
|
||||
<div className={styles.radios} role="radiogroup" aria-label={tx("Environment")}>
|
||||
<label className={styles.radio}>
|
||||
<input type="radio" name="env" checked={mode === "new"} onChange={() => setMode("new")} />{" "}
|
||||
<input
|
||||
type="radio"
|
||||
name="env"
|
||||
disabled={!!busy}
|
||||
checked={mode === "new"}
|
||||
onChange={() => setMode("new")}
|
||||
/>{" "}
|
||||
<T>New environment</T>
|
||||
</label>
|
||||
<label className={styles.radio}>
|
||||
<input type="radio" name="env" checked={mode === "existing"} onChange={() => setMode("existing")} />{" "}
|
||||
<input
|
||||
type="radio"
|
||||
name="env"
|
||||
disabled={!!busy}
|
||||
checked={mode === "existing"}
|
||||
onChange={() => setMode("existing")}
|
||||
/>{" "}
|
||||
<T>Existing environment</T>
|
||||
</label>
|
||||
</div>
|
||||
@@ -179,10 +226,12 @@ export function AddInterpreterDialog({
|
||||
<label htmlFor={ids.existing}>
|
||||
<T>Interpreter:</T>
|
||||
</label>
|
||||
<div className={styles.pathRow}>
|
||||
<div className={`${styles.pathRow} ${styles.existingPathRow}`}>
|
||||
<Select
|
||||
id={ids.existing}
|
||||
className={styles.pathInput}
|
||||
value={existing}
|
||||
disabled={!!busy}
|
||||
options={existingOptions}
|
||||
onChange={setExisting}
|
||||
placeholder={loading ? "Detecting…" : "Select an interpreter"}
|
||||
@@ -192,9 +241,18 @@ export function AddInterpreterDialog({
|
||||
icon="folder-open"
|
||||
size="small"
|
||||
title="Browse…"
|
||||
disabled={!!busy}
|
||||
onClick={async () => {
|
||||
const p = await choosePath({ directory: false, title: "Select Python Interpreter" });
|
||||
if (p) setExisting(p);
|
||||
const p = await choosePath({
|
||||
directory: true,
|
||||
title: "Select Python Environment Folder",
|
||||
defaultPath: root,
|
||||
});
|
||||
if (p)
|
||||
checkExisting(p, (sdk) => {
|
||||
setBrowsed((current) => [sdk, ...current.filter((s) => s.path !== sdk.path)]);
|
||||
setExisting(sdk.path);
|
||||
});
|
||||
}}
|
||||
/>
|
||||
</div>
|
||||
|
||||
Reference in new issue
Заблокувати користувача