Skip to content

Commit b1a70a2

Browse files
committed
remove duplication
1 parent 1723072 commit b1a70a2

File tree

2 files changed

+22
-37
lines changed

2 files changed

+22
-37
lines changed

src/common/utils/pathUtils.ts

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

4+
export function convertNotebookCellUriToNotebookUri(uri: Uri): Uri {
5+
if (uri.scheme === 'vscode-notebook-cell') {
6+
return Uri.from({
7+
scheme: 'vscode-notebook',
8+
path: uri.path,
9+
authority: uri.authority,
10+
});
11+
}
12+
return uri;
13+
}
14+
315
export function normalizePath(path: string): string {
416
const path1 = path.replace(/\\/g, '/');
517
if (isWindows()) {

src/features/pythonApi.ts

Lines changed: 10 additions & 37 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 { convertNotebookCellUriToNotebookUri } from '../common/utils/pathUtils';
5051

5152
class PythonEnvironmentApiImpl implements PythonEnvironmentApi {
5253
private readonly _onDidChangeEnvironments = new EventEmitter<DidChangeEnvironmentsEventArgs>();
@@ -209,44 +210,24 @@ class PythonEnvironmentApiImpl implements PythonEnvironmentApi {
209210
}
210211
onDidChangeEnvironments: Event<DidChangeEnvironmentsEventArgs> = this._onDidChangeEnvironments.event;
211212
setEnvironment(scope: SetEnvironmentScope, environment?: PythonEnvironment): Promise<void> {
212-
let currentScope: SetEnvironmentScope = scope;
213+
let currentScope = scope;
214+
213215
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-
});
216+
currentScope = convertNotebookCellUriToNotebookUri(scope);
219217
}
220218

221219
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-
}
220+
currentScope = scope.map((s) =>
221+
s instanceof Uri && s.scheme === 'vscode-notebook-cell' ? convertNotebookCellUriToNotebookUri(s) : s,
222+
);
238223
}
239224

240225
return this.envManagers.setEnvironment(currentScope, environment);
241226
}
242227
async getEnvironment(scope: GetEnvironmentScope): Promise<PythonEnvironment | undefined> {
243228
let currentScope: GetEnvironmentScope = scope;
244229
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-
});
230+
currentScope = convertNotebookCellUriToNotebookUri(scope);
250231
}
251232
return this.envManagers.getEnvironment(currentScope);
252233
}
@@ -321,11 +302,7 @@ class PythonEnvironmentApiImpl implements PythonEnvironmentApi {
321302
getPythonProject(uri: Uri): PythonProject | undefined {
322303
let currentUri: GetEnvironmentScope = uri;
323304
if (uri.scheme === 'vscode-notebook-cell') {
324-
currentUri = Uri.from({
325-
scheme: 'vscode-notebook',
326-
path: uri.path,
327-
authority: uri.authority,
328-
});
305+
currentUri = convertNotebookCellUriToNotebookUri(uri);
329306
}
330307
return this.projectManager.get(currentUri);
331308
}
@@ -372,11 +349,7 @@ class PythonEnvironmentApiImpl implements PythonEnvironmentApi {
372349
): Promise<{ [key: string]: string | undefined }> {
373350
let currentUri: GetEnvironmentScope = uri;
374351
if (uri.scheme === 'vscode-notebook-cell') {
375-
currentUri = Uri.from({
376-
scheme: 'vscode-notebook',
377-
path: uri.path,
378-
authority: uri.authority,
379-
});
352+
currentUri = convertNotebookCellUriToNotebookUri(uri);
380353
}
381354
return this.envVarManager.getEnvironmentVariables(currentUri, overrides, baseEnvVar);
382355
}

0 commit comments

Comments
 (0)