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

@@ -1,32 +1,35 @@
---@diagnostic disable: missing-return, lowercase-global
---@class {{.ClassName}}
{{range .Fields -}}
{{- range .Fields}}
---@field {{.Name}} {{.Type}}{{if ne .Comment ""}} {{.Comment}}{{end}}
{{end -}}
{{- end}}
{{.ClassName}} = {
{{$n := len .Methods -}}
{{$methods := len .Methods -}}
{{range $index, $method := .Methods -}}
---@param self {{$.ClassName}}
{{range $param := $method.Params -}}
---@param {{.Name}} {{.Type}}{{if ne .Comment ""}} {{.Comment}}{{end}}
{{end -}}
{{range $ret := $method.Returns -}}
---@return {{.Type}} {{if ne .Comment ""}}#{{.Comment}}{{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}}
{{- $n := len .Methods}}
{{- range $index, $method := .Methods}}
---@param self {{$.ClassName}}
{{- range $param := $method.Params}}
---@param {{.Name}} {{.Type}}{{if ne .Comment ""}} {{.Comment}}{{end}}
{{- end}}
{{- range $ret := $method.Returns}}
---@return {{.Type}}{{if ne .Comment ""}} #{{.Comment}}{{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}}
{{end}}{{end}}
{{- end}}
{{- end}}
}
-- 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
{{- 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}}{{- if ne .Comment ""}}
---{{.Comment}}
{{- end}}
{{- end}}
{{.ClassName}} = nil
-- Define class globally so it's "available" to other files