Skip to content

Commit 142d86f

Browse files
author
gmickus
committed
Implement ExpressionFormatter
1 parent 19ce270 commit 142d86f

File tree

27 files changed

+147
-103
lines changed

27 files changed

+147
-103
lines changed

resources/functionalTests/block/6do-end2/input.p

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
/* { "AblFormatter.blockFormatting": true}*/
33

44
do transaction:
5-
do while true:
6-
a = 3.
7-
end.
5+
do while true:
6+
a = 3.
7+
end.
88
end.

resources/functionalTests/block/6do-end2/target.p

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
/* { "AblFormatter.blockFormatting": true}*/
33

44
do transaction:
5-
do while true:
6-
a = 3.
7-
end.
5+
do while true:
6+
a = 3.
7+
end.
88
end.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/* formatterSettingsOverride */
2+
/* { "AblFormatter.expressionFormatting": true,
3+
"AblFormatter.expressionFormattingLogicalLocation": "New",
4+
"AblFormatter.forFormatting": true}*/
5+
6+
FOR EACH enchantedMenu WHERE enchantedMenu.StyleCode = "" OR enchantedMenu.StyleCode = "APPBAR" OR
7+
enchantedMenu.StyleCode = "EXPLORER" OR
8+
enchantedMenu.StyleCode = "TASKBAR":
9+
10+
MESSAGE "Greetings".
11+
END.
12+
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/* formatterSettingsOverride */
2+
/* { "AblFormatter.expressionFormatting": true,
3+
"AblFormatter.expressionFormattingLogicalLocation": "New",
4+
"AblFormatter.forFormatting": true}*/
5+
6+
FOR EACH enchantedMenu WHERE
7+
enchantedMenu.StyleCode = "" OR
8+
enchantedMenu.StyleCode = "APPBAR" OR
9+
enchantedMenu.StyleCode = "EXPLORER" OR
10+
enchantedMenu.StyleCode = "TASKBAR":
11+
12+
MESSAGE "Greetings".
13+
END.
14+

resources/functionalTests/expression/1logical-for-new-line/input.p

Lines changed: 0 additions & 7 deletions
This file was deleted.

resources/functionalTests/expression/1logical-for-new-line/target.p

Lines changed: 0 additions & 8 deletions
This file was deleted.
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/* formatterSettingsOverride */
2+
/* { "AblFormatter.expressionFormatting": true,
3+
"AblFormatter.expressionFormattingLogicalLocation": "Same",
4+
"AblFormatter.forFormatting": false}*/
5+
6+
7+
for each A where (Customer.CreditLimit >= low-credit) AND
8+
(Customer.CreditLimit <= hi-credit):
9+
10+
end.
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
/* formatterSettingsOverride */
2-
/* { "AblFormatter.expressionFormatting": true,
3-
"AblFormatter.expressionFormattingLogicalLocation": "Same"}*/
4-
5-
for each A where (Customer.CreditLimit >= low-credit) AND (Customer.CreditLimit <= hi-credit):
6-
7-
end.
1+
/* formatterSettingsOverride */
2+
/* { "AblFormatter.expressionFormatting": true,
3+
"AblFormatter.expressionFormattingLogicalLocation": "Same",
4+
"AblFormatter.forFormatting": false}*/
5+
6+
7+
for each A where (Customer.CreditLimit >= low-credit) AND (Customer.CreditLimit <= hi-credit):
8+
9+
end.

resources/functionalTests/expression/1logical-for-same-line/input.p

