@@ -21,7 +21,7 @@ import {readNgCompletionData, tsCompletionEntryToLspCompletionItem} from './comp
2121import { tsDiagnosticToLspDiagnostic } from './diagnostic' ;
2222import { resolveAndRunNgcc } from './ngcc' ;
2323import { 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' ;
2525import { resolve , Version } from './version_provider' ;
2626
2727export interface SessionOptions {
@@ -54,6 +54,7 @@ export class Session {
5454 private readonly ivy : boolean ;
5555 private readonly configuredProjToExternalProj = new Map < string , string > ( ) ;
5656 private readonly logToConsole : boolean ;
57+ private readonly openFiles = new MruTracker ( ) ;
5758 private diagnosticsTimeout : NodeJS . Timeout | null = null ;
5859 private isProjectLoading = false ;
5960 /**
@@ -308,12 +309,9 @@ export class Session {
308309 // not affect other files because it is local to the Component.
309310 files . push ( file ) ;
310311 } 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 ) ;
317315 if ( scriptInfo ) {
318316 files . push ( scriptInfo . fileName ) ;
319317 }
@@ -460,6 +458,7 @@ export class Session {
460458 if ( ! filePath ) {
461459 return ;
462460 }
461+ this . openFiles . update ( filePath ) ;
463462 // External templates (HTML files) should be tagged as ScriptKind.Unknown
464463 // so that they don't get parsed as TS files. See
465464 // https://github.com/microsoft/TypeScript/blob/b217f22e798c781f55d17da72ed099a9dee5c650/src/compiler/program.ts#L1897-L1899
@@ -521,6 +520,7 @@ export class Session {
521520 if ( ! filePath ) {
522521 return ;
523522 }
523+ this . openFiles . delete ( filePath ) ;
524524 this . projectService . closeClientFile ( filePath ) ;
525525 }
526526
@@ -557,6 +557,7 @@ export class Session {
557557 if ( ! filePath ) {
558558 return ;
559559 }
560+ this . openFiles . update ( filePath ) ;
560561 const scriptInfo = this . projectService . getScriptInfo ( filePath ) ;
561562 if ( ! scriptInfo ) {
562563 this . error ( `Failed to get script info for ${ filePath } ` ) ;
@@ -582,6 +583,10 @@ export class Session {
582583 private onDidSaveTextDocument ( params : lsp . DidSaveTextDocumentParams ) {
583584 const { text, textDocument} = params ;
584585 const filePath = uriToFilePath ( textDocument . uri ) ;
586+ if ( ! filePath ) {
587+ return ;
588+ }
589+ this . openFiles . update ( filePath ) ;
585590 const scriptInfo = this . projectService . getScriptInfo ( filePath ) ;
586591 if ( ! scriptInfo ) {
587592 return ;
0 commit comments