mirror of
https://github.com/golang/go
synced 2024-11-19 14:54:43 -07:00
go/*: use sort.Slice to simplify some code
Skip the ones that have multiple uses for now. Also had to rename the importComment variable as it shadowed the top-level func by the same name. Change-Id: I796285aa7b4fdf2c39e652666390427d37b063ee Reviewed-on: https://go-review.googlesource.com/63150 Run-TryBot: Daniel Martí <mvdan@mvdan.cc> Run-TryBot: Ian Lance Taylor <iant@golang.org> Reviewed-by: Ian Lance Taylor <iant@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org>
This commit is contained in:
parent
eb2dc3d3d0
commit
7377d0c7e9
@ -123,14 +123,14 @@ func sortSpecs(fset *token.FileSet, f *File, specs []Spec) []Spec {
|
||||
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.
|
||||
@ -138,7 +138,19 @@ func sortSpecs(fset *token.FileSet, f *File, specs []Spec) []Spec {
|
||||
// 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.
|
||||
@ -161,38 +173,16 @@ func sortSpecs(fset *token.FileSet, f *File, specs []Spec) []Spec {
|
||||
}
|
||||
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() }
|
||||
|
@ -94,7 +94,10 @@ func Examples(files ...*ast.File) []*Example {
|
||||
}
|
||||
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
|
||||
}
|
||||
|
||||
@ -135,12 +138,6 @@ func isTest(name, prefix string) bool {
|
||||
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 {
|
||||
|
@ -190,7 +190,10 @@ func NewMethodSet(T Type) *MethodSet {
|
||||
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}
|
||||
}
|
||||
|
||||
@ -257,10 +260,3 @@ func ptrRecv(f *Func) bool {
|
||||
_, 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] }
|
||||
|
Loading…
Reference in New Issue
Block a user