Skip to content

Commit

Permalink
Merge pull request #197 from BalticAmadeus/183-block-formatter-works-…
Browse files Browse the repository at this point in the history
…incorrectly-if-there-is-code-on-the-same-line-as-the-start-of-the-block

183 block formatter works incorrectly if there is code on the same line as the start of the block
  • Loading branch information
PauliusKu authored Sep 13, 2024
2 parents fb36d1e + 9c97e8f commit 73a3c93
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 15 deletions.
7 changes: 7 additions & 0 deletions resources/functionalTests/block/6-do-do-end1/input.p
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/* formatterSettingsOverride */
/* { "AblFormatter.blockFormatting": true}*/

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

do transaction:
do transaction:
a /= 3.
end.
end.
5 changes: 5 additions & 0 deletions resources/functionalTests/block/6do-start/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-start/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.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ PROCEDURE testCase:
i = 2.

CASE i:
WHEN 1 THEN DO: MESSAGE "Case 1".
WHEN 1 THEN DO:
MESSAGE "Case 1".
END.
OTHERWISE MESSAGE "No match found".
END CASE.
Expand Down
56 changes: 42 additions & 14 deletions src/v2/formatters/block/BlockFormatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,11 @@ export class BlockFormater extends AFormatter implements IFormatter {
);

const indentationStep = this.settings.tabSize();
const blockStatementsStartRows = node.children
let indexOfColon = -1;
let blockStatementsStartRows = node.children
.filter((child) => {
if (child.type === ":") {
if (child.type === SyntaxNodeType.ColonKeyword) {
indexOfColon = child.startPosition.column;
return false;
}
return true;
Expand All @@ -85,31 +87,59 @@ export class BlockFormater extends AFormatter implements IFormatter {
);

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

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

if (indexOfColon !== -1) {
// indexOfColon += parentIndentation;
indexOfColon -= parent.startPosition.column;
const partAfterColon = firstLine
.slice(indexOfColon + 1)
.trimStart();
// If the part after the colon is not only whitespace, put it on the next line
if (partAfterColon.trim().length !== 0) {
const firstPart = firstLine.slice(0, indexOfColon + 1);
codeLines.shift(); // pop from the start of the list
codeLines.unshift(firstPart, partAfterColon);
const firstBlockStatementRow = blockStatementsStartRows[0];
blockStatementsStartRows.shift();
blockStatementsStartRows.unshift(
firstBlockStatementRow - 1,
firstBlockStatementRow
);
blockStatementsStartRows = blockStatementsStartRows.map(
(currentRow) => currentRow + 1
);
}
}

let n = 0;
let lineChangeDelta = 0;
codeLines.forEach((codeLine, index) => {
const lineNumber = parent.startPosition.row + index;

// adjust delta
if (blockStatementsStartRows[n] === lineNumber) {
lineChangeDelta =
parentIndentation +
indentationStep -
FormatterHelper.getActualTextIndentation(
codeLine,
fullText
);
if (index === 0) {
lineChangeDelta = 0;
} else {
lineChangeDelta =
parentIndentation +
indentationStep -
FormatterHelper.getActualTextIndentation(
codeLine,
fullText
);
}

n++;
}
Expand All @@ -123,6 +153,7 @@ export class BlockFormater extends AFormatter implements IFormatter {
});

if (lastLineMatchesTypicalStructure) {
codeLines.push(lastLine);
const parentOfEndNode = formattingOnStatement
? node.parent
: parent;
Expand All @@ -141,15 +172,12 @@ export class BlockFormater extends AFormatter implements IFormatter {

if (endRowDelta !== 0) {
indentationEdits.push({
line:
parent.endPosition.row -
parent.startPosition.row,
line: codeLines.length - 1,
lineChangeDelta: endRowDelta,
});
}
}
}
codeLines.push(lastLine);
} else {
const parentOfEndNode = formattingOnStatement
? node.parent
Expand Down

0 comments on commit 73a3c93

Please sign in to comment.