diff --git a/funcinfo.go b/funcinfo.go index 66612da..5646d62 100644 --- a/funcinfo.go +++ b/funcinfo.go @@ -1,6 +1,7 @@ package docgen import ( + "go/ast" "go/parser" "go/token" "os" @@ -11,13 +12,14 @@ import ( ) type FuncInfo struct { - Pkg string `json:"pkg"` - Func string `json:"func"` - Comment string `json:"comment"` - File string `json:"file,omitempty"` - Line int `json:"line,omitempty"` - Anonymous bool `json:"anonymous,omitempty"` - Unresolvable bool `json:"unresolvable,omitempty"` + Pkg string `json:"pkg"` + Func string `json:"func"` + Comment string `json:"comment"` + File string `json:"file,omitempty"` + ASTFile *ast.File `json:"ast_file,omitempty"` + Line int `json:"line,omitempty"` + Anonymous bool `json:"anonymous,omitempty"` + Unresolvable bool `json:"unresolvable,omitempty"` } func GetFuncInfo(i interface{}) FuncInfo { @@ -60,7 +62,7 @@ func GetFuncInfo(i interface{}) FuncInfo { } if !fi.Unresolvable { - fi.Comment = getFuncComment(frame.File, frame.Line) + fi.Comment, fi.ASTFile = getFuncComment(frame.File, frame.Line) } return fi @@ -91,23 +93,23 @@ func getPkgName(file string) string { return astFile.Name.Name } -func getFuncComment(file string, line int) string { +func getFuncComment(file string, line int) (string, *ast.File) { fset := token.NewFileSet() astFile, err := parser.ParseFile(fset, file, nil, parser.ParseComments) if err != nil { - return "" + return "", nil } if len(astFile.Comments) == 0 { - return "" + return "", astFile } for _, cmt := range astFile.Comments { if fset.Position(cmt.End()).Line+1 == line { - return cmt.Text() + return cmt.Text(), astFile } } - return "" + return "", astFile }