diff --git a/class.go b/class.go index fe6bbe6..43d22a0 100644 --- a/class.go +++ b/class.go @@ -119,9 +119,33 @@ func ParseClass(file string) (*Class, error) { } res.ClassName = strings.TrimSpace(class.Text()) + res.Constructors, err = getConstructors(doc) + if err != nil { + return nil, fmt.Errorf("error getting constructors: %w", err) + } + + res.Fields, err = getFields(doc) + if err != nil { + return nil, fmt.Errorf("error getting fields: %w", err) + } + + res.Methods, err = getMethods(doc) + if err != nil { + return nil, fmt.Errorf("error getting methods: %w", err) + } + + spew.Dump(res) + return &res, nil +} + +// TODO: Implement parsing comments for parameters +// TODO: Implement parsing comments for return values +func getConstructors(doc *goquery.Document) ([]Constructor, error) { + res := []Constructor{} + codeblocks := doc.Find("div.floatright > div.codecontainer") if codeblocks.Length() == 0 { - return nil, fmt.Errorf("no codeblocks found") + return res, fmt.Errorf("no codeblocks found") } // The first code block should be the constructor @@ -144,8 +168,12 @@ func ParseClass(file string) (*Class, error) { Comment: "", }) }) - res.Constructors = append(res.Constructors, resConstructor) + return append(res, resConstructor), nil +} + +func getFields(doc *goquery.Document) ([]Field, error) { + res := []Field{} properties := doc.Find("div.floatright > div.codecontainer#Properties") properties.ChildrenFiltered("div").Each(func(i int, s *goquery.Selection) { property := Field{} @@ -156,9 +184,16 @@ func ParseClass(file string) (*Class, error) { if comment != "" { property.Comment = strings.TrimSpace(comment) } - res.Fields = append(res.Fields, property) + res = append(res, property) }) + return res, nil +} +// TODO: Implement parsing return value types and comments +func getMethods(doc *goquery.Document) ([]Method, error) { + res := []Method{} + + codeblocks := doc.Find("div.floatright > div.codecontainer") codeblocks.ChildrenFiltered("div.function").Each(func(i int, s *goquery.Selection) { method := Method{} method.Name = strings.TrimSpace(s.AttrOr("id", "")) @@ -174,10 +209,8 @@ func ParseClass(file string) (*Class, error) { method.Params = append(method.Params, param) }) - res.Methods = append(res.Methods, method) + res = append(res, method) }) - spew.Dump(res) - - return &res, nil -} + return res, nil +} \ No newline at end of file diff --git a/class.tmpl b/class.tmpl index 43aa691..6d6704e 100644 --- a/class.tmpl +++ b/class.tmpl @@ -1,7 +1,7 @@ ---@diagnostic disable: missing-return, lowercase-global ---@class {{.ClassName}} {{range .Fields -}} ----@field {{.Name}} {{.Type}} {{.Comment}} +---@field {{.Name}} {{.Type}}{{if ne .Comment ""}} {{.Comment}}{{end}} {{end -}} {{.ClassName}} = { {{$n := len .Methods -}} @@ -9,7 +9,7 @@ {{range $index, $method := .Methods -}} ---@param self {{$.ClassName}} {{range $param := $method.Params -}} - ---@param {{.Name}} {{.Type}} {{.Comment}} + ---@param {{.Name}} {{.Type}}{{if ne .Comment ""}} {{.Comment}}{{end}} {{end -}} {{range $ret := $method.Returns -}} ---@return {{.Type}} {{if ne .Comment ""}}#{{.Comment}}{{end}} diff --git a/main.go b/main.go index e74490c..cdfc09f 100644 --- a/main.go +++ b/main.go @@ -42,6 +42,8 @@ func main() { return } + // TODO: Implement parsing enums somehow + // TODO: Implement some sort of switching (between a class and an enum) wg := sync.WaitGroup{} for _, file := range files { wg.Add(1)