diff --git a/internal/ast/diagnostic.go b/internal/ast/diagnostic.go index 7757501881..692a2fcc46 100644 --- a/internal/ast/diagnostic.go +++ b/internal/ast/diagnostic.go @@ -5,6 +5,7 @@ import ( "slices" "strings" + "github.com/microsoft/typescript-go/internal/collections" "github.com/microsoft/typescript-go/internal/core" "github.com/microsoft/typescript-go/internal/diagnostics" ) @@ -122,8 +123,10 @@ func NewCompilerDiagnostic(message *diagnostics.Message, args ...any) *Diagnosti } type DiagnosticsCollection struct { - fileDiagnostics map[string][]*Diagnostic - nonFileDiagnostics []*Diagnostic + fileDiagnostics map[string][]*Diagnostic + fileDiagnosticsSorted collections.Set[string] + nonFileDiagnostics []*Diagnostic + nonFileDiagnosticsSorted bool } func (c *DiagnosticsCollection) Add(diagnostic *Diagnostic) { @@ -132,18 +135,20 @@ func (c *DiagnosticsCollection) Add(diagnostic *Diagnostic) { if c.fileDiagnostics == nil { c.fileDiagnostics = make(map[string][]*Diagnostic) } - c.fileDiagnostics[fileName] = core.InsertSorted(c.fileDiagnostics[fileName], diagnostic, CompareDiagnostics) + c.fileDiagnostics[fileName] = append(c.fileDiagnostics[fileName], diagnostic) + c.fileDiagnosticsSorted.Delete(fileName) } else { - c.nonFileDiagnostics = core.InsertSorted(c.nonFileDiagnostics, diagnostic, CompareDiagnostics) + c.nonFileDiagnostics = append(c.nonFileDiagnostics, diagnostic) + c.nonFileDiagnosticsSorted = false } } func (c *DiagnosticsCollection) Lookup(diagnostic *Diagnostic) *Diagnostic { var diagnostics []*Diagnostic if diagnostic.File() != nil { - diagnostics = c.fileDiagnostics[diagnostic.File().FileName()] + diagnostics = c.GetDiagnosticsForFile(diagnostic.File().FileName()) } else { - diagnostics = c.nonFileDiagnostics + diagnostics = c.GetGlobalDiagnostics() } if i, ok := slices.BinarySearchFunc(diagnostics, diagnostic, CompareDiagnostics); ok { return diagnostics[i] @@ -152,10 +157,18 @@ func (c *DiagnosticsCollection) Lookup(diagnostic *Diagnostic) *Diagnostic { } func (c *DiagnosticsCollection) GetGlobalDiagnostics() []*Diagnostic { + if !c.nonFileDiagnosticsSorted { + slices.SortStableFunc(c.nonFileDiagnostics, CompareDiagnostics) + c.nonFileDiagnosticsSorted = true + } return c.nonFileDiagnostics } func (c *DiagnosticsCollection) GetDiagnosticsForFile(fileName string) []*Diagnostic { + if !c.fileDiagnosticsSorted.Has(fileName) { + slices.SortStableFunc(c.fileDiagnostics[fileName], CompareDiagnostics) + c.fileDiagnosticsSorted.Add(fileName) + } return c.fileDiagnostics[fileName] }