Skip to content

Commit

Permalink
Merge branch 'develop' of https://github.com/BalticAmadeus/AblFormatter
Browse files Browse the repository at this point in the history
… into develop
  • Loading branch information
ekazbaraite committed Sep 13, 2024
2 parents e15586b + 498b477 commit b57c7f3
Show file tree
Hide file tree
Showing 14 changed files with 193 additions and 59 deletions.
8 changes: 8 additions & 0 deletions resources/functionalTests/block/6do-do-repeat-end/input.p
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/* formatterSettingsOverride */
/* { "AblFormatter.blockFormatting": true}*/

repeat while true:
do transaction:
do while true:
a = 3.
end. end. end.
10 changes: 10 additions & 0 deletions resources/functionalTests/block/6do-do-repeat-end/target.p
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/* formatterSettingsOverride */
/* { "AblFormatter.blockFormatting": true}*/

repeat while true:
do transaction:
do while true:
a = 3.
end.
end.
end.
5 changes: 5 additions & 0 deletions resources/functionalTests/block/6do-end1/input.p
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/* formatterSettingsOverride */
/* { "AblFormatter.blockFormatting": true}*/

do while true:
a = 3. end.
6 changes: 6 additions & 0 deletions resources/functionalTests/block/6do-end1/target.p
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/* formatterSettingsOverride */
/* { "AblFormatter.blockFormatting": true}*/

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

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

do transaction:
do while true:
a = 3.
end.
end.
4 changes: 4 additions & 0 deletions resources/functionalTests/block/6do-one-liner/input.p
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/* formatterSettingsOverride */
/* { "AblFormatter.blockFormatting": true}*/

do while true: define variable a as integer no-undo. end.
4 changes: 4 additions & 0 deletions resources/functionalTests/block/6do-one-liner/target.p
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/* formatterSettingsOverride */
/* { "AblFormatter.blockFormatting": true}*/

do while true: define variable a as integer no-undo. end.
10 changes: 10 additions & 0 deletions resources/functionalTests/for/7each-nested-ends/input.p
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/* formatterSettingsOverride */
/* { "AblFormatter.forFormatting": true}*/

FOR EACH ttCustomer:
FOR EACH ttOrder WHERE ttOrder.CustomerID = ttCustomer.CustomerID:
FOR EACH ttOrderLine WHERE ttOrderLine.OrderID = ttOrder.OrderID:
FOR EACH ttProduct WHERE ttProduct.ProductID = ttOrderLine.ProductID:
FOR EACH ttSupplier WHERE ttSupplier.SupplierID = ttProduct.SupplierID:
END. END.
END. END. END.
17 changes: 17 additions & 0 deletions resources/functionalTests/for/7each-nested-ends/target.p
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/* formatterSettingsOverride */
/* { "AblFormatter.forFormatting": true}*/

FOR EACH ttCustomer:
FOR EACH ttOrder WHERE
ttOrder.CustomerID = ttCustomer.CustomerID:
FOR EACH ttOrderLine WHERE
ttOrderLine.OrderID = ttOrder.OrderID:
FOR EACH ttProduct WHERE
ttProduct.ProductID = ttOrderLine.ProductID:
FOR EACH ttSupplier WHERE
ttSupplier.SupplierID = ttProduct.SupplierID:
END.
END.
END.
END.
END.
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/* formatterSettingsOverride */
/* { "AblFormatter.ifFormatting": true,
"AblFormatter.blockFormatting": true,
"AblFormatter.ifFormattingThenLocation": "Same",
"AblFormatter.ifFormattingStatementLocation": "New",
"AblFormatter.ifFormattingDoLocation": "New"}*/

if something <> ? and something <> 0 then
oObject:method(something).
else if a = 3 then
do:
oObject:method(something). end.
else
message "a"
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/* formatterSettingsOverride */
/* { "AblFormatter.ifFormatting": true,
"AblFormatter.blockFormatting": true,
"AblFormatter.ifFormattingThenLocation": "Same",
"AblFormatter.ifFormattingStatementLocation": "New",
"AblFormatter.ifFormattingDoLocation": "New"}*/

if something <> ? and something <> 0 then
oObject:method(something).
else if a = 3 then
do:
oObject:method(something).
end.
else
message "a"
117 changes: 85 additions & 32 deletions src/v2/formatters/block/BlockFormatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,21 @@ export class BlockFormater extends AFormatter implements IFormatter {
)
);

const codeLines = FormatterHelper.getCurrentText(parent, fullText)
.split(fullText.eolDelimiter)
.slice(0, -1);
let codeLines = FormatterHelper.getCurrentText(parent, fullText).split(
fullText.eolDelimiter
);

// Do not do any changes for one-liner blocks
if (codeLines.length === 1) {
const text = FormatterHelper.getCurrentText(node, fullText);
return this.getCodeEdit(node, text, text, fullText);
}
const lastLine = codeLines[codeLines.length - 1];

