Skip to content

Commit 1723072

Browse files
committed
add handling for notebook-cells as inputs to API
1 parent b275e8b commit 1723072

File tree

1 file changed

+79
-11
lines changed

1 file changed

+79
-11
lines changed

src/features/pythonApi.ts

Lines changed: 79 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -171,32 +171,84 @@ class PythonEnvironmentApiImpl implements PythonEnvironmentApi {
171171
await Promise.all(this.envManagers.managers.map((manager) => manager.refresh(scope)));
172172
return Promise.resolve();
173173
}
174-
const manager = this.envManagers.getEnvironmentManager(scope);
174+
let currentScope: GetEnvironmentScope = scope;
175+
if (scope instanceof Uri && scope.scheme === 'vscode-notebook-cell') {
176+
currentScope = Uri.from({
177+
scheme: 'vscode-notebook',
178+
path: scope.path,
179+
authority: scope.authority,
180+
});
181+
}
182+
const manager = this.envManagers.getEnvironmentManager(currentScope);
175183
if (!manager) {
176-
return Promise.reject(new Error(`No environment manager found for: ${scope.fsPath}`));
184+
return Promise.reject(new Error(`No environment manager found for: ${currentScope.fsPath}`));
177185
}
178-
return manager.refresh(scope);
186+
return manager.refresh(currentScope);
179187
}
180188
async getEnvironments(scope: GetEnvironmentsScope): Promise<PythonEnvironment[]> {
181-
if (scope === 'all' || scope === 'global') {
182-
const promises = this.envManagers.managers.map((manager) => manager.getEnvironments(scope));
189+
let currentScope: GetEnvironmentsScope = scope;
190+
if (currentScope instanceof Uri && currentScope.scheme === 'vscode-notebook-cell') {
191+
currentScope = Uri.from({
192+
scheme: 'vscode-notebook',
193+
path: currentScope.path,
194+
authority: currentScope.authority,
195+
});
196+
}
197+
if (currentScope === 'all' || currentScope === 'global') {
198+
const promises = this.envManagers.managers.map((manager) => manager.getEnvironments(currentScope));
183199
const items = await Promise.all(promises);
184200
return items.flat();
185201
}
186-
const manager = this.envManagers.getEnvironmentManager(scope);
202+
const manager = this.envManagers.getEnvironmentManager(currentScope);
187203
if (!manager) {
188204
return [];
189205
}
190206

191-
const items = await manager.getEnvironments(scope);
207+
const items = await manager.getEnvironments(currentScope);
192208
return items;
193209
}
194210
onDidChangeEnvironments: Event<DidChangeEnvironmentsEventArgs> = this._onDidChangeEnvironments.event;
195211
setEnvironment(scope: SetEnvironmentScope, environment?: PythonEnvironment): Promise<void> {
196-
return this.envManagers.setEnvironment(scope, environment);
212+
let currentScope: SetEnvironmentScope = scope;
213+
if (scope instanceof Uri && scope.scheme === 'vscode-notebook-cell') {
214+
currentScope = Uri.from({
215+
scheme: 'vscode-notebook',
216+
path: scope.path,
217+
authority: scope.authority,
218+
});
219+
}
220+
221+
if (Array.isArray(scope) && scope.length > 0) {
222+
// if scope is an array of Uri, go through each item and check if it is a notebook cell Uri
223+
// if it is, convert it to notebook Uri and push all items to the currentScope
224+
currentScope = [];
225+
for (const s of scope) {
226+
if (s instanceof Uri && s.scheme === 'vscode-notebook-cell') {
227+
currentScope.push(
228+
Uri.from({
229+
scheme: 'vscode-notebook',
230+
path: s.path,
231+
authority: s.authority,
232+
}),
233+
);
234+
} else {
235+
currentScope.push(s);
236+
}
237+
}
238+
}
239+
240+
return this.envManagers.setEnvironment(currentScope, environment);
197241
}
198242
async getEnvironment(scope: GetEnvironmentScope): Promise<PythonEnvironment | undefined> {
199-
return this.envManagers.getEnvironment(scope);
243+
let currentScope: GetEnvironmentScope = scope;
244+
if (scope instanceof Uri && scope.scheme === 'vscode-notebook-cell') {
245+
currentScope = Uri.from({
246+
scheme: 'vscode-notebook',
247+
path: scope.path,
248+
authority: scope.authority,
249+
});
250+
}
251+
return this.envManagers.getEnvironment(currentScope);
200252
}
201253
onDidChangeEnvironment: Event<DidChangeEnvironmentEventArgs> = this._onDidChangeEnvironment.event;
202254
async resolveEnvironment(context: ResolveEnvironmentContext): Promise<PythonEnvironment | undefined> {
@@ -267,7 +319,15 @@ class PythonEnvironmentApiImpl implements PythonEnvironmentApi {
267319
}
268320
onDidChangePythonProjects: Event<DidChangePythonProjectsEventArgs> = this._onDidChangePythonProjects.event;
269321
getPythonProject(uri: Uri): PythonProject | undefined {
270-
return this.projectManager.get(uri);
322+
let currentUri: GetEnvironmentScope = uri;
323+
if (uri.scheme === 'vscode-notebook-cell') {
324+
currentUri = Uri.from({
325+
scheme: 'vscode-notebook',
326+
path: uri.path,
327+
authority: uri.authority,
328+
});
329+
}
330+
return this.projectManager.get(currentUri);
271331
}
272332
registerPythonProjectCreator(creator: PythonProjectCreator): Disposable {
273333
return this.projectCreators.registerPythonProjectCreator(creator);
@@ -310,7 +370,15 @@ class PythonEnvironmentApiImpl implements PythonEnvironmentApi {
310370
overrides?: ({ [key: string]: string | undefined } | Uri)[],
311371
baseEnvVar?: { [key: string]: string | undefined },
312372
): Promise<{ [key: string]: string | undefined }> {
313-
return this.envVarManager.getEnvironmentVariables(uri, overrides, baseEnvVar);
373+
let currentUri: GetEnvironmentScope = uri;
374+
if (uri.scheme === 'vscode-notebook-cell') {
375+
currentUri = Uri.from({
376+
scheme: 'vscode-notebook',
377+
path: uri.path,
378+
authority: uri.authority,
379+
});
380+
}
381+
return this.envVarManager.getEnvironmentVariables(currentUri, overrides, baseEnvVar);
314382
}
315383
}
316384

0 commit comments

Comments
 (0)