From 491bc1fa1f351f426b8314be01d9b91e0cc78107 Mon Sep 17 00:00:00 2001 From: gmickus Date: Fri, 13 Sep 2024 11:52:42 +0300 Subject: [PATCH 1/4] testing --- .../block/6do-do-repeat-end/input.p | 8 -- .../block/6do-do-repeat-end/target.p | 10 --- .../target.p | 3 +- src/v2/formatters/block/BlockFormatter.ts | 77 +++++++++++++++++-- 4 files changed, 73 insertions(+), 25 deletions(-) delete mode 100644 resources/functionalTests/block/6do-do-repeat-end/input.p delete mode 100644 resources/functionalTests/block/6do-do-repeat-end/target.p diff --git a/resources/functionalTests/block/6do-do-repeat-end/input.p b/resources/functionalTests/block/6do-do-repeat-end/input.p deleted file mode 100644 index cd47d9f..0000000 --- a/resources/functionalTests/block/6do-do-repeat-end/input.p +++ /dev/null @@ -1,8 +0,0 @@ -/* formatterSettingsOverride */ -/* { "AblFormatter.blockFormatting": true}*/ - -repeat while true: -do transaction: -do while true: - a = 3. - end. end. end. \ No newline at end of file diff --git a/resources/functionalTests/block/6do-do-repeat-end/target.p b/resources/functionalTests/block/6do-do-repeat-end/target.p deleted file mode 100644 index 1d1a959..0000000 --- a/resources/functionalTests/block/6do-do-repeat-end/target.p +++ /dev/null @@ -1,10 +0,0 @@ -/* formatterSettingsOverride */ -/* { "AblFormatter.blockFormatting": true}*/ - -repeat while true: - do transaction: - do while true: - a = 3. - end. - end. -end. \ No newline at end of file diff --git a/resources/functionalTests/case/11thenLocationSame-doLocationSame-statementLocationSame-blockFormattingTrue/target.p b/resources/functionalTests/case/11thenLocationSame-doLocationSame-statementLocationSame-blockFormattingTrue/target.p index 8539bca..7311183 100644 --- a/resources/functionalTests/case/11thenLocationSame-doLocationSame-statementLocationSame-blockFormattingTrue/target.p +++ b/resources/functionalTests/case/11thenLocationSame-doLocationSame-statementLocationSame-blockFormattingTrue/target.p @@ -11,7 +11,8 @@ PROCEDURE testCase: i = 2. CASE i: - WHEN 1 THEN DO: MESSAGE "Case 1". + WHEN 1 THEN DO: + MESSAGE "Case 1". END. OTHERWISE MESSAGE "No match found". END CASE. diff --git a/src/v2/formatters/block/BlockFormatter.ts b/src/v2/formatters/block/BlockFormatter.ts index 246092b..d17b31d 100644 --- a/src/v2/formatters/block/BlockFormatter.ts +++ b/src/v2/formatters/block/BlockFormatter.ts @@ -62,11 +62,18 @@ export class BlockFormater extends AFormatter implements IFormatter { this.getParentIndentationSourceNode(parent), fullText ); + console.log("parIndentation: " + parentIndentation); + console.log( + "parType: " + this.getParentIndentationSourceNode(parent).type + ); + console.log("myType: " + node.type); const indentationStep = this.settings.tabSize(); - const blockStatementsStartRows = node.children + let indexOfColon = -1; + let blockStatementsStartRows = node.children .filter((child) => { if (child.type === ":") { + indexOfColon = child.startPosition.column; return false; } return true; @@ -80,15 +87,17 @@ export class BlockFormater extends AFormatter implements IFormatter { ) ); + console.log("blockStatementRowsFound:\n" + blockStatementsStartRows); let codeLines = FormatterHelper.getCurrentText(parent, fullText).split( fullText.eolDelimiter ); // Do not do any changes for one-liner blocks - if (codeLines.length === 1) { + if (codeLines.length <= 1) { const text = FormatterHelper.getCurrentText(node, fullText); return this.getCodeEdit(node, text, text, fullText); } + const firstLine = codeLines[0]; const lastLine = codeLines[codeLines.length - 1]; const lastLineMatchesTypicalStructure = this.matchEndPattern(lastLine); @@ -96,10 +105,45 @@ export class BlockFormater extends AFormatter implements IFormatter { codeLines.pop(); } + console.log("codeLinesBefore:\n" + codeLines); + console.log("blockStatementRows:\n" + blockStatementsStartRows); + + if (indexOfColon !== -1) { + // indexOfColon += parentIndentation; + console.log("indexOfColon: " + indexOfColon); + console.log("colonLine:\n" + firstLine); + indexOfColon -= parent.startPosition.column; + const partAfterColon = firstLine + .slice(indexOfColon + 1) + .trimStart(); + // If the part after the colon is not only whitespace, put it on the next line + if (partAfterColon.trim().length !== 0) { + const firstPart = firstLine.slice(0, indexOfColon + 1); + codeLines.shift(); // pop from the start of the list + codeLines.unshift(firstPart, partAfterColon); + const firstBlockStatementRow = blockStatementsStartRows[0]; + blockStatementsStartRows.shift(); + blockStatementsStartRows.unshift( + firstBlockStatementRow - 1, + firstBlockStatementRow + ); + blockStatementsStartRows = blockStatementsStartRows.map( + (currentRow) => currentRow + 1 + ); + } + } + + console.log("codeLines:\n" + codeLines); + console.log("blockStatementRows:\n" + blockStatementsStartRows); + let n = 0; let lineChangeDelta = 0; codeLines.forEach((codeLine, index) => { const lineNumber = parent.startPosition.row + index; + console.log( + "lineNumber: " + lineNumber + " " + blockStatementsStartRows[n] + ); + console.log("line:\n" + codeLine); // adjust delta if (blockStatementsStartRows[n] === lineNumber) { @@ -114,6 +158,18 @@ export class BlockFormater extends AFormatter implements IFormatter { n++; } + console.log( + "delta: " + + parentIndentation + + indentationStep + + FormatterHelper.getActualTextIndentation( + codeLine, + fullText + ) + + " " + + lineChangeDelta + ); + if (lineChangeDelta !== 0) { indentationEdits.push({ line: index, @@ -123,6 +179,7 @@ export class BlockFormater extends AFormatter implements IFormatter { }); if (lastLineMatchesTypicalStructure) { + codeLines.push(lastLine); const parentOfEndNode = formattingOnStatement ? node.parent : parent; @@ -139,17 +196,16 @@ export class BlockFormater extends AFormatter implements IFormatter { fullText ); + console.log("endRowDelta: " + endRowDelta); + if (endRowDelta !== 0) { indentationEdits.push({ - line: - parent.endPosition.row - - parent.startPosition.row, + line: codeLines.length - 1, lineChangeDelta: endRowDelta, }); } } } - codeLines.push(lastLine); } else { const parentOfEndNode = formattingOnStatement ? node.parent @@ -229,6 +285,15 @@ export class BlockFormater extends AFormatter implements IFormatter { currentLeadingSpaces + lineChangeDelta ); + console.log( + "line: " + + lines[line] + + " " + + lineChangeDelta + + " " + + newLeadingSpaces + ); + // Update the line with the new indentation lines[line] = From eb04c92f4589ae44355b7311a13cbf16ccebc90b Mon Sep 17 00:00:00 2001 From: gmickus Date: Fri, 13 Sep 2024 11:54:45 +0300 Subject: [PATCH 2/4] tests --- resources/functionalTests/block/6-do-do-end1/input.p | 7 +++++++ resources/functionalTests/block/6-do-do-end1/target.p | 8 ++++++++ resources/functionalTests/block/6do-do-end2/input.p | 8 ++++++++ resources/functionalTests/block/6do-do-end2/target.p | 10 ++++++++++ 4 files changed, 33 insertions(+) create mode 100644 resources/functionalTests/block/6-do-do-end1/input.p create mode 100644 resources/functionalTests/block/6-do-do-end1/target.p create mode 100644 resources/functionalTests/block/6do-do-end2/input.p create mode 100644 resources/functionalTests/block/6do-do-end2/target.p diff --git a/resources/functionalTests/block/6-do-do-end1/input.p b/resources/functionalTests/block/6-do-do-end1/input.p new file mode 100644 index 0000000..2621a96 --- /dev/null +++ b/resources/functionalTests/block/6-do-do-end1/input.p @@ -0,0 +1,7 @@ +/* formatterSettingsOverride */ +/* { "AblFormatter.blockFormatting": true}*/ + +do transaction: +do transaction: + a /= 3. +end. end. \ No newline at end of file diff --git a/resources/functionalTests/block/6-do-do-end1/target.p b/resources/functionalTests/block/6-do-do-end1/target.p new file mode 100644 index 0000000..098f6a9 --- /dev/null +++ b/resources/functionalTests/block/6-do-do-end1/target.p @@ -0,0 +1,8 @@ +/* formatterSettingsOverride */ +/* { "AblFormatter.blockFormatting": true}*/ + +do transaction: + do transaction: + a /= 3. + end. +end. \ No newline at end of file diff --git a/resources/functionalTests/block/6do-do-end2/input.p b/resources/functionalTests/block/6do-do-end2/input.p new file mode 100644 index 0000000..cd47d9f --- /dev/null +++ b/resources/functionalTests/block/6do-do-end2/input.p @@ -0,0 +1,8 @@ +/* formatterSettingsOverride */ +/* { "AblFormatter.blockFormatting": true}*/ + +repeat while true: +do transaction: +do while true: + a = 3. + end. end. end. \ No newline at end of file diff --git a/resources/functionalTests/block/6do-do-end2/target.p b/resources/functionalTests/block/6do-do-end2/target.p new file mode 100644 index 0000000..1d1a959 --- /dev/null +++ b/resources/functionalTests/block/6do-do-end2/target.p @@ -0,0 +1,10 @@ +/* formatterSettingsOverride */ +/* { "AblFormatter.blockFormatting": true}*/ + +repeat while true: + do transaction: + do while true: + a = 3. + end. + end. +end. \ No newline at end of file From c317e9a59d168536066f0c740c685a86b3c6078b Mon Sep 17 00:00:00 2001 From: gmickus Date: Fri, 13 Sep 2024 11:58:47 +0300 Subject: [PATCH 3/4] Remove comments --- src/v2/formatters/block/BlockFormatter.ts | 41 ----------------------- 1 file changed, 41 deletions(-) diff --git a/src/v2/formatters/block/BlockFormatter.ts b/src/v2/formatters/block/BlockFormatter.ts index d17b31d..cd31bd0 100644 --- a/src/v2/formatters/block/BlockFormatter.ts +++ b/src/v2/formatters/block/BlockFormatter.ts @@ -62,11 +62,6 @@ export class BlockFormater extends AFormatter implements IFormatter { this.getParentIndentationSourceNode(parent), fullText ); - console.log("parIndentation: " + parentIndentation); - console.log( - "parType: " + this.getParentIndentationSourceNode(parent).type - ); - console.log("myType: " + node.type); const indentationStep = this.settings.tabSize(); let indexOfColon = -1; @@ -87,7 +82,6 @@ export class BlockFormater extends AFormatter implements IFormatter { ) ); - console.log("blockStatementRowsFound:\n" + blockStatementsStartRows); let codeLines = FormatterHelper.getCurrentText(parent, fullText).split( fullText.eolDelimiter ); @@ -105,13 +99,8 @@ export class BlockFormater extends AFormatter implements IFormatter { codeLines.pop(); } - console.log("codeLinesBefore:\n" + codeLines); - console.log("blockStatementRows:\n" + blockStatementsStartRows); - if (indexOfColon !== -1) { // indexOfColon += parentIndentation; - console.log("indexOfColon: " + indexOfColon); - console.log("colonLine:\n" + firstLine); indexOfColon -= parent.startPosition.column; const partAfterColon = firstLine .slice(indexOfColon + 1) @@ -133,17 +122,10 @@ export class BlockFormater extends AFormatter implements IFormatter { } } - console.log("codeLines:\n" + codeLines); - console.log("blockStatementRows:\n" + blockStatementsStartRows); - let n = 0; let lineChangeDelta = 0; codeLines.forEach((codeLine, index) => { const lineNumber = parent.startPosition.row + index; - console.log( - "lineNumber: " + lineNumber + " " + blockStatementsStartRows[n] - ); - console.log("line:\n" + codeLine); // adjust delta if (blockStatementsStartRows[n] === lineNumber) { @@ -158,18 +140,6 @@ export class BlockFormater extends AFormatter implements IFormatter { n++; } - console.log( - "delta: " + - parentIndentation + - indentationStep + - FormatterHelper.getActualTextIndentation( - codeLine, - fullText - ) + - " " + - lineChangeDelta - ); - if (lineChangeDelta !== 0) { indentationEdits.push({ line: index, @@ -196,8 +166,6 @@ export class BlockFormater extends AFormatter implements IFormatter { fullText ); - console.log("endRowDelta: " + endRowDelta); - if (endRowDelta !== 0) { indentationEdits.push({ line: codeLines.length - 1, @@ -285,15 +253,6 @@ export class BlockFormater extends AFormatter implements IFormatter { currentLeadingSpaces + lineChangeDelta ); - console.log( - "line: " + - lines[line] + - " " + - lineChangeDelta + - " " + - newLeadingSpaces - ); - // Update the line with the new indentation lines[line] = From 9c97e8fc10dc9e8961e6d3fb6e2af5c853c21ca6 Mon Sep 17 00:00:00 2001 From: gmickus Date: Fri, 13 Sep 2024 12:09:41 +0300 Subject: [PATCH 4/4] Improve start block formatting --- .../functionalTests/block/6do-start/input.p | 5 +++++ .../functionalTests/block/6do-start/target.p | 6 ++++++ src/v2/formatters/block/BlockFormatter.ts | 20 +++++++++++-------- 3 files changed, 23 insertions(+), 8 deletions(-) create mode 100644 resources/functionalTests/block/6do-start/input.p create mode 100644 resources/functionalTests/block/6do-start/target.p diff --git a/resources/functionalTests/block/6do-start/input.p b/resources/functionalTests/block/6do-start/input.p new file mode 100644 index 0000000..b9f5d48 --- /dev/null +++ b/resources/functionalTests/block/6do-start/input.p @@ -0,0 +1,5 @@ +/* formatterSettingsOverride */ +/* { "AblFormatter.blockFormatting": true}*/ + +do while true: a = 3. +end. \ No newline at end of file diff --git a/resources/functionalTests/block/6do-start/target.p b/resources/functionalTests/block/6do-start/target.p new file mode 100644 index 0000000..a130272 --- /dev/null +++ b/resources/functionalTests/block/6do-start/target.p @@ -0,0 +1,6 @@ +/* formatterSettingsOverride */ +/* { "AblFormatter.blockFormatting": true}*/ + +do while true: + a = 3. +end. \ No newline at end of file diff --git a/src/v2/formatters/block/BlockFormatter.ts b/src/v2/formatters/block/BlockFormatter.ts index cd31bd0..23c33eb 100644 --- a/src/v2/formatters/block/BlockFormatter.ts +++ b/src/v2/formatters/block/BlockFormatter.ts @@ -67,7 +67,7 @@ export class BlockFormater extends AFormatter implements IFormatter { let indexOfColon = -1; let blockStatementsStartRows = node.children .filter((child) => { - if (child.type === ":") { + if (child.type === SyntaxNodeType.ColonKeyword) { indexOfColon = child.startPosition.column; return false; } @@ -129,13 +129,17 @@ export class BlockFormater extends AFormatter implements IFormatter { // adjust delta if (blockStatementsStartRows[n] === lineNumber) { - lineChangeDelta = - parentIndentation + - indentationStep - - FormatterHelper.getActualTextIndentation( - codeLine, - fullText - ); + if (index === 0) { + lineChangeDelta = 0; + } else { + lineChangeDelta = + parentIndentation + + indentationStep - + FormatterHelper.getActualTextIndentation( + codeLine, + fullText + ); + } n++; }