Skip to content

Commit 2a8da33

Browse files
egonelbregopherbot
authored andcommitted
internal/godoc/dochtml: use natural sorting for items
This ensures that simd/archsimd package items are sorted nicely. For example: "Uint8x64" < "Uint32x16" < "Uint64x8" Fixes golang/go#77160 Change-Id: Ie08696262496e226120ae1b31ca81e02b7f20c65 Reviewed-on: https://go-review.googlesource.com/c/pkgsite/+/736561 Auto-Submit: Sean Liao <sean@liao.dev> kokoro-CI: kokoro <noreply+kokoro@google.com> Reviewed-by: Michael Knyszek <mknyszek@google.com> Reviewed-by: Ethan Lee <ethanalee@google.com> Reviewed-by: Sean Liao <sean@liao.dev> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
1 parent d0bf40e commit 2a8da33

File tree

4 files changed

+368
-2
lines changed

4 files changed

+368
-2
lines changed

internal/godoc/dochtml/dochtml.go

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"go/doc"
2020
"go/printer"
2121
"go/token"
22+
"slices"
2223
"sort"
2324
"strings"
2425

@@ -30,6 +31,7 @@ import (
3031
"golang.org/x/pkgsite/internal/derrors"
3132
"golang.org/x/pkgsite/internal/godoc/dochtml/internal/render"
3233
"golang.org/x/pkgsite/internal/log"
34+
"golang.org/x/pkgsite/internal/natural"
3335
"golang.org/x/text/cases"
3436
"golang.org/x/text/language"
3537
)
@@ -151,9 +153,26 @@ func packageToItems(p *doc.Package, exmap map[string][]*example) (consts, vars,
151153
for _, t := range p.Types {
152154
types = append(types, typeToItem(t, exmap))
153155
}
156+
157+
slices.SortFunc(consts, compareItems)
158+
slices.SortFunc(vars, compareItems)
159+
slices.SortFunc(funcs, compareItems)
160+
slices.SortFunc(types, compareItems)
161+
162+
for _, typ := range types {
163+
slices.SortFunc(typ.Consts, compareItems)
164+
slices.SortFunc(typ.Vars, compareItems)
165+
slices.SortFunc(typ.Funcs, compareItems)
166+
slices.SortFunc(typ.Methods, compareItems)
167+
}
168+
154169
return consts, vars, funcs, types
155170
}
156171

172+
func compareItems(a, b *item) int {
173+
return natural.Compare(a.Name, b.Name)
174+
}
175+
157176
func valuesToItems(vs []*doc.Value) []*item {
158177
var r []*item
159178
for _, v := range vs {
@@ -402,7 +421,7 @@ func collectExamples(p *doc.Package) *examples {
402421
sort.SliceStable(exs.List, func(i, j int) bool {
403422
// TODO: Break ties by sorting by suffix, unless
404423
// not needed because of upstream slice order.
405-
return exs.List[i].ParentID < exs.List[j].ParentID
424+
return natural.Less(exs.List[i].ParentID, exs.List[j].ParentID)
406425
})
407426
return exs
408427
}

internal/godoc/dochtml/dochtml_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ var testRenderOptions = RenderOptions{
4848
func TestRender(t *testing.T) {
4949
ctx := context.Background()
5050
LoadTemplates(templateFS)
51-
for _, pkg := range []string{"everydecl", "comments"} {
51+
for _, pkg := range []string{"everydecl", "comments", "order"} {
5252
t.Run(pkg, func(t *testing.T) {
5353
fset, d := mustLoadPackage(pkg)
5454
parts, err := Render(ctx, fset, d, testRenderOptions)
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// Copyright 2026 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
// Package order exercises natural sorting of symbols.
6+
package order
7+
8+
// Uint32x16 represents a 128-bit unsigned integer.
9+
type Uint32x16 struct {
10+
data [16]uint32
11+
}
12+
13+
// AsUint8x64 converts a Uint32x16 to a Uint8x64.
14+
func (u Uint32x16) AsUint8x64() Uint8x64 {
15+
return Uint8x64{}
16+
}
17+
18+
// AsUint64x8 converts a Uint32x16 to a Uint64x8.
19+
func (u Uint32x16) AsUint64x8() Uint8x64 {
20+
return Uint64x8{}
21+
}
22+
23+
// Uint8x64 represents a 128-bit unsigned integer.
24+
type Uint8x64 struct {
25+
data [64]uint8
26+
}
27+
28+
// AsUint32x16 converts a Uint8x64 to a Uint32x16.
29+
func (u Uint8x64) AsUint32x16() Uint32x16 {
30+
return Uint32x16{}
31+
}
32+
33+
// AsUint64x8 converts a Uint8x64 to a Uint64x8.
34+
func (u Uint8x64) AsUint64x8() Uint64x8 {
35+
return Uint64x8{}
36+
}
37+
38+
// Uint64x8 represents a 128-bit unsigned integer.
39+
type Uint64x8 struct {
40+
data [8]uint64
41+
}
42+
43+
// AsUint8x64 converts a Uint64x8 to a Uint8x64.
44+
func (u Uint64x8) AsUint8x64() Uint8x64 {
45+
return Uint8x64{}
46+
}
47+
48+
// AsUint32x16 converts a Uint64x8 to a Uint32x16.
49+
func (u Uint64x8) AsUint32x16() Uint32x16 {
50+
return Uint32x16{}
51+
}
52+
53+
func ExampleUint64x8_AsUint32x16() {}
54+
55+
func ExampleUint64x8_AsUint8x64() {}

0 commit comments

Comments
 (0)