1
+ /* eslint-disable max-classes-per-file */
1
2
import { inject , injectable } from 'inversify' ;
3
+ import { flatten } from 'lodash' ;
2
4
import {
3
5
Disposable , Event , EventEmitter , Uri ,
4
6
} from 'vscode' ;
@@ -21,15 +23,14 @@ import {
21
23
} from '../../../interpreter/contracts' ;
22
24
import { IServiceContainer } from '../../../ioc/types' ;
23
25
import { PythonEnvInfo } from '../../base/info' ;
24
- import { ILocator , Locator , NOOP_ITERATOR , PythonEnvsIterator , PythonLocatorQuery } from '../../base/locator' ;
26
+ import {
27
+ ILocator , Locator , NOOP_ITERATOR , PythonEnvsIterator , PythonLocatorQuery ,
28
+ } from '../../base/locator' ;
25
29
import { DisableableLocator , Locators } from '../../base/locators' ;
26
30
import { PythonEnvironment } from '../../info' ;
27
31
import { isHiddenInterpreter } from './services/interpreterFilter' ;
28
32
import { GetInterpreterLocatorOptions } from './types' ;
29
33
30
- // tslint:disable-next-line:no-require-imports no-var-requires
31
- const flatten = require ( 'lodash/flatten' ) as typeof import ( 'lodash/flatten' ) ;
32
-
33
34
/**
34
35
* A wrapper around all locators used by the extension.
35
36
*/
@@ -39,7 +40,7 @@ export class ExtensionLocators extends Locators {
39
40
nonWorkspace : ILocator [ ] ,
40
41
// This is expected to be a locator wrapping any found in
41
42
// the workspace (i.e. WorkspaceLocators).
42
- workspace : ILocator
43
+ workspace : ILocator ,
43
44
) {
44
45
super ( [ ...nonWorkspace , workspace ] ) ;
45
46
}
@@ -62,10 +63,12 @@ type RootURI = string;
62
63
*/
63
64
export class WorkspaceLocators extends Locator {
64
65
private readonly locators : Record < RootURI , DisableableLocator > = { } ;
66
+
65
67
private readonly roots : Record < RootURI , Uri > = { } ;
68
+
66
69
constructor (
67
70
// used to produce the per-root locators:
68
- private readonly factories : WorkspaceLocatorFactory [ ]
71
+ private readonly factories : WorkspaceLocatorFactory [ ] ,
69
72
) {
70
73
super ( ) ;
71
74
}
@@ -75,10 +78,10 @@ export class WorkspaceLocators extends Locator {
75
78
*
76
79
* @param folders - the info used to keep track of the workspace folders
77
80
*/
78
- public activate ( folders : IWorkspaceFolders ) {
79
- for ( const root of folders . roots ) {
81
+ public activate ( folders : IWorkspaceFolders ) : void {
82
+ folders . roots . forEach ( ( root ) => {
80
83
this . addRoot ( root ) ;
81
- }
84
+ } ) ;
82
85
folders . onAdded ( ( root : Uri ) => this . addRoot ( root ) ) ;
83
86
folders . onRemoved ( ( root : Uri ) => this . removeRoot ( root ) ) ;
84
87
}
@@ -106,7 +109,11 @@ export class WorkspaceLocators extends Locator {
106
109
}
107
110
}
108
111
// Fall back to checking all the roots.
112
+ // The eslint disable below should be removed after we have a
113
+ // better solution for these. We need asyncFind for this.
114
+ // eslint-disable-next-line no-restricted-syntax
109
115
for ( const key of Object . keys ( this . locators ) ) {
116
+ // eslint-disable-next-line no-await-in-loop
110
117
const resolved = await this . locators [ key ] . resolveEnv ( env ) ;
111
118
if ( resolved !== undefined ) {
112
119
return resolved ;
@@ -120,9 +127,9 @@ export class WorkspaceLocators extends Locator {
120
127
this . removeRoot ( root ) ;
121
128
// Create the root's locator, wrapping each factory-generated locator.
122
129
const locators : ILocator [ ] = [ ] ;
123
- for ( const create of this . factories ) {
130
+ this . factories . forEach ( ( create ) => {
124
131
locators . push ( ...create ( root ) ) ;
125
- }
132
+ } ) ;
126
133
const locator = new DisableableLocator ( new Locators ( locators ) ) ;
127
134
// Cache it.
128
135
const key = root . toString ( ) ;
@@ -158,17 +165,18 @@ export class WorkspaceLocators extends Locator {
158
165
* or the URI must be a parent of one of the candidates.
159
166
*/
160
167
function matchURI ( uri : Uri , ...candidates : Uri [ ] ) : boolean {
161
- const uriPath = uri . path . endsWith ( '/' ) ? uri . path : ` {uri.path}/` ;
162
- for ( const candidate of candidates ) {
168
+ const uriPath = uri . path . endsWith ( '/' ) ? uri . path : ' {uri.path}/' ;
169
+ const matchedUri = candidates . find ( ( candidate ) => {
163
170
if ( candidate . scheme === uri . scheme ) {
164
171
if ( candidate . path === uri . path ) {
165
172
return true ;
166
- } else if ( candidate . path . startsWith ( uriPath ) ) {
173
+ } if ( candidate . path . startsWith ( uriPath ) ) {
167
174
return true ;
168
175
}
169
176
}
170
- }
171
- return false ;
177
+ return false ;
178
+ } ) ;
179
+ return matchedUri !== undefined ;
172
180
}
173
181
174
182
/**
@@ -186,6 +194,9 @@ export class PythonInterpreterLocatorService implements IInterpreterLocatorServi
186
194
187
195
private readonly _hasInterpreters : Deferred < boolean > ;
188
196
197
+ private readonly onLocatingEmitter :EventEmitter < Promise < PythonEnvironment [ ] > > =
198
+ new EventEmitter < Promise < PythonEnvironment [ ] > > ( ) ;
199
+
189
200
constructor ( @inject ( IServiceContainer ) private serviceContainer : IServiceContainer ) {
190
201
this . _hasInterpreters = createDeferred < boolean > ( ) ;
191
202
serviceContainer . get < Disposable [ ] > ( IDisposableRegistry ) . push ( this ) ;
@@ -196,14 +207,14 @@ export class PythonInterpreterLocatorService implements IInterpreterLocatorServi
196
207
197
208
/**
198
209
* This class should never emit events when we're locating.
199
- * The events will be fired by the indivitual locators retrieved in `getLocators`.
210
+ * The events will be fired by the individual locators retrieved in `getLocators`.
200
211
*
201
212
* @readonly
202
213
* @type {Event<Promise<PythonEnvironment[]>> }
203
214
* @memberof PythonInterpreterLocatorService
204
215
*/
205
216
public get onLocating ( ) : Event < Promise < PythonEnvironment [ ] > > {
206
- return new EventEmitter < Promise < PythonEnvironment [ ] > > ( ) . event ;
217
+ return this . onLocatingEmitter . event ;
207
218
}
208
219
209
220
public get hasInterpreters ( ) : Promise < boolean > {
@@ -215,7 +226,7 @@ export class PythonInterpreterLocatorService implements IInterpreterLocatorServi
215
226
*
216
227
* Called by VS Code to indicate it is done with the resource.
217
228
*/
218
- public dispose ( ) {
229
+ public dispose ( ) : void {
219
230
this . disposables . forEach ( ( disposable ) => disposable . dispose ( ) ) ;
220
231
}
221
232
@@ -276,7 +287,9 @@ export class PythonInterpreterLocatorService implements IInterpreterLocatorServi
276
287
// Set it to true the first time the user selects an interpreter
277
288
if ( ! this . didTriggerInterpreterSuggestions && options ?. onSuggestion === true ) {
278
289
this . didTriggerInterpreterSuggestions = true ;
279
- locators . forEach ( ( locator ) => ( locator . didTriggerInterpreterSuggestions = true ) ) ;
290
+ locators . forEach ( ( locator ) => {
291
+ locator . didTriggerInterpreterSuggestions = true ;
292
+ } ) ;
280
293
}
281
294
282
295
return locators ;
0 commit comments