16
16
import java .util .HashMap ;
17
17
import java .util .List ;
18
18
import java .util .Map ;
19
+ import java .util .concurrent .CancellationException ;
19
20
import java .util .concurrent .ExecutionException ;
20
21
import java .util .concurrent .TimeUnit ;
22
+ import java .util .concurrent .TimeoutException ;
21
23
22
24
import org .eclipse .core .resources .IFile ;
23
25
import org .eclipse .core .resources .IMarker ;
24
26
import org .eclipse .core .resources .IProject ;
25
27
import org .eclipse .core .resources .IResource ;
26
28
import org .eclipse .core .runtime .CoreException ;
27
29
import org .eclipse .core .runtime .IProgressMonitor ;
30
+ import org .eclipse .core .runtime .OperationCanceledException ;
31
+ import org .eclipse .core .runtime .SubMonitor ;
28
32
import org .eclipse .jface .text .IDocument ;
33
+ import org .eclipse .osgi .util .NLS ;
29
34
30
35
import ts .TypeScriptException ;
31
36
import ts .TypeScriptNoContentAvailableException ;
51
56
import ts .eclipse .ide .core .utils .TypeScriptResourceUtil ;
52
57
import ts .eclipse .ide .core .utils .WorkbenchResourceUtil ;
53
58
import ts .eclipse .ide .internal .core .Trace ;
59
+ import ts .eclipse .ide .internal .core .TypeScriptCoreMessages ;
54
60
import ts .eclipse .ide .internal .core .compiler .IDETypeScriptCompiler ;
55
61
import ts .eclipse .ide .internal .core .console .TypeScriptConsoleConnectorManager ;
56
62
import ts .eclipse .ide .internal .core .resources .jsonconfig .JsonConfigResourcesManager ;
@@ -376,19 +382,19 @@ protected ITypeScriptLint createTslint(File tslintFile, File tslintJsonFile, Fil
376
382
@ Override
377
383
public void compileWithTsserver (List <IFile > updatedTsFiles , List <IFile > removedTsFiles , IProgressMonitor monitor )
378
384
throws TypeScriptException {
385
+ SubMonitor subMonitor = SubMonitor .convert (monitor , TypeScriptCoreMessages .IDETypeScriptProject_compile_task ,
386
+ 100 );
387
+ List <IFile > tsFilesToClose = new ArrayList <>();
379
388
try {
380
389
List <String > tsFilesToCompile = new ArrayList <>();
381
390
// Collect ts files to compile by using tsserver to retrieve
382
391
// dependencies files.
383
392
// It works only if tsconfig.json declares "compileOnSave: true".
384
- if (collectTsFilesToCompile (updatedTsFiles , getClient (), tsFilesToCompile , false , monitor )) {
385
- return ;
386
- }
393
+ collectTsFilesToCompile (updatedTsFiles , tsFilesToCompile , tsFilesToClose , getClient (), subMonitor );
387
394
388
395
// Compile ts files with tsserver.
389
- if (compileTsFiles (tsFilesToCompile , getClient (), monitor )) {
390
- return ;
391
- }
396
+ compileTsFiles (tsFilesToCompile , getClient (), subMonitor );
397
+
392
398
if (removedTsFiles .size () > 0 ) {
393
399
// ts files was removed, how to get referenced files which must
394
400
// be recompiled (with errors)?
@@ -397,54 +403,86 @@ public void compileWithTsserver(List<IFile> updatedTsFiles, List<IFile> removedT
397
403
throw e ;
398
404
} catch (Exception e ) {
399
405
throw new TypeScriptException (e );
406
+ } finally {
407
+ for (IFile tsFile : tsFilesToClose ) {
408
+ closeFile (tsFile );
409
+ }
410
+ subMonitor .done ();
400
411
}
401
412
}
402
413
403
414
/**
404
415
* Collect ts files to compile from the given ts files list.
405
416
*
406
- * @param tsFiles
407
- * @param client
417
+ * @param updatedTsFiles
418
+ * list of TypeScript files which have changed.
408
419
* @param tsFilesToCompile
409
- * @param exclude
410
- * @param monitor
420
+ * list of collected ts files to compile.
421
+ * @param tsFilesToClose
422
+ * list of ts files to close.
423
+ * @param client
424
+ * @param subMonitor
411
425
* @throws Exception
412
426
*/
413
- private boolean collectTsFilesToCompile (List <IFile > tsFiles , ITypeScriptServiceClient client ,
414
- List <String > tsFilesToCompile , boolean exclude , IProgressMonitor monitor ) throws Exception {
415
- for (IFile tsFile : tsFiles ) {
416
- if (monitor .isCanceled ()) {
417
- return true ;
427
+ private void collectTsFilesToCompile (List <IFile > updatedTsFiles , List <String > tsFilesToCompile ,
428
+ List <IFile > tsFilesToClose , ITypeScriptServiceClient client , SubMonitor subMonitor ) throws Exception {
429
+ SubMonitor loopMonitor = subMonitor .split (50 ).setWorkRemaining (updatedTsFiles .size ());
430
+ loopMonitor .subTask (TypeScriptCoreMessages .IDETypeScriptProject_compile_collecting_step );
431
+ for (IFile tsFile : updatedTsFiles ) {
432
+ if (loopMonitor .isCanceled ()) {
433
+ throw new OperationCanceledException ();
418
434
}
419
435
String filename = WorkbenchResourceUtil .getFileName (tsFile );
436
+ loopMonitor
437
+ .subTask (NLS .bind (TypeScriptCoreMessages .IDETypeScriptProject_compile_collecting_file , filename ));
420
438
if (!tsFilesToCompile .contains (filename )) {
421
- collectTsFilesToCompile (filename , client , tsFilesToCompile , exclude );
439
+ collectTsFilesToCompile (filename , client , tsFilesToCompile , tsFilesToClose , loopMonitor );
422
440
}
441
+ loopMonitor .worked (1 );
442
+ // loopMonitor.split(1);
423
443
}
424
- return false ;
444
+ // subMonitor.setWorkRemaining(50) ;
425
445
}
426
446
427
447
/**
428
- * Collect ts files to compile from the given ts file name .
448
+ * Collect ts files to compile from the given TypeScript file.
429
449
*
430
450
* @param filename
431
451
* @param client
432
452
* @param tsFilesToCompile
433
- * @param exclude
453
+ * @param tsFilesToClose
454
+ * @param monitor
434
455
* @throws Exception
435
456
*/
436
457
private void collectTsFilesToCompile (String filename , ITypeScriptServiceClient client ,
437
- List <String > tsFilesToCompile , boolean exclude ) throws Exception {
438
- // call tsserver compileOnSaveAffectedFileList to retrieve file
439
- // dependencies of the given filename
440
- List <CompileOnSaveAffectedFileListSingleProject > affectedProjects = client
441
- .compileOnSaveAffectedFileList (filename ).get (5000 , TimeUnit .MILLISECONDS );
442
- for (CompileOnSaveAffectedFileListSingleProject affectedProject : affectedProjects ) {
443
- List <String > affectedTsFilenames = affectedProject .getFileNames ();
444
- for (String affectedFilename : affectedTsFilenames ) {
445
- if (!tsFilesToCompile .contains (affectedFilename ) && !(exclude && filename .equals (affectedFilename ))) {
446
- tsFilesToCompile .add (affectedFilename );
458
+ List <String > tsFilesToCompile , List <IFile > tsFilesToClose , IProgressMonitor monitor ) throws Exception {
459
+ while (!monitor .isCanceled ()) {
460
+ try {
461
+ // When tsserver is not started, it takes time, we try to collect TypeScript
462
+ // files every time and stop the search if user stops the builder.
463
+ List <CompileOnSaveAffectedFileListSingleProject > affectedProjects = client
464
+ .compileOnSaveAffectedFileList (filename ).get (5000 , TimeUnit .MILLISECONDS );
465
+ if (affectedProjects .size () == 0 && getOpenedFile (filename ) == null ) {
466
+ // Case when none TypeScript files are opened.
467
+ // In this case, compileOnSaveAffectedFileList returns null, the tsserver needs
468
+ // having just one opened TypeScript file
469
+ // in order to compileOnSaveAffectedFileList returns the well list.
470
+ IFile tsFile = WorkbenchResourceUtil .findFileFromWorkspace (filename );
471
+ openFile (tsFile , null );
472
+ tsFilesToClose .add (tsFile );
473
+ affectedProjects = client .compileOnSaveAffectedFileList (filename ).get (5000 , TimeUnit .MILLISECONDS );
447
474
}
475
+ for (CompileOnSaveAffectedFileListSingleProject affectedProject : affectedProjects ) {
476
+ List <String > affectedTsFilenames = affectedProject .getFileNames ();
477
+ for (String affectedFilename : affectedTsFilenames ) {
478
+ if (!tsFilesToCompile .contains (affectedFilename )) {
479
+ tsFilesToCompile .add (affectedFilename );
480
+ }
481
+ }
482
+ }
483
+ return ;
484
+ } catch (TimeoutException e ) {
485
+ // tsserver is not initialized, retry again...
448
486
}
449
487
}
450
488
}
@@ -454,28 +492,32 @@ private void collectTsFilesToCompile(String filename, ITypeScriptServiceClient c
454
492
*
455
493
* @param tsFilesToCompile
456
494
* @param client
457
- * @param monitor
458
- * @return true if process must be stopped and false otherwise.
495
+ * @param subMonitor
459
496
* @throws Exception
460
497
*/
461
- private boolean compileTsFiles (List <String > tsFilesToCompile , ITypeScriptServiceClient client ,
462
- IProgressMonitor monitor ) throws Exception {
498
+ private void compileTsFiles (List <String > tsFilesToCompile , ITypeScriptServiceClient client , SubMonitor subMonitor )
499
+ throws Exception {
500
+ SubMonitor loopMonitor = subMonitor .newChild (50 ).setWorkRemaining (tsFilesToCompile .size ());// subMonitor.split(50).setWorkRemaining(tsFilesToCompile.size());
501
+ loopMonitor .subTask (TypeScriptCoreMessages .IDETypeScriptProject_compile_compiling_step );
463
502
for (String filename : tsFilesToCompile ) {
464
- if (monitor .isCanceled ()) {
465
- return true ;
466
- }
467
503
try {
504
+ if (loopMonitor .isCanceled ()) {
505
+ throw new OperationCanceledException ();
506
+ }
507
+ loopMonitor .subTask (
508
+ NLS .bind (TypeScriptCoreMessages .IDETypeScriptProject_compile_compiling_file , filename ));
468
509
compileTsFile (filename , client );
510
+ loopMonitor .worked (1 );
511
+ // loopMonitor.split(1);
469
512
} catch (ExecutionException e ) {
470
513
if (e .getCause () instanceof TypeScriptNoContentAvailableException ) {
471
514
// Ignore "No content available" error.
472
- }
473
- else {
515
+ } else {
474
516
throw e ;
475
517
}
476
518
}
477
519
}
478
- return false ;
520
+ // subMonitor.setWorkRemaining(100) ;
479
521
}
480
522
481
523
/**
@@ -493,7 +535,7 @@ private void compileTsFile(String filename, ITypeScriptServiceClient client) thr
493
535
if (tsFile != null ) {
494
536
// Delete TypeScript error marker
495
537
TypeScriptResourceUtil .deleteTscMarker (tsFile );
496
- // Add TypeScript error marker if there error errors.
538
+ // Add TypeScript error marker if there are errors.
497
539
DiagnosticEventBody event = client .syntacticDiagnosticsSync (filename , true ).get (5000 ,
498
540
TimeUnit .MILLISECONDS );
499
541
addMarker (tsFile , event );
0 commit comments