Skip to content

Commit

Permalink
Fix Formatting of while and for Loops
Browse files Browse the repository at this point in the history
Fix bug in formatting of empty `while` loops.

Fix formatting of `for` loops.
  • Loading branch information
cwarden committed Jul 23, 2024
1 parent c27d124 commit 0d0c7f4
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
11 changes: 10 additions & 1 deletion formatter/format_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,15 @@ OneDayDischargeFollowUp.twoHoursDelay,
OneDayDischargeFollowUp.twoHoursDelay,
1
);`},
{
`while(true);`,
`while (true);`},
{
`for (Campaign campaign : results) resultList.add(campaign.Group__c);`,
`for (Campaign campaign : results) {
resultList.add(campaign.Group__c);
}
`},
}
for _, tt := range tests {
input := antlr.NewInputStream(tt.input)
Expand All @@ -269,7 +278,7 @@ OneDayDischargeFollowUp.twoHoursDelay,
t.Errorf("Unexpected result parsing apex")
}
if out != tt.output {
t.Errorf("unexpected format. expected:\n%s\ngot:\n%s\n", tt.output, out)
t.Errorf("unexpected format. expected:\n%q\ngot:\n%q\n", tt.output, out)
}
}

Expand Down
4 changes: 2 additions & 2 deletions formatter/visitors.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ func (v *FormatVisitor) VisitIfStatement(ctx *parser.IfStatementContext) interfa
}

func (v *FormatVisitor) VisitWhileStatement(ctx *parser.WhileStatementContext) interface{} {
if s := ctx.Statement; s == nil {
if s := ctx.Statement(); s == nil {
return fmt.Sprintf("while %s;", v.visitRule(ctx.ParExpression()))
}
if block := ctx.Statement().Block(); block != nil {
Expand All @@ -250,7 +250,7 @@ func (v *FormatVisitor) VisitForStatement(ctx *parser.ForStatementContext) inter
if statement.Block() != nil {
return fmt.Sprintf("for (%s) %s", v.visitRule(ctx.ForControl()), v.visitRule(ctx.Statement()))
} else {
return fmt.Sprintf("for (%s) {\n%s}\n", v.visitRule(ctx.ForControl()), v.indent(v.visitRule(ctx.Statement()).(string)))
return fmt.Sprintf("for (%s) {\n%s\n}\n", v.visitRule(ctx.ForControl()), v.indent(v.visitRule(ctx.Statement()).(string)))
}
} else {
return fmt.Sprintf("for (%s);", v.visitRule(ctx.ForControl()))
Expand Down

0 comments on commit 0d0c7f4

Please sign in to comment.