.gitignore added
[dotfiles/.git] / .config / coc / extensions / coc-go-data / tools / pkg / mod / honnef.co / go / tools@v0.1.1 / go / ast / astutil / util.go
1 package astutil
2
3 import (
4         "go/ast"
5         "go/token"
6         "strings"
7 )
8
9 func IsIdent(expr ast.Expr, ident string) bool {
10         id, ok := expr.(*ast.Ident)
11         return ok && id.Name == ident
12 }
13
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 == "_"
19 }
20
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
24 }
25
26 // Deprecated: use IsIntLiteral instead
27 func IsZero(expr ast.Expr) bool {
28         return IsIntLiteral(expr, "0")
29 }
30
31 func Preamble(f *ast.File) string {
32         cutoff := f.Package
33         if f.Doc != nil {
34                 cutoff = f.Doc.Pos()
35         }
36         var out []string
37         for _, cmt := range f.Comments {
38                 if cmt.Pos() >= cutoff {
39                         break
40                 }
41                 out = append(out, cmt.Text())
42         }
43         return strings.Join(out, "\n")
44 }
45
46 func GroupSpecs(fset *token.FileSet, specs []ast.Spec) [][]ast.Spec {
47         if len(specs) == 0 {
48                 return nil
49         }
50         groups := make([][]ast.Spec, 1)
51         groups[0] = append(groups[0], specs[0])
52
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 {
57
58                         groups = append(groups, nil)
59                 }
60
61                 groups[len(groups)-1] = append(groups[len(groups)-1], spec)
62         }
63
64         return groups
65 }