From e85dca24168becfd718e150e03c8ddbcf9010bb4 Mon Sep 17 00:00:00 2001 From: PhatPhuckDave Date: Thu, 12 Sep 2024 20:32:05 +0200 Subject: [PATCH] Add support for constructor comments (and refactor the template a little) --- class.go | 7 +++++++ class.tmpl | 51 +++++++++++++++++++++++++++------------------------ 2 files changed, 34 insertions(+), 24 deletions(-) diff --git a/class.go b/class.go index e1116da..7beed9d 100644 --- a/class.go +++ b/class.go @@ -144,6 +144,7 @@ func ParseClass(file string) (*Class, error) { // 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 +// TODO: Implement parsing comments for classes and constructors func getConstructors(doc *goquery.Document) ([]Constructor, error) { res := []Constructor{} @@ -184,6 +185,12 @@ func getConstructors(doc *goquery.Document) ([]Constructor, error) { 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) return append(res, resConstructor), nil } diff --git a/class.tmpl b/class.tmpl index 0703adf..951584e 100644 --- a/class.tmpl +++ b/class.tmpl @@ -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 \ No newline at end of file +{{- 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 \ No newline at end of file