Skip to content

Commit

Permalink
Fix #16 - long expressions not breaking into multi lines
Browse files Browse the repository at this point in the history
  • Loading branch information
dangmai committed Mar 6, 2019
1 parent c95bd2c commit 1bca96e
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 17 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
- Fix formatting for trailing comments at the end of block statements ([issue](https://github.com/dangmai/prettier-plugin-apex/issues/12)).
- Fix issue in which comments right before semi colon are not printed ([issue](https://github.com/dangmai/prettier-plugin-apex/issues/11)).
- Assert that all comments have been printed out in the formatted code.
- Fix long expressions not breaking into multi lines ([issue](https://github.com/dangmai/prettier-plugin-apex/issues/16)).

## 1.0.0-alpha.5

Expand Down
24 changes: 14 additions & 10 deletions src/printer.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,20 +82,20 @@ function handleReturnStatement(path, print) {
return concat(docs);
}

function handleBinaryExpression(path, print) {
function handleGenericExpression(path, print) {
const docs = [];
const leftDoc = path.call(print, "left");
const operationDoc = path.call(print, "op");
const rightDoc = path.call(print, "right");
docs.push(leftDoc);
docs.push(" ");
docs.push(operationDoc);
docs.push(" ");
docs.push(line);
docs.push(rightDoc);
return concat(docs);
return groupConcat(docs);
}

function handleGenericExpression(path, print) {
function handleAssignmentExpression(path, print) {
const docs = [];
const leftDoc = path.call(print, "left");
const operationDoc = path.call(print, "op");
Expand All @@ -105,7 +105,7 @@ function handleGenericExpression(path, print) {
docs.push(operationDoc);
docs.push(" ");
docs.push(rightDoc);
return concat(docs);
return groupConcat(docs);
}

function handleVariableExpression(path, print) {
Expand Down Expand Up @@ -947,12 +947,16 @@ function handleIfBlock(path, print) {
const statementDoc = path.call(print, "stmnt");

const parts = [];
const conditionParts = [];
parts.push("if");
parts.push(" ");
// Condition expression
parts.push("(");
parts.push(path.call(print, "expr"));
parts.push(")");
conditionParts.push("(");
conditionParts.push(softline);
conditionParts.push(path.call(print, "expr"));
conditionParts.push(dedent(softline));
conditionParts.push(")");
parts.push(groupIndentConcat(conditionParts));
// Body block
if (statementType === apexNames.BLOCK_STATEMENT) {
parts.push(" ");
Expand Down Expand Up @@ -2072,11 +2076,11 @@ nodeHandler[apexNames.INNER_INTERFACE_MEMBER] = _handlePassthroughCall("body");
// Expression
nodeHandler[apexNames.TERNARY_EXPRESSION] = handleTernaryExpression;
nodeHandler[apexNames.BOOLEAN_EXPRESSION] = handleGenericExpression;
nodeHandler[apexNames.ASSIGNMENT_EXPRESSION] = handleGenericExpression;
nodeHandler[apexNames.ASSIGNMENT_EXPRESSION] = handleAssignmentExpression;
nodeHandler[apexNames.NESTED_EXPRESSION] = handleNestedExpression;
nodeHandler[apexNames.VARIABLE_EXPRESSION] = handleVariableExpression;
nodeHandler[apexNames.LITERAL_EXPRESSION] = handleLiteralExpression;
nodeHandler[apexNames.BINARY_EXPRESSION] = handleBinaryExpression;
nodeHandler[apexNames.BINARY_EXPRESSION] = handleGenericExpression;
nodeHandler[apexNames.TRIGGER_VARIABLE_EXPRESSION] = (path, print) =>
concat(["Trigger", ".", path.call(print, "variable")]);
nodeHandler[apexNames.NEW_EXPRESSION] = handleNewExpression;
Expand Down
21 changes: 14 additions & 7 deletions tests/comments/__snapshots__/jsfmt.spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -165,24 +165,31 @@ class Comments {
Integer a;
a = // Comment
i + j;
a = i - // Comment
i +
j;
a = i -
// Comment
j;
a = i
/* Comment */ * j;
a = i / /* Comment */
/* Comment */ *
j;
a = i /
/* Comment */
j;
a = // Comment
i << /* Comment */
i <<
/* Comment */
j;
a = i /* Comment */ >> /* Comment 2 */ j;
a = /* Comments */ i >>> j /* Comments 2 */;
a = /* Comments */ i ^ /* Comments 2 */ j;
a = // Comments
i & // Comments
i &
// Comments
j;
a = /* Comments */
i | j
i |
j
/* Comments */;
}
}
Expand Down
6 changes: 6 additions & 0 deletions tests/conditional/ConditionalClass.cls
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,11 @@ class Conditional {
System.debug('false');
}
}

void veryLongConditional() {
if (!SObjectType.Attribute__c.fields.Name.isAccessible() && !SObjectType.Attribute__c.fields.Display_on_Actions_Tab__c.isAccessible() || !SObjectType.Attribute__c.fields.Display_Order__c.isAccessible() || !SObjectType.Attribute__c.fields.Icon__c.isAccessible() || !SObjectType.Action__c.fields.Name.isAccessible() || !SObjectType.Action__c.fields.Description__c.isAccessible() || !SObjectType.Action__c.fields.Translation_Index__c.isAccessible() || !SObjectType.Action__c.fields.State__c.isAccessible() || !SObjectType.Action__c.fields.Display_Tab__c.isAccessible() || !SObjectType.Action__c.fields.Display_Tile__c.isAccessible() || !SObjectType.Action__c.fields.Action_Type_Attribute__c.isAccessible() || !SObjectType.Action__c.fields.Tile_Type_Attribute__c.isAccessible() || !SObjectType.Action__c.fields.From_Date__c.isAccessible() || !SObjectType.Action__c.fields.To_Date__c.isAccessible() || !SObjectType.Action__c.fields.Parent__c.isAccessible() | !SObjectType.Action__c.fields.RecordTypeId.isAccessible()) {
System.debug('true');
}
}
}

29 changes: 29 additions & 0 deletions tests/conditional/__snapshots__/jsfmt.spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ class Conditional {
System.debug('false');
}
}
void veryLongConditional() {
if (!SObjectType.Attribute__c.fields.Name.isAccessible() && !SObjectType.Attribute__c.fields.Display_on_Actions_Tab__c.isAccessible() || !SObjectType.Attribute__c.fields.Display_Order__c.isAccessible() || !SObjectType.Attribute__c.fields.Icon__c.isAccessible() || !SObjectType.Action__c.fields.Name.isAccessible() || !SObjectType.Action__c.fields.Description__c.isAccessible() || !SObjectType.Action__c.fields.Translation_Index__c.isAccessible() || !SObjectType.Action__c.fields.State__c.isAccessible() || !SObjectType.Action__c.fields.Display_Tab__c.isAccessible() || !SObjectType.Action__c.fields.Display_Tile__c.isAccessible() || !SObjectType.Action__c.fields.Action_Type_Attribute__c.isAccessible() || !SObjectType.Action__c.fields.Tile_Type_Attribute__c.isAccessible() || !SObjectType.Action__c.fields.From_Date__c.isAccessible() || !SObjectType.Action__c.fields.To_Date__c.isAccessible() || !SObjectType.Action__c.fields.Parent__c.isAccessible() | !SObjectType.Action__c.fields.RecordTypeId.isAccessible()) {
System.debug('true');
}
}
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Expand Down Expand Up @@ -91,6 +97,29 @@ class Conditional {
System.debug('false');
}
}
void veryLongConditional() {
if (
!SObjectType.Attribute__c.fields.Name.isAccessible() &&
!SObjectType.Attribute__c.fields.Display_on_Actions_Tab__c.isAccessible() ||
!SObjectType.Attribute__c.fields.Display_Order__c.isAccessible() ||
!SObjectType.Attribute__c.fields.Icon__c.isAccessible() ||
!SObjectType.Action__c.fields.Name.isAccessible() ||
!SObjectType.Action__c.fields.Description__c.isAccessible() ||
!SObjectType.Action__c.fields.Translation_Index__c.isAccessible() ||
!SObjectType.Action__c.fields.State__c.isAccessible() ||
!SObjectType.Action__c.fields.Display_Tab__c.isAccessible() ||
!SObjectType.Action__c.fields.Display_Tile__c.isAccessible() ||
!SObjectType.Action__c.fields.Action_Type_Attribute__c.isAccessible() ||
!SObjectType.Action__c.fields.Tile_Type_Attribute__c.isAccessible() ||
!SObjectType.Action__c.fields.From_Date__c.isAccessible() ||
!SObjectType.Action__c.fields.To_Date__c.isAccessible() ||
!SObjectType.Action__c.fields.Parent__c.isAccessible() |
!SObjectType.Action__c.fields.RecordTypeId.isAccessible()
) {
System.debug('true');
}
}
}
`;

0 comments on commit 1bca96e

Please sign in to comment.