Source File
commentmap.go
Belonging Package
go/ast
type CommentMap map[Node][]*CommentGroup
func ( CommentMap) ( Node, *CommentGroup) {
:= []
if len() == 0 {
= []*CommentGroup{}
} else {
= append(, )
}
[] =
}
type byInterval []Node
func ( byInterval) () int { return len() }
func ( byInterval) (, int) bool {
, := [].Pos(), [].Pos()
return < || == && [].End() > [].End()
}
func ( byInterval) (, int) { [], [] = [], [] }
return
}
type commentListReader struct {
fset *token.FileSet
list []*CommentGroup
index int
comment *CommentGroup // comment group at current index
pos, end token.Position // source interval of comment group at current index
}
func ( *commentListReader) () bool {
return .index >= len(.list)
}
func ( *commentListReader) () {
if !.eol() {
.comment = .list[.index]
.pos = .fset.Position(.comment.Pos())
.end = .fset.Position(.comment.End())
.index++
}
}
func ( *token.FileSet, Node, []*CommentGroup) CommentMap {
if len() == 0 {
return nil // no comments to map
}
:= make(CommentMap)
:= make([]*CommentGroup, len())
copy(, ) // don't change incoming comments
sortComments()
:= commentListReader{fset: , list: } // !r.eol() because len(comments) > 0
.next()
=
panic("internal error: no comments should be associated with sentinel")
}
=
}
.addComment(, .comment)
if .eol() {
return
}
.next()
}
func ( CommentMap) ( Node) CommentMap {
:= make(CommentMap)
Inspect(, func( Node) bool {
if := []; len() > 0 {
[] =
}
return true
})
return
}
func ( CommentMap) () []*CommentGroup {
:= make([]*CommentGroup, 0, len())
for , := range {
= append(, ...)
}
sortComments()
return
}
func ( []*CommentGroup) string {
const = 40
var bytes.Buffer
:
for , := range .List {
if .Len() >= {
break
}
.WriteString(.Text)
}
}
if .Len() > {
.Truncate( - 3)
.WriteString("...")
}
![]() |
The pages are generated with Golds v0.3.2-preview. (GOOS=darwin GOARCH=amd64) Golds is a Go 101 project developed by Tapir Liu. PR and bug reports are welcome and can be submitted to the issue list. Please follow @Go100and1 (reachable from the left QR code) to get the latest news of Golds. |