9 func IsIdent(expr ast.Expr, ident string) bool {
10 id, ok := expr.(*ast.Ident)
11 return ok && id.Name == ident
14 // isBlank returns whether id is the blank identifier "_".
15 // If id == nil, the answer is false.
16 func IsBlank(id ast.Expr) bool {
17 ident, _ := id.(*ast.Ident)
18 return ident != nil && ident.Name == "_"
21 func IsIntLiteral(expr ast.Expr, literal string) bool {
22 lit, ok := expr.(*ast.BasicLit)
23 return ok && lit.Kind == token.INT && lit.Value == literal
26 // Deprecated: use IsIntLiteral instead
27 func IsZero(expr ast.Expr) bool {
28 return IsIntLiteral(expr, "0")
31 func Preamble(f *ast.File) string {
37 for _, cmt := range f.Comments {
38 if cmt.Pos() >= cutoff {
41 out = append(out, cmt.Text())
43 return strings.Join(out, "\n")
46 func GroupSpecs(fset *token.FileSet, specs []ast.Spec) [][]ast.Spec {
50 groups := make([][]ast.Spec, 1)
51 groups[0] = append(groups[0], specs[0])
53 for _, spec := range specs[1:] {
54 g := groups[len(groups)-1]
55 if fset.PositionFor(spec.Pos(), false).Line-1 !=
56 fset.PositionFor(g[len(g)-1].End(), false).Line {
58 groups = append(groups, nil)
61 groups[len(groups)-1] = append(groups[len(groups)-1], spec)