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

151 Block Formatter incorrectly formats end #182

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
5 changes: 5 additions & 0 deletions resources/functionalTests/block/6do-end1/input.p
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/* formatterSettingsOverride */
/* { "AblFormatter.blockFormatting": true}*/

do while true:
a = 3. end.
6 changes: 6 additions & 0 deletions resources/functionalTests/block/6do-end1/target.p
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/* formatterSettingsOverride */
/* { "AblFormatter.blockFormatting": true}*/

do while true:
a = 3.
end.
7 changes: 7 additions & 0 deletions resources/functionalTests/block/6do-end2/input.p
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/* formatterSettingsOverride */
/* { "AblFormatter.blockFormatting": true}*/

do transaction:
do while true:
a = 3. end.
end.
8 changes: 8 additions & 0 deletions resources/functionalTests/block/6do-end2/target.p
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/* formatterSettingsOverride */
/* { "AblFormatter.blockFormatting": true}*/

do transaction:
do while true:
a = 3.
end.
end.
4 changes: 4 additions & 0 deletions resources/functionalTests/block/6do-one-liner/input.p
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/* formatterSettingsOverride */
/* { "AblFormatter.blockFormatting": true}*/

do while true: define variable a as integer no-undo. end.
4 changes: 4 additions & 0 deletions resources/functionalTests/block/6do-one-liner/target.p
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/* formatterSettingsOverride */
/* { "AblFormatter.blockFormatting": true}*/

do while true: define variable a as integer no-undo. end.
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/* formatterSettingsOverride */
/* { "AblFormatter.ifFormatting": true,
"AblFormatter.blockFormatting": true,
"AblFormatter.ifFormattingThenLocation": "Same",
"AblFormatter.ifFormattingStatementLocation": "New",
"AblFormatter.ifFormattingDoLocation": "New"}*/

if something <> ? and something <> 0 then
oObject:method(something).
else if a = 3 then
do:
oObject:method(something). end.
else
message "a"
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/* formatterSettingsOverride */
/* { "AblFormatter.ifFormatting": true,
"AblFormatter.blockFormatting": true,
"AblFormatter.ifFormattingThenLocation": "Same",
"AblFormatter.ifFormattingStatementLocation": "New",
"AblFormatter.ifFormattingDoLocation": "New"}*/

if something <> ? and something <> 0 then
oObject:method(something).
else if a = 3 then
do:
oObject:method(something).
end.
else
message "a"
117 changes: 85 additions & 32 deletions src/v2/formatters/block/BlockFormatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,21 @@ export class BlockFormater extends AFormatter implements IFormatter {
)
);

const codeLines = FormatterHelper.getCurrentText(parent, fullText)
.split(fullText.eolDelimiter)
.slice(0, -1);
let codeLines = FormatterHelper.getCurrentText(parent, fullText).split(
fullText.eolDelimiter
);

// Do not do any changes for one-liner blocks
if (codeLines.length === 1) {
const text = FormatterHelper.getCurrentText(node, fullText);
return this.getCodeEdit(node, text, text, fullText);
}
const lastLine = codeLines[codeLines.length - 1];

const lastLineMatchesTypicalStructure = this.matchEndPattern(lastLine);
if (lastLineMatchesTypicalStructure) {
codeLines.pop();
}

let n = 0;
let lineChangeDelta = 0;
Expand Down Expand Up @@ -110,62 +122,95 @@ export class BlockFormater extends AFormatter implements IFormatter {
}
});

const lastLine = FormatterHelper.getCurrentText(parent, fullText)
.split(fullText.eolDelimiter)
.slice(-1)[0];

