Skip to content

Commit e2939cd

Browse files
committed
support non pointer types on formatter
1 parent 7fae447 commit e2939cd

File tree

6 files changed

+79
-9
lines changed

6 files changed

+79
-9
lines changed

examples/chat/components/button.go

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,28 @@ func Button(props ButtonProps) h.Ren {
2828
text := h.Text(props.Text)
2929

3030
button := h.Button(
31-
h.If(props.Id != "", h.Id(props.Id)),
32-
h.If(props.Children != nil, h.Children(props.Children...)),
31+
h.If(
32+
props.Id != "",
33+
h.Id(props.Id),
34+
),
35+
h.If(
36+
props.Children != nil,
37+
h.Children(props.Children...),
38+
),
3339
h.Class("flex gap-1 items-center justify-center border p-4 rounded cursor-hover", props.Class),
34-
h.If(props.Get != "", h.Get(props.Get)),
35-
h.If(props.Target != "", h.HxTarget(props.Target)),
36-
h.IfElse(props.Type != "", h.Type(props.Type), h.Type("button")),
40+
h.If(
41+
props.Get != "",
42+
h.Get(props.Get),
43+
),
44+
h.If(
45+
props.Target != "",
46+
h.HxTarget(props.Target),
47+
),
48+
h.IfElse(
49+
props.Type != "",
50+
h.Type(props.Type),
51+
h.Type("button"),
52+
),
3753
text,
3854
)
3955

examples/hackernews/pages/root.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,17 @@ import (
55
)
66

77
func RootPage(children ...h.Ren) h.Ren {
8-
banner := h.A(h.Class("bg-neutral-200 text-neutral-600 text-center p-2 flex items-center justify-center"),
8+
banner := h.A(
9+
h.Class("bg-neutral-200 text-neutral-600 text-center p-2 flex items-center justify-center"),
910
h.Href("https://github.com/maddalax/htmgo"),
1011
h.Attribute("target", "_blank"),
1112
h.Text("Built with htmgo.dev"),
1213
)
1314

1415
return h.Html(
15-
h.HxExtensions(h.BaseExtensions()),
16+
h.HxExtensions(
17+
h.BaseExtensions(),
18+
),
1619
h.Head(
1720
h.Meta("viewport", "width=device-width, initial-scale=1"),
1821
h.Link("/public/favicon.ico", "icon"),

examples/simple-auth/pages/root.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ import (
66

77
func RootPage(children ...h.Ren) h.Ren {
88
return h.Html(
9-
h.HxExtensions(h.BaseExtensions()),
9+
h.HxExtensions(
10+
h.BaseExtensions(),
11+
),
1012
h.Head(
1113
h.Meta("viewport", "width=device-width, initial-scale=1"),
1214
h.Link("/public/favicon.ico", "icon"),

examples/todo-list/pages/base/root.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ import (
66

77
func RootPage(children ...h.Ren) h.Ren {
88
return h.Html(
9-
h.HxExtension(h.BaseExtensions()),
9+
h.HxExtension(
10+
h.BaseExtensions(),
11+
),
1012
h.Head(
1113
h.Meta("viewport", "width=device-width, initial-scale=1"),
1214
h.Meta("title", "htmgo todo mvc"),

tools/html-to-htmgo/htmltogo/indent.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,15 @@ func Indent(input string) string {
4848
}
4949
}
5050

51+
// support non-pointer return types
52+
if v, ok := returnType.(*ast.SelectorExpr); ok {
53+
if x, ok := v.X.(*ast.Ident); ok {
54+
name := x.Name
55+
str := name + "." + v.Sel.Name
56+
isHtmgoComponent = slices.Contains(htmgoComponentTypes, str)
57+
}
58+
}
59+
5160
if !isHtmgoComponent {
5261
continue
5362
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package htmltogo
2+
3+
import (
4+
"github.com/stretchr/testify/assert"
5+
"testing"
6+
)
7+
8+
func TestIdentHRen(t *testing.T) {
9+
input := `
10+
package main
11+
import (
12+
"github.com/maddalax/htmgo/framework/h"
13+
)
14+
func Button(props ButtonProps) h.Ren {
15+
return h.Div(
16+
h.Div(h.Div(),h.P(),h.P(),
17+
),
18+
)
19+
}
20+
`
21+
indented := Indent(input)
22+
assert.Equal(t, `package main
23+
24+
import (
25+
"github.com/maddalax/htmgo/framework/h"
26+
)
27+
28+
func Button(props ButtonProps) h.Ren {
29+
return h.Div(
30+
h.Div(
31+
h.Div(),
32+
h.P(),
33+
h.P(),
34+
),
35+
)
36+
}
37+
`, indented)
38+
}

0 commit comments

Comments
 (0)