@@ -17,12 +17,14 @@ import { EventName } from '../telemetry/constants';
1717import { IExtensionActivationService , ILanguageServerActivator , LanguageServerActivator } from './types' ;
1818
1919const jediEnabledSetting : keyof IPythonSettings = 'jediEnabled' ;
20+ const workspacePathNameForGlobalWorkspaces = '' ;
2021type ActivatorInfo = { jedi : boolean ; activator : ILanguageServerActivator } ;
2122
2223@injectable ( )
2324export class LanguageServerExtensionActivationService implements IExtensionActivationService , Disposable {
25+ private lsActivatedWorkspaces = new Map < string , ILanguageServerActivator > ( ) ;
2426 private currentActivator ?: ActivatorInfo ;
25- private activatedOnce : boolean = false ;
27+ private jediActivatedOnce : boolean = false ;
2628 private readonly workspaceService : IWorkspaceService ;
2729 private readonly output : OutputChannel ;
2830 private readonly appShell : IApplicationShell ;
@@ -40,45 +42,54 @@ export class LanguageServerExtensionActivationService implements IExtensionActiv
4042 const disposables = serviceContainer . get < IDisposableRegistry > ( IDisposableRegistry ) ;
4143 disposables . push ( this ) ;
4244 disposables . push ( this . workspaceService . onDidChangeConfiguration ( this . onDidChangeConfiguration . bind ( this ) ) ) ;
45+ disposables . push ( this . workspaceService . onDidChangeWorkspaceFolders ( this . onWorkspaceFoldersChanged , this ) ) ;
4346 }
4447
4548 public async activate ( resource : Resource ) : Promise < void > {
46- if ( this . currentActivator || this . activatedOnce ) {
47- return ;
48- }
49- this . resource = resource ;
50- this . activatedOnce = true ;
51-
5249 let jedi = this . useJedi ( ) ;
5350 if ( ! jedi ) {
51+ if ( this . lsActivatedWorkspaces . has ( this . getWorkspacePathKey ( resource ) ) ) {
52+ return ;
53+ }
5454 const diagnostic = await this . lsNotSupportedDiagnosticService . diagnose ( undefined ) ;
5555 this . lsNotSupportedDiagnosticService . handle ( diagnostic ) . ignoreErrors ( ) ;
5656 if ( diagnostic . length ) {
5757 sendTelemetryEvent ( EventName . PYTHON_LANGUAGE_SERVER_PLATFORM_NOT_SUPPORTED ) ;
5858 jedi = true ;
5959 }
60+ } else {
61+ if ( this . jediActivatedOnce ) {
62+ return ;
63+ }
64+ this . jediActivatedOnce = true ;
6065 }
6166
67+ this . resource = resource ;
6268 await this . logStartup ( jedi ) ;
63-
6469 let activatorName = jedi ? LanguageServerActivator . Jedi : LanguageServerActivator . DotNet ;
6570 let activator = this . serviceContainer . get < ILanguageServerActivator > ( ILanguageServerActivator , activatorName ) ;
6671 this . currentActivator = { jedi, activator } ;
6772
6873 try {
69- await activator . activate ( ) ;
70- return ;
74+ await activator . activate ( resource ) ;
75+ if ( ! jedi ) {
76+ this . lsActivatedWorkspaces . set ( this . getWorkspacePathKey ( resource ) , activator ) ;
77+ }
7178 } catch ( ex ) {
7279 if ( jedi ) {
7380 return ;
7481 }
7582 //Language server fails, reverting to jedi
83+ if ( this . jediActivatedOnce ) {
84+ return ;
85+ }
86+ this . jediActivatedOnce = true ;
7687 jedi = true ;
7788 await this . logStartup ( jedi ) ;
7889 activatorName = LanguageServerActivator . Jedi ;
7990 activator = this . serviceContainer . get < ILanguageServerActivator > ( ILanguageServerActivator , activatorName ) ;
8091 this . currentActivator = { jedi, activator } ;
81- await activator . activate ( ) ;
92+ await activator . activate ( resource ) ;
8293 }
8394 }
8495
@@ -88,6 +99,19 @@ export class LanguageServerExtensionActivationService implements IExtensionActiv
8899 }
89100 }
90101
102+ protected onWorkspaceFoldersChanged ( ) {
103+ //If an activated workspace folder was removed, dispose its activator
104+ const workspaceKeys = this . workspaceService . workspaceFolders ! . map ( workspaceFolder => this . getWorkspacePathKey ( workspaceFolder . uri ) ) ;
105+ const activatedWkspcKeys = Array . from ( this . lsActivatedWorkspaces . keys ( ) ) ;
106+ const activatedWkspcFoldersRemoved = activatedWkspcKeys . filter ( item => workspaceKeys . indexOf ( item ) < 0 ) ;
107+ if ( activatedWkspcFoldersRemoved . length > 0 ) {
108+ for ( const folder of activatedWkspcFoldersRemoved ) {
109+ this . lsActivatedWorkspaces . get ( folder ) . dispose ( ) ;
110+ this . lsActivatedWorkspaces . delete ( folder ) ;
111+ }
112+ }
113+ }
114+
91115 private async logStartup ( isJedi : boolean ) : Promise < void > {
92116 const outputLine = isJedi
93117 ? 'Starting Jedi Python language engine.'
@@ -119,4 +143,7 @@ export class LanguageServerExtensionActivationService implements IExtensionActiv
119143 const configurationService = this . serviceContainer . get < IConfigurationService > ( IConfigurationService ) ;
120144 return configurationService . getSettings ( this . resource ) . jediEnabled ;
121145 }
146+ private getWorkspacePathKey ( resource : Resource ) : string {
147+ return this . workspaceService . getWorkspaceFolderIdentifier ( resource , workspacePathNameForGlobalWorkspaces ) ;
148+ }
122149}
0 commit comments