Lines changed: 0 additions & 7 deletions
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
/* formatterSettingsOverride */
2-
/* { "AblFormatter.expressionFormatting": true,
3-
"AblFormatter.expressionFormattingLogicalLocation": "New"}*/
4-
5-
FOR EACH customer WHERE (customer.balance > 1000 AND customer.status = "Active")
6-
OR (customer.balance <= 1000 AND customer.status = "Inactive")
7-
AND customer.country = "USA":
8-
9-
DISPLAY customer.name customer.balance customer.status customer.country.
10-
END.
1+
/* formatterSettingsOverride */
2+
/* { "AblFormatter.expressionFormatting": true,
3+
"AblFormatter.expressionFormattingLogicalLocation": "New",
4+
"AblFormatter.forFormatting": false}*/
5+
6+
FOR EACH customer WHERE (customer.balance > 1000 AND customer.status = "Active") OR (customer.balance <= 1000 AND customer.status = "Inactive")
7+
AND customer.country = "USA":
8+
9+
DISPLAY customer.name customer.balance customer.status customer.country.
10+
END.
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1-
/* formatterSettingsOverride */
2-
/* { "AblFormatter.expressionFormatting": true,
3-
"AblFormatter.expressionFormattingLogicalLocation": "New"}*/
4-
5-
FOR EACH customer WHERE (customer.balance > 1000 AND
6-
customer.status = "Active") OR
7-
(customer.balance <= 1000 AND
8-
customer.status = "Inactive") AND
9-
customer.country = "USA":
10-
11-
DISPLAY customer.name customer.balance customer.status customer.country.
12-
END.
1+
/* formatterSettingsOverride */
2+
/* { "AblFormatter.expressionFormatting": true,
3+
"AblFormatter.expressionFormattingLogicalLocation": "New",
4+
"AblFormatter.forFormatting": false}*/
5+
6+
FOR EACH customer WHERE (customer.balance > 1000 AND
7+
customer.status = "Active") OR
8+
(customer.balance <= 1000 AND
9+
customer.status = "Inactive") AND
10+
customer.country = "USA":
11+
12+
DISPLAY customer.name customer.balance customer.status customer.country.
13+
END.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/* formatterSettingsOverride */
2+
/* { "AblFormatter.expressionFormatting": true,
3+
"AblFormatter.expressionFormattingLogicalLocation": "New",
4+
"AblFormatter.forFormatting": true}*/
5+
6+
FOR EACH
7+
enchantedAttribute WHERE enchantedAttribute.MagicValueGuid = sourceBuffer::MagicValueGuid
8+
OR (enchantedAttribute.MagicInstanceGuid = sourceBuffer::MagicInstanceGuid AND enchantedAttribute.MagicLabel = sourceBuffer::MagicLabel):
9+
delete enchantedAttribute .
10+
end.
11+
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/* formatterSettingsOverride */
2+
/* { "AblFormatter.expressionFormatting": true,
3+
"AblFormatter.expressionFormattingLogicalLocation": "New",
4+
"AblFormatter.forFormatting": true}*/
5+
6+
FOR EACH enchantedAttribute WHERE
7+
enchantedAttribute.MagicValueGuid = sourceBuffer::MagicValueGuid OR
8+
(enchantedAttribute.MagicInstanceGuid = sourceBuffer::MagicInstanceGuid AND
9+
enchantedAttribute.MagicLabel = sourceBuffer::MagicLabel):
10+
delete enchantedAttribute .
11+
end.
12+
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
/* formatterSettingsOverride */
22
/* { "AblFormatter.expressionFormatting": true}*/
33

4-
a = b >= c.
4+
a = b >= c.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
/* formatterSettingsOverride */
22
/* { "AblFormatter.expressionFormatting": true}*/
33

4-
a [i] = b[j].
4+
a[i] = c + b[x * 3].
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
/* formatterSettingsOverride */
22
/* { "AblFormatter.expressionFormatting": true}*/
33

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

resources/functionalTests/expression/4assign-dynamic-new/input.p

Lines changed: 0 additions & 4 deletions
This file was deleted.

resources/functionalTests/expression/4assign-dynamic-new/target.p

Lines changed: 0 additions & 4 deletions
This file was deleted.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
/* formatterSettingsOverride */
22
/* { "AblFormatter.expressionFormatting": true}*/
33

4-
a = b modulo c.
4+
a = b modulo c.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
/* formatterSettingsOverride */
22
/* { "AblFormatter.expressionFormatting": true}*/
33

4-
myCustObj = NEW acme.myObjs.CustObj ( ) .
4+
myCustObj = NEW acme.myObjs.CustObj(a + 3 * 3).
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
/* formatterSettingsOverride */
22
/* { "AblFormatter.expressionFormatting": true}*/
33

4-
myCustObj = NEW acme.myObjs.CustObj().
4+
myCustObj = NEW acme.myObjs.CustObj(a + 3 * 3).
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
/* formatterSettingsOverride */
22
/* { "AblFormatter.expressionFormatting": true}*/
33

4-
a = not c.
4+
a = not c.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
/* formatterSettingsOverride */
22
/* { "AblFormatter.expressionFormatting": true}*/
33

4-
a = ( b + c ) * ( d - e ) / f.
4+
a = ( b + c ) * ( d - e ) / f.

src/v2/formatterFramework/FormatterHelper.ts

Lines changed: 35 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
import { SyntaxNode } from "web-tree-sitter";
22
import { FullText } from "../model/FullText";
3-
import {
4-
arithmeticOperators,
5-
SyntaxNodeType,
6-
} from "../../model/SyntaxNodeType";
3+
import { SyntaxNodeType } from "../../model/SyntaxNodeType";
74

