From ece38cda729dd4a761f08b213ba70eaad9815154 Mon Sep 17 00:00:00 2001 From: PhatPhuckDave Date: Thu, 12 Sep 2024 00:45:41 +0200 Subject: [PATCH] Implement constructors --- class.tmpl | 9 +++++++-- main.go | 11 +++++++++++ 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/class.tmpl b/class.tmpl index 73105b3..593963a 100644 --- a/class.tmpl +++ b/class.tmpl @@ -1,7 +1,11 @@ ---@diagnostic disable: missing-return {{range .Fields -}} ---@field {{.Name}} {{.Type}} {{.Comment}} -{{end}} +{{end -}} +{{range .Constructors -}} +---@overload fun({{range $index, $param := .Params}}{{if $index}}, {{end}}{{$param.Name}}: {{$param.Type}}{{end}}): {{$.ClassName}} +{{end -}} +---@class {{.ClassName}} {{.ClassName}} = { {{$n := len .Methods -}} {{$methods := len .Methods -}} @@ -19,5 +23,6 @@ {{end}}{{end}} } ----@type ({{$.ClassName}} | fun(): {{$.ClassName}}) +---@type {{$.ClassName}} +-- Define class globally so it's "available" to other files {{$.ClassName}} = nil \ No newline at end of file diff --git a/main.go b/main.go index 7657e18..f8e5664 100644 --- a/main.go +++ b/main.go @@ -77,6 +77,17 @@ func main() { Comment: "test method", }, }, + Constructors: []Constructor{ + { + Params: []Param{}, + }, + { + Params: []Param{ + {Name: "a", Type: "int", Comment: "test param 1"}, + {Name: "b", Type: "string", Comment: "test param 2"}, + }, + }, + }, } ltemplate, err := template.New("class").Funcs(fns).Parse(templatestr)