33import * as path from 'path' ;
44import * as os from 'os' ;
55import * as fs from 'fs' ;
6- import { workspace , extensions , ExtensionContext , window , StatusBarAlignment , commands , ViewColumn , Uri , CancellationToken , TextDocumentContentProvider , TextEditor , WorkspaceConfiguration , languages , IndentAction , ProgressLocation , InputBoxOptions , Selection , Position , EventEmitter , OutputChannel } from 'vscode' ;
6+ import { workspace , extensions , ExtensionContext , window , StatusBarAlignment , commands , ViewColumn , Uri , CancellationToken , TextDocumentContentProvider , WorkspaceConfiguration , languages , IndentAction , ProgressLocation , InputBoxOptions , Selection , Position , EventEmitter , OutputChannel , TextDocument } from 'vscode' ;
77import { ExecuteCommandParams , ExecuteCommandRequest , LanguageClient , LanguageClientOptions , RevealOutputChannelOn , Position as LSPosition , Location as LSLocation , StreamInfo , VersionedTextDocumentIdentifier , ErrorHandler , Message , ErrorAction , CloseAction , InitializationFailedHandler , DidChangeConfigurationNotification } from 'vscode-languageclient' ;
88import { onExtensionChange , collectJavaExtensions } from './plugin' ;
99import { prepareExecutable , awaitServerConnection } from './javaServerStarter' ;
@@ -135,8 +135,9 @@ export function activate(context: ExtensionContext): Promise<ExtensionAPI> {
135135 } ) ;
136136 // rethrow to disrupt the chain.
137137 throw error ;
138- } ) . then ( requirements => {
139- return new Promise ( ( resolve , reject ) => {
138+ } ) . then ( async ( requirements ) => {
139+ const triggerFiles = await getTriggerFiles ( ) ;
140+ return new Promise < ExtensionAPI > ( ( resolve , reject ) => {
140141 const workspacePath = path . resolve ( storagePath + '/jdt_ws' ) ;
141142 // Options to control the language client
142143 const clientOptions : LanguageClientOptions = {
@@ -167,7 +168,7 @@ export function activate(context: ExtensionContext): Promise<ExtensionAPI> {
167168 moveRefactoringSupport : true ,
168169 clientHoverProvider : true ,
169170 } ,
170- triggerFiles : getTriggerFiles ( )
171+ triggerFiles,
171172 } ,
172173 middleware : {
173174 workspace : {
@@ -822,9 +823,9 @@ async function executeRangeFormat(editor, startPosition, lineOffset) {
822823 await commands . executeCommand ( 'editor.action.formatSelection' ) ;
823824}
824825
825- function getTriggerFiles ( ) : string [ ] {
826+ async function getTriggerFiles ( ) : Promise < string [ ] > {
826827 const openedJavaFiles = [ ] ;
827- const activeJavaFile = getJavaFilePathOfTextEditor ( window . activeTextEditor ) ;
828+ const activeJavaFile = getJavaFilePathOfTextDocument ( window . activeTextEditor && window . activeTextEditor . document ) ;
828829 if ( activeJavaFile ) {
829830 openedJavaFiles . push ( Uri . file ( activeJavaFile ) . toString ( ) ) ;
830831 }
@@ -833,31 +834,53 @@ function getTriggerFiles(): string[] {
833834 return openedJavaFiles ;
834835 }
835836
836- for ( const rootFolder of workspace . workspaceFolders ) {
837+ await Promise . all ( workspace . workspaceFolders . map ( async ( rootFolder ) => {
837838 if ( rootFolder . uri . scheme !== 'file' ) {
838- continue ;
839+ return ;
839840 }
840841
841842 const rootPath = path . normalize ( rootFolder . uri . fsPath ) ;
842843 if ( isPrefix ( rootPath , activeJavaFile ) ) {
843- continue ;
844+ return ;
844845 }
845846
846847 for ( const textEditor of window . visibleTextEditors ) {
847- const javaFileInTextEditor = getJavaFilePathOfTextEditor ( textEditor ) ;
848+ const javaFileInTextEditor = getJavaFilePathOfTextDocument ( textEditor . document ) ;
848849 if ( isPrefix ( rootPath , javaFileInTextEditor ) ) {
849850 openedJavaFiles . push ( Uri . file ( javaFileInTextEditor ) . toString ( ) ) ;
850- break ;
851+ return ;
851852 }
852853 }
853- }
854+
855+ for ( const textDocument of workspace . textDocuments ) {
856+ const javaFileInTextDocument = getJavaFilePathOfTextDocument ( textDocument ) ;
857+ if ( isPrefix ( rootPath , javaFileInTextDocument ) ) {
858+ openedJavaFiles . push ( Uri . file ( javaFileInTextDocument ) . toString ( ) ) ;
859+ return ;
860+ }
861+ }
862+
863+ for ( const javaFile of await workspace . findFiles ( "*.java" , undefined , 1 ) ) { // Find at most 1 java file
864+ if ( isPrefix ( rootPath , javaFile . fsPath ) ) {
865+ openedJavaFiles . push ( javaFile . toString ( ) ) ;
866+ return ;
867+ }
868+ }
869+
870+ for ( const javaFile of await workspace . findFiles ( "{src, test}/**/*.java" , undefined , 1 ) ) {
871+ if ( isPrefix ( rootPath , javaFile . fsPath ) ) {
872+ openedJavaFiles . push ( javaFile . toString ( ) ) ;
873+ return ;
874+ }
875+ }
876+ } ) ) ;
854877
855878 return openedJavaFiles ;
856879}
857880
858- function getJavaFilePathOfTextEditor ( editor : TextEditor ) : string | undefined {
859- if ( editor ) {
860- const resource = editor . document . uri ;
881+ function getJavaFilePathOfTextDocument ( document : TextDocument ) : string | undefined {
882+ if ( document ) {
883+ const resource = document . uri ;
861884 if ( resource . scheme === 'file' && resource . fsPath . endsWith ( '.java' ) ) {
862885 return path . normalize ( resource . fsPath ) ;
863886 }
0 commit comments