-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvisitor.go
More file actions
167 lines (134 loc) · 4.47 KB
/
visitor.go
File metadata and controls
167 lines (134 loc) · 4.47 KB
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package markers
import (
"go/ast"
"go/token"
)
type commentVisitor struct {
allComments []*ast.CommentGroup
nextCommentIndex int
packageMarkers []markerComment
declarationMarkers []markerComment
nodeMarkers map[ast.Node][]markerComment
}
func newCommentVisitor(allComments []*ast.CommentGroup) *commentVisitor {
return &commentVisitor{
allComments: allComments,
nodeMarkers: make(map[ast.Node][]markerComment),
}
}
func (visitor *commentVisitor) Visit(node ast.Node) (w ast.Visitor) {
if node == nil {
return nil
}
switch node.(type) {
case *ast.CommentGroup:
return nil
case *ast.Ident:
return nil
case *ast.ImportSpec:
return nil
case *ast.FieldList:
return visitor
case *ast.InterfaceType:
return visitor
case *ast.FuncType:
return nil
}
lastCommentIndex := visitor.nextCommentIndex
var markersFromComment []markerComment
var markersFromDocument []markerComment
if visitor.nextCommentIndex < len(visitor.allComments) {
nextCommentGroup := visitor.allComments[visitor.nextCommentIndex]
for nextCommentGroup.Pos() < node.Pos() {
lastCommentIndex++
if lastCommentIndex >= len(visitor.allComments) {
break
}
nextCommentGroup = visitor.allComments[lastCommentIndex]
}
lastCommentIndex--
docCommentGroup := visitor.getCommentsForNode(node)
markerCommentIndex := lastCommentIndex
if docCommentGroup != nil && visitor.allComments[markerCommentIndex].Pos() == docCommentGroup.Pos() {
markerCommentIndex--
}
if markerCommentIndex >= visitor.nextCommentIndex {
markersFromComment = visitor.getMarkerComments(markerCommentIndex, markerCommentIndex+1)
markersFromDocument = visitor.getMarkerComments(markerCommentIndex+1, lastCommentIndex+1)
} else {
markersFromDocument = visitor.getMarkerComments(markerCommentIndex+1, lastCommentIndex+1)
}
}
switch typedNode := node.(type) {
case *ast.File:
visitor.packageMarkers = append(visitor.packageMarkers, markersFromComment...)
visitor.packageMarkers = append(visitor.packageMarkers, markersFromDocument...)
case *ast.TypeSpec:
visitor.nodeMarkers[node] = append(visitor.nodeMarkers[node], visitor.declarationMarkers...)
visitor.nodeMarkers[node] = append(visitor.nodeMarkers[node], markersFromComment...)
visitor.nodeMarkers[node] = append(visitor.nodeMarkers[node], markersFromDocument...)
visitor.declarationMarkers = nil
case *ast.GenDecl:
if typedNode.Tok == token.IMPORT {
visitor.nodeMarkers[node] = append(visitor.nodeMarkers[node], markersFromComment...)
visitor.nodeMarkers[node] = append(visitor.nodeMarkers[node], markersFromDocument...)
} else {
visitor.declarationMarkers = append(visitor.declarationMarkers, markersFromComment...)
visitor.declarationMarkers = append(visitor.declarationMarkers, markersFromDocument...)
}
case *ast.Field, *ast.FuncDecl:
visitor.nodeMarkers[node] = append(visitor.nodeMarkers[node], markersFromComment...)
visitor.nodeMarkers[node] = append(visitor.nodeMarkers[node], markersFromDocument...)
}
visitor.nextCommentIndex = lastCommentIndex + 1
return visitor
}
func (visitor *commentVisitor) getMarkerComments(startIndex, endIndex int) []markerComment {
if startIndex < 0 || endIndex < 0 {
return nil
}
markerComments := make([]markerComment, 0)
for index := startIndex; index < endIndex; index++ {
commentGroup := visitor.allComments[index]
var markerComment *markerComment
var isMultiLine bool
for _, comment := range commentGroup.List {
containsMarker := isMarkerComment(comment.Text)
if containsMarker {
if markerComment != nil {
markerComments = append(markerComments, *markerComment)
}
markerComment = newMarkerComment(comment)
isMultiLine = false
} else if isMultiLine {
if markerComment != nil {
markerComment.append(comment)
}
}
isMultiLine = isMultiLineComment(comment.Text)
}
if markerComment != nil {
markerComments = append(markerComments, *markerComment)
}
}
return markerComments
}
func (visitor *commentVisitor) getCommentsForNode(node ast.Node) (docCommentGroup *ast.CommentGroup) {
switch typedNode := node.(type) {
case *ast.File:
docCommentGroup = typedNode.Doc
case *ast.ImportSpec:
docCommentGroup = typedNode.Doc
case *ast.TypeSpec:
docCommentGroup = typedNode.Doc
case *ast.GenDecl:
docCommentGroup = typedNode.Doc
case *ast.Field:
docCommentGroup = typedNode.Doc
case *ast.FuncDecl:
docCommentGroup = typedNode.Doc
case *ast.ValueSpec:
docCommentGroup = typedNode.Doc
}
return docCommentGroup
}