Skip to content

Commit c6a890b

Browse files
committed
chore: refactor
1 parent 7f18fb7 commit c6a890b

File tree

3 files changed

+33
-57
lines changed

3 files changed

+33
-57
lines changed

src/common/utils/pathUtils.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import * as path from 'path';
12
import { Uri } from 'vscode';
23
import { isWindows } from '../../managers/common/utils';
34

@@ -19,8 +20,8 @@ export function checkUri(scope?: Uri | Uri[] | string): Uri | Uri[] | string | u
1920
return scope;
2021
}
2122

22-
export function normalizePath(path: string): string {
23-
const path1 = path.replace(/\\/g, '/');
23+
export function normalizePath(fsPath: string): string {
24+
const path1 = fsPath.replace(/\\/g, '/');
2425
if (isWindows()) {
2526
return path1.toLowerCase();
2627
}
@@ -38,14 +39,11 @@ export function getResourceUri(resourcePath: string, root?: string): Uri | undef
3839
return Uri.parse(normalizedPath);
3940
}
4041

41-
const path = require('path');
4242
if (!path.isAbsolute(resourcePath) && root) {
4343
const absolutePath = path.resolve(root, resourcePath);
4444
return Uri.file(absolutePath);
4545
}
46-
47-
const pathWithForwardSlashes = resourcePath.replace(/\\/g, '/');
48-
return Uri.file(pathWithForwardSlashes);
46+
return Uri.file(resourcePath);
4947
} catch (_err) {
5048
return undefined;
5149
}

src/managers/builtin/venvManager.ts

Lines changed: 15 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,10 @@ import * as path from 'path';
3636
import { NativePythonFinder } from '../common/nativePythonFinder';
3737
import { PYTHON_EXTENSION_ID } from '../../common/constants';
3838
import { createDeferred, Deferred } from '../../common/utils/deferred';
39-
import { getLatest, isWindows, shortVersion, sortEnvironments } from '../common/utils';
39+
import { getLatest, shortVersion, sortEnvironments } from '../common/utils';
4040
import { withProgress } from '../../common/window.apis';
4141
import { VenvManagerStrings } from '../../common/localize';
4242
import { showErrorMessage } from '../../common/errors/utils';
43-
import { normalizePath } from '../../common/utils/pathUtils';
4443

