@@ -7,7 +7,7 @@ import { inject, injectable, multiInject } from 'inversify';
77import { TextDocument , workspace } from 'vscode' ;
88import { IApplicationDiagnostics } from '../application/types' ;
99import { IDocumentManager , IWorkspaceService } from '../common/application/types' ;
10- import { isTestExecution } from '../common/constants' ;
10+ import { PYTHON_LANGUAGE } from '../common/constants' ;
1111import { traceDecorators } from '../common/logger' ;
1212import { IDisposable , Resource } from '../common/types' ;
1313import { IInterpreterAutoSelectionService } from '../interpreter/autoSelection/types' ;
@@ -26,27 +26,40 @@ export class ExtensionActivationManager implements IExtensionActivationManager {
2626 @inject ( IInterpreterAutoSelectionService ) private readonly autoSelection : IInterpreterAutoSelectionService ,
2727 @inject ( IApplicationDiagnostics ) private readonly appDiagnostics : IApplicationDiagnostics ,
2828 @inject ( IWorkspaceService ) private readonly workspaceService : IWorkspaceService
29- ) { }
29+ ) { }
3030
3131 public dispose ( ) {
3232 while ( this . disposables . length > 0 ) {
33- const disposable = this . disposables . shift ( ) ;
33+ const disposable = this . disposables . shift ( ) ! ;
3434 disposable . dispose ( ) ;
3535 }
36- if ( this . docOpenedHandler ) {
36+ if ( this . docOpenedHandler ) {
3737 this . docOpenedHandler . dispose ( ) ;
3838 this . docOpenedHandler = undefined ;
3939 }
4040 }
4141 public async activate ( ) : Promise < void > {
4242 await this . initialize ( ) ;
4343 await this . activateWorkspace ( this . getActiveResource ( ) ) ;
44+ await this . autoSelection . autoSelectInterpreter ( undefined ) ;
45+ }
46+ @traceDecorators . error ( 'Failed to activate a workspace' )
47+ public async activateWorkspace ( resource : Resource ) {
48+ const key = this . getWorkspaceKey ( resource ) ;
49+ if ( this . activatedWorkspaces . has ( key ) ) {
50+ return ;
51+ }
52+ this . activatedWorkspaces . add ( key ) ;
53+ // Get latest interpreter list in the background.
54+ this . interpreterService . getInterpreters ( resource ) . ignoreErrors ( ) ;
55+
56+ await this . autoSelection . autoSelectInterpreter ( resource ) ;
57+ await Promise . all ( this . activationServices . map ( item => item . activate ( resource ) ) ) ;
58+ await this . appDiagnostics . performPreStartupHealthCheck ( resource ) ;
4459 }
4560 protected async initialize ( ) {
46- // Get latest interpreter list.
47- const mainWorkspaceUri = this . getActiveResource ( ) ;
48- this . interpreterService . getInterpreters ( mainWorkspaceUri ) . ignoreErrors ( ) ;
4961 this . addHandlers ( ) ;
62+ this . addRemoveDocOpenedHandlers ( ) ;
5063 }
5164 protected addHandlers ( ) {
5265 this . disposables . push ( this . workspaceService . onDidChangeWorkspaceFolders ( this . onWorkspaceFoldersChanged , this ) ) ;
@@ -67,45 +80,32 @@ export class ExtensionActivationManager implements IExtensionActivationManager {
6780 this . addRemoveDocOpenedHandlers ( ) ;
6881 }
6982 protected hasMultipleWorkspaces ( ) {
70- return this . workspaceService . hasWorkspaceFolders && this . workspaceService . workspaceFolders . length > 1 ;
83+ return this . workspaceService . hasWorkspaceFolders && this . workspaceService . workspaceFolders ! . length > 1 ;
7184 }
7285 protected onDocOpened ( doc : TextDocument ) {
86+ if ( doc . languageId !== PYTHON_LANGUAGE ) {
87+ return ;
88+ }
7389 const key = this . getWorkspaceKey ( doc . uri ) ;
90+ // If we have opened a doc that does not belong to workspace, then do nothing.
91+ if ( key === '' && this . workspaceService . hasWorkspaceFolders ) {
92+ return ;
93+ }
7494 if ( this . activatedWorkspaces . has ( key ) ) {
7595 return ;
7696 }
7797 const folder = this . workspaceService . getWorkspaceFolder ( doc . uri ) ;
7898 this . activateWorkspace ( folder ? folder . uri : undefined ) . ignoreErrors ( ) ;
7999 }
80- @traceDecorators . error ( 'Failed to activate a worksapce' )
81- protected async activateWorkspace ( resource : Resource ) {
82- const key = this . getWorkspaceKey ( resource ) ;
83- this . activatedWorkspaces . add ( key ) ;
84-
85- await Promise . all ( this . activationServices . map ( item => item . activate ( resource ) ) ) ;
86-
87- // When testing, do not perform health checks, as modal dialogs can be displayed.
88- if ( ! isTestExecution ( ) ) {
89- await this . appDiagnostics . performPreStartupHealthCheck ( resource ) ;
90- }
91- await this . autoSelection . autoSelectInterpreter ( resource ) ;
92- }
93100 protected getWorkspaceKey ( resource : Resource ) {
94- if ( ! resource ) {
95- return '' ;
96- }
97- const workspaceFolder = this . workspaceService . getWorkspaceFolder ( resource ) ;
98- if ( ! workspaceFolder ) {
99- return '' ;
100- }
101- return workspaceFolder . uri . fsPath ;
101+ return this . workspaceService . getWorkspaceFolderIdentifier ( resource , '' ) ;
102102 }
103103 private getActiveResource ( ) : Resource {
104104 if ( this . documentManager . activeTextEditor && ! this . documentManager . activeTextEditor . document . isUntitled ) {
105105 return this . documentManager . activeTextEditor . document . uri ;
106106 }
107- return Array . isArray ( this . workspaceService . workspaceFolders ) && workspace . workspaceFolders . length > 0
108- ? workspace . workspaceFolders [ 0 ] . uri
107+ return Array . isArray ( this . workspaceService . workspaceFolders ) && workspace . workspaceFolders ! . length > 0
108+ ? workspace . workspaceFolders ! [ 0 ] . uri
109109 : undefined ;
110110 }
111111}
0 commit comments