const lastLineMatchesTypicalStructure = this.matchEndPattern(lastLine);
if (lastLineMatchesTypicalStructure) {
codeLines.pop();
}

let n = 0;
let lineChangeDelta = 0;
Expand Down Expand Up @@ -110,62 +122,95 @@ export class BlockFormater extends AFormatter implements IFormatter {
}
});

const lastLine = FormatterHelper.getCurrentText(parent, fullText)
.split(fullText.eolDelimiter)
.slice(-1)[0];

const parentOfEndNode = formattingOnStatement ? node.parent : parent;
if (parentOfEndNode !== null) {
const endNode = parentOfEndNode.children.find(
(node) => node.type === SyntaxNodeType.EndKeyword
);

if (endNode !== undefined) {
const endRowDelta =
parentIndentation -
FormatterHelper.getActualTextIndentation(
lastLine,
fullText
);
if (lastLineMatchesTypicalStructure) {
const parentOfEndNode = formattingOnStatement
? node.parent
: parent;
if (parentOfEndNode !== null) {
const endNode = parentOfEndNode.children.find(
(node) => node.type === SyntaxNodeType.EndKeyword
);

if (endRowDelta !== 0) {
indentationEdits.push({
line: parent.endPosition.row - parent.startPosition.row,
lineChangeDelta: endRowDelta,
});
if (endNode !== undefined) {
const endRowDelta =
parentIndentation -
FormatterHelper.getActualTextIndentation(
lastLine,
fullText
);

if (endRowDelta !== 0) {
indentationEdits.push({
line:
parent.endPosition.row -
parent.startPosition.row,
lineChangeDelta: endRowDelta,
});
}
}
}
codeLines.push(lastLine);
} else {
const parentOfEndNode = formattingOnStatement
? node.parent
: parent;
if (parentOfEndNode !== null) {
const endNode = parentOfEndNode.children.find(
(node) => node.type === SyntaxNodeType.EndKeyword
);
if (endNode !== undefined) {
const index = endNode.startPosition.column;
const firstPart = lastLine.slice(0, index);
const secondPart = lastLine.slice(index).trimEnd();
codeLines[codeLines.length - 1] = firstPart;
codeLines.push(secondPart);
const endRowDelta =
parentIndentation -
FormatterHelper.getActualTextIndentation(
secondPart,
fullText
);

if (endRowDelta !== 0) {
indentationEdits.push({
line: codeLines.length - 1,
lineChangeDelta: endRowDelta,
});
}
}
}
}

return this.getCodeEditsFromIndentationEdits(
parent,
fullText,
indentationEdits
indentationEdits,
codeLines
);
}

private getCodeEditsFromIndentationEdits(
node: SyntaxNode,
fullText: FullText,
indentationEdits: IndentationEdits[]
indentationEdits: IndentationEdits[],
codeLines: string[]
): CodeEdit | CodeEdit[] | undefined {
const text = FormatterHelper.getCurrentText(node, fullText);
const newText = this.applyIndentationEdits(
text,
indentationEdits,
fullText
fullText,
codeLines
);

return this.getCodeEdit(node, text, newText, fullText);
}

private applyIndentationEdits(
code: string,
edits: IndentationEdits[],
fullText: FullText
fullText: FullText,
lines: string[]
): string {
// Split the code into lines
const lines = code.split(fullText.eolDelimiter);

// Apply each edit
edits.forEach((edit) => {
Expand Down Expand Up @@ -221,6 +266,14 @@ export class BlockFormater extends AFormatter implements IFormatter {
}
return node;
}

private matchEndPattern(str: string): boolean {
/* Returns true if string matches the pattern: (any characters that do not include a dot)end(any characters that do not include a dot).(any characters)
In essence, it returns true on the case when on a line there is nothing but an end statement.
*/
const pattern = /^[^.]*end[^.]*\.[^.]*$/i;
return pattern.test(str);
}
}

interface IndentationEdits {
Expand Down
27 changes: 0 additions & 27 deletions src/v2/formatters/body/BodyFormatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,33 +110,6 @@ export class BodyFormatter extends AFormatter implements IFormatter {
}
});

const lastLine = FormatterHelper.getCurrentText(parent, fullText)
.split(fullText.eolDelimiter)
.slice(-1)[0];

const parentOfEndNode = formattingOnStatement ? node.parent : parent;
if (parentOfEndNode !== null) {
const endNode = parentOfEndNode.children.find(
(node) => node.type === SyntaxNodeType.EndKeyword
);

if (endNode !== undefined) {
const endRowDelta =
parentIndentation -
FormatterHelper.getActualTextIndentation(
lastLine,
fullText
);

if (endRowDelta !== 0) {
indentationEdits.push({
line: parent.endPosition.row - parent.startPosition.row,
lineChangeDelta: endRowDelta,
});
}
}
}

return this.getCodeEditsFromIndentationEdits(
node,
fullText,
Expand Down

0 comments on commit b57c7f3

Please sign in to comment.