forked from microsoft/vscode-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlocatorUtils.ts
More file actions
108 lines (101 loc) · 3.19 KB
/
locatorUtils.ts
File metadata and controls
108 lines (101 loc) · 3.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
import { Uri } from 'vscode';
import { createDeferred } from '../../common/utils/async';
import { getURIFilter } from '../../common/utils/misc';
import { PythonEnvInfo } from './info';
import {
IPythonEnvsIterator,
PythonEnvUpdatedEvent,
PythonLocatorQuery,
} from './locator';
/**
* Create a filter function to match the given query.
*/
export function getQueryFilter(query: PythonLocatorQuery): (env: PythonEnvInfo) => boolean {
const kinds = (query.kinds !== undefined && query.kinds.length > 0)
? query.kinds
: undefined;
let includeNonRooted = true;
if (query.searchLocations !== undefined) {
if (query.searchLocations.includeNonRooted !== undefined) {
includeNonRooted = query.searchLocations.includeNonRooted;
} else {
// We default to `false`.
includeNonRooted = false;
}
}
const locationFilters = getSearchLocationFilters(query);
function checkKind(env: PythonEnvInfo): boolean {
if (kinds === undefined) {
return true;
}
return kinds.includes(env.kind);
}
function checkSearchLocation(env: PythonEnvInfo): boolean {
if (env.searchLocation === undefined) {
// It is not a "rooted" env.
return includeNonRooted;
}
// It is a "rooted" env.
const loc = env.searchLocation;
if (locationFilters !== undefined) {
// Check against the requested roots. (There may be none.)
return locationFilters.some((filter) => filter(loc));
}
return true;
}
return (env) => {
if (!checkKind(env)) {
return false;
}
if (!checkSearchLocation(env)) {
return false;
}
return true;
};
}
function getSearchLocationFilters(query: PythonLocatorQuery): ((u: Uri) => boolean)[] | undefined {
if (query.searchLocations === undefined) {
return undefined;
}
if (query.searchLocations.roots.length === 0) {
return [];
}
return query.searchLocations.roots.map((loc) => getURIFilter(loc, {
checkParent: true,
checkExact: true,
}));
}
/**
* Unroll the given iterator into an array.
*
* This includes applying any received updates.
*/
export async function getEnvs(iterator: IPythonEnvsIterator): Promise<PythonEnvInfo[]> {
const envs: PythonEnvInfo[] = [];
const updatesDone = createDeferred<void>();
if (iterator.onUpdated === undefined) {
updatesDone.resolve();
} else {
iterator.onUpdated((event: PythonEnvUpdatedEvent | null) => {
if (event === null) {
updatesDone.resolve();
return;
}
const oldEnv = envs[event.index];
if (oldEnv === undefined) {
// XXX log or fail
} else {
envs[event.index] = event.update;
}
});
}
let result = await iterator.next();
while (!result.done) {
envs.push(result.value);
result = await iterator.next();
}
await updatesDone.promise;
return envs;
}