55 "slices"
66 "strings"
77
8+ "github.com/microsoft/typescript-go/internal/collections"
89 "github.com/microsoft/typescript-go/internal/core"
910 "github.com/microsoft/typescript-go/internal/diagnostics"
1011 "github.com/microsoft/typescript-go/internal/locale"
@@ -140,8 +141,10 @@ func NewCompilerDiagnostic(message *diagnostics.Message, args ...any) *Diagnosti
140141}
141142
142143type DiagnosticsCollection struct {
143- fileDiagnostics map [string ][]* Diagnostic
144- nonFileDiagnostics []* Diagnostic
144+ fileDiagnostics map [string ][]* Diagnostic
145+ fileDiagnosticsSorted collections.Set [string ]
146+ nonFileDiagnostics []* Diagnostic
147+ nonFileDiagnosticsSorted bool
145148}
146149
147150func (c * DiagnosticsCollection ) Add (diagnostic * Diagnostic ) {
@@ -150,18 +153,20 @@ func (c *DiagnosticsCollection) Add(diagnostic *Diagnostic) {
150153 if c .fileDiagnostics == nil {
151154 c .fileDiagnostics = make (map [string ][]* Diagnostic )
152155 }
153- c .fileDiagnostics [fileName ] = core .InsertSorted (c .fileDiagnostics [fileName ], diagnostic , CompareDiagnostics )
156+ c .fileDiagnostics [fileName ] = append (c .fileDiagnostics [fileName ], diagnostic )
157+ c .fileDiagnosticsSorted .Delete (fileName )
154158 } else {
155- c .nonFileDiagnostics = core .InsertSorted (c .nonFileDiagnostics , diagnostic , CompareDiagnostics )
159+ c .nonFileDiagnostics = append (c .nonFileDiagnostics , diagnostic )
160+ c .nonFileDiagnosticsSorted = false
156161 }
157162}
158163
159164func (c * DiagnosticsCollection ) Lookup (diagnostic * Diagnostic ) * Diagnostic {
160165 var diagnostics []* Diagnostic
161166 if diagnostic .File () != nil {
162- diagnostics = c .fileDiagnostics [ diagnostic .File ().FileName ()]
167+ diagnostics = c .GetDiagnosticsForFile ( diagnostic .File ().FileName ())
163168 } else {
164- diagnostics = c .nonFileDiagnostics
169+ diagnostics = c .GetGlobalDiagnostics ()
165170 }
166171 if i , ok := slices .BinarySearchFunc (diagnostics , diagnostic , CompareDiagnostics ); ok {
167172 return diagnostics [i ]
@@ -170,10 +175,18 @@ func (c *DiagnosticsCollection) Lookup(diagnostic *Diagnostic) *Diagnostic {
170175}
171176
172177func (c * DiagnosticsCollection ) GetGlobalDiagnostics () []* Diagnostic {
178+ if ! c .nonFileDiagnosticsSorted {
179+ slices .SortStableFunc (c .nonFileDiagnostics , CompareDiagnostics )
180+ c .nonFileDiagnosticsSorted = true
181+ }
173182 return c .nonFileDiagnostics
174183}
175184
176185func (c * DiagnosticsCollection ) GetDiagnosticsForFile (fileName string ) []* Diagnostic {
186+ if ! c .fileDiagnosticsSorted .Has (fileName ) {
187+ slices .SortStableFunc (c .fileDiagnostics [fileName ], CompareDiagnostics )
188+ c .fileDiagnosticsSorted .Add (fileName )
189+ }
177190 return c .fileDiagnostics [fileName ]
178191}
179192
0 commit comments