Skip to content

Commit d3d4575

Browse files
authored
add handling for notebook-cells as inputs to API (#293)
fixes #278
1 parent 9dd0fd9 commit d3d4575

File tree

2 files changed

+36
-13
lines changed

2 files changed

+36
-13
lines changed

src/common/utils/pathUtils.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,24 @@
1+
import { Uri } from 'vscode';
12
import { isWindows } from '../../managers/common/utils';
23

4+
export function checkUri(scope?: Uri | Uri[] | string): Uri | Uri[] | string | undefined {
5+
if (scope instanceof Uri) {
6+
if (scope.scheme === 'vscode-notebook-cell') {
7+
return Uri.from({
8+
scheme: 'vscode-notebook',
9+
path: scope.path,
10+
authority: scope.authority,
11+
});
12+
}
13+
}
14+
if (Array.isArray(scope)) {
15+
return scope.map((item) => {
16+
return checkUri(item) as Uri;
17+
});
18+
}
19+
return scope;
20+
}
21+
322
export function normalizePath(path: string): string {
423
const path1 = path.replace(/\\/g, '/');
524
if (isWindows()) {

src/features/pythonApi.ts

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ import { runAsTask } from './execution/runAsTask';
4747
import { runInTerminal } from './terminal/runInTerminal';
4848
import { runInBackground } from './execution/runInBackground';
4949
import { EnvVarManager } from './execution/envVariableManager';
50+
import { checkUri } from '../common/utils/pathUtils';
5051

5152
class PythonEnvironmentApiImpl implements PythonEnvironmentApi {
5253
private readonly _onDidChangeEnvironments = new EventEmitter<DidChangeEnvironmentsEventArgs>();
@@ -167,36 +168,39 @@ class PythonEnvironmentApiImpl implements PythonEnvironmentApi {
167168
return manager.remove(environment);
168169
}
169170
async refreshEnvironments(scope: RefreshEnvironmentsScope): Promise<void> {
170-
if (scope === undefined) {
171-
await Promise.all(this.envManagers.managers.map((manager) => manager.refresh(scope)));
171+
const currentScope = checkUri(scope) as RefreshEnvironmentsScope;
172+
173+
if (currentScope === undefined) {
174+
await Promise.all(this.envManagers.managers.map((manager) => manager.refresh(currentScope)));
172175
return Promise.resolve();
173176
}
174-
const manager = this.envManagers.getEnvironmentManager(scope);
177+
const manager = this.envManagers.getEnvironmentManager(currentScope);
175178
if (!manager) {
176-
return Promise.reject(new Error(`No environment manager found for: ${scope.fsPath}`));
179+
return Promise.reject(new Error(`No environment manager found for: ${currentScope.fsPath}`));
177180
}
178-
return manager.refresh(scope);
181+
return manager.refresh(currentScope);
179182
}
180183
async getEnvironments(scope: GetEnvironmentsScope): Promise<PythonEnvironment[]> {
181-
if (scope === 'all' || scope === 'global') {
182-
const promises = this.envManagers.managers.map((manager) => manager.getEnvironments(scope));
184+
const currentScope = checkUri(scope) as GetEnvironmentsScope;
185+
if (currentScope === 'all' || currentScope === 'global') {
186+
const promises = this.envManagers.managers.map((manager) => manager.getEnvironments(currentScope));
183187
const items = await Promise.all(promises);
184188
return items.flat();
185189
}
186-
const manager = this.envManagers.getEnvironmentManager(scope);
190+
const manager = this.envManagers.getEnvironmentManager(currentScope);
187191
if (!manager) {
188192
return [];
189193
}
190194

191-
const items = await manager.getEnvironments(scope);
195+
const items = await manager.getEnvironments(currentScope);
192196
return items;
193197
}
194198
onDidChangeEnvironments: Event<DidChangeEnvironmentsEventArgs> = this._onDidChangeEnvironments.event;
195199
setEnvironment(scope: SetEnvironmentScope, environment?: PythonEnvironment): Promise<void> {
196-
return this.envManagers.setEnvironment(scope, environment);
200+
return this.envManagers.setEnvironment(checkUri(scope) as SetEnvironmentScope, environment);
197201
}
198202
async getEnvironment(scope: GetEnvironmentScope): Promise<PythonEnvironment | undefined> {
199-
return this.envManagers.getEnvironment(scope);
203+
return this.envManagers.getEnvironment(checkUri(scope) as GetEnvironmentScope);
200204
}
201205
onDidChangeEnvironment: Event<DidChangeEnvironmentEventArgs> = this._onDidChangeEnvironment.event;
202206
async resolveEnvironment(context: ResolveEnvironmentContext): Promise<PythonEnvironment | undefined> {
@@ -267,7 +271,7 @@ class PythonEnvironmentApiImpl implements PythonEnvironmentApi {
267271
}
268272
onDidChangePythonProjects: Event<DidChangePythonProjectsEventArgs> = this._onDidChangePythonProjects.event;
269273
getPythonProject(uri: Uri): PythonProject | undefined {
270-
return this.projectManager.get(uri);
274+
return this.projectManager.get(checkUri(uri) as Uri);
271275
}
272276
registerPythonProjectCreator(creator: PythonProjectCreator): Disposable {
273277
return this.projectCreators.registerPythonProjectCreator(creator);
@@ -310,7 +314,7 @@ class PythonEnvironmentApiImpl implements PythonEnvironmentApi {
310314
overrides?: ({ [key: string]: string | undefined } | Uri)[],
311315
baseEnvVar?: { [key: string]: string | undefined },
312316
): Promise<{ [key: string]: string | undefined }> {
313-
return this.envVarManager.getEnvironmentVariables(uri, overrides, baseEnvVar);
317+
return this.envVarManager.getEnvironmentVariables(checkUri(uri) as Uri, overrides, baseEnvVar);
314318
}
315319
}
316320

0 commit comments

Comments
 (0)