Re: [go-nuts] get function body comments using ast

2022-10-10 Thread 'wagner riffel' via golang-nuts

On 10/9/22 11:09, Dmitry Belyaev wrote:

For a function like below I'd get only *example // doc comment*,

// doc comment
func example() {
   // body comment   < looking to extract this comment
}

but I am looking also to extract *// body comment*.

Please advise on how to extract function name, doc comment AND body 
comment altogether. Thank you!




I don't think go/parser adds comments to functions body []Stmt list, 
though if go through all comments in ast.File and filter by those that 
are in range with function's block statement, maybe that's enough.


https://go.dev/play/p/S4Iz504K8Gm

-w


--
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/3758c4a4-bff7-3c15-91ac-8b5cb3d550d3%40104d.net.


[go-nuts] get function body comments using ast

2022-10-09 Thread Dmitry Belyaev
Hey fellow gophers,

I am trying to get comments from inside of function declaration while also 
accessing function name, but haven't had much success. Getting name and doc 
is quite straightforward, I do it with:

fset := token.NewFileSet()

f, _ := parser.ParseFile(fset, "testfile", nil, parser.ParseComments)

ast.Inspect(f, func(n ast.Node) bool {
fn, ok := n.(*ast.FuncDecl)
if ok {
fmt.Println(fn.Name.String(), fn.Doc.Text())
}
 }

For a function like below I'd get only *example // doc comment*,

// doc comment
func example() {
  // body comment   < looking to extract this comment
}

but I am looking also to extract *// body comment*.

Please advise on how to extract function name, doc comment AND body comment 
altogether. Thank you!

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/cad2e200-fb20-4b3c-b143-c3eb858323d4n%40googlegroups.com.