@@ -18,24 +18,39 @@ import {
1818 WORKSPACE_VIRTUAL_ENV_SERVICE
1919} from '../contracts' ;
2020
21+ /**
22+ * Facilitates locating Python interpreters.
23+ */
2124@injectable ( )
2225export class PythonInterpreterLocatorService implements IInterpreterLocatorService {
2326 private readonly disposables : Disposable [ ] = [ ] ;
2427 private readonly platform : IPlatformService ;
2528 private readonly interpreterLocatorHelper : IInterpreterLocatorHelper ;
2629
27- constructor ( @inject ( IServiceContainer ) private serviceContainer : IServiceContainer ) {
30+ constructor (
31+ @inject ( IServiceContainer ) private serviceContainer : IServiceContainer
32+ ) {
2833 serviceContainer . get < Disposable [ ] > ( IDisposableRegistry ) . push ( this ) ;
2934 this . platform = serviceContainer . get < IPlatformService > ( IPlatformService ) ;
3035 this . interpreterLocatorHelper = serviceContainer . get < IInterpreterLocatorHelper > ( IInterpreterLocatorHelper ) ;
3136 }
32- public async getInterpreters ( resource ?: Uri ) : Promise < PythonInterpreter [ ] > {
33- return this . getInterpretersPerResource ( resource ) ;
34- }
37+
38+ /**
39+ * Release any held resources.
40+ *
41+ * Called by VS Code to indicate it is done with the resource.
42+ */
3543 public dispose ( ) {
3644 this . disposables . forEach ( disposable => disposable . dispose ( ) ) ;
3745 }
38- private async getInterpretersPerResource ( resource ?: Uri ) : Promise < PythonInterpreter [ ] > {
46+
47+ /**
48+ * Return the list of known Python interpreters.
49+ *
50+ * The optional resource arg may control where locators look for
51+ * interpreters.
52+ */
53+ public async getInterpreters ( resource ?: Uri ) : Promise < PythonInterpreter [ ] > {
3954 const locators = this . getLocators ( ) ;
4055 const promises = locators . map ( async provider => provider . getInterpreters ( resource ) ) ;
4156 const listOfInterpreters = await Promise . all ( promises ) ;
@@ -45,26 +60,47 @@ export class PythonInterpreterLocatorService implements IInterpreterLocatorServi
4560 . map ( item => item ! ) ;
4661 return this . interpreterLocatorHelper . mergeInterpreters ( items ) ;
4762 }
63+
64+ /**
65+ * Return the list of applicable interpreter locators.
66+ *
67+ * The locators are pulled from the registry.
68+ */
4869 private getLocators ( ) : IInterpreterLocatorService [ ] {
49- const locators : IInterpreterLocatorService [ ] = [ ] ;
5070 // The order of the services is important.
5171 // The order is important because the data sources at the bottom of the list do not contain all,
5272 // the information about the interpreters (e.g. type, environment name, etc).
5373 // This way, the items returned from the top of the list will win, when we combine the items returned.
54- if ( this . platform . isWindows ) {
55- locators . push ( this . serviceContainer . get < IInterpreterLocatorService > ( IInterpreterLocatorService , WINDOWS_REGISTRY_SERVICE ) ) ;
56- }
57- locators . push ( this . serviceContainer . get < IInterpreterLocatorService > ( IInterpreterLocatorService , CONDA_ENV_SERVICE ) ) ;
58- locators . push ( this . serviceContainer . get < IInterpreterLocatorService > ( IInterpreterLocatorService , CONDA_ENV_FILE_SERVICE ) ) ;
59- locators . push ( this . serviceContainer . get < IInterpreterLocatorService > ( IInterpreterLocatorService , PIPENV_SERVICE ) ) ;
60- locators . push ( this . serviceContainer . get < IInterpreterLocatorService > ( IInterpreterLocatorService , GLOBAL_VIRTUAL_ENV_SERVICE ) ) ;
61- locators . push ( this . serviceContainer . get < IInterpreterLocatorService > ( IInterpreterLocatorService , WORKSPACE_VIRTUAL_ENV_SERVICE ) ) ;
74+ const keys : [ string , string ] [ ] = [
75+ [ WINDOWS_REGISTRY_SERVICE , 'win' ] ,
76+ [ CONDA_ENV_SERVICE , '' ] ,
77+ [ CONDA_ENV_FILE_SERVICE , '' ] ,
78+ [ PIPENV_SERVICE , '' ] ,
79+ [ GLOBAL_VIRTUAL_ENV_SERVICE , '' ] ,
80+ [ WORKSPACE_VIRTUAL_ENV_SERVICE , '' ] ,
81+ [ KNOWN_PATH_SERVICE , '-win' ] ,
82+ [ CURRENT_PATH_SERVICE , '' ]
83+ ] ;
84+ return getLocators ( keys , this . platform , ( key ) => {
85+ return this . serviceContainer . get < IInterpreterLocatorService > ( IInterpreterLocatorService , key ) ;
86+ } ) ;
87+ }
88+ }
6289
63- if ( ! this . platform . isWindows ) {
64- locators . push ( this . serviceContainer . get < IInterpreterLocatorService > ( IInterpreterLocatorService , KNOWN_PATH_SERVICE ) ) ;
65- }
66- locators . push ( this . serviceContainer . get < IInterpreterLocatorService > ( IInterpreterLocatorService , CURRENT_PATH_SERVICE ) ) ;
90+ type PlatformName = string ;
6791
68- return locators ;
92+ function getLocators (
93+ keys : [ string , PlatformName ] [ ] ,
94+ platform : IPlatformService ,
95+ getService : ( string ) => IInterpreterLocatorService
96+ ) : IInterpreterLocatorService [ ] {
97+ const locators : IInterpreterLocatorService [ ] = [ ] ;
98+ for ( const [ key , platformName ] of keys ) {
99+ if ( ! platform . os . matchPlatform ( platformName ) ) {
100+ continue ;
101+ }
102+ const locator = getService ( key ) ;
103+ locators . push ( locator ) ;
69104 }
105+ return locators ;
70106}
0 commit comments