Implement constructor returns parsing

This commit is contained in:
2024-09-15 11:54:05 +02:00
parent ae19630819
commit 3a652e2d7e
2 changed files with 11 additions and 4 deletions

View File

@@ -62,6 +62,7 @@ type (
} }
Constructor struct { Constructor struct {
Params []Param Params []Param
Returns string
Comment string Comment string
} }
) )
@@ -89,6 +90,7 @@ func (c *Class) Write(root string, tmpl *template.Template) error {
if err != nil { if err != nil {
return fmt.Errorf("error creating output file %v: %v", c.ClassName, err) return fmt.Errorf("error creating output file %v: %v", c.ClassName, err)
} }
// spew.Dump(c)
err = tmpl.Execute(outfile, c) err = tmpl.Execute(outfile, c)
if err != nil { if err != nil {
return fmt.Errorf("error writing output file %v: %v", c.ClassName, err) return fmt.Errorf("error writing output file %v: %v", c.ClassName, err)
@@ -135,7 +137,7 @@ func ParseClass(file string) (*Class, error) {
if err != nil { if err != nil {
return nil, fmt.Errorf("error getting constructor: %w", err) return nil, fmt.Errorf("error getting constructor: %w", err)
} }
constructor = constructor res.Constructors = append(res.Constructors, constructor)
// Doing div+div specifically skips only the constructor which is usually the first div // Doing div+div specifically skips only the constructor which is usually the first div
// So the constructor would then be located at h1 + p + div OR h1 + div if there is no description (no p) // So the constructor would then be located at h1 + p + div OR h1 + div if there is no description (no p)
@@ -200,7 +202,7 @@ func getConstructor(dataContainer *goquery.Selection) (Constructor, error) {
types.Each(func(i int, s *goquery.Selection) { types.Each(func(i int, s *goquery.Selection) {
resConstructor.Params = append(resConstructor.Params, Param{ resConstructor.Params = append(resConstructor.Params, Param{
Name: strings.TrimSpace(params.Eq(i).Text()), Name: strings.TrimSpace(params.Eq(i).Text()),
Type: strings.TrimSpace(types.Eq(i).Text()), Type: MapType(strings.TrimSpace(types.Eq(i).Text())),
Comment: "", Comment: "",
}) })
}) })
@@ -251,7 +253,11 @@ func getConstructor(dataContainer *goquery.Selection) (Constructor, error) {
} }
} }
case 2: case 2:
log.Printf("%#v", s.Text()) cleaned := CleanUp(s.Text())
if resConstructor.Returns != "" {
resConstructor.Returns += "\n"
}
resConstructor.Returns = cleaned
} }
} }
}) })

View File

@@ -27,7 +27,8 @@
{{- if ne .Comment ""}} {{- if ne .Comment ""}}
---{{.Name}} ({{.Type}}) -> {{.Comment}} ---{{.Name}} ({{.Type}}) -> {{.Comment}}
{{- end}} {{- end}}
{{- end}} {{- end}}{{if ne .Returns ""}}
---Returns {{.Returns}}{{end}}
---@overload fun({{range $index, $param := .Params}}{{if $index}}, {{end}}{{$param.Name}}: {{$param.Type}}{{end}}): {{$.ClassName}}{{- if ne .Comment ""}} ---@overload fun({{range $index, $param := .Params}}{{if $index}}, {{end}}{{$param.Name}}: {{$param.Type}}{{end}}): {{$.ClassName}}{{- if ne .Comment ""}}
---{{.Comment}} ---{{.Comment}}
{{- end}} {{- end}}