Skip to content

Commit 2d0df02

Browse files
committed
Add option to control if environment is selected after creation.
1 parent 2442dfb commit 2d0df02

File tree

3 files changed

+63
-23
lines changed

3 files changed

+63
-23
lines changed

src/client/pythonEnvironments/creation/createEnvApi.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,12 @@ import { IInterpreterQuickPick } from '../../interpreter/configuration/types';
99
import { getCreationEvents, handleCreateEnvironmentCommand } from './createEnvironment';
1010
import { condaCreationProvider } from './provider/condaCreationProvider';
1111
import { VenvCreationProvider } from './provider/venvCreationProvider';
12-
import { CreateEnvironmentOptions, CreateEnvironmentProvider, CreateEnvironmentResult } from './types';
12+
import {
13+
CreateEnvironmentExitedEventArgs,
14+
CreateEnvironmentOptions,
15+
CreateEnvironmentProvider,
16+
CreateEnvironmentResult,
17+
} from './types';
1318
import { showInformationMessage } from '../../common/vscodeApis/windowApis';
1419
import { CreateEnv } from '../../common/utils/localize';
1520

@@ -62,10 +67,10 @@ export function registerCreateEnvironmentFeatures(
6267
disposables.push(registerCreateEnvironmentProvider(new VenvCreationProvider(interpreterQuickPick)));
6368
disposables.push(registerCreateEnvironmentProvider(condaCreationProvider()));
6469
disposables.push(
65-
onCreateEnvironmentExited(async (e: CreateEnvironmentResult | undefined) => {
66-
if (e && e.path) {
67-
await interpreterPathService.update(e.uri, ConfigurationTarget.WorkspaceFolder, e.path);
68-
showInformationMessage(`${CreateEnv.informEnvCreation} ${pathUtils.getDisplayName(e.path)}`);
70+
onCreateEnvironmentExited(async (e: CreateEnvironmentExitedEventArgs) => {
71+
if (e.result?.path && e.options?.selectEnvironment) {
72+
await interpreterPathService.update(e.result.uri, ConfigurationTarget.WorkspaceFolder, e.result.path);
73+
showInformationMessage(`${CreateEnv.informEnvCreation} ${pathUtils.getDisplayName(e.result.path)}`);
6974
}
7075
}),
7176
);

src/client/pythonEnvironments/creation/createEnvironment.ts

Lines changed: 34 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,30 +10,36 @@ import {
1010
showQuickPickWithBack,
1111
} from '../../common/vscodeApis/windowApis';
1212
import { traceError, traceVerbose } from '../../logging';
13-
import { CreateEnvironmentOptions, CreateEnvironmentProvider, CreateEnvironmentResult } from './types';
13+
import {
14+
CreateEnvironmentExitedEventArgs,
15+
CreateEnvironmentOptions,
16+
CreateEnvironmentProvider,
17+
CreateEnvironmentResult,
18+
CreateEnvironmentStartedEventArgs,
19+
} from './types';
1420

15-
const onCreateEnvironmentStartedEvent = new EventEmitter<void>();
16-
const onCreateEnvironmentExitedEvent = new EventEmitter<CreateEnvironmentResult | undefined>();
21+
const onCreateEnvironmentStartedEvent = new EventEmitter<CreateEnvironmentStartedEventArgs>();
22+
const onCreateEnvironmentExitedEvent = new EventEmitter<CreateEnvironmentExitedEventArgs>();
1723

1824
let startedEventCount = 0;
1925

2026
function isBusyCreatingEnvironment(): boolean {
2127
return startedEventCount > 0;
2228
}
2329

24-
function fireStartedEvent(): void {
25-
onCreateEnvironmentStartedEvent.fire();
30+
function fireStartedEvent(options?: CreateEnvironmentOptions): void {
31+
onCreateEnvironmentStartedEvent.fire({ options });
2632
startedEventCount += 1;
2733
}
2834

29-
function fireExitedEvent(result: CreateEnvironmentResult | undefined): void {
30-
onCreateEnvironmentExitedEvent.fire(result);
35+
function fireExitedEvent(result?: CreateEnvironmentResult, options?: CreateEnvironmentOptions, error?: unknown): void {
36+
onCreateEnvironmentExitedEvent.fire({ result, options, error });
3137
startedEventCount -= 1;
3238
}
3339

3440
export function getCreationEvents(): {
35-
onCreateEnvironmentStarted: Event<void>;
36-
onCreateEnvironmentExited: Event<CreateEnvironmentResult | undefined>;
41+
onCreateEnvironmentStarted: Event<CreateEnvironmentStartedEventArgs>;
42+
onCreateEnvironmentExited: Event<CreateEnvironmentExitedEventArgs>;
3743
isCreatingEnvironment: () => boolean;
3844
} {
3945
return {
@@ -45,14 +51,12 @@ export function getCreationEvents(): {
4551

4652
async function createEnvironment(
4753
provider: CreateEnvironmentProvider,
48-
options: CreateEnvironmentOptions = {
49-
ignoreSourceControl: true,
50-
installPackages: true,
51-
},
54+
options: CreateEnvironmentOptions,
5255
): Promise<CreateEnvironmentResult | undefined> {
5356
let result: CreateEnvironmentResult | undefined;
57+
let err: unknown | undefined;
5458
try {
55-
fireStartedEvent();
59+
fireStartedEvent(options);
5660
result = await provider.createEnvironment(options);
5761
} catch (ex) {
5862
if (ex === QuickInputButtons.Back) {
@@ -61,9 +65,10 @@ async function createEnvironment(
6165
return undefined;
6266
}
6367
}
64-
throw ex;
68+
err = ex;
69+
throw err;
6570
} finally {
66-
fireExitedEvent(result);
71+
fireExitedEvent(result, options, err);
6772
}
6873
return result;
6974
}
@@ -110,17 +115,28 @@ async function showCreateEnvironmentQuickPick(
110115
return undefined;
111116
}
112117

118+
function getOptionsWithDefaults(options?: CreateEnvironmentOptions): CreateEnvironmentOptions {
119+
return {
120+
installPackages: true,
121+
ignoreSourceControl: true,
122+
showBackButton: false,
123+
selectEnvironment: true,
124+
...options,
125+
};
126+
}
127+
113128
export async function handleCreateEnvironmentCommand(
114129
providers: readonly CreateEnvironmentProvider[],
115130
options?: CreateEnvironmentOptions,
116131
): Promise<CreateEnvironmentResult | undefined> {
132+
const optionsWithDefaults = getOptionsWithDefaults(options);
117133
let selectedProvider: CreateEnvironmentProvider | undefined;
118134
const envTypeStep = new MultiStepNode(
119135
undefined,
120136
async (context?: MultiStepAction) => {
121137
if (providers.length > 0) {
122138
try {
123-
selectedProvider = await showCreateEnvironmentQuickPick(providers, options);
139+
selectedProvider = await showCreateEnvironmentQuickPick(providers, optionsWithDefaults);
124140
} catch (ex) {
125141
if (ex === MultiStepAction.Back || ex === MultiStepAction.Cancel) {
126142
return ex;
@@ -152,7 +168,7 @@ export async function handleCreateEnvironmentCommand(
152168
}
153169
if (selectedProvider) {
154170
try {
155-
result = await createEnvironment(selectedProvider, options);
171+
result = await createEnvironment(selectedProvider, optionsWithDefaults);
156172
} catch (ex) {
157173
if (ex === MultiStepAction.Back || ex === MultiStepAction.Cancel) {
158174
return ex;

src/client/pythonEnvironments/creation/types.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,18 @@ import { Progress, Uri } from 'vscode';
66
export interface CreateEnvironmentProgress extends Progress<{ message?: string; increment?: number }> {}
77

88
export interface CreateEnvironmentOptions {
9+
// Default `true`. If `true`, the environment creation handler is expected to install packages.
910
installPackages?: boolean;
11+
12+
// Default `true`. If `true`, the environment creation provider is expected to add the environment to ignore list
13+
// for the source control.
1014
ignoreSourceControl?: boolean;
15+
16+
// Default `false`. If `true` the creation provider should show back button when showing QuickPick or QuickInput.
1117
showBackButton?: boolean;
18+
19+
// Default `true`. If `true`, the environment will be selected as the environment to be used for the workspace.
20+
selectEnvironment?: boolean;
1221
}
1322

1423
export interface CreateEnvironmentResult {
@@ -17,6 +26,16 @@ export interface CreateEnvironmentResult {
1726
action?: 'Back' | 'Cancel';
1827
}
1928

29+
export interface CreateEnvironmentStartedEventArgs {
30+
options: CreateEnvironmentOptions | undefined;
31+
}
32+
33+
export interface CreateEnvironmentExitedEventArgs {
34+
result: CreateEnvironmentResult | undefined;
35+
error?: unknown;
36+
options: CreateEnvironmentOptions | undefined;
37+
}
38+
2039
export interface CreateEnvironmentProvider {
2140
createEnvironment(options?: CreateEnvironmentOptions): Promise<CreateEnvironmentResult | undefined>;
2241
name: string;

0 commit comments

Comments
 (0)