-
Notifications
You must be signed in to change notification settings - Fork 0
/
aster.go
129 lines (109 loc) · 3.22 KB
/
aster.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/*
Package aster provides a high level interface for parsing Go code using the stdlib's
[`go/ast`](https://pkg.go.dev/go/ast) module.
*/
package aster
import (
"go/ast"
"go/parser"
"go/token"
"regexp"
"sort"
"strings"
)
// FromDirectory creates a new `aster` struct from the contents of the specified directory.
func FromDirectory(path string) *Aster {
a := &Aster{
fileset: token.NewFileSet(),
}
a.packages, a.Error = parser.ParseDir(a.fileset, path, nil, parser.ParseComments)
return a
}
/*
Aster is the main struct and pipeline object for the `aster` module. It provides all
the methods and interfaces used by any wrapper methods at the package level.
*/
type Aster struct {
// Error is populated when an error occurs in the course of processing results.
Error error
// fileset is the `token.Fileset` object used when parsing all the underyling Go code.
fileset *token.FileSet
// packages is the map of `ast.Package` structs populated from the parsed Go files.
packages map[string]*ast.Package
}
// Packages filters the returned packages to only those with the specified names.
func (a *Aster) Packages(names []string) *Aster {
newPackages := map[string]*ast.Package{}
for pkgName, pkg := range a.packages {
for _, name := range names {
if pkgName == name {
newPackages[pkgName] = pkg
continue
}
}
}
a.packages = newPackages
return a
}
// PackageComment returns the comments for all parsed packages. If more than one is present,
// it will return a concatenated group of them with each one prefaced with the package name.
// If an error has occurred during processing, then it will return a blank string.
func (a *Aster) PackageComment() string {
if a.Error != nil {
return ""
}
packageComments := []string{}
for pkgName, pkg := range a.packages {
var b strings.Builder
if len(a.packages) > 1 {
b.WriteString(pkgName + ":\n")
}
for _, f := range pkg.Files {
b.WriteString(f.Doc.Text())
}
packageComments = append(packageComments, b.String())
}
sort.Strings(packageComments)
return strings.Join(packageComments, "")
}
// MatchComment returns all declarations that have a related comment matching the given string.
// If an error has occurred during processing, it returns an empty slice.
func (a *Aster) MatchComment(match string) ([]Result, error) {
results := []Result{}
if a.Error != nil {
return results, a.Error
}
r, err := regexp.Compile(match)
if err != nil {
a.Error = err
return results, a.Error
}
for _, pkg := range a.packages {
for _, f := range pkg.Files {
matchingCommentGroups := []*ast.CommentGroup{}
for _, cGroup := range f.Comments {
if r.MatchString(cGroup.Text()) {
matchingCommentGroups = append(matchingCommentGroups, cGroup)
continue
}
}
if len(matchingCommentGroups) > 0 {
for _, cGroup := range matchingCommentGroups {
cGroupLine := a.fileset.Position(cGroup.End()).Line
for _, decl := range f.Decls {
declLine := a.fileset.Position(decl.Pos()).Line
if cGroupLine == declLine-1 {
result, err := resultFromDecl(decl)
if err != nil {
a.Error = err
return results, a.Error
}
results = append(results, result)
}
}
}
}
}
}
return results, nil
}