From e2939cd8ba8e199d5936cf4eb3ca0c8a078d5d8b Mon Sep 17 00:00:00 2001 From: maddalax Date: Fri, 25 Oct 2024 11:11:22 -0500 Subject: [PATCH] support non pointer types on formatter --- examples/chat/components/button.go | 26 +++++++++++--- examples/hackernews/pages/root.go | 7 ++-- examples/simple-auth/pages/root.go | 4 ++- examples/todo-list/pages/base/root.go | 4 ++- tools/html-to-htmgo/htmltogo/indent.go | 9 +++++ tools/html-to-htmgo/htmltogo/indent_test.go | 38 +++++++++++++++++++++ 6 files changed, 79 insertions(+), 9 deletions(-) create mode 100644 tools/html-to-htmgo/htmltogo/indent_test.go diff --git a/examples/chat/components/button.go b/examples/chat/components/button.go index bc767c8..3f9c8c1 100644 --- a/examples/chat/components/button.go +++ b/examples/chat/components/button.go @@ -28,12 +28,28 @@ func Button(props ButtonProps) h.Ren { text := h.Text(props.Text) button := h.Button( - h.If(props.Id != "", h.Id(props.Id)), - h.If(props.Children != nil, h.Children(props.Children...)), + h.If( + props.Id != "", + h.Id(props.Id), + ), + h.If( + props.Children != nil, + h.Children(props.Children...), + ), h.Class("flex gap-1 items-center justify-center border p-4 rounded cursor-hover", props.Class), - h.If(props.Get != "", h.Get(props.Get)), - h.If(props.Target != "", h.HxTarget(props.Target)), - h.IfElse(props.Type != "", h.Type(props.Type), h.Type("button")), + h.If( + props.Get != "", + h.Get(props.Get), + ), + h.If( + props.Target != "", + h.HxTarget(props.Target), + ), + h.IfElse( + props.Type != "", + h.Type(props.Type), + h.Type("button"), + ), text, ) diff --git a/examples/hackernews/pages/root.go b/examples/hackernews/pages/root.go index 1df43ef..3358b22 100644 --- a/examples/hackernews/pages/root.go +++ b/examples/hackernews/pages/root.go @@ -5,14 +5,17 @@ import ( ) func RootPage(children ...h.Ren) h.Ren { - banner := h.A(h.Class("bg-neutral-200 text-neutral-600 text-center p-2 flex items-center justify-center"), + banner := h.A( + h.Class("bg-neutral-200 text-neutral-600 text-center p-2 flex items-center justify-center"), h.Href("https://github.com/maddalax/htmgo"), h.Attribute("target", "_blank"), h.Text("Built with htmgo.dev"), ) return h.Html( - h.HxExtensions(h.BaseExtensions()), + h.HxExtensions( + h.BaseExtensions(), + ), h.Head( h.Meta("viewport", "width=device-width, initial-scale=1"), h.Link("/public/favicon.ico", "icon"), diff --git a/examples/simple-auth/pages/root.go b/examples/simple-auth/pages/root.go index bacdd61..510163e 100644 --- a/examples/simple-auth/pages/root.go +++ b/examples/simple-auth/pages/root.go @@ -6,7 +6,9 @@ import ( func RootPage(children ...h.Ren) h.Ren { return h.Html( - h.HxExtensions(h.BaseExtensions()), + h.HxExtensions( + h.BaseExtensions(), + ), h.Head( h.Meta("viewport", "width=device-width, initial-scale=1"), h.Link("/public/favicon.ico", "icon"), diff --git a/examples/todo-list/pages/base/root.go b/examples/todo-list/pages/base/root.go index 7a9ca91..f612a62 100644 --- a/examples/todo-list/pages/base/root.go +++ b/examples/todo-list/pages/base/root.go @@ -6,7 +6,9 @@ import ( func RootPage(children ...h.Ren) h.Ren { return h.Html( - h.HxExtension(h.BaseExtensions()), + h.HxExtension( + h.BaseExtensions(), + ), h.Head( h.Meta("viewport", "width=device-width, initial-scale=1"), h.Meta("title", "htmgo todo mvc"), diff --git a/tools/html-to-htmgo/htmltogo/indent.go b/tools/html-to-htmgo/htmltogo/indent.go index 519fbe9..da7146c 100644 --- a/tools/html-to-htmgo/htmltogo/indent.go +++ b/tools/html-to-htmgo/htmltogo/indent.go @@ -48,6 +48,15 @@ func Indent(input string) string { } } + // support non-pointer return types + if v, ok := returnType.(*ast.SelectorExpr); ok { + if x, ok := v.X.(*ast.Ident); ok { + name := x.Name + str := name + "." + v.Sel.Name + isHtmgoComponent = slices.Contains(htmgoComponentTypes, str) + } + } + if !isHtmgoComponent { continue } diff --git a/tools/html-to-htmgo/htmltogo/indent_test.go b/tools/html-to-htmgo/htmltogo/indent_test.go new file mode 100644 index 0000000..b29679f --- /dev/null +++ b/tools/html-to-htmgo/htmltogo/indent_test.go @@ -0,0 +1,38 @@ +package htmltogo + +import ( + "github.com/stretchr/testify/assert" + "testing" +) + +func TestIdentHRen(t *testing.T) { + input := ` + package main + import ( + "github.com/maddalax/htmgo/framework/h" + ) + func Button(props ButtonProps) h.Ren { + return h.Div( + h.Div(h.Div(),h.P(),h.P(), + ), + ) + } + ` + indented := Indent(input) + assert.Equal(t, `package main + +import ( + "github.com/maddalax/htmgo/framework/h" +) + +func Button(props ButtonProps) h.Ren { + return h.Div( + h.Div( + h.Div(), + h.P(), + h.P(), + ), + ) +} +`, indented) +}