Precompute canonical paths before sorting

This commit is contained in:
2026-10-09 03:27:42 +00:00
parent 9ac65ccc08
commit d34a98d34b
11 changed files with 209 additions and 39 deletions

View File

@@ -85,15 +85,32 @@ func canonicalDiagnostics(diagnostics []Diagnostic) []Diagnostic {
for i := range canonical {
slices.SortFunc(canonical[i].Related, compareLocation)
}
slices.SortFunc(canonical, compareDiagnostic)
type orderedDiagnostic struct {
diagnostic Diagnostic
pointer string
}
ordered := make([]orderedDiagnostic, len(canonical))
for i, diagnostic := range canonical {
ordered[i] = orderedDiagnostic{diagnostic: diagnostic, pointer: diagnostic.Path.String()}
}
slices.SortFunc(ordered, func(left, right orderedDiagnostic) int {
return compareDiagnosticWithPaths(left.diagnostic, right.diagnostic, left.pointer, right.pointer)
})
for i, item := range ordered {
canonical[i] = item.diagnostic
}
return canonical
}
func compareDiagnostic(left, right Diagnostic) int {
return compareDiagnosticWithPaths(left, right, left.Path.String(), right.Path.String())
}
func compareDiagnosticWithPaths(left, right Diagnostic, leftPath, rightPath string) int {
if order := compareOptionalLocation(left.Primary, right.Primary); order != 0 {
return order
}
if order := cmp.Compare(left.Path.String(), right.Path.String()); order != 0 {
if order := cmp.Compare(leftPath, rightPath); order != 0 {
return order
}
if order := cmp.Compare(left.Code, right.Code); order != 0 {