Skip to content

Commit

Permalink
Merge pull request #263 from BalticAmadeus/252-formatting-broken-if-f…
Browse files Browse the repository at this point in the history
…unction-statement-is-written-over-multiple-lines

252 Formatting broken if  statement is written over multiple lines
  • Loading branch information
PauliusKu authored Nov 13, 2024
2 parents f1f9cc1 + 757bcaf commit bbf417c
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/* formatterSettingsOverride */
/* { "AblFormatter.blockFormatting": true}*/

function GetDLC returns character
():
define variable cValue as character no-undo.
end function.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/* formatterSettingsOverride */
/* { "AblFormatter.blockFormatting": true}*/

function GetDLC returns character
():
define variable cValue as character no-undo.
end function.
Original file line number Diff line number Diff line change
@@ -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.
Original file line number Diff line number Diff line change
@@ -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.
20 changes: 19 additions & 1 deletion src/v2/formatters/block/BlockFormatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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) {
Expand All @@ -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--) {
Expand Down Expand Up @@ -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) => {
Expand Down

0 comments on commit bbf417c

Please sign in to comment.