comments := f.Comments[cstart:cend]
// Assign each comment to the import spec preceding it.
- importComment := map[*ImportSpec][]*CommentGroup{}
+ importComments := map[*ImportSpec][]*CommentGroup{}
specIndex := 0
for _, g := range comments {
for specIndex+1 < len(specs) && pos[specIndex+1].Start <= g.Pos() {
specIndex++
}
s := specs[specIndex].(*ImportSpec)
- importComment[s] = append(importComment[s], g)
+ importComments[s] = append(importComments[s], g)
}
// Sort the import specs by import path.
// Reassign the import paths to have the same position sequence.
// Reassign each comment to abut the end of its spec.
// Sort the comments by new position.
- sort.Sort(byImportSpec(specs))
+ sort.Slice(specs, func(i, j int) bool {
+ ipath := importPath(specs[i])
+ jpath := importPath(specs[j])
+ if ipath != jpath {
+ return ipath < jpath
+ }
+ iname := importName(specs[i])
+ jname := importName(specs[j])
+ if iname != jname {
+ return iname < jname
+ }
+ return importComment(specs[i]) < importComment(specs[j])
+ })
// Dedup. Thanks to our sorting, we can just consider
// adjacent pairs of imports.
}
s.Path.ValuePos = pos[i].Start
s.EndPos = pos[i].End
- for _, g := range importComment[s] {
+ for _, g := range importComments[s] {
for _, c := range g.List {
c.Slash = pos[i].End
}
}
}
- sort.Sort(byCommentPos(comments))
+ sort.Slice(comments, func(i, j int) bool {
+ return comments[i].Pos() < comments[j].Pos()
+ })
return specs
}
-
-type byImportSpec []Spec // slice of *ImportSpec
-
-func (x byImportSpec) Len() int { return len(x) }
-func (x byImportSpec) Swap(i, j int) { x[i], x[j] = x[j], x[i] }
-func (x byImportSpec) Less(i, j int) bool {
- ipath := importPath(x[i])
- jpath := importPath(x[j])
- if ipath != jpath {
- return ipath < jpath
- }
- iname := importName(x[i])
- jname := importName(x[j])
- if iname != jname {
- return iname < jname
- }
- return importComment(x[i]) < importComment(x[j])
-}
-
-type byCommentPos []*CommentGroup
-
-func (x byCommentPos) Len() int { return len(x) }
-func (x byCommentPos) Swap(i, j int) { x[i], x[j] = x[j], x[i] }
-func (x byCommentPos) Less(i, j int) bool { return x[i].Pos() < x[j].Pos() }
}
list = append(list, flist...)
}
- sort.Sort(exampleByName(list))
+ // sort by name
+ sort.Slice(list, func(i, j int) bool {
+ return list[i].Name < list[j].Name
+ })
return list
}
return !unicode.IsLower(rune)
}
-type exampleByName []*Example
-
-func (s exampleByName) Len() int { return len(s) }
-func (s exampleByName) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
-func (s exampleByName) Less(i, j int) bool { return s[i].Name < s[j].Name }
-
// playExample synthesizes a new *ast.File based on the provided
// file with the provided function body as the body of main.
func playExample(file *ast.File, body *ast.BlockStmt) *ast.File {
list = append(list, m)
}
}
- sort.Sort(byUniqueName(list))
+ // sort by unique name
+ sort.Slice(list, func(i, j int) bool {
+ return list[i].obj.Id() < list[j].obj.Id()
+ })
return &MethodSet{list}
}
_, isPtr := deref(f.typ.(*Signature).recv.typ)
return isPtr
}
-
-// byUniqueName function lists can be sorted by their unique names.
-type byUniqueName []*Selection
-
-func (a byUniqueName) Len() int { return len(a) }
-func (a byUniqueName) Less(i, j int) bool { return a[i].obj.Id() < a[j].obj.Id() }
-func (a byUniqueName) Swap(i, j int) { a[i], a[j] = a[j], a[i] }