Created
December 28, 2016 07:24
-
-
Save tenntenn/eba0ac51f0d588e9ecfa47eab634ebb2 to your computer and use it in GitHub Desktop.
コメントに関連付けられたコードを取得する #golang ref: http://qiita.com/tenntenn/items/090922826c9164ac5496
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Hoge です。 | |
type Hoge struct { | |
N int | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
// comment for hoge | |
var hoge int | |
// comment for main | |
func main() { | |
// line comment 1/2 | |
// line comment 2/2 | |
/* | |
block comment1 | |
block comment2 | |
*/ | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fset := token.NewFileSet() | |
f, err := parser.ParseFile(fset, "sample.go", src, parser.ParseComments) | |
if err != nil { | |
log.Fatalln("Error:", err) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type File struct { | |
Doc *CommentGroup // associated documentation; or nil | |
Package token.Pos // position of "package" keyword | |
Name *Ident // package name | |
Decls []Decl // top-level declarations; or nil | |
Scope *Scope // package scope (this file only) | |
Imports []*ImportSpec // imports in this file | |
Unresolved []*Ident // unresolved identifiers in this file | |
Comments []*CommentGroup // list of all comments in the source file | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type CommentGroup struct { | |
List []*Comment // len(List) > 0 | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
for _, cg := range f.Comments { | |
fmt.Println(cg.Text()) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
comment for hoge | |
comment for main | |
line comment 1/2 | |
line comment 2/2 | |
block comment1 | |
block comment2 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type CommentMap map[Node][]*CommentGroup |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func NewCommentMap(fset *token.FileSet, node Node, comments []*CommentGroup) CommentMap |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func (cmap CommentMap) Filter(node Node) CommentMap { | |
umap := make(CommentMap) | |
Inspect(node, func(n Node) bool { | |
if g := cmap[n]; len(g) > 0 { | |
umap[n] = g | |
} | |
return true | |
}) | |
return umap | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment