@@ -21,7 +21,7 @@ import {readNgCompletionData, tsCompletionEntryToLspCompletionItem} from './comp
21
21
import { tsDiagnosticToLspDiagnostic } from './diagnostic' ;
22
22
import { resolveAndRunNgcc } from './ngcc' ;
23
23
import { ServerHost } from './server_host' ;
24
- import { filePathToUri , isConfiguredProject , isDebugMode , lspPositionToTsPosition , lspRangeToTsPositions , tsTextSpanToLspRange , uriToFilePath } from './utils' ;
24
+ import { filePathToUri , isConfiguredProject , isDebugMode , lspPositionToTsPosition , lspRangeToTsPositions , MruTracker , tsTextSpanToLspRange , uriToFilePath } from './utils' ;
25
25
import { resolve , Version } from './version_provider' ;
26
26
27
27
export interface SessionOptions {
@@ -54,6 +54,7 @@ export class Session {
54
54
private readonly ivy : boolean ;
55
55
private readonly configuredProjToExternalProj = new Map < string , string > ( ) ;
56
56
private readonly logToConsole : boolean ;
57
+ private readonly openFiles = new MruTracker ( ) ;
57
58
private diagnosticsTimeout : NodeJS . Timeout | null = null ;
58
59
private isProjectLoading = false ;
59
60
/**
@@ -308,12 +309,9 @@ export class Session {
308
309
// not affect other files because it is local to the Component.
309
310
files . push ( file ) ;
310
311
} else {
311
- // Get all open files. Cast is needed because the key of the map is
312
- // actually ts.Path. See
313
- // https://github.com/microsoft/TypeScript/blob/496a1d3caa21c762daa95b1ac1b75823f8774575/src/server/editorServices.ts#L978
314
- const openFiles = toArray ( this . projectService . openFiles . keys ( ) ) as ts . Path [ ] ;
315
- for ( const openFile of openFiles ) {
316
- const scriptInfo = this . projectService . getScriptInfoForPath ( openFile ) ;
312
+ // Get all open files, most recently used first.
313
+ for ( const openFile of this . openFiles . getAll ( ) ) {
314
+ const scriptInfo = this . projectService . getScriptInfo ( openFile ) ;
317
315
if ( scriptInfo ) {
318
316
files . push ( scriptInfo . fileName ) ;
319
317
}
@@ -460,6 +458,7 @@ export class Session {
460
458
if ( ! filePath ) {
461
459
return ;
462
460
}
461
+ this . openFiles . update ( filePath ) ;
463
462
// External templates (HTML files) should be tagged as ScriptKind.Unknown
464
463
// so that they don't get parsed as TS files. See
465
464
// https://github.com/microsoft/TypeScript/blob/b217f22e798c781f55d17da72ed099a9dee5c650/src/compiler/program.ts#L1897-L1899
@@ -521,6 +520,7 @@ export class Session {
521
520
if ( ! filePath ) {
522
521
return ;
523
522
}
523
+ this . openFiles . delete ( filePath ) ;
524
524
this . projectService . closeClientFile ( filePath ) ;
525
525
}
526
526
@@ -557,6 +557,7 @@ export class Session {
557
557
if ( ! filePath ) {
558
558
return ;
559
559
}
560
+ this . openFiles . update ( filePath ) ;
560
561
const scriptInfo = this . projectService . getScriptInfo ( filePath ) ;
561
562
if ( ! scriptInfo ) {
562
563
this . error ( `Failed to get script info for ${ filePath } ` ) ;
@@ -582,6 +583,10 @@ export class Session {
582
583
private onDidSaveTextDocument ( params : lsp . DidSaveTextDocumentParams ) {
583
584
const { text, textDocument} = params ;
584
585
const filePath = uriToFilePath ( textDocument . uri ) ;
586
+ if ( ! filePath ) {
587
+ return ;
588
+ }
589
+ this . openFiles . update ( filePath ) ;
585
590
const scriptInfo = this . projectService . getScriptInfo ( filePath ) ;
586
591
if ( ! scriptInfo ) {
587
592
return ;
0 commit comments