Skip to content

Commit

Permalink
fix(fmt): Preserve newline after templ elements
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexanderArvidsson committed Sep 13, 2024
1 parent 2f6aa04 commit dc3c684
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
-- in --
package main

templ x() {
<div>
@Hero()
<span></span>


@Hero()



<span></span>
</div>
}
-- out --
package main

templ x() {
<div>
@Hero()
<span></span>

@Hero()

<span></span>
</div>
}
2 changes: 1 addition & 1 deletion parser/v2/templateparser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -626,8 +626,8 @@ func TestTemplateParser(t *testing.T) {
},
},
},
TrailingSpace: SpaceHorizontal,
},
Whitespace{Value: " "},
Text{
Value: "Home",
Range: Range{
Expand Down
26 changes: 25 additions & 1 deletion parser/v2/templelementparser.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@ func (p templElementExpressionParser) Parse(pi *parse.Input) (n Node, ok bool, e
return
}

var r TemplElementExpression
r := TemplElementExpression{
// Default behavior is always a trailing space
TrailingSpace: SpaceVertical,
}

// Parse the Go expression.
if r.Expression, err = parseGo("templ element", pi, goexpression.TemplExpression); err != nil {
return r, false, err
Expand All @@ -26,6 +30,16 @@ func (p templElementExpressionParser) Parse(pi *parse.Input) (n Node, ok bool, e
return
}
if !hasOpenBrace {
// Parse trailing whitespace after expression.
ws, _, err := parse.Whitespace.Parse(pi)
if err != nil {
return r, false, err
}
r.TrailingSpace, err = NewTrailingSpace(ws, true)
if err != nil {
return r, false, err
}

return r, true, nil
}

Expand All @@ -46,6 +60,16 @@ func (p templElementExpressionParser) Parse(pi *parse.Input) (n Node, ok bool, e
return
}

// Parse trailing whitespace after closing brace.
ws, _, err := parse.Whitespace.Parse(pi)
if err != nil {
return r, false, err
}
r.TrailingSpace, err = NewTrailingSpace(ws, true)
if err != nil {
return r, false, err
}

return r, true, nil
}

Expand Down
6 changes: 5 additions & 1 deletion parser/v2/templelementparser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ func TestTemplElementExpressionParser(t *testing.T) {
},
},
},
TrailingSpace: SpaceVertical,
},
},
{
Expand All @@ -53,6 +54,7 @@ func TestTemplElementExpressionParser(t *testing.T) {
},
},
},
TrailingSpace: SpaceVertical,
},
},
{
Expand Down Expand Up @@ -80,6 +82,7 @@ func TestTemplElementExpressionParser(t *testing.T) {
},
},
},
TrailingSpace: SpaceVertical,
},
},
{
Expand Down Expand Up @@ -190,8 +193,8 @@ func TestTemplElementExpressionParser(t *testing.T) {
To: Position{28, 1, 11},
},
},
TrailingSpace: SpaceVertical,
},
Whitespace{Value: "\n\t\t\t"},
},
},
},
Expand All @@ -215,6 +218,7 @@ func TestTemplElementExpressionParser(t *testing.T) {
},
},
},
TrailingSpace: SpaceHorizontal,
},
},
{
Expand Down
6 changes: 6 additions & 0 deletions parser/v2/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -956,6 +956,12 @@ type TemplElementExpression struct {
Expression Expression
// Children returns the elements in a block element.
Children []Node
// TrailingSpace lists what happens after the element.
TrailingSpace TrailingSpace
}

func (t TemplElementExpression) Trailing() TrailingSpace {
return t.TrailingSpace
}

func (tee TemplElementExpression) ChildNodes() []Node {
Expand Down

0 comments on commit dc3c684

Please sign in to comment.