-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathtypes_comment_locator.go
More file actions
44 lines (39 loc) · 888 Bytes
/
types_comment_locator.go
File metadata and controls
44 lines (39 loc) · 888 Bytes
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
package gotype
import (
"go/ast"
"go/token"
"sort"
)
func newTypeCommentLocatorComment(typ Type, pos, end token.Pos, comments []*ast.CommentGroup) Type {
return &typeCommentLocatorComment{
Type: typ,
pos: pos,
end: end,
comments: comments,
}
}
type typeCommentLocatorComment struct {
Type
pos token.Pos
end token.Pos
comments []*ast.CommentGroup
comment *ast.CommentGroup
}
func (t *typeCommentLocatorComment) Comment() *ast.CommentGroup {
if t.comment == nil {
t.comment = commentLocator(t.pos, t.end, t.comments)
}
return t.comment
}
func commentLocator(pos, end token.Pos, comments []*ast.CommentGroup) *ast.CommentGroup {
i := sort.Search(len(comments), func(i int) bool {
return pos < comments[i].Pos()
})
if i == -1 || i >= len(comments) {
return nil
}
if comments[i].End() < end {
return comments[i]
}
return nil
}