Skip to content

Commit

Permalink
fix(fmt): Preserve newlines in most parsers (a-h#374)
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexanderArvidsson committed Sep 19, 2024
1 parent dc3c684 commit aa31f93
Show file tree
Hide file tree
Showing 25 changed files with 397 additions and 53 deletions.
5 changes: 5 additions & 0 deletions parser/v2/calltemplateparser.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,10 @@ func (p callTemplateExpressionParser) Parse(pi *parse.Input) (n Node, ok bool, e
return
}

// Parse trailing whitespace.
if _, _, err := addTrailingSpace(&r, pi, true); err != nil {
return r, false, err
}

return r, true, nil
}
22 changes: 22 additions & 0 deletions parser/v2/calltemplateparser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,28 @@ func TestCallTemplateExpressionParser(t *testing.T) {
},
},
},
{
name: "call: can parse the initial expression and leave the text",
input: `{!Other(p.Test)} Home`,
expected: CallTemplateExpression{
Expression: Expression{
Value: "Other(p.Test)",
Range: Range{
From: Position{
Index: 2,
Line: 0,
Col: 2,
},
To: Position{
Index: 15,
Line: 0,
Col: 15,
},
},
},
TrailingSpace: SpaceHorizontal,
},
},
}
for _, tt := range tests {
tt := tt
Expand Down
7 changes: 1 addition & 6 deletions parser/v2/elementparser.go
Original file line number Diff line number Diff line change
Expand Up @@ -467,12 +467,7 @@ func addTrailingSpaceAndValidate(start parse.Position, e Element, pi *parse.Inpu
return e, false, err
}
// Add trailing space.
ws, _, err := parse.Whitespace.Parse(pi)
if err != nil {
return e, false, err
}
e.TrailingSpace, err = NewTrailingSpace(ws, true)
if err != nil {
if _, _, err := addTrailingSpace(&e, pi, true); err != nil {
return e, false, err
}

Expand Down
15 changes: 14 additions & 1 deletion parser/v2/forexpressionparser.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ var forExpression parse.Parser[Node] = forExpressionParser{}
type forExpressionParser struct{}

func (forExpressionParser) Parse(pi *parse.Input) (n Node, ok bool, err error) {
var r ForExpression
r := ForExpression{
// Default behavior is always a trailing space
TrailingSpace: SpaceVertical,
}
start := pi.Index()

// Strip leading whitespace and look for `for `.
Expand Down Expand Up @@ -48,5 +51,15 @@ func (forExpressionParser) Parse(pi *parse.Input) (n Node, ok bool, err error) {
return
}

// Parse trailing whitespace.
if _, _, err := addTrailingSpace(&r, pi, true); err != nil {
return r, false, err
}

// If the trailing space is not vertical, set it to vertical.
if r.TrailingSpace != SpaceVertical && r.TrailingSpace != SpaceVerticalDouble {
r.TrailingSpace = SpaceVertical
}

return r, true, nil
}
2 changes: 2 additions & 0 deletions parser/v2/forexpressionparser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ func TestForExpressionParser(t *testing.T) {
TrailingSpace: SpaceVertical,
},
},
TrailingSpace: SpaceVertical,
},
},
{
Expand Down Expand Up @@ -117,6 +118,7 @@ func TestForExpressionParser(t *testing.T) {
TrailingSpace: SpaceVertical,
},
},
TrailingSpace: SpaceVertical,
},
},
}
Expand Down
34 changes: 34 additions & 0 deletions parser/v2/formattestdata/calltemplate_newline_is_preserved.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
-- in --
package main

templ test() {
<span></span>


{! Other(p.Test) }
{!Other(p.Test)} Home

<div>Some standard templ</div>

{!Other(p.Test) }



<span></span>

}
-- out --
package main

templ test() {
<span></span>

@Other(p.Test)
@Other(p.Test) Home
<div>Some standard templ</div>

@Other(p.Test)

<span></span>

}
31 changes: 31 additions & 0 deletions parser/v2/formattestdata/comments_newline_is_preserved.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
-- in --
package main

templ test() {
<!-- This is a comment -->

// This is not included in the output.
<div>Some standard templ</div>


/* This is not included in the output too. */
/*
Leave this alone.
*/


}
-- out --
package main

