Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

252 Formatting broken if statement is written over multiple lines #263

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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