Implement constructor parameter comment parsing

This commit is contained in:
2024-09-12 17:41:23 +02:00
parent 867c31b6d3
commit a9b0ccb3ee
2 changed files with 24 additions and 4 deletions

View File

@@ -134,12 +134,16 @@ func ParseClass(file string) (*Class, error) {
return nil, fmt.Errorf("error getting methods: %w", err)
}
spew.Dump(res)
// spew.Dump(res)
return &res, nil
}
// TODO: Implement parsing comments for parameters
// TODO: Implement parsing comments for return values
// Something like "---returns (something) -> comment"
// Where "-> comment" is only shown if there's a comment
// And "---returns" only if there's a return type
// This is NOT a luals annotation because we can not use annotations on @overload
// But just a regular plain Lua comment
func getConstructors(doc *goquery.Document) ([]Constructor, error) {
res := []Constructor{}
@@ -169,6 +173,18 @@ func getConstructors(doc *goquery.Document) ([]Constructor, error) {
})
})
constructorDetails := constructorBlock.Children().Eq(1)
constructorParameterDetails := constructorDetails.Find("span.parameter")
constructorParameterDetails.Each(func(i int, s *goquery.Selection) {
param := strings.TrimSpace(s.Text())
parameterParent := s.Parent()
parameterDescription := parameterParent.Text()
parameterDescription = strings.ReplaceAll(parameterDescription, fmt.Sprintf("\n%s\n", param), "")
parameterDescription = strings.TrimSpace(parameterDescription)
resConstructor.Params[i].Comment = parameterDescription
})
spew.Dump(resConstructor)
return append(res, resConstructor), nil
}

View File

@@ -21,8 +21,12 @@
}
-- Define class globally so it's "available" to other files
---@type {{$.ClassName}}
{{range .Constructors -}}
{{range .Params -}}
{{if ne .Comment ""}}---{{.Name}} ({{.Type}}) -> {{.Comment}}{{end}}
{{end -}}
---@overload fun({{range $index, $param := .Params}}{{if $index}}, {{end}}{{$param.Name}}: {{$param.Type}}{{end}}): {{$.ClassName}}
{{end -}}
{{$.ClassName}} = nil