diff --git a/resources/functionalTests/block/6-do-do-end1/input.p b/resources/functionalTests/block/6-do-do-end1/input.p new file mode 100644 index 0000000..2621a96 --- /dev/null +++ b/resources/functionalTests/block/6-do-do-end1/input.p @@ -0,0 +1,7 @@ +/* formatterSettingsOverride */ +/* { "AblFormatter.blockFormatting": true}*/ + +do transaction: +do transaction: + a /= 3. +end. end. \ No newline at end of file diff --git a/resources/functionalTests/block/6-do-do-end1/target.p b/resources/functionalTests/block/6-do-do-end1/target.p new file mode 100644 index 0000000..098f6a9 --- /dev/null +++ b/resources/functionalTests/block/6-do-do-end1/target.p @@ -0,0 +1,8 @@ +/* formatterSettingsOverride */ +/* { "AblFormatter.blockFormatting": true}*/ + +do transaction: + do transaction: + a /= 3. + end. +end. \ No newline at end of file diff --git a/resources/functionalTests/block/6do-do-repeat-end/input.p b/resources/functionalTests/block/6do-do-end2/input.p similarity index 100% rename from resources/functionalTests/block/6do-do-repeat-end/input.p rename to resources/functionalTests/block/6do-do-end2/input.p diff --git a/resources/functionalTests/block/6do-do-repeat-end/target.p b/resources/functionalTests/block/6do-do-end2/target.p similarity index 100% rename from resources/functionalTests/block/6do-do-repeat-end/target.p rename to resources/functionalTests/block/6do-do-end2/target.p diff --git a/resources/functionalTests/block/6do-start/input.p b/resources/functionalTests/block/6do-start/input.p new file mode 100644 index 0000000..b9f5d48 --- /dev/null +++ b/resources/functionalTests/block/6do-start/input.p @@ -0,0 +1,5 @@ +/* formatterSettingsOverride */ +/* { "AblFormatter.blockFormatting": true}*/ + +do while true: a = 3. +end. \ No newline at end of file diff --git a/resources/functionalTests/block/6do-start/target.p b/resources/functionalTests/block/6do-start/target.p new file mode 100644 index 0000000..a130272 --- /dev/null +++ b/resources/functionalTests/block/6do-start/target.p @@ -0,0 +1,6 @@ +/* formatterSettingsOverride */ +/* { "AblFormatter.blockFormatting": true}*/ + +do while true: + a = 3. +end. \ No newline at end of file diff --git a/resources/functionalTests/case/11thenLocationSame-doLocationSame-statementLocationSame-blockFormattingTrue/target.p b/resources/functionalTests/case/11thenLocationSame-doLocationSame-statementLocationSame-blockFormattingTrue/target.p index 8539bca..7311183 100644 --- a/resources/functionalTests/case/11thenLocationSame-doLocationSame-statementLocationSame-blockFormattingTrue/target.p +++ b/resources/functionalTests/case/11thenLocationSame-doLocationSame-statementLocationSame-blockFormattingTrue/target.p @@ -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. diff --git a/src/v2/formatters/block/BlockFormatter.ts b/src/v2/formatters/block/BlockFormatter.ts index 246092b..23c33eb 100644 --- a/src/v2/formatters/block/BlockFormatter.ts +++ b/src/v2/formatters/block/BlockFormatter.ts @@ -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; @@ -85,10 +87,11 @@ 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); @@ -96,6 +99,29 @@ export class BlockFormater extends AFormatter implements IFormatter { 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) => { @@ -103,13 +129,17 @@ export class BlockFormater extends AFormatter implements IFormatter { // 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++; } @@ -123,6 +153,7 @@ export class BlockFormater extends AFormatter implements IFormatter { }); if (lastLineMatchesTypicalStructure) { + codeLines.push(lastLine); const parentOfEndNode = formattingOnStatement ? node.parent : parent; @@ -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