85
export class FormatterHelper {
96
public static getActualTextIndentation(
@@ -126,6 +123,23 @@ export class FormatterHelper {
126123
return indentedLines.join(eolDelimiter);
127124
}
128125

126+
public static alignIndentation(
127+
text: string,
128+
leadingSpaces: number,
129+
eolDelimiter: string
130+
): string {
131+
// Split the text into lines
132+
const lines = text.split(eolDelimiter);
133+
134+
// Change indentation to each line except the first one
135+
const indentedLines = lines.map((line, index) => {
136+
return index === 0 ? line : " ".repeat(leadingSpaces) + line.trim();
137+
});
138+
139+
// Join the lines back into a single string
140+
return indentedLines.join(eolDelimiter);
141+
}
142+
129143
private static countLeadingSpaces(text: string): number {
130144
// Use a regular expression to match leading spaces
131145
const match = text.match(/^(\s*)/);
@@ -164,7 +178,17 @@ export class FormatterHelper {
164178
resultString = resultString.trimStart() + ".";
165179
}
166180

167-
return resultString;
181+
const parent = node.parent;
182+
if (
183+
parent !== null &&
184+
(parent.type === SyntaxNodeType.AblStatement ||
185+
parent.type === SyntaxNodeType.Assignment ||
186+
parent.type === SyntaxNodeType.WhereClause)
187+
) {
188+
return resultString.trimEnd();
189+
}
190+
191+
return resultString.trim();
168192
}
169193
}
170194

@@ -173,8 +197,6 @@ export class FormatterHelper {
173197
fullText: Readonly<FullText>,
174198
currentlyInsideParentheses: Boolean
175199
): string {
176-
console.log("nodeType: " + node.type);
177-
console.log("par? " + currentlyInsideParentheses);
178200
if (currentlyInsideParentheses === true) {
179201
return this.getParenthesizedExpressionString(node, fullText);
180202
}
@@ -202,23 +224,12 @@ export class FormatterHelper {
202224
fullText
203225
);
204226
break;
205-
// Recheck the code below after ticket #116 is closed!
206-
case SyntaxNodeType.EqualsSign:
207-
const previousSibling = node.previousSibling;
208-
newString =
209-
previousSibling !== null &&
210-
(arithmeticOperators.hasFancy(previousSibling.type, "") ||
211-
previousSibling.hasError())
212-
? FormatterHelper.getCurrentText(node, fullText).trim()
213-
: " " +
214-
FormatterHelper.getCurrentText(node, fullText).trim();
215-
break;
216-
case SyntaxNodeType.ArrayLiteral:
217-
newString = FormatterHelper.getCurrentText(
218-
node,
219-
fullText
220-
).trim();
221-
break;
227+
// case SyntaxNodeType.ArrayLiteral:
228+
// newString = FormatterHelper.getCurrentText(
229+
// node,
230+
// fullText
231+
// ).trim();
232+
// break;
222233
default:
223234
const text = FormatterHelper.getCurrentText(
224235
node,
@@ -250,7 +261,6 @@ export class FormatterHelper {
250261
} else {
251262
newString = FormatterHelper.getCurrentText(node, fullText);
252263
}
253-
console.log("newString:\n" + newString);
254264
return newString;
255265
}
256266
}

src/v2/formatterFramework/enableFormatterDecorators.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,5 @@ export function enableFormatterDecorators(): void {
3434
EnumFormatter;
3535
ProcedureParameterFormatter;
3636
FunctionParameterFormatter;
37+
ExpressionFormatter;
3738
}

src/v2/formatters/expression/ExpressionFormatter.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,7 @@ export class ExpressionFormatter extends AFormatter implements IFormatter {
3333
node.type === SyntaxNodeType.ParenthesizedExpression ||
3434
node.type === SyntaxNodeType.AdditiveExpression ||
3535
node.type === SyntaxNodeType.MultiplicativeExpression ||
36-
node.type === SyntaxNodeType.Assignment ||
37-
node.type === SyntaxNodeType.UnaryExpression ||
38-
node.type === SyntaxNodeType.NewExpression ||
39-
node.type === SyntaxNodeType.VariableAssignment ||
40-
node.type === SyntaxNodeType.FunctionalCallArgument
36+
node.type === SyntaxNodeType.UnaryExpression
4137
) {
4238
return true;
4339
}

src/v2/formatters/for/ForFormatter.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,11 @@ export class ForFormatter extends AFormatter implements IFormatter {
104104
fullText,
105105
alignColumn
106106
);
107+
newString = FormatterHelper.alignIndentation(
108+
newString,
109+
alignColumn + 1,
110+
fullText.eolDelimiter
111+
);
107112
break;
108113
case SyntaxNodeType.EndKeyword:
109114
newString =

0 commit comments

Comments
 (0)