Skip to content

Commit

Permalink
Implement ExpressionFormatter
Browse files Browse the repository at this point in the history
  • Loading branch information
gmickus committed Dec 18, 2024
1 parent 19ce270 commit 142d86f
Show file tree
Hide file tree
Showing 27 changed files with 147 additions and 103 deletions.
6 changes: 3 additions & 3 deletions resources/functionalTests/block/6do-end2/input.p
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/* { "AblFormatter.blockFormatting": true}*/

do transaction:
do while true:
a = 3.
end.
do while true:
a = 3.
end.
end.
6 changes: 3 additions & 3 deletions resources/functionalTests/block/6do-end2/target.p
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/* { "AblFormatter.blockFormatting": true}*/

do transaction:
do while true:
a = 3.
end.
do while true:
a = 3.
end.
end.
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/* formatterSettingsOverride */
/* { "AblFormatter.expressionFormatting": true,
"AblFormatter.expressionFormattingLogicalLocation": "New",
"AblFormatter.forFormatting": true}*/

FOR EACH enchantedMenu WHERE enchantedMenu.StyleCode = "" OR enchantedMenu.StyleCode = "APPBAR" OR
enchantedMenu.StyleCode = "EXPLORER" OR
enchantedMenu.StyleCode = "TASKBAR":

MESSAGE "Greetings".
END.

Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/* formatterSettingsOverride */
/* { "AblFormatter.expressionFormatting": true,
"AblFormatter.expressionFormattingLogicalLocation": "New",
"AblFormatter.forFormatting": true}*/

FOR EACH enchantedMenu WHERE
enchantedMenu.StyleCode = "" OR
enchantedMenu.StyleCode = "APPBAR" OR
enchantedMenu.StyleCode = "EXPLORER" OR
enchantedMenu.StyleCode = "TASKBAR":

MESSAGE "Greetings".
END.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/* formatterSettingsOverride */
/* { "AblFormatter.expressionFormatting": true,
"AblFormatter.expressionFormattingLogicalLocation": "Same",
"AblFormatter.forFormatting": false}*/


for each A where (Customer.CreditLimit >= low-credit) AND
(Customer.CreditLimit <= hi-credit):

end.
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
/* formatterSettingsOverride */
/* { "AblFormatter.expressionFormatting": true,
"AblFormatter.expressionFormattingLogicalLocation": "Same"}*/

for each A where (Customer.CreditLimit >= low-credit) AND (Customer.CreditLimit <= hi-credit):

end.
/* formatterSettingsOverride */
/* { "AblFormatter.expressionFormatting": true,
"AblFormatter.expressionFormattingLogicalLocation": "Same",
"AblFormatter.forFormatting": false}*/


for each A where (Customer.CreditLimit >= low-credit) AND (Customer.CreditLimit <= hi-credit):

end.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
/* formatterSettingsOverride */
/* { "AblFormatter.expressionFormatting": true,
"AblFormatter.expressionFormattingLogicalLocation": "New"}*/

FOR EACH customer WHERE (customer.balance > 1000 AND customer.status = "Active")
OR (customer.balance <= 1000 AND customer.status = "Inactive")
AND customer.country = "USA":

DISPLAY customer.name customer.balance customer.status customer.country.
END.
/* formatterSettingsOverride */
/* { "AblFormatter.expressionFormatting": true,
"AblFormatter.expressionFormattingLogicalLocation": "New",
"AblFormatter.forFormatting": false}*/

FOR EACH customer WHERE (customer.balance > 1000 AND customer.status = "Active") OR (customer.balance <= 1000 AND customer.status = "Inactive")
AND customer.country = "USA":

DISPLAY customer.name customer.balance customer.status customer.country.
END.
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
/* formatterSettingsOverride */
/* { "AblFormatter.expressionFormatting": true,
"AblFormatter.expressionFormattingLogicalLocation": "New"}*/

FOR EACH customer WHERE (customer.balance > 1000 AND
customer.status = "Active") OR
(customer.balance <= 1000 AND
customer.status = "Inactive") AND
customer.country = "USA":

