1+ // tslint:disable-next-line: no-single-line-block-comment
2+ /* eslint-disable max-classes-per-file */
13import { inject , injectable } from 'inversify' ;
4+ import { flatten } from 'lodash' ;
25import {
36 Disposable , Event , EventEmitter , Uri ,
47} from 'vscode' ;
@@ -37,9 +40,6 @@ import { PythonEnvironment } from '../../info';
3740import { isHiddenInterpreter } from './services/interpreterFilter' ;
3841import { GetInterpreterLocatorOptions } from './types' ;
3942
40- // tslint:disable-next-line:no-require-imports no-var-requires
41- const flatten = require ( 'lodash/flatten' ) as typeof import ( 'lodash/flatten' ) ;
42-
4343/**
4444 * A wrapper around all locators used by the extension.
4545 */
@@ -49,7 +49,7 @@ export class ExtensionLocators extends Locators {
4949 nonWorkspace : ILocator [ ] ,
5050 // This is expected to be a locator wrapping any found in
5151 // the workspace (i.e. WorkspaceLocators).
52- workspace : ILocator
52+ workspace : ILocator ,
5353 ) {
5454 super ( [ ...nonWorkspace , workspace ] ) ;
5555 }
@@ -72,10 +72,12 @@ type RootURI = string;
7272 */
7373export class WorkspaceLocators extends Locator {
7474 private readonly locators : Record < RootURI , DisableableLocator > = { } ;
75+
7576 private readonly roots : Record < RootURI , Uri > = { } ;
77+
7678 constructor (
7779 // used to produce the per-root locators:
78- private readonly factories : WorkspaceLocatorFactory [ ]
80+ private readonly factories : WorkspaceLocatorFactory [ ] ,
7981 ) {
8082 super ( ) ;
8183 }
@@ -85,10 +87,10 @@ export class WorkspaceLocators extends Locator {
8587 *
8688 * @param folders - the info used to keep track of the workspace folders
8789 */
88- public activate ( folders : IWorkspaceFolders ) {
89- for ( const root of folders . roots ) {
90+ public activate ( folders : IWorkspaceFolders ) : void {
91+ folders . roots . forEach ( ( root ) => {
9092 this . addRoot ( root ) ;
91- }
93+ } ) ;
9294 folders . onAdded ( ( root : Uri ) => this . addRoot ( root ) ) ;
9395 folders . onRemoved ( ( root : Uri ) => this . removeRoot ( root ) ) ;
9496 }
@@ -116,7 +118,11 @@ export class WorkspaceLocators extends Locator {
116118 }
117119 }
118120 // 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
119124 for ( const key of Object . keys ( this . locators ) ) {
125+ // eslint-disable-next-line no-await-in-loop
120126 const resolved = await this . locators [ key ] . resolveEnv ( env ) ;
121127 if ( resolved !== undefined ) {
122128 return resolved ;
@@ -130,9 +136,9 @@ export class WorkspaceLocators extends Locator {
130136 this . removeRoot ( root ) ;
131137 // Create the root's locator, wrapping each factory-generated locator.
132138 const locators : ILocator [ ] = [ ] ;
133- for ( const create of this . factories ) {
139+ this . factories . forEach ( ( create ) => {
134140 locators . push ( ...create ( root ) ) ;
135- }
141+ } ) ;
136142 const locator = new DisableableLocator ( new Locators ( locators ) ) ;
137143 // Cache it.
138144 const key = root . toString ( ) ;
@@ -168,17 +174,10 @@ export class WorkspaceLocators extends Locator {
168174 * or the URI must be a parent of one of the candidates.
169175 */
170176function 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 ;
182181}
183182
184183/**
@@ -196,6 +195,9 @@ export class PythonInterpreterLocatorService implements IInterpreterLocatorServi
196195
197196 private readonly _hasInterpreters : Deferred < boolean > ;
198197
198+ private readonly onLocatingEmitter :EventEmitter < Promise < PythonEnvironment [ ] > > =
199+ new EventEmitter < Promise < PythonEnvironment [ ] > > ( ) ;
200+
199201 constructor ( @inject ( IServiceContainer ) private serviceContainer : IServiceContainer ) {
200202 this . _hasInterpreters = createDeferred < boolean > ( ) ;
201203 serviceContainer . get < Disposable [ ] > ( IDisposableRegistry ) . push ( this ) ;
@@ -206,14 +208,14 @@ export class PythonInterpreterLocatorService implements IInterpreterLocatorServi
206208
207209 /**
208210 * 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`.
210212 *
211213 * @readonly
212214 * @type {Event<Promise<PythonEnvironment[]>> }
213215 * @memberof PythonInterpreterLocatorService
214216 */
215217 public get onLocating ( ) : Event < Promise < PythonEnvironment [ ] > > {
216- return new EventEmitter < Promise < PythonEnvironment [ ] > > ( ) . event ;
218+ return this . onLocatingEmitter . event ;
217219 }
218220
219221 public get hasInterpreters ( ) : Promise < boolean > {
@@ -225,7 +227,7 @@ export class PythonInterpreterLocatorService implements IInterpreterLocatorServi
225227 *
226228 * Called by VS Code to indicate it is done with the resource.
227229 */
228- public dispose ( ) {
230+ public dispose ( ) : void {
229231 this . disposables . forEach ( ( disposable ) => disposable . dispose ( ) ) ;
230232 }
231233
@@ -286,7 +288,9 @@ export class PythonInterpreterLocatorService implements IInterpreterLocatorServi
286288 // Set it to true the first time the user selects an interpreter
287289 if ( ! this . didTriggerInterpreterSuggestions && options ?. onSuggestion === true ) {
288290 this . didTriggerInterpreterSuggestions = true ;
289- locators . forEach ( ( locator ) => ( locator . didTriggerInterpreterSuggestions = true ) ) ;
291+ locators . forEach ( ( locator ) => {
292+ locator . didTriggerInterpreterSuggestions = true ;
293+ } ) ;
290294 }
291295
292296 return locators ;
0 commit comments