Add support for constructor comments (and refactor the template a little)

This commit is contained in:
2024-09-12 20:32:05 +02:00
parent a9b0ccb3ee
commit e85dca2416
2 changed files with 34 additions and 24 deletions

View File

@@ -144,6 +144,7 @@ func ParseClass(file string) (*Class, error) {
// And "---returns" only if there's a return type // And "---returns" only if there's a return type
// This is NOT a luals annotation because we can not use annotations on @overload // This is NOT a luals annotation because we can not use annotations on @overload
// But just a regular plain Lua comment // But just a regular plain Lua comment
// TODO: Implement parsing comments for classes and constructors
func getConstructors(doc *goquery.Document) ([]Constructor, error) { func getConstructors(doc *goquery.Document) ([]Constructor, error) {
res := []Constructor{} res := []Constructor{}
@@ -184,6 +185,12 @@ func getConstructors(doc *goquery.Document) ([]Constructor, error) {
resConstructor.Params[i].Comment = parameterDescription resConstructor.Params[i].Comment = parameterDescription
}) })
constructorBlock.Find(":not(function)").Children().Each(func(i int, s *goquery.Selection) {
if s.Is("p") {
resConstructor.Comment = strings.TrimSpace(s.Text())
}
})
spew.Dump(resConstructor) spew.Dump(resConstructor)
return append(res, resConstructor), nil return append(res, resConstructor), nil
} }

View File

@@ -1,32 +1,35 @@
---@diagnostic disable: missing-return, lowercase-global ---@diagnostic disable: missing-return, lowercase-global
---@class {{.ClassName}} ---@class {{.ClassName}}
{{range .Fields -}} {{- range .Fields}}
---@field {{.Name}} {{.Type}}{{if ne .Comment ""}} {{.Comment}}{{end}} ---@field {{.Name}} {{.Type}}{{if ne .Comment ""}} {{.Comment}}{{end}}
{{end -}} {{- end}}
{{.ClassName}} = { {{.ClassName}} = {
{{$n := len .Methods -}} {{- $n := len .Methods}}
{{$methods := len .Methods -}} {{- range $index, $method := .Methods}}
{{range $index, $method := .Methods -}}
---@param self {{$.ClassName}} ---@param self {{$.ClassName}}
{{range $param := $method.Params -}} {{- range $param := $method.Params}}
---@param {{.Name}} {{.Type}}{{if ne .Comment ""}} {{.Comment}}{{end}} ---@param {{.Name}} {{.Type}}{{if ne .Comment ""}} {{.Comment}}{{end}}
{{end -}} {{- end}}
{{range $ret := $method.Returns -}} {{- range $ret := $method.Returns}}
---@return {{.Type}}{{if ne .Comment ""}} #{{.Comment}}{{end}} ---@return {{.Type}}{{if ne .Comment ""}} #{{.Comment}}{{end}}
{{end -}} {{- end}}
{{.Name}} = function(self{{if gt (len .Params) 0}}, {{range $index, $param := .Params}}{{if $index}}, {{end}}{{$param.Name}}{{end}}{{end}}) end, {{.Name}} = function(self{{if gt (len .Params) 0}}, {{range $index, $param := .Params}}{{if $index}}, {{end}}{{$param.Name}}{{end}}{{end}}) end,
{{- if ne (plus1 $index) $n}} {{- if ne (plus1 $index) $n}}
{{end}}{{end}} {{- end}}
{{- end}}
} }
-- Define class globally so it's "available" to other files
---@type {{$.ClassName}} ---@type {{$.ClassName}}
{{range .Constructors -}} {{- range .Constructors}}
{{range .Params -}} {{- range .Params}}
{{if ne .Comment ""}}---{{.Name}} ({{.Type}}) -> {{.Comment}}{{end}} {{- if ne .Comment ""}}
{{end -}} ---{{.Name}} ({{.Type}}) -> {{.Comment}}
---@overload fun({{range $index, $param := .Params}}{{if $index}}, {{end}}{{$param.Name}}: {{$param.Type}}{{end}}): {{$.ClassName}} {{- end}}
{{end -}} {{- end}}
{{$.ClassName}} = nil ---@overload fun({{range $index, $param := .Params}}{{if $index}}, {{end}}{{$param.Name}}: {{$param.Type}}{{end}}): {{$.ClassName}}{{- if ne .Comment ""}}
---{{.Comment}}
{{- end}}
{{- end}}
{{.ClassName}} = nil
-- Define class globally so it's "available" to other files