templ test() {
<!-- This is a comment -->

// This is not included in the output.
<div>Some standard templ</div>

/* This is not included in the output too. */
/*
Leave this alone.
*/
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ package test
templ input(items []string) {
<div>{ "the" }<div>{ "other" }</div>for _, item := range items {
<div>{ item }</div>
}</div>
}<span>After closing bracket</span></div>
}
-- out --
package test
Expand All @@ -16,5 +16,6 @@ templ input(items []string) {
for _, item := range items {
<div>{ item }</div>
}
<span>After closing bracket</span>
</div>
}
33 changes: 33 additions & 0 deletions parser/v2/formattestdata/for_loops_newline_is_preserved.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
-- in --
package main

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

for _, item := range items {

<div>{ item }</div>

}



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

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

for _, item := range items {
<div>{ item }</div>

}

<span>Foo Bar </span>
</div>
}
37 changes: 37 additions & 0 deletions parser/v2/formattestdata/if_statement_newline_is_preserved.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
-- in --
package main

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

if true {
<span>Test</span>

} else {

<span>Test</span>
}



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

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

if true {
<span>Test</span>

} else {
<span>Test</span>
}

<span>Foo Bar </span>
</div>
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ templ input(items []string) {
<div>{ items[0] }</div>
} else {
<div>{ items[1] }</div>
}
} <span>After closing brace</span>
</div>
}
-- out --
Expand All @@ -21,5 +21,6 @@ templ input(items []string) {
} else {
<div>{ items[1] }</div>
}
<span>After closing brace</span>
</div>
}
43 changes: 43 additions & 0 deletions parser/v2/formattestdata/switch_newline_is_preserved.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
-- in --
package main

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

switch items[0] {


case "a":
<div>{ items[0] }</div>


case "b":
<div>{ items[1] }</div>

}



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

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

switch items[0] {
case "a":
<div>{ items[0] }</div>

case "b":
<div>{ items[1] }</div>

}

<span>Foo Bar </span>
</div>
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ templ input(items []string) {
<div>{ items[0] }</div>
case "b":
<div>{ items[1] }</div>
}</div>
}<span>After closing bracket</span></div>
}
-- out --
package test
Expand All @@ -22,5 +22,6 @@ templ input(items []string) {
case "b":
<div>{ items[1] }</div>
}
<span>After closing bracket</span>
</div>
}
7 changes: 1 addition & 6 deletions parser/v2/gocodeparser.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,7 @@ var goCode = parse.Func(func(pi *parse.Input) (n Node, ok bool, err error) {
}

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

Expand Down
5 changes: 5 additions & 0 deletions parser/v2/htmlcommentparser.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,10 @@ func (p htmlCommentParser) Parse(pi *parse.Input) (n Node, ok bool, err error) {
return
}

// Parse trailing whitespace.
if _, _, err := addTrailingSpace(&c, pi, true); err != nil {
return c, false, err
}

return c, true, nil
}
15 changes: 14 additions & 1 deletion parser/v2/ifexpressionparser.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ var untilElseIfElseOrEnd = parse.Any(StripType(elseIfExpression), StripType(else
type ifExpressionParser struct{}

func (ifExpressionParser) Parse(pi *parse.Input) (n Node, ok bool, err error) {
var r IfExpression
r := IfExpression{
// Default behavior is always a trailing space
TrailingSpace: SpaceVertical,
}
start := pi.Index()

if !peekPrefix(pi, "if ") {
Expand Down Expand Up @@ -60,6 +63,16 @@ func (ifExpressionParser) Parse(pi *parse.Input) (n Node, ok bool, err error) {
return
}

// Parse trailing whitespace.
if _, _, err := addTrailingSpace(&r, pi, true); err != nil {
return r, false, err
}

// If the trailing space is not vertical, set it to vertical.
if r.TrailingSpace != SpaceVertical && r.TrailingSpace != SpaceVerticalDouble {
r.TrailingSpace = SpaceVertical
}

return r, true, nil
}

Expand Down
Loading

0 comments on commit aa31f93

Please sign in to comment.