4544
export class VenvManager implements EnvironmentManager {
4645
private collection: PythonEnvironment[] = [];
@@ -202,21 +201,12 @@ export class VenvManager implements EnvironmentManager {
202201
const changed: Uri[] = [];
203202
this.fsPathToEnv.forEach((env, uri) => {
204203
if (env.environmentPath.fsPath === environment.environmentPath.fsPath) {
205-
this.deleteFsPathToEnv(uri);
204+
this.fsPathToEnv.delete(uri);
206205
changed.push(Uri.file(uri));
207206
}
208207
});
209208
return changed;
210209
}
211-
private setFsPathToEnv(fsPath: string, environment: PythonEnvironment): void {
212-
this.fsPathToEnv.set(isWindows() ? normalizePath(fsPath) : fsPath, environment);
213-
}
214-
private getFsPathToEnv(fsPath: string): PythonEnvironment | undefined {
215-
return this.fsPathToEnv.get(isWindows() ? normalizePath(fsPath) : fsPath);
216-
}
217-
private deleteFsPathToEnv(fsPath: string): void {
218-
this.fsPathToEnv.delete(isWindows() ? normalizePath(fsPath) : fsPath);
219-
}
220210

221211
async refresh(scope: RefreshEnvironmentsScope): Promise<void> {
222212
return this.internalRefresh(scope, true, VenvManagerStrings.venvRefreshing);
@@ -272,7 +262,7 @@ export class VenvManager implements EnvironmentManager {
272262
return [];
273263
}
274264

275-
const env = this.getFsPathToEnv(scope.fsPath);
265+
const env = this.fsPathToEnv.get(scope.fsPath);
276266
return env ? [env] : [];
277267
}
278268

@@ -289,7 +279,7 @@ export class VenvManager implements EnvironmentManager {
289279
return this.globalEnv;
290280
}
291281

292-
let env = this.getFsPathToEnv(project.uri.fsPath);
282+
let env = this.fsPathToEnv.get(project.uri.fsPath);
293283
if (!env) {
294284
env = this.findEnvironmentByPath(project.uri.fsPath);
295285
}
@@ -315,11 +305,11 @@ export class VenvManager implements EnvironmentManager {
315305
return;
316306
}
317307

318-
const before = this.getFsPathToEnv(pw.uri.fsPath);
308+
const before = this.fsPathToEnv.get(pw.uri.fsPath);
319309
if (environment) {
320-
this.setFsPathToEnv(pw.uri.fsPath, environment);
310+
this.fsPathToEnv.set(pw.uri.fsPath, environment);
321311
} else {
322-
this.deleteFsPathToEnv(pw.uri.fsPath);
312+
this.fsPathToEnv.delete(pw.uri.fsPath);
323313
}
324314
await setVenvForWorkspace(pw.uri.fsPath, environment?.environmentPath.fsPath);
325315

@@ -340,11 +330,11 @@ export class VenvManager implements EnvironmentManager {
340330

341331
const before: Map<string, PythonEnvironment | undefined> = new Map();
342332
projects.forEach((p) => {
343-
before.set(p.uri.fsPath, this.getFsPathToEnv(p.uri.fsPath));
333+
before.set(p.uri.fsPath, this.fsPathToEnv.get(p.uri.fsPath));
344334
if (environment) {
345-
this.setFsPathToEnv(p.uri.fsPath, environment);
335+
this.fsPathToEnv.set(p.uri.fsPath, environment);
346336
} else {
347-
this.deleteFsPathToEnv(p.uri.fsPath);
337+
this.fsPathToEnv.delete(p.uri.fsPath);
348338
}
349339
});
350340

@@ -474,10 +464,10 @@ export class VenvManager implements EnvironmentManager {
474464

475465
if (env) {
476466
const found = this.findEnvironmentByPath(env, sorted) ?? this.findEnvironmentByPath(env, globals);
477-
const previous = this.getFsPathToEnv(p);
467+
const previous = this.fsPathToEnv.get(p);
478468
const pw = this.api.getPythonProject(Uri.file(p));
479469
if (found) {
480-
this.setFsPathToEnv(p, found);
470+
this.fsPathToEnv.set(p, found);
481471
if (pw && previous?.envId.id !== found.envId.id) {
482472
events.push(() =>
483473
this._onDidChangeEnvironment.fire({ uri: pw.uri, old: undefined, new: found }),
@@ -493,7 +483,7 @@ export class VenvManager implements EnvironmentManager {
493483
);
494484
if (resolved) {
495485
// If resolved add it to the collection
496-
this.setFsPathToEnv(p, resolved);
486+
this.fsPathToEnv.set(p, resolved);
497487
this.addEnvironment(resolved, false);
498488
if (pw && previous?.envId.id !== resolved.envId.id) {
499489
events.push(() =>
@@ -507,7 +497,7 @@ export class VenvManager implements EnvironmentManager {
507497
} else {
508498
// There is NO selected venv, then try and choose the venv that is in the workspace.
509499
if (sorted.length === 1) {
510-
this.setFsPathToEnv(p, sorted[0]);
500+
this.fsPathToEnv.set(p, sorted[0]);
511501
} else {
512502
// These are sorted by version and by path length. The assumption is that the user would want to pick
513503
// latest version and the one that is closest to the workspace.
@@ -516,7 +506,7 @@ export class VenvManager implements EnvironmentManager {
516506
return t && path.normalize(t) === p;
517507
});
518508
if (found) {
519-
this.setFsPathToEnv(p, found);
509+
this.fsPathToEnv.set(p, found);
520510
}
521511
}
522512
}

src/managers/conda/condaEnvManager.ts

Lines changed: 14 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,6 @@ import { createDeferred, Deferred } from '../../common/utils/deferred';
3838
import { withProgress } from '../../common/window.apis';
3939
import { CondaStrings } from '../../common/localize';
4040
import { showErrorMessage } from '../../common/errors/utils';
41-
import { isWindows } from '../common/utils';
42-
import { normalizePath } from '../../common/utils/pathUtils';
4341

4442
export class CondaEnvManager implements EnvironmentManager, Disposable {
4543
private collection: PythonEnvironment[] = [];
@@ -76,16 +74,6 @@ export class CondaEnvManager implements EnvironmentManager, Disposable {
7674
this.disposablesMap.forEach((d) => d.dispose());
7775
}
7876

79-
private setFsPathToEnv(fsPath: string, environment: PythonEnvironment): void {
80-
this.fsPathToEnv.set(isWindows() ? normalizePath(fsPath) : fsPath, environment);
81-
}
82-
private getFsPathToEnv(fsPath: string): PythonEnvironment | undefined {
83-
return this.fsPathToEnv.get(isWindows() ? normalizePath(fsPath) : fsPath);
84-
}
85-
private deleteFsPathToEnv(fsPath: string): void {
86-
this.fsPathToEnv.delete(isWindows() ? normalizePath(fsPath) : fsPath);
87-
}
88-
8977
private _initialized: Deferred<void> | undefined;
9078
async initialize(): Promise<void> {
9179
if (this._initialized) {
@@ -186,7 +174,7 @@ export class CondaEnvManager implements EnvironmentManager, Disposable {
186174
this.collection = this.collection.filter((env) => env.envId.id !== result?.envId.id);
187175
Array.from(this.fsPathToEnv.entries())
188176
.filter(([, env]) => env.envId.id === result?.envId.id)
189-
.forEach(([uri]) => this.deleteFsPathToEnv(uri));
177+
.forEach(([uri]) => this.fsPathToEnv.delete(uri));
190178
this.disposablesMap.delete(result.envId.id);
191179
}
192180
}),
@@ -248,13 +236,13 @@ export class CondaEnvManager implements EnvironmentManager, Disposable {
248236
async get(scope: GetEnvironmentScope): Promise<PythonEnvironment | undefined> {
249237
await this.initialize();
250238
if (scope instanceof Uri) {
251-
let env = this.getFsPathToEnv(scope.fsPath);
239+
let env = this.fsPathToEnv.get(scope.fsPath);
252240
if (env) {
253241
return env;
254242
}
255243
const project = this.api.getPythonProject(scope);
256244
if (project) {
257-
env = this.getFsPathToEnv(project.uri.fsPath);
245+
env = this.fsPathToEnv.get(project.uri.fsPath);
258246
if (env) {
259247
return env;
260248
}
@@ -272,9 +260,9 @@ export class CondaEnvManager implements EnvironmentManager, Disposable {
272260
const fsPath = folder?.uri?.fsPath ?? scope.fsPath;
273261
if (fsPath) {
274262
if (environment) {
275-
this.setFsPathToEnv(fsPath, environment);
263+
this.fsPathToEnv.set(fsPath, environment);
276264
} else {
277-
this.deleteFsPathToEnv(fsPath);
265+
this.fsPathToEnv.delete(fsPath);
278266
}
279267
await setCondaForWorkspace(fsPath, environment?.environmentPath.fsPath);
280268
}
@@ -290,11 +278,11 @@ export class CondaEnvManager implements EnvironmentManager, Disposable {
290278

291279
const before: Map<string, PythonEnvironment | undefined> = new Map();
292280
projects.forEach((p) => {
293-
before.set(p.uri.fsPath, this.getFsPathToEnv(p.uri.fsPath));
281+
before.set(p.uri.fsPath, this.fsPathToEnv.get(p.uri.fsPath));
294282
if (environment) {
295-
this.setFsPathToEnv(p.uri.fsPath, environment);
283+
this.fsPathToEnv.set(p.uri.fsPath, environment);
296284
} else {
297-
this.deleteFsPathToEnv(p.uri.fsPath);
285+
this.fsPathToEnv.delete(p.uri.fsPath);
298286
}
299287
});
300288

@@ -383,14 +371,14 @@ export class CondaEnvManager implements EnvironmentManager, Disposable {
383371
const found = this.findEnvironmentByPath(env);
384372

385373
if (found) {
386-
this.setFsPathToEnv(p, found);
374+
this.fsPathToEnv.set(p, found);
387375
} else {
388376
// If not found, resolve the conda path. Could be portable conda.
389377
const resolved = await resolveCondaPath(env, this.nativeFinder, this.api, this.log, this);
390378

391379
if (resolved) {
392380
// If resolved add it to the collection
393-
this.setFsPathToEnv(p, resolved);
381+
this.fsPathToEnv.set(p, resolved);
394382
this.collection.push(resolved);
395383
} else {
396384
this.log.error(`Failed to resolve conda environment: ${env}`);
@@ -400,7 +388,7 @@ export class CondaEnvManager implements EnvironmentManager, Disposable {
400388
// If there is not an environment already assigned by user to this project
401389
// then see if there is one in the collection
402390
if (pathSorted.length === 1) {
403-
this.setFsPathToEnv(p, pathSorted[0]);
391+
this.fsPathToEnv.set(p, pathSorted[0]);
404392
} else {
405393
// If there is more than one environment then we need to check if the project
406394
// is a subfolder of one of the environments
@@ -409,7 +397,7 @@ export class CondaEnvManager implements EnvironmentManager, Disposable {
409397
return t && path.normalize(t) === p;
410398
});
411399
if (found) {
412-
this.setFsPathToEnv(p, found);
400+
this.fsPathToEnv.set(p, found);
413401
}
414402
}
415403
}
@@ -418,15 +406,15 @@ export class CondaEnvManager implements EnvironmentManager, Disposable {
418406

419407
private fromEnvMap(uri: Uri): PythonEnvironment | undefined {
420408
// Find environment directly using the URI mapping
421-
const env = this.getFsPathToEnv(uri.fsPath);
409+
const env = this.fsPathToEnv.get(uri.fsPath);
422410
if (env) {
423411
return env;
424412
}
425413

426414
// Find environment using the Python project for the Uri
427415
const project = this.api.getPythonProject(uri);
428416
if (project) {
429-
return this.getFsPathToEnv(project.uri.fsPath);
417+
return this.fsPathToEnv.get(project.uri.fsPath);
430418
}
431419

432420
return undefined;

0 commit comments

Comments
 (0)