Skip to content

Commit 79a3aab

Browse files
author
Kartik Raj
committed
Update proposed types
1 parent b922c16 commit 79a3aab

File tree

3 files changed

+91
-71
lines changed

3 files changed

+91
-71
lines changed

src/client/proposedApiTypes.ts

Lines changed: 77 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,7 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT License.
33

4-
/* eslint-disable @typescript-eslint/ban-types */
5-
/* eslint-disable @typescript-eslint/no-explicit-any */
6-
// Copyright (c) Microsoft Corporation. All rights reserved.
7-
// Licensed under the MIT License.
8-
9-
import { Event, Uri } from 'vscode';
4+
import { Event, Uri, WorkspaceFolder } from 'vscode';
105

116
// https://github.com/microsoft/vscode-python/wiki/Proposed-Environment-APIs
127

@@ -15,7 +10,7 @@ export interface IProposedExtensionAPI {
1510
/**
1611
* This event is triggered when the active environment changes.
1712
*/
18-
onDidActiveEnvironmentChanged: Event<ActiveEnvironmentChangedParams>;
13+
onDidChangeActiveEnvironment: Event<ActiveEnvironmentChangedParams>;
1914
/**
2015
* Returns the path to the python binary selected by the user or as in the settings.
2116
* This is just the path to the python binary, this does not provide activation or any
@@ -24,44 +19,41 @@ export interface IProposedExtensionAPI {
2419
* returns what ever is set for the workspace.
2520
* @param resource : Uri of a file or workspace
2621
*/
27-
getActiveEnvironmentPath(resource?: Resource): Promise<EnvPathType | undefined>;
22+
getActiveEnvironmentPath(resource?: Resource): Promise<EnvironmentPath | undefined>;
2823
/**
29-
* Returns details for the given interpreter. Details such as absolute interpreter path,
24+
* Returns details for the given python executable. Details such as absolute python executable path,
3025
* version, type (conda, pyenv, etc). Metadata such as `sysPrefix` can be found under
3126
* metadata field.
32-
* @param path : Full path to environment folder or interpreter whose details you need.
27+
* @param path : Full path to environment folder or python executable whose details you need.
3328
* @param options : [optional]
3429
* * useCache : When true, cache is checked first for any data, returns even if there
3530
* is partial data.
3631
*/
3732
getEnvironmentDetails(
38-
path: string,
33+
path: EnvironmentPath | UniquePathType,
3934
options?: EnvironmentDetailsOptions,
4035
): Promise<EnvironmentDetails | undefined>;
4136
/**
4237
* Sets the active environment path for the python extension for the resource. Configuration target
4338
* will always be the workspace folder.
44-
* @param path : Full path to environment folder or interpreter to set.
39+
* @param path : Full path to environment folder or python executable to set.
4540
* @param resource : [optional] Uri of a file ro workspace to scope to a particular workspace
4641
* folder.
4742
*/
48-
setActiveEnvironment(path: string, resource?: Resource): Promise<void>;
43+
setActiveEnvironment(path: EnvironmentPath | UniquePathType, resource?: Resource): Promise<void>;
4944
locator: {
5045
/**
5146
* Returns paths to environments that uniquely identifies an environment found by the extension
5247
* at the time of calling. This API will *not* trigger a refresh. If a refresh is going on it
5348
* will *not* wait for the refresh to finish. This will return what is known so far. To get
5449
* complete list `await` on promise returned by `getRefreshPromise()`.
55-
*
56-
* Environments lacking an interpreter are identified by environment folder paths,
57-
* whereas other envs can be identified using executable path.
5850
*/
59-
getEnvironmentPaths(): Promise<EnvPathType[] | undefined>;
51+
getEnvironmentPaths(): Promise<EnvironmentPath[] | undefined>;
6052
/**
6153
* This event is triggered when the known environment list changes, like when a environment
6254
* is found, existing environment is removed, or some details changed on an environment.
6355
*/
64-
onDidEnvironmentsChanged: Event<EnvironmentsChangedParams[]>;
56+
onDidChangeEnvironments: Event<EnvironmentsChangedParams[]>;
6557
/**
6658
* This API will re-trigger environment discovery. Extensions can wait on the returned
6759
* promise to get the updated environment list. If there is a refresh already going on
@@ -70,12 +62,12 @@ export interface IProposedExtensionAPI {
7062
* * clearCache : When true, this will clear the cache before environment refresh
7163
* is triggered.
7264
*/
73-
refreshEnvironments(options?: RefreshEnvironmentsOptions): Promise<EnvPathType[] | undefined>;
65+
refreshEnvironments(options?: RefreshEnvironmentsOptions): Promise<EnvironmentPath[] | undefined>;
7466
/**
7567
* Returns a promise for the ongoing refresh. Returns `undefined` if there are no active
7668
* refreshes going on.
7769
*/
78-
getRefreshPromise(options?: GetRefreshEnvironmentsOptions): Promise<void> | undefined;
70+
getRefreshPromise(options?: GetRefreshPromiseOptions): Promise<void> | undefined;
7971
/**
8072
* Tracks discovery progress for current list of known environments, i.e when it starts, finishes or any other relevant
8173
* stage. Note the progress for a particular query is currently not tracked or reported, this only indicates progress of
@@ -92,26 +84,11 @@ export enum Architecture {
9284
x64 = 3,
9385
}
9486

95-
export type EnvSource = KnownEnvSourceTypes | string;
96-
97-
export enum KnownEnvSourceTypes {
98-
Conda = 'Conda',
99-
Pipenv = 'PipEnv',
100-
Poetry = 'Poetry',
101-
VirtualEnv = 'VirtualEnv',
102-
Venv = 'Venv',
103-
VirtualEnvWrapper = 'VirtualEnvWrapper',
104-
Pyenv = 'Pyenv',
105-
}
87+
export type EnvSource = KnownEnvSources | string;
88+
export type KnownEnvSources = 'Conda' | 'Pipenv' | 'Poetry' | 'VirtualEnv' | 'Venv' | 'VirtualEnvWrapper' | 'Pyenv';
10689

10790
export type EnvType = KnownEnvTypes | string;
108-
109-
export enum KnownEnvTypes {
110-
VirtualEnv = 'VirtualEnv',
111-
Conda = 'Conda',
112-
Unknown = 'Unknown',
113-
Global = 'Global',
114-
}
91+
export type KnownEnvTypes = 'VirtualEnv' | 'Conda' | 'Unknown';
11592

11693
export type BasicVersionInfo = {
11794
major: number;
@@ -141,27 +118,35 @@ export type StandardVersionInfo = BasicVersionInfo & {
141118
release?: PythonVersionRelease;
142119
};
143120

121+
// To be added later:
122+
// run: {
123+
// exec: Function;
124+
// shellExec: Function;
125+
// execObservable: Function;
126+
// terminalExec: () => void;
127+
// env?: { [key: string]: string | null | undefined };
128+
// };
129+
144130
export interface EnvironmentDetails {
145131
executable: {
146132
path: string;
147133
bitness?: Architecture;
148134
sysPrefix: string;
149-
// To be added later:
150-
// run: {
151-
// exec: Function;
152-
// shellExec: Function;
153-
// execObservable: Function;
154-
// terminalExec: () => void;
155-
// env?: { [key: string]: string | null | undefined };
156-
// };
157-
};
158-
environment?: {
159-
type: EnvType;
160-
name?: string;
161-
path: string;
162-
project?: string; // Any specific project environment is created for.
163-
source: EnvSource[];
164135
};
136+
environment:
137+
| {
138+
type: EnvType;
139+
name?: string;
140+
path: string;
141+
/**
142+
* Any specific workspace folder this environment is created for.
143+
* What if that workspace folder is not opened yet? We should still provide a workspace folder so it can be filtered out.
144+
* WorkspaceFolder type won't work as it assumes the workspace is opened, hence using URI.
145+
*/
146+
workspaceFolder?: Uri;
147+
source: EnvSource[];
148+
}
149+
| undefined;
165150
version: StandardVersionInfo & {
166151
sysVersion?: string;
167152
};
@@ -173,10 +158,13 @@ export interface EnvironmentDetails {
173158
}
174159

175160
export interface EnvironmentDetailsOptions {
161+
/**
162+
* When true, cache is checked first for any data, returns even if there is partial data.
163+
*/
176164
useCache: boolean;
177165
}
178166

179-
export interface GetRefreshEnvironmentsOptions {
167+
export interface GetRefreshPromiseOptions {
180168
/**
181169
* Get refresh promise which resolves once the following stage has been reached for the list of known environments.
182170
*/
@@ -193,30 +181,55 @@ export type ProgressNotificationEvent = {
193181
stage: ProgressReportStage;
194182
};
195183

196-
export type Resource = Uri | undefined;
184+
/**
185+
* Uri of a file inside a workspace or workspace folder itself.
186+
*/
187+
export type Resource = Uri | WorkspaceFolder;
197188

198189
/**
199-
* Path to environment folder or path to interpreter that uniquely identifies an environment.
200-
* Environments lacking an interpreter are identified by environment folder paths,
201-
* whereas other envs can be identified using interpreter path.
190+
* Path to environment folder or path to python executable that uniquely identifies an environment.
191+
* Environments lacking a python executable are identified by environment folder paths,
192+
* whereas other envs can be identified using python executable path.
202193
*/
203194
export type UniquePathType = string;
204195

205-
export interface EnvPathType {
206-
path: UniquePathType;
207-
pathType: 'envFolderPath' | 'interpreterPath';
196+
export interface EnvironmentPath {
197+
pathID: UniquePathType;
198+
/**
199+
* Path to python executable that uniquely identifies an environment.
200+
* Carries `undefined` if an executable cannot uniquely identify an
201+
* environment or does not exist within the env.
202+
*/
203+
executablePath: string | undefined;
208204
}
209205

210206
export interface EnvironmentsChangedParams {
211-
path?: UniquePathType;
207+
pathID?: UniquePathType;
208+
/**
209+
* Types:
210+
* * "add": New environment is added.
211+
* * "remove": Existing environment in the list is removed.
212+
* * "update": New information found about existing environment.
213+
* * "clear-all": Remove all of the items in the list. (This is fired when a hard refresh is triggered)
214+
*/
212215
type: 'add' | 'remove' | 'update' | 'clear-all';
213216
}
214217

215218
export interface ActiveEnvironmentChangedParams {
216-
path: UniquePathType;
217-
resource?: Uri;
219+
pathID: UniquePathType;
220+
/**
221+
* Uri of a file inside a workspace or workspace folder the environment changed for.
222+
*/
223+
resource?: Resource;
218224
}
219225

220226
export interface RefreshEnvironmentsOptions {
227+
/**
228+
* When `true`, this will clear the cache before environment refresh is triggered.
229+
*/
221230
clearCache?: boolean;
231+
/**
232+
* Only trigger a refresh if it hasn't already been triggered for this session.
233+
*/
234+
ifNotTriggerredAlready?: boolean;
222235
}

src/client/pythonEnvironments/base/locator.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -146,14 +146,18 @@ interface EnvironmentMetaData {
146146
}
147147

148148
export interface LocatorEnvsChangedEvent {
149-
/**
150-
* Any details known about the environment which can be used for query.
151-
*/
152-
env?: EnvironmentMetaData;
153149
/**
154150
* Details about how the environment was modified.
155151
* */
156152
type: EnvChangeType;
153+
/**
154+
* The unique ID for the environment affected.
155+
*/
156+
pathId: UniquePathType;
157+
/**
158+
* Any other details known about the environment which can be used for query.
159+
*/
160+
env?: EnvironmentMetaData;
157161
}
158162

159163
export type EnvChangeType = 'add' | 'remove' | 'update';

src/client/pythonEnvironments/converter.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT License.
33

4-
import { EventEmitter, Event } from 'vscode';
4+
import { EventEmitter, Event, Uri } from 'vscode';
55
import { FileChangeType } from '../common/platform/fileSystemWatcher';
66
import { traceVerbose } from '../logging';
77
import { PythonEnvInfo, PythonEnvKind } from './base/info';
@@ -97,8 +97,11 @@ export class ConvertLocator implements ILocator<BasicEnvInfo> {
9797
) {
9898
if (parentLocator.onChanged) {
9999
parentLocator.onChanged((e: LocatorEnvsChangedEvent) => {
100-
const event: PythonEnvsChangedEvent = { type: this.eventKeys[`${e.type}`] };
101-
// TODO: Add translation for other events.
100+
const event: PythonEnvsChangedEvent = {
101+
type: this.eventKeys[`${e.type}`],
102+
kind: e.env?.envSources ? convertKind(e.env?.envSources[0]) : undefined,
103+
searchLocation: Uri.file(e.pathId),
104+
};
102105
this.didChange.fire(event);
103106
});
104107
}

0 commit comments

Comments
 (0)