Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 15 additions & 13 deletions funcinfo.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package docgen

import (
"go/ast"
"go/parser"
"go/token"
"os"
Expand All @@ -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 {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
}