DISPLAY customer.name customer.balance customer.status customer.country.
END.
/* formatterSettingsOverride */
/* { "AblFormatter.expressionFormatting": true,
"AblFormatter.expressionFormattingLogicalLocation": "New",
"AblFormatter.forFormatting": false}*/

FOR EACH customer WHERE (customer.balance > 1000 AND
customer.status = "Active") OR
(customer.balance <= 1000 AND
customer.status = "Inactive") AND
customer.country = "USA":

DISPLAY customer.name customer.balance customer.status customer.country.
END.
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/* formatterSettingsOverride */
/* { "AblFormatter.expressionFormatting": true,
"AblFormatter.expressionFormattingLogicalLocation": "New",
"AblFormatter.forFormatting": true}*/

FOR EACH
enchantedAttribute WHERE enchantedAttribute.MagicValueGuid = sourceBuffer::MagicValueGuid
OR (enchantedAttribute.MagicInstanceGuid = sourceBuffer::MagicInstanceGuid AND enchantedAttribute.MagicLabel = sourceBuffer::MagicLabel):
delete enchantedAttribute .
end.

Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/* formatterSettingsOverride */
/* { "AblFormatter.expressionFormatting": true,
"AblFormatter.expressionFormattingLogicalLocation": "New",
"AblFormatter.forFormatting": true}*/

FOR EACH enchantedAttribute WHERE
enchantedAttribute.MagicValueGuid = sourceBuffer::MagicValueGuid OR
(enchantedAttribute.MagicInstanceGuid = sourceBuffer::MagicInstanceGuid AND
enchantedAttribute.MagicLabel = sourceBuffer::MagicLabel):
delete enchantedAttribute .
end.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* formatterSettingsOverride */
/* { "AblFormatter.expressionFormatting": true}*/

a = b >= c.
a = b >= c.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* formatterSettingsOverride */
/* { "AblFormatter.expressionFormatting": true}*/

a [i] = b[j].
a[i] = c + b[x * 3].
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* formatterSettingsOverride */
/* { "AblFormatter.expressionFormatting": true}*/

a[i] = b[j].
a[i] = c + b[x * 3].

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* formatterSettingsOverride */
/* { "AblFormatter.expressionFormatting": true}*/

a = b modulo c.
a = b modulo c.
2 changes: 1 addition & 1 deletion resources/functionalTests/expression/4assign-new/input.p
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* formatterSettingsOverride */
/* { "AblFormatter.expressionFormatting": true}*/

myCustObj = NEW acme.myObjs.CustObj ( ) .
myCustObj = NEW acme.myObjs.CustObj(a + 3 * 3).
2 changes: 1 addition & 1 deletion resources/functionalTests/expression/4assign-new/target.p
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* formatterSettingsOverride */
/* { "AblFormatter.expressionFormatting": true}*/

myCustObj = NEW acme.myObjs.CustObj().
myCustObj = NEW acme.myObjs.CustObj(a + 3 * 3).
2 changes: 1 addition & 1 deletion resources/functionalTests/expression/4assign-unary/input.p
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* formatterSettingsOverride */
/* { "AblFormatter.expressionFormatting": true}*/

a = not c.
a = not c.
2 changes: 1 addition & 1 deletion resources/functionalTests/expression/4assign/input.p
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* formatterSettingsOverride */
/* { "AblFormatter.expressionFormatting": true}*/

a = ( b + c ) * ( d - e ) / f.
a = ( b + c ) * ( d - e ) / f.
60 changes: 35 additions & 25 deletions src/v2/formatterFramework/FormatterHelper.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import { SyntaxNode } from "web-tree-sitter";
import { FullText } from "../model/FullText";
import {
arithmeticOperators,
SyntaxNodeType,
} from "../../model/SyntaxNodeType";
import { SyntaxNodeType } from "../../model/SyntaxNodeType";