const parentOfEndNode = formattingOnStatement ? node.parent : parent;
if (parentOfEndNode !== null) {
const endNode = parentOfEndNode.children.find(
(node) => node.type === SyntaxNodeType.EndKeyword
);

if (endNode !== undefined) {
const endRowDelta =
parentIndentation -
FormatterHelper.getActualTextIndentation(
lastLine,
fullText
);
if (lastLineMatchesTypicalStructure) {
const parentOfEndNode = formattingOnStatement
? node.parent
: parent;
if (parentOfEndNode !== null) {
const endNode = parentOfEndNode.children.find(
(node) => node.type === SyntaxNodeType.EndKeyword
);

if (endRowDelta !== 0) {
indentationEdits.push({
line: parent.endPosition.row - parent.startPosition.row,
lineChangeDelta: endRowDelta,
});
if (endNode !== undefined) {
const endRowDelta =
parentIndentation -
FormatterHelper.getActualTextIndentation(
lastLine,
fullText
);

if (endRowDelta !== 0) {
indentationEdits.push({
line:
parent.endPosition.row -
parent.startPosition.row,
lineChangeDelta: endRowDelta,
});
}
}
}
codeLines.push(lastLine);
} else {
const parentOfEndNode = formattingOnStatement
? node.parent
: parent;
if (parentOfEndNode !== null) {
const endNode = parentOfEndNode.children.find(
(node) => node.type === SyntaxNodeType.EndKeyword
);
if (endNode !== undefined) {
const index = endNode.startPosition.column;
const firstPart = lastLine.slice(0, index);
const secondPart = lastLine.slice(index);
codeLines[codeLines.length - 1] = firstPart;
codeLines.push(secondPart);
const endRowDelta =
parentIndentation -
FormatterHelper.getActualTextIndentation(
secondPart,
fullText
);

if (endRowDelta !== 0) {
indentationEdits.push({
line: codeLines.length - 1,
lineChangeDelta: endRowDelta,
});
}
}
}
}

return this.getCodeEditsFromIndentationEdits(
parent,
fullText,
indentationEdits
indentationEdits,
codeLines
);
}

private getCodeEditsFromIndentationEdits(
node: SyntaxNode,
fullText: FullText,
indentationEdits: IndentationEdits[]
indentationEdits: IndentationEdits[],
codeLines: string[]
): CodeEdit | CodeEdit[] | undefined {
const text = FormatterHelper.getCurrentText(node, fullText);
const newText = this.applyIndentationEdits(
text,
indentationEdits,
fullText
fullText,
codeLines
);

return this.getCodeEdit(node, text, newText, fullText);
}

private applyIndentationEdits(
code: string,
edits: IndentationEdits[],
fullText: FullText
fullText: FullText,
lines: string[]
): string {
// Split the code into lines
const lines = code.split(fullText.eolDelimiter);

// Apply each edit
edits.forEach((edit) => {
Expand Down Expand Up @@ -221,6 +266,14 @@ export class BlockFormater extends AFormatter implements IFormatter {
}
return node;
}

private matchEndPattern(str: string): boolean {
/* Returns true if string matches the pattern: (any characters that do not include a dot)end(any characters that do not include a dot).(any characters)
In essence, it returns true on the case when on a line there is nothing but an end statement.
*/
const pattern = /^[^.]*end[^.]*\..*$/i;
return pattern.test(str);
}
}

interface IndentationEdits {
Expand Down
27 changes: 0 additions & 27 deletions src/v2/formatters/body/BodyFormatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,33 +110,6 @@ export class BodyFormatter extends AFormatter implements IFormatter {
}
});

const lastLine = FormatterHelper.getCurrentText(parent, fullText)
.split(fullText.eolDelimiter)
.slice(-1)[0];

const parentOfEndNode = formattingOnStatement ? node.parent : parent;
if (parentOfEndNode !== null) {
const endNode = parentOfEndNode.children.find(
(node) => node.type === SyntaxNodeType.EndKeyword
);

if (endNode !== undefined) {
const endRowDelta =
parentIndentation -
FormatterHelper.getActualTextIndentation(
lastLine,
fullText
);

if (endRowDelta !== 0) {
indentationEdits.push({
line: parent.endPosition.row - parent.startPosition.row,
lineChangeDelta: endRowDelta,
});
}
}
}

return this.getCodeEditsFromIndentationEdits(
node,
fullText,
Expand Down