1
+ // tslint:disable-next-line: no-single-line-block-comment
2
+ /* eslint-disable max-classes-per-file */
1
3
import { inject , injectable } from 'inversify' ;
4
+ import { flatten } from 'lodash' ;
2
5
import {
3
6
Disposable , Event , EventEmitter , Uri ,
4
7
} from 'vscode' ;
@@ -37,9 +40,6 @@ import { PythonEnvironment } from '../../info';
37
40
import { isHiddenInterpreter } from './services/interpreterFilter' ;
38
41
import { GetInterpreterLocatorOptions } from './types' ;
39
42
40
- // tslint:disable-next-line:no-require-imports no-var-requires
41
- const flatten = require ( 'lodash/flatten' ) as typeof import ( 'lodash/flatten' ) ;
42
-
43
43
/**
44
44
* A wrapper around all locators used by the extension.
45
45
*/
@@ -49,7 +49,7 @@ export class ExtensionLocators extends Locators {
49
49
nonWorkspace : ILocator [ ] ,
50
50
// This is expected to be a locator wrapping any found in
51
51
// the workspace (i.e. WorkspaceLocators).
52
- workspace : ILocator
52
+ workspace : ILocator ,
53
53
) {
54
54
super ( [ ...nonWorkspace , workspace ] ) ;
55
55
}
@@ -72,10 +72,12 @@ type RootURI = string;
72
72
*/
73
73
export class WorkspaceLocators extends Locator {
74
74
private readonly locators : Record < RootURI , DisableableLocator > = { } ;
75
+
75
76
private readonly roots : Record < RootURI , Uri > = { } ;
77
+
76
78
constructor (
77
79
// used to produce the per-root locators:
78
- private readonly factories : WorkspaceLocatorFactory [ ]
80
+ private readonly factories : WorkspaceLocatorFactory [ ] ,
79
81
) {
80
82
super ( ) ;
81
83
}
@@ -85,10 +87,10 @@ export class WorkspaceLocators extends Locator {
85
87
*
86
88
* @param folders - the info used to keep track of the workspace folders
87
89
*/
88
- public activate ( folders : IWorkspaceFolders ) {
89
- for ( const root of folders . roots ) {
90
+ public activate ( folders : IWorkspaceFolders ) : void {
91
+ folders . roots . forEach ( ( root ) => {
90
92
this . addRoot ( root ) ;
91
- }
93
+ } ) ;
92
94
folders . onAdded ( ( root : Uri ) => this . addRoot ( root ) ) ;
93
95
folders . onRemoved ( ( root : Uri ) => this . removeRoot ( root ) ) ;
94
96
}
@@ -116,7 +118,11 @@ export class WorkspaceLocators extends Locator {
116
118
}
117
119
}
118
120
// Fall back to checking all the roots.
121
+ // The eslint disable below should be removed after we have a
122
+ // better solution for these. We need asyncFind for this.
123
+ // eslint-disable-next-line no-restricted-syntax
119
124
for ( const key of Object . keys ( this . locators ) ) {
125
+ // eslint-disable-next-line no-await-in-loop
120
126
const resolved = await this . locators [ key ] . resolveEnv ( env ) ;
121
127
if ( resolved !== undefined ) {
122
128
return resolved ;
@@ -130,9 +136,9 @@ export class WorkspaceLocators extends Locator {
130
136
this . removeRoot ( root ) ;
131
137
// Create the root's locator, wrapping each factory-generated locator.
132
138
const locators : ILocator [ ] = [ ] ;
133
- for ( const create of this . factories ) {
139
+ this . factories . forEach ( ( create ) => {
134
140
locators . push ( ...create ( root ) ) ;
135
- }
141
+ } ) ;
136
142
const locator = new DisableableLocator ( new Locators ( locators ) ) ;
137
143
// Cache it.
138
144
const key = root . toString ( ) ;
@@ -168,17 +174,10 @@ export class WorkspaceLocators extends Locator {
168
174
* or the URI must be a parent of one of the candidates.
169
175
*/
170
176
function matchURI ( uri : Uri , ...candidates : Uri [ ] ) : boolean {
171
- const uriPath = uri . path . endsWith ( '/' ) ? uri . path : `{uri.path}/` ;
172
- for ( const candidate of candidates ) {
173
- if ( candidate . scheme === uri . scheme ) {
174
- if ( candidate . path === uri . path ) {
175
- return true ;
176
- } else if ( candidate . path . startsWith ( uriPath ) ) {
177
- return true ;
178
- }
179
- }
180
- }
181
- return false ;
177
+ const uriPath = uri . path . endsWith ( '/' ) ? uri . path : '{uri.path}/' ;
178
+ const matchedUri = candidates . find ( ( candidate ) => ( candidate . scheme === uri . scheme )
179
+ && ( candidate . path === uri . path || candidate . path . startsWith ( uriPath ) ) ) ;
180
+ return matchedUri !== undefined ;
182
181
}
183
182
184
183
/**
@@ -196,6 +195,9 @@ export class PythonInterpreterLocatorService implements IInterpreterLocatorServi
196
195
197
196
private readonly _hasInterpreters : Deferred < boolean > ;
198
197
198
+ private readonly onLocatingEmitter :EventEmitter < Promise < PythonEnvironment [ ] > > =
199
+ new EventEmitter < Promise < PythonEnvironment [ ] > > ( ) ;
200
+
199
201
constructor ( @inject ( IServiceContainer ) private serviceContainer : IServiceContainer ) {
200
202
this . _hasInterpreters = createDeferred < boolean > ( ) ;
201
203
serviceContainer . get < Disposable [ ] > ( IDisposableRegistry ) . push ( this ) ;
@@ -206,14 +208,14 @@ export class PythonInterpreterLocatorService implements IInterpreterLocatorServi
206
208
207
209
/**
208
210
* This class should never emit events when we're locating.
209
- * The events will be fired by the indivitual locators retrieved in `getLocators`.
211
+ * The events will be fired by the individual locators retrieved in `getLocators`.
210
212
*
211
213
* @readonly
212
214
* @type {Event<Promise<PythonEnvironment[]>> }
213
215
* @memberof PythonInterpreterLocatorService
214
216
*/
215
217
public get onLocating ( ) : Event < Promise < PythonEnvironment [ ] > > {
216
- return new EventEmitter < Promise < PythonEnvironment [ ] > > ( ) . event ;
218
+ return this . onLocatingEmitter . event ;
217
219
}
218
220
219
221
public get hasInterpreters ( ) : Promise < boolean > {
@@ -225,7 +227,7 @@ export class PythonInterpreterLocatorService implements IInterpreterLocatorServi
225
227
*
226
228
* Called by VS Code to indicate it is done with the resource.
227
229
*/
228
- public dispose ( ) {
230
+ public dispose ( ) : void {
229
231
this . disposables . forEach ( ( disposable ) => disposable . dispose ( ) ) ;
230
232
}
231
233
@@ -286,7 +288,9 @@ export class PythonInterpreterLocatorService implements IInterpreterLocatorServi
286
288
// Set it to true the first time the user selects an interpreter
287
289
if ( ! this . didTriggerInterpreterSuggestions && options ?. onSuggestion === true ) {
288
290
this . didTriggerInterpreterSuggestions = true ;
289
- locators . forEach ( ( locator ) => ( locator . didTriggerInterpreterSuggestions = true ) ) ;
291
+ locators . forEach ( ( locator ) => {
292
+ locator . didTriggerInterpreterSuggestions = true ;
293
+ } ) ;
290
294
}
291
295
292
296
return locators ;
0 commit comments