export class FormatterHelper {
public static getActualTextIndentation(
Expand Down Expand Up @@ -126,6 +123,23 @@ export class FormatterHelper {
return indentedLines.join(eolDelimiter);
}

public static alignIndentation(
text: string,
leadingSpaces: number,
eolDelimiter: string
): string {
// Split the text into lines
const lines = text.split(eolDelimiter);

// Change indentation to each line except the first one
const indentedLines = lines.map((line, index) => {
return index === 0 ? line : " ".repeat(leadingSpaces) + line.trim();
});

// Join the lines back into a single string
return indentedLines.join(eolDelimiter);
}

private static countLeadingSpaces(text: string): number {
// Use a regular expression to match leading spaces
const match = text.match(/^(\s*)/);
Expand Down Expand Up @@ -164,7 +178,17 @@ export class FormatterHelper {
resultString = resultString.trimStart() + ".";
}

return resultString;
const parent = node.parent;
if (
parent !== null &&
(parent.type === SyntaxNodeType.AblStatement ||
parent.type === SyntaxNodeType.Assignment ||
parent.type === SyntaxNodeType.WhereClause)
) {
return resultString.trimEnd();
}

return resultString.trim();
}
}

Expand All @@ -173,8 +197,6 @@ export class FormatterHelper {
fullText: Readonly<FullText>,
currentlyInsideParentheses: Boolean
): string {
console.log("nodeType: " + node.type);
console.log("par? " + currentlyInsideParentheses);
if (currentlyInsideParentheses === true) {
return this.getParenthesizedExpressionString(node, fullText);
}
Expand Down Expand Up @@ -202,23 +224,12 @@ export class FormatterHelper {
fullText
);
break;
// Recheck the code below after ticket #116 is closed!
case SyntaxNodeType.EqualsSign:
const previousSibling = node.previousSibling;
newString =
previousSibling !== null &&
(arithmeticOperators.hasFancy(previousSibling.type, "") ||
previousSibling.hasError())
? FormatterHelper.getCurrentText(node, fullText).trim()
: " " +
FormatterHelper.getCurrentText(node, fullText).trim();
break;
case SyntaxNodeType.ArrayLiteral:
newString = FormatterHelper.getCurrentText(
node,
fullText
).trim();
break;
// case SyntaxNodeType.ArrayLiteral:
// newString = FormatterHelper.getCurrentText(
// node,
// fullText
// ).trim();
// break;
default:
const text = FormatterHelper.getCurrentText(
node,
Expand Down Expand Up @@ -250,7 +261,6 @@ export class FormatterHelper {
} else {
newString = FormatterHelper.getCurrentText(node, fullText);
}
console.log("newString:\n" + newString);
return newString;
}
}
1 change: 1 addition & 0 deletions src/v2/formatterFramework/enableFormatterDecorators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,5 @@ export function enableFormatterDecorators(): void {
EnumFormatter;
ProcedureParameterFormatter;
FunctionParameterFormatter;
ExpressionFormatter;
}
6 changes: 1 addition & 5 deletions src/v2/formatters/expression/ExpressionFormatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,7 @@ export class ExpressionFormatter extends AFormatter implements IFormatter {
node.type === SyntaxNodeType.ParenthesizedExpression ||
node.type === SyntaxNodeType.AdditiveExpression ||
node.type === SyntaxNodeType.MultiplicativeExpression ||
node.type === SyntaxNodeType.Assignment ||
node.type === SyntaxNodeType.UnaryExpression ||
node.type === SyntaxNodeType.NewExpression ||
node.type === SyntaxNodeType.VariableAssignment ||
node.type === SyntaxNodeType.FunctionalCallArgument
node.type === SyntaxNodeType.UnaryExpression
) {
return true;
}
Expand Down
5 changes: 5 additions & 0 deletions src/v2/formatters/for/ForFormatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,11 @@ export class ForFormatter extends AFormatter implements IFormatter {
fullText,
alignColumn
);
newString = FormatterHelper.alignIndentation(
newString,
alignColumn + 1,
fullText.eolDelimiter
);
break;
case SyntaxNodeType.EndKeyword:
newString =
Expand Down

0 comments on commit 142d86f

Please sign in to comment.