diff --git a/resources/functionalTests/block/56function-block-multiple-line/input.p b/resources/functionalTests/block/56function-block-multiple-line/input.p new file mode 100644 index 0000000..db0a0a7 --- /dev/null +++ b/resources/functionalTests/block/56function-block-multiple-line/input.p @@ -0,0 +1,7 @@ +/* formatterSettingsOverride */ +/* { "AblFormatter.blockFormatting": true}*/ + +function GetDLC returns character +(): + define variable cValue as character no-undo. +end function. \ No newline at end of file diff --git a/resources/functionalTests/block/56function-block-multiple-line/target.p b/resources/functionalTests/block/56function-block-multiple-line/target.p new file mode 100644 index 0000000..6668889 --- /dev/null +++ b/resources/functionalTests/block/56function-block-multiple-line/target.p @@ -0,0 +1,7 @@ +/* formatterSettingsOverride */ +/* { "AblFormatter.blockFormatting": true}*/ + +function GetDLC returns character +(): + define variable cValue as character no-undo. +end function. \ No newline at end of file diff --git a/resources/functionalTests/block/57class-block-multiple-line/input.p b/resources/functionalTests/block/57class-block-multiple-line/input.p new file mode 100644 index 0000000..96c973f --- /dev/null +++ b/resources/functionalTests/block/57class-block-multiple-line/input.p @@ -0,0 +1,14 @@ +/* formatterSettingsOverride */ +/* { "AblFormatter.blockFormatting": true}*/ + +CLASS Validation.Something + INHERITS BaseStuff + IMPLEMENTS IThing + {&SERIALIZABLE}: + + METHOD PUBLIC OVERRIDE IType GetType(): + DEFINE VARIABLE oType AS IType NO-UNDO . + RETURN oType . + END METHOD. + +END CLASS. diff --git a/resources/functionalTests/block/57class-block-multiple-line/target.p b/resources/functionalTests/block/57class-block-multiple-line/target.p new file mode 100644 index 0000000..614aedf --- /dev/null +++ b/resources/functionalTests/block/57class-block-multiple-line/target.p @@ -0,0 +1,14 @@ +/* formatterSettingsOverride */ +/* { "AblFormatter.blockFormatting": true}*/ + +CLASS Validation.Something + INHERITS BaseStuff + IMPLEMENTS IThing + {&SERIALIZABLE}: + + METHOD PUBLIC OVERRIDE IType GetType(): + define VARIABLE oType AS IType NO-UNDO. + RETURN oType . + END METHOD. + +END CLASS. diff --git a/src/v2/formatters/block/BlockFormatter.ts b/src/v2/formatters/block/BlockFormatter.ts index 6d11091..a32c422 100644 --- a/src/v2/formatters/block/BlockFormatter.ts +++ b/src/v2/formatters/block/BlockFormatter.ts @@ -65,10 +65,13 @@ export class BlockFormater extends AFormatter implements IFormatter { const indentationStep = this.settings.tabSize(); let indexOfColon = -1; + let deltaBetweenStartAndColon = 0; let blockStatementsStartRows = node.children .filter((child) => { if (child.type === SyntaxNodeType.ColonKeyword) { indexOfColon = child.startPosition.column; + deltaBetweenStartAndColon = + child.startPosition.row - parent.startPosition.row; return false; } return true; @@ -82,9 +85,19 @@ export class BlockFormater extends AFormatter implements IFormatter { ) ); + let linesBeforeColumn = FormatterHelper.getCurrentText(parent, fullText) + .split(fullText.eolDelimiter) + .slice(0, deltaBetweenStartAndColon); + const onlyWhiteSpacesBeforeColumnLine = linesBeforeColumn.every( + (line) => line.trim() === "" + ); + let codeLines = FormatterHelper.getCurrentText(parent, fullText).split( fullText.eolDelimiter ); + if (!onlyWhiteSpacesBeforeColumnLine) { + codeLines = codeLines.slice(deltaBetweenStartAndColon); + } // Do not do any changes for one-liner blocks if (codeLines.length <= 1) { @@ -99,7 +112,7 @@ export class BlockFormater extends AFormatter implements IFormatter { codeLines.pop(); } - if (indexOfColon !== -1) { + if (indexOfColon !== -1 && deltaBetweenStartAndColon === 0) { // indexOfColon += parentIndentation; indexOfColon -= parent.startPosition.column; for (let i = indexOfColon + 1; i >= indexOfColon - 1; i--) { @@ -129,6 +142,11 @@ export class BlockFormater extends AFormatter implements IFormatter { } } + // Add back the first lines before column of the block statement + if (!onlyWhiteSpacesBeforeColumnLine) { + codeLines = linesBeforeColumn.concat(codeLines); + } + let n = 0; let lineChangeDelta = 0; codeLines.forEach((codeLine, index) => {