Skip to content

Commit

Permalink
Fix Wrapping After Identifier Where Not Allowed
Browse files Browse the repository at this point in the history
If we have an inner class, we can't wrap before calling a method on the
class (or we would have to move the dot operator to the end of the
line).
  • Loading branch information
cwarden committed Dec 8, 2023
1 parent 0fd753b commit 2998c4e
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 6 deletions.
1 change: 1 addition & 0 deletions formatter/chain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ func TestChain(t *testing.T) {
}{
{`Schema.SObjectType.Account.getRecordTypeInfosByDeveloperName().get('Business').getRecordTypeId()`, 3},
{`Fixtures.Contact(account).put(Contact.RecordTypeId, Schema.SObjectType.Contact.getRecordTypeInfosByDeveloperName().get('Person').getRecordTypeId()).put(Contact.My_Lookup__c, newRecord[0].Id).save()`, 4},
{`Fixtures.InquiryFactory.inquiry(program1).standardInquiry().patient(patient1).save()`, 4},
}

for _, tt := range tests {
Expand Down
16 changes: 11 additions & 5 deletions formatter/format_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,7 @@ func TestStatement(t *testing.T) {
{
`Psychological__c psyc = Fixtures.psychological(inq).put(Psychological__c.RecordTypeId, Schema.SObjectType.Psychological__c.getRecordTypeInfosByDeveloperName().get('ICD_10').getRecordTypeId()).put(Psychological__c.Diagnosis_Lookup__c, newDiagnosis[0].Id).save();`,
`Psychological__c psyc = Fixtures.psychological(inq)
.put(Psychological__c.RecordTypeId, Schema.SObjectType.Psychological__c
.getRecordTypeInfosByDeveloperName()
.put(Psychological__c.RecordTypeId, Schema.SObjectType.Psychological__c.getRecordTypeInfosByDeveloperName()
.get('ICD_10')
.getRecordTypeId())
.put(Psychological__c.Diagnosis_Lookup__c, newDiagnosis[0].Id)
Expand All @@ -149,6 +148,13 @@ func TestStatement(t *testing.T) {
cl_record.Last_Placement__c != Trigger.OldMap.get(cl_record.Id).Last_Placement__c))) {
x = 1;
}`},
{
// Don't wrap at what might be an inner class
`CRC_Inquiry__c inquiry1 = Fixtures.InquiryFactory.inquiry(program1).standardInquiry().patient(patient1).save();`,
`CRC_Inquiry__c inquiry1 = Fixtures.InquiryFactory.inquiry(program1)
.standardInquiry()
.patient(patient1)
.save();`},
}
for _, tt := range tests {
input := antlr.NewInputStream(tt.input)
Expand All @@ -164,7 +170,7 @@ func TestStatement(t *testing.T) {
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%s\ngot:\n%s\n", tt.output, out)
}
}

Expand Down Expand Up @@ -198,7 +204,7 @@ func TestCompilationUnit(t *testing.T) {
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%s\ngot:\n%s\n", tt.output, out)
}
}
}
Expand Down Expand Up @@ -240,7 +246,7 @@ func TestSOQL(t *testing.T) {
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%s\ngot:\n%s\n", tt.output, out)
}
}
}
7 changes: 6 additions & 1 deletion formatter/visitors.go
Original file line number Diff line number Diff line change
Expand Up @@ -531,9 +531,14 @@ func (v *FormatVisitor) VisitDotExpression(ctx *parser.DotExpressionContext) int
if depth == 0 {
depth = 1
}
switch ctx.Expression().(type) {
switch left := ctx.Expression().(type) {
case *parser.PrimaryExpressionContext:
log.Debug(fmt.Sprintf("NOT wrapping after between %q (%T)", expr, ctx.Expression()))
case *parser.DotExpressionContext:
if left.DotMethodCall() != nil {
log.Debug(fmt.Sprintf("%q is method call; safe to wrap before %q", expr, ctx.DotMethodCall().GetText()))
return expr.(string) + "\n" + v.indentTo(fmt.Sprintf("%s%s", dot, v.visitRule(ctx.DotMethodCall())), depth)
}
default:
log.Debug(fmt.Sprintf("Wrapping in between %q (%T) and %q", expr, ctx.Expression(), ctx.DotMethodCall().GetText()))
return expr.(string) + "\n" + v.indentTo(fmt.Sprintf("%s%s", dot, v.visitRule(ctx.DotMethodCall())), depth)
Expand Down

0 comments on commit 2998c4e

Please sign in to comment.