Skip to content

Commit 757bcaf

Browse files
author
gmickus
committed
Fix case when block statement is multi-line
1 parent f1f9cc1 commit 757bcaf

File tree

5 files changed

+61
-1
lines changed

5 files changed

+61
-1
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
/* formatterSettingsOverride */
2+
/* { "AblFormatter.blockFormatting": true}*/
3+
4+
function GetDLC returns character
5+
():
6+
define variable cValue as character no-undo.
7+
end function.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
/* formatterSettingsOverride */
2+
/* { "AblFormatter.blockFormatting": true}*/
3+
4+
function GetDLC returns character
5+
():
6+
define variable cValue as character no-undo.
7+
end function.
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/* formatterSettingsOverride */
2+
/* { "AblFormatter.blockFormatting": true}*/
3+
4+
CLASS Validation.Something
5+
INHERITS BaseStuff
6+
IMPLEMENTS IThing
7+
{&SERIALIZABLE}:
8+
9+
METHOD PUBLIC OVERRIDE IType GetType():
10+
DEFINE VARIABLE oType AS IType NO-UNDO .
11+
RETURN oType .
12+
END METHOD.
13+
14+
END CLASS.
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/* formatterSettingsOverride */
2+
/* { "AblFormatter.blockFormatting": true}*/
3+
4+
CLASS Validation.Something
5+
INHERITS BaseStuff
6+
IMPLEMENTS IThing
7+
{&SERIALIZABLE}:
8+
9+
METHOD PUBLIC OVERRIDE IType GetType():
10+
define VARIABLE oType AS IType NO-UNDO.
11+
RETURN oType .
12+
END METHOD.
13+
14+
END CLASS.

src/v2/formatters/block/BlockFormatter.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,13 @@ export class BlockFormater extends AFormatter implements IFormatter {
6565

6666
const indentationStep = this.settings.tabSize();
6767
let indexOfColon = -1;
68+
let deltaBetweenStartAndColon = 0;
6869
let blockStatementsStartRows = node.children
6970
.filter((child) => {
7071
if (child.type === SyntaxNodeType.ColonKeyword) {
7172
indexOfColon = child.startPosition.column;
73+
deltaBetweenStartAndColon =
74+
child.startPosition.row - parent.startPosition.row;
7275
return false;
7376
}
7477
return true;
@@ -82,9 +85,19 @@ export class BlockFormater extends AFormatter implements IFormatter {
8285
)
8386
);
8487

88+
let linesBeforeColumn = FormatterHelper.getCurrentText(parent, fullText)
89+
.split(fullText.eolDelimiter)
90+
.slice(0, deltaBetweenStartAndColon);
91+
const onlyWhiteSpacesBeforeColumnLine = linesBeforeColumn.every(
92+
(line) => line.trim() === ""
93+
);
94+
8595
let codeLines = FormatterHelper.getCurrentText(parent, fullText).split(
8696
fullText.eolDelimiter
8797
);
98+
if (!onlyWhiteSpacesBeforeColumnLine) {
99+
codeLines = codeLines.slice(deltaBetweenStartAndColon);
100+
}
88101

89102
// Do not do any changes for one-liner blocks
90103
if (codeLines.length <= 1) {
@@ -99,7 +112,7 @@ export class BlockFormater extends AFormatter implements IFormatter {
99112
codeLines.pop();
100113
}
101114

102-
if (indexOfColon !== -1) {
115+
if (indexOfColon !== -1 && deltaBetweenStartAndColon === 0) {
103116
// indexOfColon += parentIndentation;
104117
indexOfColon -= parent.startPosition.column;
105118
for (let i = indexOfColon + 1; i >= indexOfColon - 1; i--) {
@@ -129,6 +142,11 @@ export class BlockFormater extends AFormatter implements IFormatter {
129142
}
130143
}
131144

145+
// Add back the first lines before column of the block statement
146+
if (!onlyWhiteSpacesBeforeColumnLine) {
147+
codeLines = linesBeforeColumn.concat(codeLines);
148+
}
149+
132150
let n = 0;
133151
let lineChangeDelta = 0;
134152
codeLines.forEach((codeLine, index) => {

0 commit comments

Comments
 (0)