From ff66089ac3bf1a1004c5c96d27378c4d3a9cf1ce Mon Sep 17 00:00:00 2001 From: PauliusKu Date: Wed, 14 Aug 2024 23:03:37 +0300 Subject: [PATCH 01/13] initial tests --- resources/functionalTests/for1-each-false/input.p | 10 ++++++++++ resources/functionalTests/for1-each-false/target.p | 10 ++++++++++ resources/functionalTests/for2-each-true/input.p | 10 ++++++++++ resources/functionalTests/for2-each-true/target.p | 8 ++++++++ resources/functionalTests/for3-each-where/input.p | 8 ++++++++ resources/functionalTests/for3-each-where/target.p | 9 +++++++++ resources/functionalTests/for4-each-where3/input.p | 8 ++++++++ resources/functionalTests/for4-each-where3/target.p | 9 +++++++++ 8 files changed, 72 insertions(+) create mode 100644 resources/functionalTests/for1-each-false/input.p create mode 100644 resources/functionalTests/for1-each-false/target.p create mode 100644 resources/functionalTests/for2-each-true/input.p create mode 100644 resources/functionalTests/for2-each-true/target.p create mode 100644 resources/functionalTests/for3-each-where/input.p create mode 100644 resources/functionalTests/for3-each-where/target.p create mode 100644 resources/functionalTests/for4-each-where3/input.p create mode 100644 resources/functionalTests/for4-each-where3/target.p diff --git a/resources/functionalTests/for1-each-false/input.p b/resources/functionalTests/for1-each-false/input.p new file mode 100644 index 0000000..d675f87 --- /dev/null +++ b/resources/functionalTests/for1-each-false/input.p @@ -0,0 +1,10 @@ +/* formatterSettingsOverride */ +/* { "AblFormatter.blockFormatting": true, +"AblFormatter.forFormatting": false +}*/ + +for +each +Customer: + Customer.var += 1. +end. \ No newline at end of file diff --git a/resources/functionalTests/for1-each-false/target.p b/resources/functionalTests/for1-each-false/target.p new file mode 100644 index 0000000..d675f87 --- /dev/null +++ b/resources/functionalTests/for1-each-false/target.p @@ -0,0 +1,10 @@ +/* formatterSettingsOverride */ +/* { "AblFormatter.blockFormatting": true, +"AblFormatter.forFormatting": false +}*/ + +for +each +Customer: + Customer.var += 1. +end. \ No newline at end of file diff --git a/resources/functionalTests/for2-each-true/input.p b/resources/functionalTests/for2-each-true/input.p new file mode 100644 index 0000000..ef42047 --- /dev/null +++ b/resources/functionalTests/for2-each-true/input.p @@ -0,0 +1,10 @@ +/* formatterSettingsOverride */ +/* { "AblFormatter.blockFormatting": true, +"AblFormatter.forFormatting": true +}*/ + +for +each +Customer: + Customer.var += 1. +end. \ No newline at end of file diff --git a/resources/functionalTests/for2-each-true/target.p b/resources/functionalTests/for2-each-true/target.p new file mode 100644 index 0000000..dbbe313 --- /dev/null +++ b/resources/functionalTests/for2-each-true/target.p @@ -0,0 +1,8 @@ +/* formatterSettingsOverride */ +/* { "AblFormatter.blockFormatting": true, +"AblFormatter.forFormatting": true +}*/ + +for each Customer: + Customer.var += 1. +end. \ No newline at end of file diff --git a/resources/functionalTests/for3-each-where/input.p b/resources/functionalTests/for3-each-where/input.p new file mode 100644 index 0000000..bfed1d6 --- /dev/null +++ b/resources/functionalTests/for3-each-where/input.p @@ -0,0 +1,8 @@ +/* formatterSettingsOverride */ +/* { "AblFormatter.blockFormatting": true, +"AblFormatter.forFormatting": true +}*/ + +for each Customer where Customer.var = 1: + Customer.var += 1. +end. \ No newline at end of file diff --git a/resources/functionalTests/for3-each-where/target.p b/resources/functionalTests/for3-each-where/target.p new file mode 100644 index 0000000..a376627 --- /dev/null +++ b/resources/functionalTests/for3-each-where/target.p @@ -0,0 +1,9 @@ +/* formatterSettingsOverride */ +/* { "AblFormatter.blockFormatting": true, +"AblFormatter.forFormatting": true +}*/ + +for each Customer where + Customer.var = 1: + Customer.var += 1. +end. \ No newline at end of file diff --git a/resources/functionalTests/for4-each-where3/input.p b/resources/functionalTests/for4-each-where3/input.p new file mode 100644 index 0000000..38eba14 --- /dev/null +++ b/resources/functionalTests/for4-each-where3/input.p @@ -0,0 +1,8 @@ +/* formatterSettingsOverride */ +/* { "AblFormatter.blockFormatting": true, +"AblFormatter.forFormatting": true +}*/ + +for each Customer where Customer.var = 1 or Customer.var = 2 or Customer.var = 3: + Customer.var += 1. +end. \ No newline at end of file diff --git a/resources/functionalTests/for4-each-where3/target.p b/resources/functionalTests/for4-each-where3/target.p new file mode 100644 index 0000000..154260b --- /dev/null +++ b/resources/functionalTests/for4-each-where3/target.p @@ -0,0 +1,9 @@ +/* formatterSettingsOverride */ +/* { "AblFormatter.blockFormatting": true, +"AblFormatter.forFormatting": true +}*/ + +for each Customer where + Customer.var = 1 or Customer.var = 2 or Customer.var = 3: + Customer.var += 1. +end. \ No newline at end of file From e43c32a6d0c666123c89d5e768cbc66b402dc7da Mon Sep 17 00:00:00 2001 From: sjonynaite Date: Mon, 26 Aug 2024 16:46:55 +0300 Subject: [PATCH 02/13] Not working new For Formatter --- .../{ => for}/for1-each-false/input.p | 0 .../{ => for}/for1-each-false/target.p | 0 .../{ => for}/for2-each-true/input.p | 0 .../{ => for}/for2-each-true/target.p | 0 .../{ => for}/for3-each-where/input.p | 0 .../{ => for}/for3-each-where/target.p | 0 .../{ => for}/for4-each-where3/input.p | 0 .../{ => for}/for4-each-where3/target.p | 0 src/model/SyntaxNodeType.ts | 2 + .../enableFormatterDecorators.ts | 2 + src/v2/formatters/for/ForFormatter.ts | 157 ++++++++++++++++++ src/v2/formatters/for/ForSettings.ts | 8 + 12 files changed, 169 insertions(+) rename resources/functionalTests/{ => for}/for1-each-false/input.p (100%) rename resources/functionalTests/{ => for}/for1-each-false/target.p (100%) rename resources/functionalTests/{ => for}/for2-each-true/input.p (100%) rename resources/functionalTests/{ => for}/for2-each-true/target.p (100%) rename resources/functionalTests/{ => for}/for3-each-where/input.p (100%) rename resources/functionalTests/{ => for}/for3-each-where/target.p (100%) rename resources/functionalTests/{ => for}/for4-each-where3/input.p (100%) rename resources/functionalTests/{ => for}/for4-each-where3/target.p (100%) create mode 100644 src/v2/formatters/for/ForFormatter.ts create mode 100644 src/v2/formatters/for/ForSettings.ts diff --git a/resources/functionalTests/for1-each-false/input.p b/resources/functionalTests/for/for1-each-false/input.p similarity index 100% rename from resources/functionalTests/for1-each-false/input.p rename to resources/functionalTests/for/for1-each-false/input.p diff --git a/resources/functionalTests/for1-each-false/target.p b/resources/functionalTests/for/for1-each-false/target.p similarity index 100% rename from resources/functionalTests/for1-each-false/target.p rename to resources/functionalTests/for/for1-each-false/target.p diff --git a/resources/functionalTests/for2-each-true/input.p b/resources/functionalTests/for/for2-each-true/input.p similarity index 100% rename from resources/functionalTests/for2-each-true/input.p rename to resources/functionalTests/for/for2-each-true/input.p diff --git a/resources/functionalTests/for2-each-true/target.p b/resources/functionalTests/for/for2-each-true/target.p similarity index 100% rename from resources/functionalTests/for2-each-true/target.p rename to resources/functionalTests/for/for2-each-true/target.p diff --git a/resources/functionalTests/for3-each-where/input.p b/resources/functionalTests/for/for3-each-where/input.p similarity index 100% rename from resources/functionalTests/for3-each-where/input.p rename to resources/functionalTests/for/for3-each-where/input.p diff --git a/resources/functionalTests/for3-each-where/target.p b/resources/functionalTests/for/for3-each-where/target.p similarity index 100% rename from resources/functionalTests/for3-each-where/target.p rename to resources/functionalTests/for/for3-each-where/target.p diff --git a/resources/functionalTests/for4-each-where3/input.p b/resources/functionalTests/for/for4-each-where3/input.p similarity index 100% rename from resources/functionalTests/for4-each-where3/input.p rename to resources/functionalTests/for/for4-each-where3/input.p diff --git a/resources/functionalTests/for4-each-where3/target.p b/resources/functionalTests/for/for4-each-where3/target.p similarity index 100% rename from resources/functionalTests/for4-each-where3/target.p rename to resources/functionalTests/for/for4-each-where3/target.p diff --git a/src/model/SyntaxNodeType.ts b/src/model/SyntaxNodeType.ts index 26f88e2..35461cc 100644 --- a/src/model/SyntaxNodeType.ts +++ b/src/model/SyntaxNodeType.ts @@ -19,6 +19,7 @@ export enum SyntaxNodeType { WhereClause = "where_clause", AssignStatement = "assign_statement", Assignment = "assignment", + Identifier = "identifier", SourceCode = "source_code", ForStatement = "for_statement", QueryTuning = "query_tuning", @@ -55,4 +56,5 @@ export enum SyntaxNodeType { EachKeyword = "EACH", EndKeyword = "END", IfKeyword = "IF", + ForKeyword = "FOR", } diff --git a/src/v2/formatterFramework/enableFormatterDecorators.ts b/src/v2/formatterFramework/enableFormatterDecorators.ts index 4881628..12668e1 100644 --- a/src/v2/formatterFramework/enableFormatterDecorators.ts +++ b/src/v2/formatterFramework/enableFormatterDecorators.ts @@ -4,6 +4,7 @@ import { IfFormatter } from "../formatters/if/IfFormatter"; import { DefineFormatter } from "../formatters/define/DefineFormatter"; import { UsingFormatter } from "../formatters/using/UsingFormatter"; import { CaseFormatter } from "../formatters/case/CaseFormatter"; +import { ForFormatter } from "../formatters/for/ForFormatter"; // needed just for enabling decorators. Decorators does not work if there is no usage of a class in the reachable code export function enableFormatterDecorators(): void { @@ -13,4 +14,5 @@ export function enableFormatterDecorators(): void { DefineFormatter; UsingFormatter; CaseFormatter; + ForFormatter; } diff --git a/src/v2/formatters/for/ForFormatter.ts b/src/v2/formatters/for/ForFormatter.ts new file mode 100644 index 0000000..69969c4 --- /dev/null +++ b/src/v2/formatters/for/ForFormatter.ts @@ -0,0 +1,157 @@ +import { SyntaxNode } from "web-tree-sitter"; +import { IFormatter } from "../../formatterFramework/IFormatter"; +import { SyntaxNodeType } from "../../../model/SyntaxNodeType"; +import { CodeEdit } from "../../model/CodeEdit"; +import { FullText } from "../../model/FullText"; +import { FormatterHelper } from "../../formatterFramework/FormatterHelper"; +import { AFormatter } from "../AFormatter"; +import { RegisterFormatter } from "../../formatterFramework/formatterDecorator"; +import { ForSettings } from "./ForSettings"; +import { IConfigurationManager } from "../../../utils/IConfigurationManager"; + +@RegisterFormatter +export class ForFormatter extends AFormatter implements IFormatter { + private startColumn = 0; + private forBodyValue = ""; + + public static readonly formatterLabel = "forFormatting"; + private readonly settings: ForSettings; + + public constructor(configurationManager: IConfigurationManager) { + super(configurationManager); + this.settings = new ForSettings(configurationManager); + } + + match(node: Readonly): boolean { + return node.type === SyntaxNodeType.ForStatement; + } + + parse( + node: Readonly, + fullText: Readonly + ): CodeEdit | CodeEdit[] | undefined { + this.collectCaseStructure(node, fullText); + return this.getCodeEdit( + node, + FormatterHelper.getCurrentText(node, fullText), + this.forBodyValue, + fullText + ); + } + + private collectCaseStructure( + node: SyntaxNode, + fullText: Readonly + ) { + this.startColumn = node.startPosition.column; + console.log("startColumn"); + console.log(this.startColumn); + this.forBodyValue = this.getForStatementBlock(node, fullText); + } + + private getForStatementBlock( + node: SyntaxNode, + fullText: Readonly + ): string { + let resultString = ""; + let alignColumn = 0; + + node.children.forEach((child) => { + console.log("BEFOREE:::::"); + console.log("child.type:", child.type); + console.log("resultString:", resultString); + + if (child.type === SyntaxNodeType.Identifier) { + alignColumn = this.startColumn + resultString.length; + } + resultString = resultString.concat( + this.getForExpressionString(child, fullText, alignColumn) + ); + + console.log("AFFTERRR:::::"); + console.log("child.type:", child.type); + console.log("resultString:", resultString); + }); + + return resultString; + } + + private getForExpressionString( + node: SyntaxNode, + fullText: Readonly, + alignColumn: number + ): string { + let newString = ""; + + switch (node.type) { + case SyntaxNodeType.ForKeyword: + console.log("Node Type:", node.type); + console.log("Node Start Position:", node.startIndex); + console.log("Node End Position:", node.endIndex); + + newString = FormatterHelper.getCurrentText( + node, + fullText + ).trim(); + console.log("Extracted newString for ForKeyword:", newString); + break; + case SyntaxNodeType.WhereClause: + console.log("Node Type:", node.type); + console.log("Node Start Position:", node.startIndex); + console.log("Node End Position:", node.endIndex); + newString = this.getWhereClauseBlock( + node, + fullText, + alignColumn + ); + break; + default: + console.log("Node Type:", node.type); + console.log("Node Start Position:", node.startIndex); + console.log("Node End Position:", node.endIndex); + const text = FormatterHelper.getCurrentText( + node, + fullText + ).trim(); + newString = text.length === 0 ? "" : " " + text; + console.log("Default Case - Text:", newString); + break; + } + + return newString; + } + + private getWhereClauseBlock( + node: SyntaxNode, + fullText: Readonly, + alignColumn: number + ): string { + let resultString = ""; + + node.children.forEach((child) => { + switch (child.type) { + case SyntaxNodeType.WhereKeyword: + console.log("WhereKeyword"); + resultString = resultString.concat( + " ", + FormatterHelper.getCurrentText(child, fullText).trim(), + fullText.eolDelimiter, + " ".repeat(alignColumn) + ); + break; + default: + const text = FormatterHelper.getCurrentText( + child, + fullText + ).trim(); + console.log("default getWhereClauseBlock"); + resultString = resultString.concat( + text.length === 0 ? "" : " " + text + ); + break; + } + }); + + return resultString; + } +} diff --git a/src/v2/formatters/for/ForSettings.ts b/src/v2/formatters/for/ForSettings.ts new file mode 100644 index 0000000..9561209 --- /dev/null +++ b/src/v2/formatters/for/ForSettings.ts @@ -0,0 +1,8 @@ +import { ASettings } from "../ASettings"; + +export class ForSettings extends ASettings { + // token settings + public forFormatting() { + return this.configurationManager.get("forFormatting") ? true : false; + } +} \ No newline at end of file From 93e66ece9deceac23b4b8cd792ce89b87788e344 Mon Sep 17 00:00:00 2001 From: sjonynaite Date: Tue, 27 Aug 2024 09:35:19 +0300 Subject: [PATCH 03/13] update --- src/v2/formatters/for/ForFormatter.ts | 41 +++++++++++++-------------- 1 file changed, 19 insertions(+), 22 deletions(-) diff --git a/src/v2/formatters/for/ForFormatter.ts b/src/v2/formatters/for/ForFormatter.ts index 69969c4..46b2a88 100644 --- a/src/v2/formatters/for/ForFormatter.ts +++ b/src/v2/formatters/for/ForFormatter.ts @@ -23,14 +23,19 @@ export class ForFormatter extends AFormatter implements IFormatter { } match(node: Readonly): boolean { - return node.type === SyntaxNodeType.ForStatement; + if (node.type === SyntaxNodeType.ForStatement) { + return true; + } + + return false; } parse( node: Readonly, fullText: Readonly ): CodeEdit | CodeEdit[] | undefined { - this.collectCaseStructure(node, fullText); + this.collectForStructure(node, fullText); + return this.getCodeEdit( node, FormatterHelper.getCurrentText(node, fullText), @@ -39,7 +44,7 @@ export class ForFormatter extends AFormatter implements IFormatter { ); } - private collectCaseStructure( + private collectForStructure( node: SyntaxNode, fullText: Readonly ) { @@ -56,21 +61,17 @@ export class ForFormatter extends AFormatter implements IFormatter { let resultString = ""; let alignColumn = 0; - node.children.forEach((child) => { - console.log("BEFOREE:::::"); - console.log("child.type:", child.type); - console.log("resultString:", resultString); + console.log("Node Type:", node.type); + console.log("Node Start Position:", node.startIndex); + console.log("Node End Position:", node.endIndex); + node.children.forEach((child) => { if (child.type === SyntaxNodeType.Identifier) { alignColumn = this.startColumn + resultString.length; } resultString = resultString.concat( this.getForExpressionString(child, fullText, alignColumn) ); - - console.log("AFFTERRR:::::"); - console.log("child.type:", child.type); - console.log("resultString:", resultString); }); return resultString; @@ -83,22 +84,20 @@ export class ForFormatter extends AFormatter implements IFormatter { ): string { let newString = ""; + console.log("Node Type:", node.type); + console.log("Node Start Position:", node.startIndex); + console.log("Node End Position:", node.endIndex); + switch (node.type) { case SyntaxNodeType.ForKeyword: - console.log("Node Type:", node.type); - console.log("Node Start Position:", node.startIndex); - console.log("Node End Position:", node.endIndex); - + console.log("ForKeyword"); newString = FormatterHelper.getCurrentText( node, fullText ).trim(); - console.log("Extracted newString for ForKeyword:", newString); break; case SyntaxNodeType.WhereClause: - console.log("Node Type:", node.type); - console.log("Node Start Position:", node.startIndex); - console.log("Node End Position:", node.endIndex); + console.log("WhereClause"); newString = this.getWhereClauseBlock( node, fullText, @@ -106,9 +105,7 @@ export class ForFormatter extends AFormatter implements IFormatter { ); break; default: - console.log("Node Type:", node.type); - console.log("Node Start Position:", node.startIndex); - console.log("Node End Position:", node.endIndex); + console.log("default"); const text = FormatterHelper.getCurrentText( node, fullText From c1a16f9669e38dc43970cc715bf759e32521e534 Mon Sep 17 00:00:00 2001 From: sjonynaite Date: Tue, 27 Aug 2024 10:33:49 +0300 Subject: [PATCH 04/13] update --- src/v2/formatters/for/ForFormatter.ts | 287 ++++++++++++++-------- src/v2/formatters/for/ForFormatterrrrr.ts | 154 ++++++++++++ 2 files changed, 345 insertions(+), 96 deletions(-) create mode 100644 src/v2/formatters/for/ForFormatterrrrr.ts diff --git a/src/v2/formatters/for/ForFormatter.ts b/src/v2/formatters/for/ForFormatter.ts index 46b2a88..34bdaec 100644 --- a/src/v2/formatters/for/ForFormatter.ts +++ b/src/v2/formatters/for/ForFormatter.ts @@ -11,23 +11,31 @@ import { IConfigurationManager } from "../../../utils/IConfigurationManager"; @RegisterFormatter export class ForFormatter extends AFormatter implements IFormatter { - private startColumn = 0; - private forBodyValue = ""; - public static readonly formatterLabel = "forFormatting"; private readonly settings: ForSettings; + private startColumn = 0; + private recordValueColumn = 0; + private forBlockValueColumn = 0; + private forKey = ""; // FOR + private forTypeKey = ""; // EACH | FIRST | LAST + private recordValue = ""; + private byValue = ""; // BY + private whereKey = ""; // WHERE + private whereValue = ""; + private forBodyValue = ""; + private useIndexValue = ""; //USE-INDEX + private queryTuningLockKey = ""; // SHARE-LOCK | EXCLUSIVE-LOCK | NO-LOCK + private queryTuningNoPrefetchKey = ""; // NO-PREFETCH + private endValue = ""; + public constructor(configurationManager: IConfigurationManager) { super(configurationManager); this.settings = new ForSettings(configurationManager); } match(node: Readonly): boolean { - if (node.type === SyntaxNodeType.ForStatement) { - return true; - } - - return false; + return node.type === SyntaxNodeType.ForStatement; } parse( @@ -36,12 +44,14 @@ export class ForFormatter extends AFormatter implements IFormatter { ): CodeEdit | CodeEdit[] | undefined { this.collectForStructure(node, fullText); - return this.getCodeEdit( - node, - FormatterHelper.getCurrentText(node, fullText), - this.forBodyValue, - fullText - ); + const formattedBlock = this.getPrettyBlock(); + + const originalText = FormatterHelper.getCurrentText(node, fullText); + if (originalText === formattedBlock) { + return undefined; // No changes needed + } + + return this.getCodeEdit(node, originalText, formattedBlock, fullText); } private collectForStructure( @@ -49,106 +59,191 @@ export class ForFormatter extends AFormatter implements IFormatter { fullText: Readonly ) { this.startColumn = node.startPosition.column; - console.log("startColumn"); - console.log(this.startColumn); - this.forBodyValue = this.getForStatementBlock(node, fullText); + console.log("startColumn", this.startColumn); + this.forKey = FormatterHelper.getCurrentText(node, fullText).trim(); + console.log("forKey", this.forKey); + this.forTypeKey = this.getForTypeKey(node); + console.log("forTypeKey", this.forTypeKey); + this.recordValue = node.text; + console.log("recordValue", this.recordValue); + this.recordValueColumn = + this.startColumn + this.forKey.length + this.forTypeKey.length + 1; // +1 is a space between FOR EACH + console.log("recordValueColumn", this.recordValueColumn); + this.forBlockValueColumn = this.startColumn + this.settings.tabSize(); + console.log("forBlockValueColumn", this.forBlockValueColumn); + + this.assignQueryTuningStatements(node); + + + const whereNode = this.getWhereNode(node); + console.log("whereNode", whereNode); + + if (whereNode !== undefined) { + this.whereKey = this.getWhereKey(whereNode, fullText); + this.whereValue = this.getPrettyWhereBlock( + whereNode, + "\r\n".concat(" ".repeat(this.recordValueColumn)), + fullText + ); + } + + this.assignByValue(node, fullText); + + const bodyNode = this.getForBodyNode(node); + console.log("bodyNode", bodyNode); + + if (bodyNode !== undefined) { + this.forBodyValue = this.getPrettyBodyBlock( + bodyNode, + "\r\n".concat(" ".repeat(this.forBlockValueColumn)), + fullText + ); + } + + this.assignEndValue(node, fullText); } - private getForStatementBlock( - node: SyntaxNode, - fullText: Readonly - ): string { - let resultString = ""; - let alignColumn = 0; + private getForTypeKey(node: SyntaxNode): string { + const forTypeNode = node.child(1); - console.log("Node Type:", node.type); - console.log("Node Start Position:", node.startIndex); - console.log("Node End Position:", node.endIndex); + if (forTypeNode === null) { + return ""; + } + + if ( + forTypeNode.type === SyntaxNodeType.EachKeyword || + forTypeNode.type === SyntaxNodeType.FirstKeyword || + forTypeNode.type === SyntaxNodeType.LastKeyword + ) { + return forTypeNode.text; + } else { + return ""; + } + } + private assignQueryTuningStatements(node: SyntaxNode): void { node.children.forEach((child) => { - if (child.type === SyntaxNodeType.Identifier) { - alignColumn = this.startColumn + resultString.length; + if (child.type === SyntaxNodeType.QueryTuning) { + const tuneNode = child.child(0); + if (tuneNode !== null) { + const text = tuneNode.text; + + switch (tuneNode.type) { + case SyntaxNodeType.ShareLockKeyword: + case SyntaxNodeType.ExclLockKeyword: + case SyntaxNodeType.NoLockKeyword: + this.queryTuningLockKey = text; + break; + case SyntaxNodeType.NoPrefetchKeyword: + this.queryTuningNoPrefetchKey = text; + break; + } + } } - resultString = resultString.concat( - this.getForExpressionString(child, fullText, alignColumn) - ); }); + } - return resultString; + private getWhereNode(node: SyntaxNode): SyntaxNode | undefined { + return node.children.find( + (child) => child.type === SyntaxNodeType.WhereClause + ); } - private getForExpressionString( + private getWhereKey( + whereNode: SyntaxNode, + fullText: Readonly + ): string { + return FormatterHelper.getCurrentText(whereNode, fullText).trim(); // Assuming first child is WHERE keyword + } + + private getPrettyWhereBlock( node: SyntaxNode, - fullText: Readonly, - alignColumn: number + separator: string, + fullText: Readonly ): string { - let newString = ""; - - console.log("Node Type:", node.type); - console.log("Node Start Position:", node.startIndex); - console.log("Node End Position:", node.endIndex); - - switch (node.type) { - case SyntaxNodeType.ForKeyword: - console.log("ForKeyword"); - newString = FormatterHelper.getCurrentText( - node, - fullText - ).trim(); - break; - case SyntaxNodeType.WhereClause: - console.log("WhereClause"); - newString = this.getWhereClauseBlock( - node, - fullText, - alignColumn - ); - break; - default: - console.log("default"); - const text = FormatterHelper.getCurrentText( - node, - fullText - ).trim(); - newString = text.length === 0 ? "" : " " + text; - console.log("Default Case - Text:", newString); - break; - } + return node.children + .map((child) => + FormatterHelper.getCurrentText(child, fullText).trim() + ) + .join(separator); + } + + private assignByValue( + node: SyntaxNode, + fullText: Readonly + ): void { + const byNode = node.children.find( + (child) => child.type === SyntaxNodeType.SortClause + ); + this.byValue = byNode + ? FormatterHelper.getCurrentText(byNode, fullText).trim() + : ""; + } - return newString; + private getForBodyNode(node: SyntaxNode): SyntaxNode | undefined { + return node.children.find( + (child) => child.type === SyntaxNodeType.Body + ); } - private getWhereClauseBlock( + private getPrettyBodyBlock( node: SyntaxNode, - fullText: Readonly, - alignColumn: number + separator: string, + fullText: Readonly ): string { - let resultString = ""; + return node.children + .map( + (child) => + FormatterHelper.getCurrentText(child, fullText).trim() + + separator + ) + .join(""); + } - node.children.forEach((child) => { - switch (child.type) { - case SyntaxNodeType.WhereKeyword: - console.log("WhereKeyword"); - resultString = resultString.concat( - " ", - FormatterHelper.getCurrentText(child, fullText).trim(), - fullText.eolDelimiter, - " ".repeat(alignColumn) - ); - break; - default: - const text = FormatterHelper.getCurrentText( - child, - fullText - ).trim(); - console.log("default getWhereClauseBlock"); - resultString = resultString.concat( - text.length === 0 ? "" : " " + text - ); - break; - } - }); + private assignEndValue( + node: SyntaxNode, + fullText: Readonly + ): void { + const endNode = node.children.find( + (child) => child.type === SyntaxNodeType.EndKeyword + ); + this.endValue = endNode + ? FormatterHelper.getCurrentText(endNode, fullText).trim() + : ""; + } - return resultString; + private getPrettyBlock(): string { + return "" + .concat(this.forKey) + .concat(this.forTypeKey === "" ? "" : " ") + .concat(this.forTypeKey.trim()) + .concat(this.recordValue === "" ? "" : " ") + .concat(this.recordValue.trim()) + .concat(this.queryTuningLockKey === "" ? "" : " ") + .concat(this.queryTuningLockKey.trim()) + .concat(this.whereKey === "" ? "" : " ") + .concat(this.whereKey.trim()) + .concat(this.whereValue === "" ? "" : " ") + .concat(this.whereValue === "" ? " " : "\r\n") + .concat( + this.whereValue === "" ? "" : " ".repeat(this.recordValueColumn) + ) + .concat(this.whereValue.trim()) + .concat(this.byValue === "" ? "" : " ") + .concat(this.byValue.trim()) + .concat(":") + .concat(this.forBodyValue === "" ? " " : "\r\n") + .concat( + this.forBodyValue === "" + ? "" + : " ".repeat(this.forBlockValueColumn) + ) + .concat(this.forBodyValue.trim()) + .concat(this.queryTuningNoPrefetchKey === "" ? "" : " ") + .concat(this.queryTuningNoPrefetchKey.trim()) + .concat("\r\n") + .concat(" ".repeat(this.startColumn)) + .concat(this.endValue) + .concat("."); } } diff --git a/src/v2/formatters/for/ForFormatterrrrr.ts b/src/v2/formatters/for/ForFormatterrrrr.ts new file mode 100644 index 0000000..46b2a88 --- /dev/null +++ b/src/v2/formatters/for/ForFormatterrrrr.ts @@ -0,0 +1,154 @@ +import { SyntaxNode } from "web-tree-sitter"; +import { IFormatter } from "../../formatterFramework/IFormatter"; +import { SyntaxNodeType } from "../../../model/SyntaxNodeType"; +import { CodeEdit } from "../../model/CodeEdit"; +import { FullText } from "../../model/FullText"; +import { FormatterHelper } from "../../formatterFramework/FormatterHelper"; +import { AFormatter } from "../AFormatter"; +import { RegisterFormatter } from "../../formatterFramework/formatterDecorator"; +import { ForSettings } from "./ForSettings"; +import { IConfigurationManager } from "../../../utils/IConfigurationManager"; + +@RegisterFormatter +export class ForFormatter extends AFormatter implements IFormatter { + private startColumn = 0; + private forBodyValue = ""; + + public static readonly formatterLabel = "forFormatting"; + private readonly settings: ForSettings; + + public constructor(configurationManager: IConfigurationManager) { + super(configurationManager); + this.settings = new ForSettings(configurationManager); + } + + match(node: Readonly): boolean { + if (node.type === SyntaxNodeType.ForStatement) { + return true; + } + + return false; + } + + parse( + node: Readonly, + fullText: Readonly + ): CodeEdit | CodeEdit[] | undefined { + this.collectForStructure(node, fullText); + + return this.getCodeEdit( + node, + FormatterHelper.getCurrentText(node, fullText), + this.forBodyValue, + fullText + ); + } + + private collectForStructure( + node: SyntaxNode, + fullText: Readonly + ) { + this.startColumn = node.startPosition.column; + console.log("startColumn"); + console.log(this.startColumn); + this.forBodyValue = this.getForStatementBlock(node, fullText); + } + + private getForStatementBlock( + node: SyntaxNode, + fullText: Readonly + ): string { + let resultString = ""; + let alignColumn = 0; + + console.log("Node Type:", node.type); + console.log("Node Start Position:", node.startIndex); + console.log("Node End Position:", node.endIndex); + + node.children.forEach((child) => { + if (child.type === SyntaxNodeType.Identifier) { + alignColumn = this.startColumn + resultString.length; + } + resultString = resultString.concat( + this.getForExpressionString(child, fullText, alignColumn) + ); + }); + + return resultString; + } + + private getForExpressionString( + node: SyntaxNode, + fullText: Readonly, + alignColumn: number + ): string { + let newString = ""; + + console.log("Node Type:", node.type); + console.log("Node Start Position:", node.startIndex); + console.log("Node End Position:", node.endIndex); + + switch (node.type) { + case SyntaxNodeType.ForKeyword: + console.log("ForKeyword"); + newString = FormatterHelper.getCurrentText( + node, + fullText + ).trim(); + break; + case SyntaxNodeType.WhereClause: + console.log("WhereClause"); + newString = this.getWhereClauseBlock( + node, + fullText, + alignColumn + ); + break; + default: + console.log("default"); + const text = FormatterHelper.getCurrentText( + node, + fullText + ).trim(); + newString = text.length === 0 ? "" : " " + text; + console.log("Default Case - Text:", newString); + break; + } + + return newString; + } + + private getWhereClauseBlock( + node: SyntaxNode, + fullText: Readonly, + alignColumn: number + ): string { + let resultString = ""; + + node.children.forEach((child) => { + switch (child.type) { + case SyntaxNodeType.WhereKeyword: + console.log("WhereKeyword"); + resultString = resultString.concat( + " ", + FormatterHelper.getCurrentText(child, fullText).trim(), + fullText.eolDelimiter, + " ".repeat(alignColumn) + ); + break; + default: + const text = FormatterHelper.getCurrentText( + child, + fullText + ).trim(); + console.log("default getWhereClauseBlock"); + resultString = resultString.concat( + text.length === 0 ? "" : " " + text + ); + break; + } + }); + + return resultString; + } +} From bb3c3faba61a576f2a91a135a9dde4ffab5e080a Mon Sep 17 00:00:00 2001 From: sjonynaite Date: Tue, 27 Aug 2024 21:33:29 +0300 Subject: [PATCH 05/13] Update for formatter --- .../{for1-each-false => 1each-false}/input.p | 2 +- .../{for1-each-false => 1each-false}/target.p | 2 +- .../{for2-each-true => 2each-true}/input.p | 2 +- .../{for2-each-true => 2each-true}/target.p | 2 +- .../functionalTests/for/3each-each/input.p | 8 + .../functionalTests/for/3each-each/target.p | 13 + .../{for3-each-where => 3each-where}/input.p | 2 +- .../{for3-each-where => 3each-where}/target.p | 2 +- .../functionalTests/for/3first-by/input.p | 9 + .../functionalTests/for/3first-by/target.p | 9 + .../input.p | 2 +- .../target.p | 2 +- src/model/SyntaxNodeType.ts | 4 + src/v2/formatters/for/ForFormatter.ts | 331 ++++++++---------- src/v2/formatters/for/ForFormatterrrrr.ts | 154 -------- 15 files changed, 194 insertions(+), 350 deletions(-) rename resources/functionalTests/for/{for1-each-false => 1each-false}/input.p (72%) rename resources/functionalTests/for/{for1-each-false => 1each-false}/target.p (72%) rename resources/functionalTests/for/{for2-each-true => 2each-true}/input.p (72%) rename resources/functionalTests/for/{for2-each-true => 2each-true}/target.p (72%) create mode 100644 resources/functionalTests/for/3each-each/input.p create mode 100644 resources/functionalTests/for/3each-each/target.p rename resources/functionalTests/for/{for3-each-where => 3each-where}/input.p (75%) rename resources/functionalTests/for/{for3-each-where => 3each-where}/target.p (76%) create mode 100644 resources/functionalTests/for/3first-by/input.p create mode 100644 resources/functionalTests/for/3first-by/target.p rename resources/functionalTests/for/{for4-each-where3 => 4each-where3}/input.p (80%) rename resources/functionalTests/for/{for4-each-where3 => 4each-where3}/target.p (80%) delete mode 100644 src/v2/formatters/for/ForFormatterrrrr.ts diff --git a/resources/functionalTests/for/for1-each-false/input.p b/resources/functionalTests/for/1each-false/input.p similarity index 72% rename from resources/functionalTests/for/for1-each-false/input.p rename to resources/functionalTests/for/1each-false/input.p index d675f87..a8ac99f 100644 --- a/resources/functionalTests/for/for1-each-false/input.p +++ b/resources/functionalTests/for/1each-false/input.p @@ -1,5 +1,5 @@ /* formatterSettingsOverride */ -/* { "AblFormatter.blockFormatting": true, +/* { "AblFormatter.blockFormatting": false, "AblFormatter.forFormatting": false }*/ diff --git a/resources/functionalTests/for/for1-each-false/target.p b/resources/functionalTests/for/1each-false/target.p similarity index 72% rename from resources/functionalTests/for/for1-each-false/target.p rename to resources/functionalTests/for/1each-false/target.p index d675f87..a8ac99f 100644 --- a/resources/functionalTests/for/for1-each-false/target.p +++ b/resources/functionalTests/for/1each-false/target.p @@ -1,5 +1,5 @@ /* formatterSettingsOverride */ -/* { "AblFormatter.blockFormatting": true, +/* { "AblFormatter.blockFormatting": false, "AblFormatter.forFormatting": false }*/ diff --git a/resources/functionalTests/for/for2-each-true/input.p b/resources/functionalTests/for/2each-true/input.p similarity index 72% rename from resources/functionalTests/for/for2-each-true/input.p rename to resources/functionalTests/for/2each-true/input.p index ef42047..9e0bcf5 100644 --- a/resources/functionalTests/for/for2-each-true/input.p +++ b/resources/functionalTests/for/2each-true/input.p @@ -1,5 +1,5 @@ /* formatterSettingsOverride */ -/* { "AblFormatter.blockFormatting": true, +/* { "AblFormatter.blockFormatting": false, "AblFormatter.forFormatting": true }*/ diff --git a/resources/functionalTests/for/for2-each-true/target.p b/resources/functionalTests/for/2each-true/target.p similarity index 72% rename from resources/functionalTests/for/for2-each-true/target.p rename to resources/functionalTests/for/2each-true/target.p index dbbe313..c17ee35 100644 --- a/resources/functionalTests/for/for2-each-true/target.p +++ b/resources/functionalTests/for/2each-true/target.p @@ -1,5 +1,5 @@ /* formatterSettingsOverride */ -/* { "AblFormatter.blockFormatting": true, +/* { "AblFormatter.blockFormatting": false, "AblFormatter.forFormatting": true }*/ diff --git a/resources/functionalTests/for/3each-each/input.p b/resources/functionalTests/for/3each-each/input.p new file mode 100644 index 0000000..6fd53d6 --- /dev/null +++ b/resources/functionalTests/for/3each-each/input.p @@ -0,0 +1,8 @@ +/* formatterSettingsOverride */ +/* { "AblFormatter.forFormatting": true, +"AblFormatter.blockFormatting": false, +"abl.completion.upperCase": true}*/ + +FOR EACH Customer NO-LOCK, EACH Order OF Customer NO-LOCK, EACH OrderLine OF Order NO-LOCK BY Order.PromiseDate BY Customer.CustNum BY OrderLine.LineNum: + DISPLAY Order.PromiseDate. +END. \ No newline at end of file diff --git a/resources/functionalTests/for/3each-each/target.p b/resources/functionalTests/for/3each-each/target.p new file mode 100644 index 0000000..e1b1447 --- /dev/null +++ b/resources/functionalTests/for/3each-each/target.p @@ -0,0 +1,13 @@ +/* formatterSettingsOverride */ +/* { "AblFormatter.forFormatting": true, +"AblFormatter.blockFormatting": false, +"abl.completion.upperCase": true}*/ + +FOR EACH Customer NO-LOCK, + EACH Order OF Customer NO-LOCK, + EACH OrderLine OF Order NO-LOCK + BY Order.PromiseDate + BY Customer.CustNum + BY OrderLine.LineNum: + DISPLAY Order.PromiseDate. +END. \ No newline at end of file diff --git a/resources/functionalTests/for/for3-each-where/input.p b/resources/functionalTests/for/3each-where/input.p similarity index 75% rename from resources/functionalTests/for/for3-each-where/input.p rename to resources/functionalTests/for/3each-where/input.p index bfed1d6..89fdd87 100644 --- a/resources/functionalTests/for/for3-each-where/input.p +++ b/resources/functionalTests/for/3each-where/input.p @@ -1,5 +1,5 @@ /* formatterSettingsOverride */ -/* { "AblFormatter.blockFormatting": true, +/* { "AblFormatter.blockFormatting": false, "AblFormatter.forFormatting": true }*/ diff --git a/resources/functionalTests/for/for3-each-where/target.p b/resources/functionalTests/for/3each-where/target.p similarity index 76% rename from resources/functionalTests/for/for3-each-where/target.p rename to resources/functionalTests/for/3each-where/target.p index a376627..241df44 100644 --- a/resources/functionalTests/for/for3-each-where/target.p +++ b/resources/functionalTests/for/3each-where/target.p @@ -1,5 +1,5 @@ /* formatterSettingsOverride */ -/* { "AblFormatter.blockFormatting": true, +/* { "AblFormatter.blockFormatting": false, "AblFormatter.forFormatting": true }*/ diff --git a/resources/functionalTests/for/3first-by/input.p b/resources/functionalTests/for/3first-by/input.p new file mode 100644 index 0000000..34adf0a --- /dev/null +++ b/resources/functionalTests/for/3first-by/input.p @@ -0,0 +1,9 @@ +/* formatterSettingsOverride */ +/* { "AblFormatter.blockFormatting": false, +"AblFormatter.forFormatting": true +}*/ + +FOR FIRST Customer NO-LOCK + BY Customer.CreditLimit: + DISPLAY Customer. + END. \ No newline at end of file diff --git a/resources/functionalTests/for/3first-by/target.p b/resources/functionalTests/for/3first-by/target.p new file mode 100644 index 0000000..bebe9df --- /dev/null +++ b/resources/functionalTests/for/3first-by/target.p @@ -0,0 +1,9 @@ +/* formatterSettingsOverride */ +/* { "AblFormatter.blockFormatting": false, +"AblFormatter.forFormatting": true +}*/ + +FOR FIRST Customer NO-LOCK + BY Customer.CreditLimit: + DISPLAY Customer. +END. \ No newline at end of file diff --git a/resources/functionalTests/for/for4-each-where3/input.p b/resources/functionalTests/for/4each-where3/input.p similarity index 80% rename from resources/functionalTests/for/for4-each-where3/input.p rename to resources/functionalTests/for/4each-where3/input.p index 38eba14..038c7ab 100644 --- a/resources/functionalTests/for/for4-each-where3/input.p +++ b/resources/functionalTests/for/4each-where3/input.p @@ -1,5 +1,5 @@ /* formatterSettingsOverride */ -/* { "AblFormatter.blockFormatting": true, +/* { "AblFormatter.blockFormatting": false, "AblFormatter.forFormatting": true }*/ diff --git a/resources/functionalTests/for/for4-each-where3/target.p b/resources/functionalTests/for/4each-where3/target.p similarity index 80% rename from resources/functionalTests/for/for4-each-where3/target.p rename to resources/functionalTests/for/4each-where3/target.p index 154260b..3f9c1e4 100644 --- a/resources/functionalTests/for/for4-each-where3/target.p +++ b/resources/functionalTests/for/4each-where3/target.p @@ -1,5 +1,5 @@ /* formatterSettingsOverride */ -/* { "AblFormatter.blockFormatting": true, +/* { "AblFormatter.blockFormatting": false, "AblFormatter.forFormatting": true }*/ diff --git a/src/model/SyntaxNodeType.ts b/src/model/SyntaxNodeType.ts index 3ad55a6..d3ab2fb 100644 --- a/src/model/SyntaxNodeType.ts +++ b/src/model/SyntaxNodeType.ts @@ -24,6 +24,7 @@ export enum SyntaxNodeType { Assignment = "assignment", Identifier = "identifier", SourceCode = "source_code", + ForPhrase = "for_phrase", ForStatement = "for_statement", QueryTuning = "query_tuning", SortClause = "sort_clause", @@ -69,4 +70,7 @@ export enum SyntaxNodeType { EndKeyword = "END", IfKeyword = "IF", ForKeyword = "FOR", + DotKeyword = ".", + ColonKeyword = ":", + CommaKeyword = ",", } diff --git a/src/v2/formatters/for/ForFormatter.ts b/src/v2/formatters/for/ForFormatter.ts index 34bdaec..f793c7e 100644 --- a/src/v2/formatters/for/ForFormatter.ts +++ b/src/v2/formatters/for/ForFormatter.ts @@ -11,23 +11,11 @@ import { IConfigurationManager } from "../../../utils/IConfigurationManager"; @RegisterFormatter export class ForFormatter extends AFormatter implements IFormatter { - public static readonly formatterLabel = "forFormatting"; - private readonly settings: ForSettings; - private startColumn = 0; - private recordValueColumn = 0; - private forBlockValueColumn = 0; - private forKey = ""; // FOR - private forTypeKey = ""; // EACH | FIRST | LAST - private recordValue = ""; - private byValue = ""; // BY - private whereKey = ""; // WHERE - private whereValue = ""; private forBodyValue = ""; - private useIndexValue = ""; //USE-INDEX - private queryTuningLockKey = ""; // SHARE-LOCK | EXCLUSIVE-LOCK | NO-LOCK - private queryTuningNoPrefetchKey = ""; // NO-PREFETCH - private endValue = ""; + + public static readonly formatterLabel = "forFormatting"; + private readonly settings: ForSettings; public constructor(configurationManager: IConfigurationManager) { super(configurationManager); @@ -35,7 +23,11 @@ export class ForFormatter extends AFormatter implements IFormatter { } match(node: Readonly): boolean { - return node.type === SyntaxNodeType.ForStatement; + if (node.type === SyntaxNodeType.ForStatement) { + return true; + } + + return false; } parse( @@ -44,14 +36,12 @@ export class ForFormatter extends AFormatter implements IFormatter { ): CodeEdit | CodeEdit[] | undefined { this.collectForStructure(node, fullText); - const formattedBlock = this.getPrettyBlock(); - - const originalText = FormatterHelper.getCurrentText(node, fullText); - if (originalText === formattedBlock) { - return undefined; // No changes needed - } - - return this.getCodeEdit(node, originalText, formattedBlock, fullText); + return this.getCodeEdit( + node, + FormatterHelper.getCurrentText(node, fullText), + this.forBodyValue, + fullText + ); } private collectForStructure( @@ -59,191 +49,156 @@ export class ForFormatter extends AFormatter implements IFormatter { fullText: Readonly ) { this.startColumn = node.startPosition.column; - console.log("startColumn", this.startColumn); - this.forKey = FormatterHelper.getCurrentText(node, fullText).trim(); - console.log("forKey", this.forKey); - this.forTypeKey = this.getForTypeKey(node); - console.log("forTypeKey", this.forTypeKey); - this.recordValue = node.text; - console.log("recordValue", this.recordValue); - this.recordValueColumn = - this.startColumn + this.forKey.length + this.forTypeKey.length + 1; // +1 is a space between FOR EACH - console.log("recordValueColumn", this.recordValueColumn); - this.forBlockValueColumn = this.startColumn + this.settings.tabSize(); - console.log("forBlockValueColumn", this.forBlockValueColumn); - - this.assignQueryTuningStatements(node); - - - const whereNode = this.getWhereNode(node); - console.log("whereNode", whereNode); - - if (whereNode !== undefined) { - this.whereKey = this.getWhereKey(whereNode, fullText); - this.whereValue = this.getPrettyWhereBlock( - whereNode, - "\r\n".concat(" ".repeat(this.recordValueColumn)), - fullText - ); - } - - this.assignByValue(node, fullText); - - const bodyNode = this.getForBodyNode(node); - console.log("bodyNode", bodyNode); - - if (bodyNode !== undefined) { - this.forBodyValue = this.getPrettyBodyBlock( - bodyNode, - "\r\n".concat(" ".repeat(this.forBlockValueColumn)), - fullText - ); - } - - this.assignEndValue(node, fullText); + this.forBodyValue = this.getForStatementBlock(node, fullText); } - private getForTypeKey(node: SyntaxNode): string { - const forTypeNode = node.child(1); - - if (forTypeNode === null) { - return ""; - } - - if ( - forTypeNode.type === SyntaxNodeType.EachKeyword || - forTypeNode.type === SyntaxNodeType.FirstKeyword || - forTypeNode.type === SyntaxNodeType.LastKeyword - ) { - return forTypeNode.text; - } else { - return ""; - } - } + private getForStatementBlock( + node: SyntaxNode, + fullText: Readonly + ): string { + let resultString = ""; + let alignColumn = 0; - private assignQueryTuningStatements(node: SyntaxNode): void { node.children.forEach((child) => { - if (child.type === SyntaxNodeType.QueryTuning) { - const tuneNode = child.child(0); - if (tuneNode !== null) { - const text = tuneNode.text; - - switch (tuneNode.type) { - case SyntaxNodeType.ShareLockKeyword: - case SyntaxNodeType.ExclLockKeyword: - case SyntaxNodeType.NoLockKeyword: - this.queryTuningLockKey = text; - break; - case SyntaxNodeType.NoPrefetchKeyword: - this.queryTuningNoPrefetchKey = text; - break; - } - } + if (child.type === SyntaxNodeType.Identifier) { + alignColumn = this.startColumn + resultString.length; } + resultString = resultString.concat( + this.getForExpressionString(child, fullText, alignColumn) + ); }); - } - private getWhereNode(node: SyntaxNode): SyntaxNode | undefined { - return node.children.find( - (child) => child.type === SyntaxNodeType.WhereClause - ); + return resultString; } - private getWhereKey( - whereNode: SyntaxNode, - fullText: Readonly + private getForExpressionString( + node: SyntaxNode, + fullText: Readonly, + alignColumn: number ): string { - return FormatterHelper.getCurrentText(whereNode, fullText).trim(); // Assuming first child is WHERE keyword + let newString = ""; + + switch (node.type) { + case SyntaxNodeType.ForKeyword: + case SyntaxNodeType.DotKeyword: + case SyntaxNodeType.ColonKeyword: + case SyntaxNodeType.CommaKeyword: + newString = FormatterHelper.getCurrentText( + node, + fullText + ).trim(); + break; + case SyntaxNodeType.WhereClause: + newString = this.getWhereClauseBlock( + node, + fullText, + alignColumn + ); + break; + case SyntaxNodeType.EndKeyword: + newString = + fullText.eolDelimiter + + " ".repeat(this.startColumn) + + FormatterHelper.getCurrentText(node, fullText).trim(); + break; + case SyntaxNodeType.SortClause: + newString = + fullText.eolDelimiter + + " ".repeat(alignColumn + 1) + + FormatterHelper.getCurrentText(node, fullText).trim(); + break; + case SyntaxNodeType.ForPhrase: + newString = this.getSortClauseBlock( + node, + fullText, + alignColumn + ); + break; + case SyntaxNodeType.Body: + newString = + fullText.eolDelimiter + + " ".repeat(this.startColumn + this.settings.tabSize()) + + FormatterHelper.getCurrentText(node, fullText).trim(); + break; + default: + const text = FormatterHelper.getCurrentText( + node, + fullText + ).trim(); + newString = text.length === 0 ? "" : " " + text; + break; + } + + return newString; } - private getPrettyWhereBlock( + private getWhereClauseBlock( node: SyntaxNode, - separator: string, - fullText: Readonly + fullText: Readonly, + alignColumn: number ): string { - return node.children - .map((child) => - FormatterHelper.getCurrentText(child, fullText).trim() - ) - .join(separator); - } + let resultString = ""; - private assignByValue( - node: SyntaxNode, - fullText: Readonly - ): void { - const byNode = node.children.find( - (child) => child.type === SyntaxNodeType.SortClause - ); - this.byValue = byNode - ? FormatterHelper.getCurrentText(byNode, fullText).trim() - : ""; - } + node.children.forEach((child) => { + switch (child.type) { + case SyntaxNodeType.WhereKeyword: + resultString = resultString.concat( + " ", + FormatterHelper.getCurrentText(child, fullText).trim(), + fullText.eolDelimiter, + " ".repeat(alignColumn) + ); + break; + default: + const text = FormatterHelper.getCurrentText( + child, + fullText + ).trim(); + resultString = resultString.concat( + text.length === 0 ? "" : " " + text + ); + break; + } + }); - private getForBodyNode(node: SyntaxNode): SyntaxNode | undefined { - return node.children.find( - (child) => child.type === SyntaxNodeType.Body - ); + return resultString; } - private getPrettyBodyBlock( + private getSortClauseBlock( node: SyntaxNode, - separator: string, - fullText: Readonly + fullText: Readonly, + alignColumn: number ): string { - return node.children - .map( - (child) => - FormatterHelper.getCurrentText(child, fullText).trim() + - separator - ) - .join(""); - } + let resultString = ""; - private assignEndValue( - node: SyntaxNode, - fullText: Readonly - ): void { - const endNode = node.children.find( - (child) => child.type === SyntaxNodeType.EndKeyword - ); - this.endValue = endNode - ? FormatterHelper.getCurrentText(endNode, fullText).trim() - : ""; - } + node.children.forEach((child) => { + switch (child.type) { + case SyntaxNodeType.EachKeyword: + resultString = resultString.concat( + fullText.eolDelimiter, + " ".repeat(this.startColumn + this.settings.tabSize()), + FormatterHelper.getCurrentText(child, fullText).trim() + ); + break; + case SyntaxNodeType.SortClause: + resultString = resultString.concat( + fullText.eolDelimiter, + " ".repeat(alignColumn + 1), + FormatterHelper.getCurrentText(child, fullText).trim() + ); + break; + default: + const text = FormatterHelper.getCurrentText( + child, + fullText + ).trim(); + resultString = resultString.concat( + text.length === 0 ? "" : " " + text + ); + break; + } + }); - private getPrettyBlock(): string { - return "" - .concat(this.forKey) - .concat(this.forTypeKey === "" ? "" : " ") - .concat(this.forTypeKey.trim()) - .concat(this.recordValue === "" ? "" : " ") - .concat(this.recordValue.trim()) - .concat(this.queryTuningLockKey === "" ? "" : " ") - .concat(this.queryTuningLockKey.trim()) - .concat(this.whereKey === "" ? "" : " ") - .concat(this.whereKey.trim()) - .concat(this.whereValue === "" ? "" : " ") - .concat(this.whereValue === "" ? " " : "\r\n") - .concat( - this.whereValue === "" ? "" : " ".repeat(this.recordValueColumn) - ) - .concat(this.whereValue.trim()) - .concat(this.byValue === "" ? "" : " ") - .concat(this.byValue.trim()) - .concat(":") - .concat(this.forBodyValue === "" ? " " : "\r\n") - .concat( - this.forBodyValue === "" - ? "" - : " ".repeat(this.forBlockValueColumn) - ) - .concat(this.forBodyValue.trim()) - .concat(this.queryTuningNoPrefetchKey === "" ? "" : " ") - .concat(this.queryTuningNoPrefetchKey.trim()) - .concat("\r\n") - .concat(" ".repeat(this.startColumn)) - .concat(this.endValue) - .concat("."); + return resultString; } } diff --git a/src/v2/formatters/for/ForFormatterrrrr.ts b/src/v2/formatters/for/ForFormatterrrrr.ts deleted file mode 100644 index 46b2a88..0000000 --- a/src/v2/formatters/for/ForFormatterrrrr.ts +++ /dev/null @@ -1,154 +0,0 @@ -import { SyntaxNode } from "web-tree-sitter"; -import { IFormatter } from "../../formatterFramework/IFormatter"; -import { SyntaxNodeType } from "../../../model/SyntaxNodeType"; -import { CodeEdit } from "../../model/CodeEdit"; -import { FullText } from "../../model/FullText"; -import { FormatterHelper } from "../../formatterFramework/FormatterHelper"; -import { AFormatter } from "../AFormatter"; -import { RegisterFormatter } from "../../formatterFramework/formatterDecorator"; -import { ForSettings } from "./ForSettings"; -import { IConfigurationManager } from "../../../utils/IConfigurationManager"; - -@RegisterFormatter -export class ForFormatter extends AFormatter implements IFormatter { - private startColumn = 0; - private forBodyValue = ""; - - public static readonly formatterLabel = "forFormatting"; - private readonly settings: ForSettings; - - public constructor(configurationManager: IConfigurationManager) { - super(configurationManager); - this.settings = new ForSettings(configurationManager); - } - - match(node: Readonly): boolean { - if (node.type === SyntaxNodeType.ForStatement) { - return true; - } - - return false; - } - - parse( - node: Readonly, - fullText: Readonly - ): CodeEdit | CodeEdit[] | undefined { - this.collectForStructure(node, fullText); - - return this.getCodeEdit( - node, - FormatterHelper.getCurrentText(node, fullText), - this.forBodyValue, - fullText - ); - } - - private collectForStructure( - node: SyntaxNode, - fullText: Readonly - ) { - this.startColumn = node.startPosition.column; - console.log("startColumn"); - console.log(this.startColumn); - this.forBodyValue = this.getForStatementBlock(node, fullText); - } - - private getForStatementBlock( - node: SyntaxNode, - fullText: Readonly - ): string { - let resultString = ""; - let alignColumn = 0; - - console.log("Node Type:", node.type); - console.log("Node Start Position:", node.startIndex); - console.log("Node End Position:", node.endIndex); - - node.children.forEach((child) => { - if (child.type === SyntaxNodeType.Identifier) { - alignColumn = this.startColumn + resultString.length; - } - resultString = resultString.concat( - this.getForExpressionString(child, fullText, alignColumn) - ); - }); - - return resultString; - } - - private getForExpressionString( - node: SyntaxNode, - fullText: Readonly, - alignColumn: number - ): string { - let newString = ""; - - console.log("Node Type:", node.type); - console.log("Node Start Position:", node.startIndex); - console.log("Node End Position:", node.endIndex); - - switch (node.type) { - case SyntaxNodeType.ForKeyword: - console.log("ForKeyword"); - newString = FormatterHelper.getCurrentText( - node, - fullText - ).trim(); - break; - case SyntaxNodeType.WhereClause: - console.log("WhereClause"); - newString = this.getWhereClauseBlock( - node, - fullText, - alignColumn - ); - break; - default: - console.log("default"); - const text = FormatterHelper.getCurrentText( - node, - fullText - ).trim(); - newString = text.length === 0 ? "" : " " + text; - console.log("Default Case - Text:", newString); - break; - } - - return newString; - } - - private getWhereClauseBlock( - node: SyntaxNode, - fullText: Readonly, - alignColumn: number - ): string { - let resultString = ""; - - node.children.forEach((child) => { - switch (child.type) { - case SyntaxNodeType.WhereKeyword: - console.log("WhereKeyword"); - resultString = resultString.concat( - " ", - FormatterHelper.getCurrentText(child, fullText).trim(), - fullText.eolDelimiter, - " ".repeat(alignColumn) - ); - break; - default: - const text = FormatterHelper.getCurrentText( - child, - fullText - ).trim(); - console.log("default getWhereClauseBlock"); - resultString = resultString.concat( - text.length === 0 ? "" : " " + text - ); - break; - } - }); - - return resultString; - } -} From f50de77eedc0bf5112a303af9dd35d1cbcc9a5e5 Mon Sep 17 00:00:00 2001 From: sjonynaite Date: Fri, 30 Aug 2024 10:05:44 +0300 Subject: [PATCH 06/13] changed some block test, because forFormatter was turned on --- resources/functionalTests/block/12for-block-end2/input.p | 3 ++- resources/functionalTests/block/12for-block-end2/target.p | 3 ++- resources/functionalTests/block/15for-block-inner2/input.p | 3 ++- resources/functionalTests/block/15for-block-inner2/target.p | 3 ++- resources/functionalTests/block/17for-block-inner4/input.p | 3 ++- resources/functionalTests/block/17for-block-inner4/target.p | 3 ++- .../functionalTests/block/51do-for-block-inner1/input.p | 3 ++- .../functionalTests/block/51do-for-block-inner1/target.p | 3 ++- .../functionalTests/block/51for-for-block-inner1/input.p | 3 ++- .../functionalTests/block/51for-for-block-inner1/target.p | 3 ++- resources/functionalTests/emptyBlock/11for/input.p | 3 ++- resources/functionalTests/emptyBlock/11for/target.p | 3 ++- src/v2/formatters/for/ForFormatter.ts | 5 ++++- 13 files changed, 28 insertions(+), 13 deletions(-) diff --git a/resources/functionalTests/block/12for-block-end2/input.p b/resources/functionalTests/block/12for-block-end2/input.p index 8dc92c9..51b7eb7 100644 --- a/resources/functionalTests/block/12for-block-end2/input.p +++ b/resources/functionalTests/block/12for-block-end2/input.p @@ -1,5 +1,6 @@ /* formatterSettingsOverride */ -/* { "AblFormatter.blockFormatting": false}*/ +/* { "AblFormatter.blockFormatting": false, +"AblFormatter.forFormatting": false}*/ for each Customer no-lock: Customer.var += 1. diff --git a/resources/functionalTests/block/12for-block-end2/target.p b/resources/functionalTests/block/12for-block-end2/target.p index 8dc92c9..51b7eb7 100644 --- a/resources/functionalTests/block/12for-block-end2/target.p +++ b/resources/functionalTests/block/12for-block-end2/target.p @@ -1,5 +1,6 @@ /* formatterSettingsOverride */ -/* { "AblFormatter.blockFormatting": false}*/ +/* { "AblFormatter.blockFormatting": false, +"AblFormatter.forFormatting": false}*/ for each Customer no-lock: Customer.var += 1. diff --git a/resources/functionalTests/block/15for-block-inner2/input.p b/resources/functionalTests/block/15for-block-inner2/input.p index 9718d0f..b3ff775 100644 --- a/resources/functionalTests/block/15for-block-inner2/input.p +++ b/resources/functionalTests/block/15for-block-inner2/input.p @@ -1,5 +1,6 @@ /* formatterSettingsOverride */ -/* { "AblFormatter.blockFormatting": false}*/ +/* { "AblFormatter.blockFormatting": false, +"AblFormatter.forFormatting": false}*/ for each Customer no-lock: Customer.var += 1. diff --git a/resources/functionalTests/block/15for-block-inner2/target.p b/resources/functionalTests/block/15for-block-inner2/target.p index 9718d0f..b3ff775 100644 --- a/resources/functionalTests/block/15for-block-inner2/target.p +++ b/resources/functionalTests/block/15for-block-inner2/target.p @@ -1,5 +1,6 @@ /* formatterSettingsOverride */ -/* { "AblFormatter.blockFormatting": false}*/ +/* { "AblFormatter.blockFormatting": false, +"AblFormatter.forFormatting": false}*/ for each Customer no-lock: Customer.var += 1. diff --git a/resources/functionalTests/block/17for-block-inner4/input.p b/resources/functionalTests/block/17for-block-inner4/input.p index 9ca202f..e021889 100644 --- a/resources/functionalTests/block/17for-block-inner4/input.p +++ b/resources/functionalTests/block/17for-block-inner4/input.p @@ -1,5 +1,6 @@ /* formatterSettingsOverride */ -/* { "AblFormatter.blockFormatting": false}*/ +/* { "AblFormatter.blockFormatting": false, +"AblFormatter.forFormatting": false}*/ for each Customer no-lock: Customer.var += 1. diff --git a/resources/functionalTests/block/17for-block-inner4/target.p b/resources/functionalTests/block/17for-block-inner4/target.p index 9ca202f..e021889 100644 --- a/resources/functionalTests/block/17for-block-inner4/target.p +++ b/resources/functionalTests/block/17for-block-inner4/target.p @@ -1,5 +1,6 @@ /* formatterSettingsOverride */ -/* { "AblFormatter.blockFormatting": false}*/ +/* { "AblFormatter.blockFormatting": false, +"AblFormatter.forFormatting": false}*/ for each Customer no-lock: Customer.var += 1. diff --git a/resources/functionalTests/block/51do-for-block-inner1/input.p b/resources/functionalTests/block/51do-for-block-inner1/input.p index fc9a255..3da3c26 100644 --- a/resources/functionalTests/block/51do-for-block-inner1/input.p +++ b/resources/functionalTests/block/51do-for-block-inner1/input.p @@ -1,5 +1,6 @@ /* formattersettingsoverride */ -/* { "AblFormatter.blockFormatting": true}*/ +/* { "AblFormatter.blockFormatting": true, +"AblFormatter.forFormatting": false}*/ do transaction: for each Customer no-lock: diff --git a/resources/functionalTests/block/51do-for-block-inner1/target.p b/resources/functionalTests/block/51do-for-block-inner1/target.p index 90344ec..5c03e7c 100644 --- a/resources/functionalTests/block/51do-for-block-inner1/target.p +++ b/resources/functionalTests/block/51do-for-block-inner1/target.p @@ -1,5 +1,6 @@ /* formattersettingsoverride */ -/* { "AblFormatter.blockFormatting": true}*/ +/* { "AblFormatter.blockFormatting": true, +"AblFormatter.forFormatting": false}*/ do transaction: for each Customer no-lock: diff --git a/resources/functionalTests/block/51for-for-block-inner1/input.p b/resources/functionalTests/block/51for-for-block-inner1/input.p index 9b59fba..c19d971 100644 --- a/resources/functionalTests/block/51for-for-block-inner1/input.p +++ b/resources/functionalTests/block/51for-for-block-inner1/input.p @@ -1,5 +1,6 @@ /* formatterSettingsOverride */ -/* { "AblFormatter.blockFormatting": true}*/ +/* { "AblFormatter.blockFormatting": true, +"AblFormatter.forFormatting": false}*/ for each A no-lock: for each B no-lock where A.id = B.id: diff --git a/resources/functionalTests/block/51for-for-block-inner1/target.p b/resources/functionalTests/block/51for-for-block-inner1/target.p index 097c6be..b0383df 100644 --- a/resources/functionalTests/block/51for-for-block-inner1/target.p +++ b/resources/functionalTests/block/51for-for-block-inner1/target.p @@ -1,5 +1,6 @@ /* formatterSettingsOverride */ -/* { "AblFormatter.blockFormatting": true}*/ +/* { "AblFormatter.blockFormatting": true, +"AblFormatter.forFormatting": false}*/ for each A no-lock: for each B no-lock where A.id = B.id: diff --git a/resources/functionalTests/emptyBlock/11for/input.p b/resources/functionalTests/emptyBlock/11for/input.p index e0c04cc..bde39dc 100644 --- a/resources/functionalTests/emptyBlock/11for/input.p +++ b/resources/functionalTests/emptyBlock/11for/input.p @@ -1,5 +1,6 @@ /* formatterSettingsOverride */ -/* { "AblFormatter.emptyBlockFormatting": true}*/ +/* { "AblFormatter.emptyBlockFormatting": true, +"AblFormatter.forFormatting": false}*/ for each Customer by Customer.CreditLimit by Customer.Name: end. diff --git a/resources/functionalTests/emptyBlock/11for/target.p b/resources/functionalTests/emptyBlock/11for/target.p index bfcf75d..c4e7b25 100644 --- a/resources/functionalTests/emptyBlock/11for/target.p +++ b/resources/functionalTests/emptyBlock/11for/target.p @@ -1,5 +1,6 @@ /* formatterSettingsOverride */ -/* { "AblFormatter.emptyBlockFormatting": true}*/ +/* { "AblFormatter.emptyBlockFormatting": true, +"AblFormatter.forFormatting": false}*/ for each Customer by Customer.CreditLimit by Customer.Name: end. diff --git a/src/v2/formatters/for/ForFormatter.ts b/src/v2/formatters/for/ForFormatter.ts index f793c7e..14ca3b9 100644 --- a/src/v2/formatters/for/ForFormatter.ts +++ b/src/v2/formatters/for/ForFormatter.ts @@ -48,7 +48,10 @@ export class ForFormatter extends AFormatter implements IFormatter { node: SyntaxNode, fullText: Readonly ) { - this.startColumn = node.startPosition.column; + this.startColumn = FormatterHelper.getActualStatementIndentation( + node, + fullText + ); this.forBodyValue = this.getForStatementBlock(node, fullText); } From ad4979281d45722b8ab2a57065a90bd8b5397dd8 Mon Sep 17 00:00:00 2001 From: gmickus Date: Fri, 6 Sep 2024 11:48:24 +0300 Subject: [PATCH 07/13] Change block node instead of parent --- src/v2/formatters/block/BlockFormatter.ts | 42 +++++++++++++++-------- src/v2/formatters/for/ForFormatter.ts | 4 +++ 2 files changed, 32 insertions(+), 14 deletions(-) diff --git a/src/v2/formatters/block/BlockFormatter.ts b/src/v2/formatters/block/BlockFormatter.ts index 87fb72b..155be66 100644 --- a/src/v2/formatters/block/BlockFormatter.ts +++ b/src/v2/formatters/block/BlockFormatter.ts @@ -1,6 +1,9 @@ import { SyntaxNode } from "web-tree-sitter"; import { IFormatter } from "../../formatterFramework/IFormatter"; -import { bodyBlockKeywords, SyntaxNodeType } from "../../../model/SyntaxNodeType"; +import { + bodyBlockKeywords, + SyntaxNodeType, +} from "../../../model/SyntaxNodeType"; import { CodeEdit } from "../../model/CodeEdit"; import { FullText } from "../../model/FullText"; import { FormatterHelper } from "../../formatterFramework/FormatterHelper"; @@ -32,6 +35,7 @@ export class BlockFormater extends AFormatter implements IFormatter { node: Readonly, fullText: Readonly ): CodeEdit | CodeEdit[] | undefined { + console.log("found: " + node.type); let indentationEdits: IndentationEdits[] = []; let parent = node.parent; @@ -58,25 +62,32 @@ export class BlockFormater extends AFormatter implements IFormatter { ); const indentationStep = this.settings.tabSize(); - const blockStatementsStartRows = node.children.map( - (node) => - node.startPosition.row + - FormatterHelper.getActualTextRow( - FormatterHelper.getCurrentText(node, fullText), - fullText - ) - ); + console.log("text:\n" + FormatterHelper.getCurrentText(node, fullText)); + const blockStatementsStartRows = node.children + .filter((child) => { + if (child.type === ":") { + return false; + } + return true; + }) + .map( + (child) => + child.startPosition.row + + FormatterHelper.getActualTextRow( + FormatterHelper.getCurrentText(child, fullText), + fullText + ) + ); const codeLines = FormatterHelper.getCurrentText(parent, fullText) .split(fullText.eolDelimiter) - .slice(1, -1); + .slice(0, -1); let n = 0; let lineChangeDelta = 0; codeLines.forEach((codeLine, index) => { - // the first line was removed, so index needs to be incremented - index++; const lineNumber = parent.startPosition.row + index; + console.log("line:\n" + codeLine); // adjust delta if (blockStatementsStartRows[n] === lineNumber) { @@ -127,7 +138,7 @@ export class BlockFormater extends AFormatter implements IFormatter { } return this.getCodeEditsFromIndentationEdits( - parent, + node, fullText, indentationEdits ); @@ -145,6 +156,9 @@ export class BlockFormater extends AFormatter implements IFormatter { fullText ); + console.log("oldText:\n" + text); + console.log("newBlockText:\n" + newText); + return this.getCodeEdit(node, text, newText, fullText); } @@ -197,7 +211,7 @@ export class BlockFormater extends AFormatter implements IFormatter { node.parent?.type === SyntaxNodeType.CaseOtherwiseBranch) ) { return node.parent; - }else if ( + } else if ( node.type === SyntaxNodeType.DoBlock && (node.parent?.type === SyntaxNodeType.ElseIfStatement || node.parent?.type === SyntaxNodeType.ElseStatement) diff --git a/src/v2/formatters/for/ForFormatter.ts b/src/v2/formatters/for/ForFormatter.ts index 14ca3b9..df2aed7 100644 --- a/src/v2/formatters/for/ForFormatter.ts +++ b/src/v2/formatters/for/ForFormatter.ts @@ -66,6 +66,10 @@ export class ForFormatter extends AFormatter implements IFormatter { if (child.type === SyntaxNodeType.Identifier) { alignColumn = this.startColumn + resultString.length; } + console.log("child: " + child.type); + console.log( + "childText: " + FormatterHelper.getCurrentText(child, fullText) + ); resultString = resultString.concat( this.getForExpressionString(child, fullText, alignColumn) ); From 8650ecd43e968be7e1c958fd9cdc6fc15f07dd5e Mon Sep 17 00:00:00 2001 From: gmickus Date: Fri, 6 Sep 2024 13:41:43 +0300 Subject: [PATCH 08/13] Improve for and block formatting --- src/v2/formatterFramework/FormatterHelper.ts | 9 +++++++ src/v2/formatters/block/BlockFormatter.ts | 27 +++++++++++++++++--- src/v2/formatters/for/ForFormatter.ts | 8 +++--- 3 files changed, 36 insertions(+), 8 deletions(-) diff --git a/src/v2/formatterFramework/FormatterHelper.ts b/src/v2/formatterFramework/FormatterHelper.ts index 1874241..6887a56 100644 --- a/src/v2/formatterFramework/FormatterHelper.ts +++ b/src/v2/formatterFramework/FormatterHelper.ts @@ -79,6 +79,15 @@ export class FormatterHelper { return ""; } + public static getBodyText( + node: Readonly, + fullText: Readonly + ): string { + let text = this.getCurrentText(node, fullText); + let firstColonIndex = text.indexOf(":"); + return text.substring(firstColonIndex + 1); + } + public static getCurrentTextMultilineAdjust( node: Readonly, fullText: Readonly, diff --git a/src/v2/formatters/block/BlockFormatter.ts b/src/v2/formatters/block/BlockFormatter.ts index 6867847..9eabea7 100644 --- a/src/v2/formatters/block/BlockFormatter.ts +++ b/src/v2/formatters/block/BlockFormatter.ts @@ -77,14 +77,19 @@ export class BlockFormater extends AFormatter implements IFormatter { ) ); - const codeLines = FormatterHelper.getCurrentText(parent, fullText) - .split(fullText.eolDelimiter) - .slice(0, -1); + console.log( + "Body text:\n " + FormatterHelper.getBodyText(node, fullText) + ); + console.log("blockStatement:\n" + blockStatementsStartRows); + const codeLines = FormatterHelper.getBodyText(node, fullText).split( + fullText.eolDelimiter + ); let n = 0; let lineChangeDelta = 0; codeLines.forEach((codeLine, index) => { - const lineNumber = parent.startPosition.row + index; + const lineNumber = node.startPosition.row + index; + console.log("line nr " + lineNumber + " :\n" + codeLine); // adjust delta if (blockStatementsStartRows[n] === lineNumber) { @@ -96,9 +101,23 @@ export class BlockFormater extends AFormatter implements IFormatter { fullText ); + console.log( + "ind: " + + parentIndentation + + " " + + indentationStep + + " " + + FormatterHelper.getActualTextIndentation( + codeLine, + fullText + ) + ); + n++; } + console.log("myDelta: " + lineChangeDelta); + if (lineChangeDelta !== 0) { indentationEdits.push({ line: index, diff --git a/src/v2/formatters/for/ForFormatter.ts b/src/v2/formatters/for/ForFormatter.ts index df2aed7..7df9505 100644 --- a/src/v2/formatters/for/ForFormatter.ts +++ b/src/v2/formatters/for/ForFormatter.ts @@ -122,10 +122,10 @@ export class ForFormatter extends AFormatter implements IFormatter { ); break; case SyntaxNodeType.Body: - newString = - fullText.eolDelimiter + - " ".repeat(this.startColumn + this.settings.tabSize()) + - FormatterHelper.getCurrentText(node, fullText).trim(); + newString = FormatterHelper.getCurrentText( + node, + fullText + ).trim(); break; default: const text = FormatterHelper.getCurrentText( From e68add4ab7803cdaed6318143f98cb4d392a28b3 Mon Sep 17 00:00:00 2001 From: gmickus Date: Fri, 6 Sep 2024 13:48:33 +0300 Subject: [PATCH 09/13] Update for tests --- resources/functionalTests/for/2each-true/input.p | 4 ++-- resources/functionalTests/for/2each-true/target.p | 2 +- resources/functionalTests/for/3each-each/target.p | 2 +- resources/functionalTests/for/3each-where/target.p | 2 +- resources/functionalTests/for/3first-by/input.p | 2 +- resources/functionalTests/for/3first-by/target.p | 2 +- 6 files changed, 7 insertions(+), 7 deletions(-) diff --git a/resources/functionalTests/for/2each-true/input.p b/resources/functionalTests/for/2each-true/input.p index 9e0bcf5..018ffa2 100644 --- a/resources/functionalTests/for/2each-true/input.p +++ b/resources/functionalTests/for/2each-true/input.p @@ -1,9 +1,9 @@ /* formatterSettingsOverride */ -/* { "AblFormatter.blockFormatting": false, +/* { "AblFormatter.blockFormatting": true, "AblFormatter.forFormatting": true }*/ -for +for each Customer: Customer.var += 1. diff --git a/resources/functionalTests/for/2each-true/target.p b/resources/functionalTests/for/2each-true/target.p index c17ee35..dbbe313 100644 --- a/resources/functionalTests/for/2each-true/target.p +++ b/resources/functionalTests/for/2each-true/target.p @@ -1,5 +1,5 @@ /* formatterSettingsOverride */ -/* { "AblFormatter.blockFormatting": false, +/* { "AblFormatter.blockFormatting": true, "AblFormatter.forFormatting": true }*/ diff --git a/resources/functionalTests/for/3each-each/target.p b/resources/functionalTests/for/3each-each/target.p index e1b1447..12005a1 100644 --- a/resources/functionalTests/for/3each-each/target.p +++ b/resources/functionalTests/for/3each-each/target.p @@ -1,6 +1,6 @@ /* formatterSettingsOverride */ /* { "AblFormatter.forFormatting": true, -"AblFormatter.blockFormatting": false, +"AblFormatter.blockFormatting": true, "abl.completion.upperCase": true}*/ FOR EACH Customer NO-LOCK, diff --git a/resources/functionalTests/for/3each-where/target.p b/resources/functionalTests/for/3each-where/target.p index 241df44..a376627 100644 --- a/resources/functionalTests/for/3each-where/target.p +++ b/resources/functionalTests/for/3each-where/target.p @@ -1,5 +1,5 @@ /* formatterSettingsOverride */ -/* { "AblFormatter.blockFormatting": false, +/* { "AblFormatter.blockFormatting": true, "AblFormatter.forFormatting": true }*/ diff --git a/resources/functionalTests/for/3first-by/input.p b/resources/functionalTests/for/3first-by/input.p index 34adf0a..4a7983a 100644 --- a/resources/functionalTests/for/3first-by/input.p +++ b/resources/functionalTests/for/3first-by/input.p @@ -1,5 +1,5 @@ /* formatterSettingsOverride */ -/* { "AblFormatter.blockFormatting": false, +/* { "AblFormatter.blockFormatting": true, "AblFormatter.forFormatting": true }*/ diff --git a/resources/functionalTests/for/3first-by/target.p b/resources/functionalTests/for/3first-by/target.p index bebe9df..02ca12e 100644 --- a/resources/functionalTests/for/3first-by/target.p +++ b/resources/functionalTests/for/3first-by/target.p @@ -1,5 +1,5 @@ /* formatterSettingsOverride */ -/* { "AblFormatter.blockFormatting": false, +/* { "AblFormatter.blockFormatting": true, "AblFormatter.forFormatting": true }*/ From 3002e9df50c8257521ae1ef081385b5ceedf8769 Mon Sep 17 00:00:00 2001 From: gmickus Date: Mon, 9 Sep 2024 12:11:34 +0300 Subject: [PATCH 10/13] Fix for support --- .../input.p | 2 +- .../target.p | 2 +- .../input.p | 2 +- .../target.p | 2 +- .../input.p | 2 +- .../target.p | 2 +- .../block/45repeat-block-end1/input.p | 2 +- .../functionalTests/emptyBlock/11for/input.p | 3 +- .../functionalTests/emptyBlock/11for/target.p | 7 +- .../functionalTests/for/2each-true/input.p | 2 +- .../functionalTests/for/3each-each/input.p | 2 +- .../functionalTests/for/3each-where/input.p | 11 +- .../functionalTests/for/3each-where/target.p | 2 + .../if/10-then-same-do-same-else-do/input.p | 10 +- .../if/10-then-same-do-same-else-do/target.p | 1 - .../ifFunction/11else-new-line-in-for/input.p | 16 ++ .../11else-new-line-in-for/target.p | 18 ++ .../enableFormatterDecorators.ts | 2 +- src/v2/formatters/block/BlockFormatter.ts | 42 +-- .../emptyblock/EmptyBlockFormatter.ts | 271 ++++++++++++------ 20 files changed, 262 insertions(+), 139 deletions(-) rename resources/functionalTests/block/{12for-block-end2 => 12for-block-end2-disabled}/input.p (72%) rename resources/functionalTests/block/{12for-block-end2 => 12for-block-end2-disabled}/target.p (72%) rename resources/functionalTests/block/{15for-block-inner2 => 15for-block-inner2-disabled}/input.p (71%) rename resources/functionalTests/block/{15for-block-inner2 => 15for-block-inner2-disabled}/target.p (71%) rename resources/functionalTests/block/{17for-block-inner4 => 17for-block-inner4-disabled}/input.p (72%) rename resources/functionalTests/block/{17for-block-inner4 => 17for-block-inner4-disabled}/target.p (72%) create mode 100644 resources/functionalTests/ifFunction/11else-new-line-in-for/input.p create mode 100644 resources/functionalTests/ifFunction/11else-new-line-in-for/target.p diff --git a/resources/functionalTests/block/12for-block-end2/input.p b/resources/functionalTests/block/12for-block-end2-disabled/input.p similarity index 72% rename from resources/functionalTests/block/12for-block-end2/input.p rename to resources/functionalTests/block/12for-block-end2-disabled/input.p index 51b7eb7..fb0925b 100644 --- a/resources/functionalTests/block/12for-block-end2/input.p +++ b/resources/functionalTests/block/12for-block-end2-disabled/input.p @@ -1,5 +1,5 @@ /* formatterSettingsOverride */ -/* { "AblFormatter.blockFormatting": false, +/* { "AblFormatter.emptyBlockFormatting": false, "AblFormatter.forFormatting": false}*/ for each Customer no-lock: diff --git a/resources/functionalTests/block/12for-block-end2/target.p b/resources/functionalTests/block/12for-block-end2-disabled/target.p similarity index 72% rename from resources/functionalTests/block/12for-block-end2/target.p rename to resources/functionalTests/block/12for-block-end2-disabled/target.p index 51b7eb7..fb0925b 100644 --- a/resources/functionalTests/block/12for-block-end2/target.p +++ b/resources/functionalTests/block/12for-block-end2-disabled/target.p @@ -1,5 +1,5 @@ /* formatterSettingsOverride */ -/* { "AblFormatter.blockFormatting": false, +/* { "AblFormatter.emptyBlockFormatting": false, "AblFormatter.forFormatting": false}*/ for each Customer no-lock: diff --git a/resources/functionalTests/block/15for-block-inner2/input.p b/resources/functionalTests/block/15for-block-inner2-disabled/input.p similarity index 71% rename from resources/functionalTests/block/15for-block-inner2/input.p rename to resources/functionalTests/block/15for-block-inner2-disabled/input.p index b3ff775..70700ee 100644 --- a/resources/functionalTests/block/15for-block-inner2/input.p +++ b/resources/functionalTests/block/15for-block-inner2-disabled/input.p @@ -1,5 +1,5 @@ /* formatterSettingsOverride */ -/* { "AblFormatter.blockFormatting": false, +/* { "AblFormatter.emptyBlockFormatting": false, "AblFormatter.forFormatting": false}*/ for each Customer no-lock: diff --git a/resources/functionalTests/block/15for-block-inner2/target.p b/resources/functionalTests/block/15for-block-inner2-disabled/target.p similarity index 71% rename from resources/functionalTests/block/15for-block-inner2/target.p rename to resources/functionalTests/block/15for-block-inner2-disabled/target.p index b3ff775..70700ee 100644 --- a/resources/functionalTests/block/15for-block-inner2/target.p +++ b/resources/functionalTests/block/15for-block-inner2-disabled/target.p @@ -1,5 +1,5 @@ /* formatterSettingsOverride */ -/* { "AblFormatter.blockFormatting": false, +/* { "AblFormatter.emptyBlockFormatting": false, "AblFormatter.forFormatting": false}*/ for each Customer no-lock: diff --git a/resources/functionalTests/block/17for-block-inner4/input.p b/resources/functionalTests/block/17for-block-inner4-disabled/input.p similarity index 72% rename from resources/functionalTests/block/17for-block-inner4/input.p rename to resources/functionalTests/block/17for-block-inner4-disabled/input.p index e021889..d0e8b53 100644 --- a/resources/functionalTests/block/17for-block-inner4/input.p +++ b/resources/functionalTests/block/17for-block-inner4-disabled/input.p @@ -1,5 +1,5 @@ /* formatterSettingsOverride */ -/* { "AblFormatter.blockFormatting": false, +/* { "AblFormatter.emptyBlockFormatting": false, "AblFormatter.forFormatting": false}*/ for each Customer no-lock: diff --git a/resources/functionalTests/block/17for-block-inner4/target.p b/resources/functionalTests/block/17for-block-inner4-disabled/target.p similarity index 72% rename from resources/functionalTests/block/17for-block-inner4/target.p rename to resources/functionalTests/block/17for-block-inner4-disabled/target.p index e021889..d0e8b53 100644 --- a/resources/functionalTests/block/17for-block-inner4/target.p +++ b/resources/functionalTests/block/17for-block-inner4-disabled/target.p @@ -1,5 +1,5 @@ /* formatterSettingsOverride */ -/* { "AblFormatter.blockFormatting": false, +/* { "AblFormatter.emptyBlockFormatting": false, "AblFormatter.forFormatting": false}*/ for each Customer no-lock: diff --git a/resources/functionalTests/block/45repeat-block-end1/input.p b/resources/functionalTests/block/45repeat-block-end1/input.p index 11fc3bd..bac821e 100644 --- a/resources/functionalTests/block/45repeat-block-end1/input.p +++ b/resources/functionalTests/block/45repeat-block-end1/input.p @@ -3,4 +3,4 @@ repeat: define variable a as integer no-undo. - end. \ No newline at end of file + end. \ No newline at end of file diff --git a/resources/functionalTests/emptyBlock/11for/input.p b/resources/functionalTests/emptyBlock/11for/input.p index bde39dc..7d52f5d 100644 --- a/resources/functionalTests/emptyBlock/11for/input.p +++ b/resources/functionalTests/emptyBlock/11for/input.p @@ -1,6 +1,5 @@ /* formatterSettingsOverride */ -/* { "AblFormatter.emptyBlockFormatting": true, -"AblFormatter.forFormatting": false}*/ +/* { "AblFormatter.forFormatting": true}*/ for each Customer by Customer.CreditLimit by Customer.Name: end. diff --git a/resources/functionalTests/emptyBlock/11for/target.p b/resources/functionalTests/emptyBlock/11for/target.p index c4e7b25..f1b51da 100644 --- a/resources/functionalTests/emptyBlock/11for/target.p +++ b/resources/functionalTests/emptyBlock/11for/target.p @@ -1,6 +1,7 @@ /* formatterSettingsOverride */ -/* { "AblFormatter.emptyBlockFormatting": true, -"AblFormatter.forFormatting": false}*/ +/* { "AblFormatter.forFormatting": true}*/ -for each Customer by Customer.CreditLimit by Customer.Name: +for each Customer + by Customer.CreditLimit + by Customer.Name: end. diff --git a/resources/functionalTests/for/2each-true/input.p b/resources/functionalTests/for/2each-true/input.p index 018ffa2..b554fef 100644 --- a/resources/functionalTests/for/2each-true/input.p +++ b/resources/functionalTests/for/2each-true/input.p @@ -6,5 +6,5 @@ for each Customer: - Customer.var += 1. + Customer.var += 1. end. \ No newline at end of file diff --git a/resources/functionalTests/for/3each-each/input.p b/resources/functionalTests/for/3each-each/input.p index 6fd53d6..fe21641 100644 --- a/resources/functionalTests/for/3each-each/input.p +++ b/resources/functionalTests/for/3each-each/input.p @@ -1,6 +1,6 @@ /* formatterSettingsOverride */ /* { "AblFormatter.forFormatting": true, -"AblFormatter.blockFormatting": false, +"AblFormatter.blockFormatting": true, "abl.completion.upperCase": true}*/ FOR EACH Customer NO-LOCK, EACH Order OF Customer NO-LOCK, EACH OrderLine OF Order NO-LOCK BY Order.PromiseDate BY Customer.CustNum BY OrderLine.LineNum: diff --git a/resources/functionalTests/for/3each-where/input.p b/resources/functionalTests/for/3each-where/input.p index 89fdd87..3ea0e31 100644 --- a/resources/functionalTests/for/3each-where/input.p +++ b/resources/functionalTests/for/3each-where/input.p @@ -1,8 +1,11 @@ /* formatterSettingsOverride */ -/* { "AblFormatter.blockFormatting": false, +/* { "AblFormatter.blockFormatting": true, "AblFormatter.forFormatting": true }*/ -for each Customer where Customer.var = 1: - Customer.var += 1. -end. \ No newline at end of file +for each Customer where + Customer.var = 1: + Customer.var += 1. + Customer.var *= 2. + Customer.var /= 3. + end. \ No newline at end of file diff --git a/resources/functionalTests/for/3each-where/target.p b/resources/functionalTests/for/3each-where/target.p index a376627..4ce7747 100644 --- a/resources/functionalTests/for/3each-where/target.p +++ b/resources/functionalTests/for/3each-where/target.p @@ -6,4 +6,6 @@ for each Customer where Customer.var = 1: Customer.var += 1. + Customer.var *= 2. + Customer.var /= 3. end. \ No newline at end of file diff --git a/resources/functionalTests/if/10-then-same-do-same-else-do/input.p b/resources/functionalTests/if/10-then-same-do-same-else-do/input.p index 39b38c1..b6570f1 100644 --- a/resources/functionalTests/if/10-then-same-do-same-else-do/input.p +++ b/resources/functionalTests/if/10-then-same-do-same-else-do/input.p @@ -1,14 +1,12 @@ /* formatterSettingsOverride */ /* { "AblFormatter.ifFormatting": true, -"AblFormatter.blockFormatting": true, "AblFormatter.ifFormattingThenLocation": "Same", "AblFormatter.ifFormattingDoLocation": "Same"}*/ -if a = b then do: +if a = b then do: return a. end. - - else do: - return b. - end. +else do: + return b. + end. diff --git a/resources/functionalTests/if/10-then-same-do-same-else-do/target.p b/resources/functionalTests/if/10-then-same-do-same-else-do/target.p index 50d0f6c..9b81c0f 100644 --- a/resources/functionalTests/if/10-then-same-do-same-else-do/target.p +++ b/resources/functionalTests/if/10-then-same-do-same-else-do/target.p @@ -1,6 +1,5 @@ /* formatterSettingsOverride */ /* { "AblFormatter.ifFormatting": true, -"AblFormatter.blockFormatting": true, "AblFormatter.ifFormattingThenLocation": "Same", "AblFormatter.ifFormattingDoLocation": "Same"}*/ diff --git a/resources/functionalTests/ifFunction/11else-new-line-in-for/input.p b/resources/functionalTests/ifFunction/11else-new-line-in-for/input.p new file mode 100644 index 0000000..5ed6237 --- /dev/null +++ b/resources/functionalTests/ifFunction/11else-new-line-in-for/input.p @@ -0,0 +1,16 @@ +/* formatterSettingsOverride */ +/* { "AblFormatter.ifFunctionFormatting": true, + "AblFormatter.ifFunctionFormattingAddParentheses": "No", + "AblFormatter.ifFunctionFormattingElseLocation": "New"}*/ + +FOR EACH Customer NO-LOCK +BY IF Customer.Balance > 10000 THEN 1 ELSE + IF Customer.Balance > 5000 THEN 1.5 ELSE + IF Customer.Balance > 1000 THEN 2 ELSE + IF Customer.Balance > 500 THEN 2.5 ELSE 3 + BY IF Customer.SalesRep = "John" THEN 1 ELSE + IF Customer.SalesRep = "Jane" THEN 2 ELSE + IF Customer.SalesRep = "Doe" THEN 3 ELSE 4: + DISPLAY Customer.SalesRep Customer.Balance Customer.Name. + END. + \ No newline at end of file diff --git a/resources/functionalTests/ifFunction/11else-new-line-in-for/target.p b/resources/functionalTests/ifFunction/11else-new-line-in-for/target.p new file mode 100644 index 0000000..c64cd6c --- /dev/null +++ b/resources/functionalTests/ifFunction/11else-new-line-in-for/target.p @@ -0,0 +1,18 @@ +/* formatterSettingsOverride */ +/* { "AblFormatter.ifFunctionFormatting": true, + "AblFormatter.ifFunctionFormattingAddParentheses": "No", + "AblFormatter.ifFunctionFormattingElseLocation": "New"}*/ + +FOR EACH Customer NO-LOCK + BY IF Customer.Balance > 10000 THEN 1 + ELSE IF Customer.Balance > 5000 THEN 1.5 + ELSE IF Customer.Balance > 1000 THEN 2 + ELSE IF Customer.Balance > 500 THEN 2.5 + ELSE 3 + BY IF Customer.SalesRep = "John" THEN 1 + ELSE IF Customer.SalesRep = "Jane" THEN 2 + ELSE IF Customer.SalesRep = "Doe" THEN 3 + ELSE 4: + DISPLAY Customer.SalesRep Customer.Balance Customer.Name. +END. + \ No newline at end of file diff --git a/src/v2/formatterFramework/enableFormatterDecorators.ts b/src/v2/formatterFramework/enableFormatterDecorators.ts index 41f9c03..1ef5bd5 100644 --- a/src/v2/formatterFramework/enableFormatterDecorators.ts +++ b/src/v2/formatterFramework/enableFormatterDecorators.ts @@ -13,13 +13,13 @@ import { PropertyFormatter } from "../formatters/property/PropertyFormatter"; // needed just for enabling decorators. Decorators does not work if there is no usage of a class in the reachable code export function enableFormatterDecorators(): void { AssignFormatter; + EmptyBlockFormatter; BlockFormater; IfFormatter; DefineFormatter; UsingFormatter; CaseFormatter; ForFormatter; - EmptyBlockFormatter; TempTableFormatter; PropertyFormatter; IfFunctionFormatter; diff --git a/src/v2/formatters/block/BlockFormatter.ts b/src/v2/formatters/block/BlockFormatter.ts index 9eabea7..8e8ca68 100644 --- a/src/v2/formatters/block/BlockFormatter.ts +++ b/src/v2/formatters/block/BlockFormatter.ts @@ -22,14 +22,17 @@ export class BlockFormater extends AFormatter implements IFormatter { this.settings = new BlockSettings(configurationManager); } - public match(node: Readonly): boolean { - let found: boolean = false; + match(node: Readonly): boolean { + if (!bodyBlockKeywords.hasFancy(node.type, "")) { + return false; + } - if (bodyBlockKeywords.hasFancy(node.type, "")) { - found = true; + let parent = node.parent; + if (parent === null || parent.type === SyntaxNodeType.ForStatement) { + return false; } - return found; + return true; } public parse( node: Readonly, @@ -77,19 +80,14 @@ export class BlockFormater extends AFormatter implements IFormatter { ) ); - console.log( - "Body text:\n " + FormatterHelper.getBodyText(node, fullText) - ); - console.log("blockStatement:\n" + blockStatementsStartRows); - const codeLines = FormatterHelper.getBodyText(node, fullText).split( - fullText.eolDelimiter - ); + const codeLines = FormatterHelper.getCurrentText(parent, fullText) + .split(fullText.eolDelimiter) + .slice(0, -1); let n = 0; let lineChangeDelta = 0; codeLines.forEach((codeLine, index) => { - const lineNumber = node.startPosition.row + index; - console.log("line nr " + lineNumber + " :\n" + codeLine); + const lineNumber = parent.startPosition.row + index; // adjust delta if (blockStatementsStartRows[n] === lineNumber) { @@ -101,23 +99,9 @@ export class BlockFormater extends AFormatter implements IFormatter { fullText ); - console.log( - "ind: " + - parentIndentation + - " " + - indentationStep + - " " + - FormatterHelper.getActualTextIndentation( - codeLine, - fullText - ) - ); - n++; } - console.log("myDelta: " + lineChangeDelta); - if (lineChangeDelta !== 0) { indentationEdits.push({ line: index, @@ -154,7 +138,7 @@ export class BlockFormater extends AFormatter implements IFormatter { } return this.getCodeEditsFromIndentationEdits( - node, + parent, fullText, indentationEdits ); diff --git a/src/v2/formatters/emptyblock/EmptyBlockFormatter.ts b/src/v2/formatters/emptyblock/EmptyBlockFormatter.ts index 1557591..898bab4 100644 --- a/src/v2/formatters/emptyblock/EmptyBlockFormatter.ts +++ b/src/v2/formatters/emptyblock/EmptyBlockFormatter.ts @@ -6,7 +6,10 @@ import { FullText } from "../../model/FullText"; import { AFormatter } from "../AFormatter"; import { EmptyBlockSettings } from "./EmptyBlockSettings"; import { IConfigurationManager } from "../../../utils/IConfigurationManager"; -import { SyntaxNodeType } from "../../../model/SyntaxNodeType"; +import { + bodyBlockKeywords, + SyntaxNodeType, +} from "../../../model/SyntaxNodeType"; import { FormatterHelper } from "../../formatterFramework/FormatterHelper"; @RegisterFormatter @@ -19,129 +22,229 @@ export class EmptyBlockFormatter extends AFormatter implements IFormatter { this.settings = new EmptyBlockSettings(configurationManager); } - match(node: Readonly): boolean { - // Special case for ON DO block - if (node.type === SyntaxNodeType.DoBlock) { - if ( - node.parent !== null && - node.parent.type === SyntaxNodeType.OnStatement - ) { - return false; - } + public match(node: Readonly): boolean { + if (!bodyBlockKeywords.hasFancy(node.type, "")) { + return false; } - // If the node is not a block type, do not match - if ( - node.type !== SyntaxNodeType.DoBlock && - node.type !== SyntaxNodeType.ClassStatement && - node.type !== SyntaxNodeType.CatchStatement && - node.type !== SyntaxNodeType.ConstructorDefinition && - node.type !== SyntaxNodeType.DestructorDefinition && - node.type !== SyntaxNodeType.ForStatement && - node.type !== SyntaxNodeType.FinallyStatement && - node.type !== SyntaxNodeType.OnStatement && - node.type !== SyntaxNodeType.RepeatStatement && - node.type !== SyntaxNodeType.MethodDefinition && - node.type !== SyntaxNodeType.FunctionStatement && - node.type !== SyntaxNodeType.ProcedureStatement && - node.type !== SyntaxNodeType.Getter && - node.type !== SyntaxNodeType.Setter - ) { + let parent = node.parent; + if (parent === null || parent.type !== SyntaxNodeType.ForStatement) { return false; } - // Do not match if there is a body inside the block - for (let child of node.children) { - if ( - child.type === SyntaxNodeType.Body || - child.type === SyntaxNodeType.CaseBody || - child.type === SyntaxNodeType.ClassBody - ) { - return false; - } - } return true; } - parse( + public parse( node: Readonly, fullText: Readonly ): CodeEdit | CodeEdit[] | undefined { - const text = FormatterHelper.getCurrentText(node, fullText); + let indentationEdits: IndentationEdits[] = []; - const statementIndentation = - FormatterHelper.getActualStatementIndentation(node, fullText); + let parent = node.parent; - const lastLine = FormatterHelper.getCurrentText(node, fullText) - .split(fullText.eolDelimiter) - .slice(-1)[0]; + if (parent === null) { + return undefined; + } let formattingOnStatement = false; - // Special case for ON DO block: to find the end keyword, we need to search for it from the start of the DO block - if (node.type === SyntaxNodeType.OnStatement) { - const doNode = node.children.find( - (child) => child.type === SyntaxNodeType.DoBlock - ); - if (doNode === undefined) { - return undefined; + if (parent.type === SyntaxNodeType.DoBlock) { + const grandParent = parent.parent; + if ( + grandParent !== null && + grandParent.type === SyntaxNodeType.OnStatement + ) { + parent = grandParent; + formattingOnStatement = true; } - node = doNode; - formattingOnStatement = true; } - const endNode = node.children.find( - (child) => child.type === SyntaxNodeType.EndKeyword + const parentIndentation = FormatterHelper.getActualStatementIndentation( + this.getParentIndentationSourceNode(parent), + fullText ); - if (endNode === undefined) { - return undefined; - } + const indentationStep = this.settings.tabSize(); + const blockStatementsStartRows = node.children + .filter((child) => { + if (child.type === ":") { + return false; + } + return true; + }) + .map( + (child) => + child.startPosition.row + + FormatterHelper.getActualTextRow( + FormatterHelper.getCurrentText(child, fullText), + fullText + ) + ); + + console.log( + "Body text:\n " + FormatterHelper.getBodyText(node, fullText) + ); + console.log("blockStatement:\n" + blockStatementsStartRows); + const codeLines = FormatterHelper.getBodyText(node, fullText).split( + fullText.eolDelimiter + ); - if (formattingOnStatement) { - // REturn back to ON statement from the DO block - if (node.parent === null) { - return undefined; + let n = 0; + let lineChangeDelta = 0; + codeLines.forEach((codeLine, index) => { + const lineNumber = node.startPosition.row + index; + console.log("line nr " + lineNumber + " :\n" + codeLine); + + // adjust delta + if (blockStatementsStartRows[n] === lineNumber) { + lineChangeDelta = + parentIndentation + + indentationStep - + FormatterHelper.getActualTextIndentation( + codeLine, + fullText + ); + + console.log( + "ind: " + + parentIndentation + + " " + + indentationStep + + " " + + FormatterHelper.getActualTextIndentation( + codeLine, + fullText + ) + ); + + n++; } - node = node.parent; - } - const endRowDelta = - statementIndentation - - FormatterHelper.getActualTextIndentation(lastLine, fullText); + console.log("myDelta: " + lineChangeDelta); - const indentationEdit: IndentationEdits = { - line: node.endPosition.row - node.startPosition.row, - lineChangeDelta: endRowDelta, - }; + if (lineChangeDelta !== 0) { + indentationEdits.push({ + line: index, + lineChangeDelta: lineChangeDelta, + }); + } + }); - const newText = this.applyIndentationEdit( + 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 + ); + + console.log("endDelta: " + endRowDelta); + + if (endRowDelta !== 0) { + indentationEdits.push({ + line: parent.endPosition.row - parent.startPosition.row, + lineChangeDelta: endRowDelta, + }); + } + } + } + + return this.getCodeEditsFromIndentationEdits( + node, + fullText, + indentationEdits + ); + } + + private getCodeEditsFromIndentationEdits( + node: SyntaxNode, + fullText: FullText, + indentationEdits: IndentationEdits[] + ): CodeEdit | CodeEdit[] | undefined { + const text = FormatterHelper.getCurrentText(node, fullText); + const newText = this.applyIndentationEdits( text, - indentationEdit, + indentationEdits, fullText ); + console.log("oldText:\n" + text); + console.log("text:\n" + newText); + return this.getCodeEdit(node, text, newText, fullText); } - private applyIndentationEdit( + private applyIndentationEdits( code: string, - indentationEdit: IndentationEdits, + edits: IndentationEdits[], fullText: FullText ): string { + // Split the code into lines const lines = code.split(fullText.eolDelimiter); - const currentLeadingSpaces = - RegExp(/^\s*/).exec(lines[indentationEdit.line])?.[0].length || 0; - const newLeadingSpaces = Math.max( - 0, - currentLeadingSpaces + indentationEdit.lineChangeDelta - ); + // Apply each edit + edits.forEach((edit) => { + const { line, lineChangeDelta } = edit; - lines[indentationEdit.line] = - " ".repeat(newLeadingSpaces) + - lines[indentationEdit.line].trimStart(); + // Ensure the line number is within the range + if (line >= 0 && line < lines.length) { + const currentLine = lines[line]; + // Count current leading spaces + const currentLeadingSpaces = + RegExp(/^\s*/).exec(currentLine)?.[0].length || 0; + // Calculate new indentation + const newLeadingSpaces = Math.max( + 0, + currentLeadingSpaces + lineChangeDelta + ); + + // Update the line with the new indentation + + lines[line] = + " ".repeat(newLeadingSpaces) + currentLine.trimStart(); + } + }); + + // Join the lines back into a single string return lines.join(fullText.eolDelimiter); } + + //refactor + private getParentIndentationSourceNode(node: SyntaxNode): SyntaxNode { + if ( + node.type === SyntaxNodeType.DoBlock && + node.parent?.type === SyntaxNodeType.IfStatement + ) { + return node.parent; + } else if ( + node.type === SyntaxNodeType.DoBlock && + (node.parent?.type === SyntaxNodeType.CaseWhenBranch || + node.parent?.type === SyntaxNodeType.CaseOtherwiseBranch) + ) { + return node.parent; + } else if ( + node.type === SyntaxNodeType.DoBlock && + (node.parent?.type === SyntaxNodeType.ElseIfStatement || + node.parent?.type === SyntaxNodeType.ElseStatement) + ) { + if (node.parent.parent === null) { + return node.parent; + } + + return node.parent.parent; + } + return node; + } } interface IndentationEdits { From cbecc7068b23a6e8c44289ec89d2e936c351d6b4 Mon Sep 17 00:00:00 2001 From: gmickus Date: Mon, 9 Sep 2024 12:46:08 +0300 Subject: [PATCH 11/13] Remove comments ; update package.json --- package.json | 2 +- .../emptyblock/EmptyBlockFormatter.ts | 24 ------------------- 2 files changed, 1 insertion(+), 25 deletions(-) diff --git a/package.json b/package.json index ffa4d8e..f904192 100644 --- a/package.json +++ b/package.json @@ -201,7 +201,7 @@ "AblFormatter.ifFunctionFormattingAddParentheses": { "order": 1201, "type": "string", - "default": "Yes", + "default": "No", "enum": [ "Yes", "No" diff --git a/src/v2/formatters/emptyblock/EmptyBlockFormatter.ts b/src/v2/formatters/emptyblock/EmptyBlockFormatter.ts index 898bab4..b134dcc 100644 --- a/src/v2/formatters/emptyblock/EmptyBlockFormatter.ts +++ b/src/v2/formatters/emptyblock/EmptyBlockFormatter.ts @@ -80,10 +80,6 @@ export class EmptyBlockFormatter extends AFormatter implements IFormatter { ) ); - console.log( - "Body text:\n " + FormatterHelper.getBodyText(node, fullText) - ); - console.log("blockStatement:\n" + blockStatementsStartRows); const codeLines = FormatterHelper.getBodyText(node, fullText).split( fullText.eolDelimiter ); @@ -92,7 +88,6 @@ export class EmptyBlockFormatter extends AFormatter implements IFormatter { let lineChangeDelta = 0; codeLines.forEach((codeLine, index) => { const lineNumber = node.startPosition.row + index; - console.log("line nr " + lineNumber + " :\n" + codeLine); // adjust delta if (blockStatementsStartRows[n] === lineNumber) { @@ -104,23 +99,9 @@ export class EmptyBlockFormatter extends AFormatter implements IFormatter { fullText ); - console.log( - "ind: " + - parentIndentation + - " " + - indentationStep + - " " + - FormatterHelper.getActualTextIndentation( - codeLine, - fullText - ) - ); - n++; } - console.log("myDelta: " + lineChangeDelta); - if (lineChangeDelta !== 0) { indentationEdits.push({ line: index, @@ -147,8 +128,6 @@ export class EmptyBlockFormatter extends AFormatter implements IFormatter { fullText ); - console.log("endDelta: " + endRowDelta); - if (endRowDelta !== 0) { indentationEdits.push({ line: parent.endPosition.row - parent.startPosition.row, @@ -177,9 +156,6 @@ export class EmptyBlockFormatter extends AFormatter implements IFormatter { fullText ); - console.log("oldText:\n" + text); - console.log("text:\n" + newText); - return this.getCodeEdit(node, text, newText, fullText); } From fe4f6ac231f314313780eb3af2c4ba4d1afc4ed9 Mon Sep 17 00:00:00 2001 From: gmickus Date: Mon, 9 Sep 2024 14:27:57 +0300 Subject: [PATCH 12/13] Improve for formatting --- .../functionalTests/for/3each-each/input.p | 2 +- .../ifFunction/10else-new-line-in-for/input.p | 1 + .../10else-new-line-in-for/target.p | 9 +- .../ifFunction/11else-new-line-in-for/input.p | 2 +- .../11else-new-line-in-for/target.p | 20 ++-- .../ifFunction/12else-new-line-in-for/input.p | 16 ++++ .../12else-new-line-in-for/target.p | 18 ++++ .../target.p | 8 +- src/model/SyntaxNodeType.ts | 2 + src/v2/formatters/for/ForFormatter.ts | 93 ++++++++++++++++++- 10 files changed, 149 insertions(+), 22 deletions(-) create mode 100644 resources/functionalTests/ifFunction/12else-new-line-in-for/input.p create mode 100644 resources/functionalTests/ifFunction/12else-new-line-in-for/target.p diff --git a/resources/functionalTests/for/3each-each/input.p b/resources/functionalTests/for/3each-each/input.p index fe21641..b16ddb7 100644 --- a/resources/functionalTests/for/3each-each/input.p +++ b/resources/functionalTests/for/3each-each/input.p @@ -4,5 +4,5 @@ "abl.completion.upperCase": true}*/ FOR EACH Customer NO-LOCK, EACH Order OF Customer NO-LOCK, EACH OrderLine OF Order NO-LOCK BY Order.PromiseDate BY Customer.CustNum BY OrderLine.LineNum: - DISPLAY Order.PromiseDate. +DISPLAY Order.PromiseDate. END. \ No newline at end of file diff --git a/resources/functionalTests/ifFunction/10else-new-line-in-for/input.p b/resources/functionalTests/ifFunction/10else-new-line-in-for/input.p index 1a825bb..0642d9c 100644 --- a/resources/functionalTests/ifFunction/10else-new-line-in-for/input.p +++ b/resources/functionalTests/ifFunction/10else-new-line-in-for/input.p @@ -1,5 +1,6 @@ /* formatterSettingsOverride */ /* { "AblFormatter.ifFunctionFormatting": true, + "AblFormatter.forFormatting": true, "AblFormatter.ifFunctionFormattingAddParentheses": "No", "AblFormatter.ifFunctionFormattingElseLocation": "New"}*/ diff --git a/resources/functionalTests/ifFunction/10else-new-line-in-for/target.p b/resources/functionalTests/ifFunction/10else-new-line-in-for/target.p index 2830c70..a617cd7 100644 --- a/resources/functionalTests/ifFunction/10else-new-line-in-for/target.p +++ b/resources/functionalTests/ifFunction/10else-new-line-in-for/target.p @@ -1,10 +1,13 @@ /* formatterSettingsOverride */ /* { "AblFormatter.ifFunctionFormatting": true, + "AblFormatter.forFormatting": true, "AblFormatter.ifFunctionFormattingAddParentheses": "No", "AblFormatter.ifFunctionFormattingElseLocation": "New"}*/ -FOR EACH Customer NO-LOCK BY IF Customer.Balance > 10000 THEN 1 - ELSE (IF Customer.Balance > 1000 THEN 2 - ELSE 3) BY Customer.SalesRep: +FOR EACH Customer NO-LOCK + BY IF Customer.Balance > 10000 THEN 1 + ELSE (IF Customer.Balance > 1000 THEN 2 + ELSE 3) + BY Customer.SalesRep: DISPLAY Customer.SalesRep Customer.Balance Customer.Name. END. \ No newline at end of file diff --git a/resources/functionalTests/ifFunction/11else-new-line-in-for/input.p b/resources/functionalTests/ifFunction/11else-new-line-in-for/input.p index 5ed6237..a264da0 100644 --- a/resources/functionalTests/ifFunction/11else-new-line-in-for/input.p +++ b/resources/functionalTests/ifFunction/11else-new-line-in-for/input.p @@ -1,6 +1,6 @@ /* formatterSettingsOverride */ /* { "AblFormatter.ifFunctionFormatting": true, - "AblFormatter.ifFunctionFormattingAddParentheses": "No", + "AblFormatter.ifFunctionFormattingAddParentheses": "Yes", "AblFormatter.ifFunctionFormattingElseLocation": "New"}*/ FOR EACH Customer NO-LOCK diff --git a/resources/functionalTests/ifFunction/11else-new-line-in-for/target.p b/resources/functionalTests/ifFunction/11else-new-line-in-for/target.p index c64cd6c..0e156bf 100644 --- a/resources/functionalTests/ifFunction/11else-new-line-in-for/target.p +++ b/resources/functionalTests/ifFunction/11else-new-line-in-for/target.p @@ -1,18 +1,18 @@ /* formatterSettingsOverride */ /* { "AblFormatter.ifFunctionFormatting": true, - "AblFormatter.ifFunctionFormattingAddParentheses": "No", + "AblFormatter.ifFunctionFormattingAddParentheses": "Yes", "AblFormatter.ifFunctionFormattingElseLocation": "New"}*/ FOR EACH Customer NO-LOCK - BY IF Customer.Balance > 10000 THEN 1 - ELSE IF Customer.Balance > 5000 THEN 1.5 - ELSE IF Customer.Balance > 1000 THEN 2 - ELSE IF Customer.Balance > 500 THEN 2.5 - ELSE 3 - BY IF Customer.SalesRep = "John" THEN 1 - ELSE IF Customer.SalesRep = "Jane" THEN 2 - ELSE IF Customer.SalesRep = "Doe" THEN 3 - ELSE 4: + BY (IF Customer.Balance > 10000 THEN 1 + ELSE (IF Customer.Balance > 5000 THEN 1.5 + ELSE (IF Customer.Balance > 1000 THEN 2 + ELSE (IF Customer.Balance > 500 THEN 2.5 + ELSE 3)))) + BY (IF Customer.SalesRep = "John" THEN 1 + ELSE (IF Customer.SalesRep = "Jane" THEN 2 + ELSE (IF Customer.SalesRep = "Doe" THEN 3 + ELSE 4))): DISPLAY Customer.SalesRep Customer.Balance Customer.Name. END. \ No newline at end of file diff --git a/resources/functionalTests/ifFunction/12else-new-line-in-for/input.p b/resources/functionalTests/ifFunction/12else-new-line-in-for/input.p new file mode 100644 index 0000000..a9cdd45 --- /dev/null +++ b/resources/functionalTests/ifFunction/12else-new-line-in-for/input.p @@ -0,0 +1,16 @@ +/* formatterSettingsOverride */ +/* { "AblFormatter.ifFunctionFormatting": true, + "AblFormatter.ifFunctionFormattingAddParentheses": "No", + "AblFormatter.ifFunctionFormattingElseLocation": "New"}*/ + +FOR EACH Customer +BY IF Customer.Balance > 10000 THEN 1 ELSE + IF Customer.Balance > 5000 THEN 1.5 ELSE + IF Customer.Balance > 1000 THEN 2 ELSE + IF Customer.Balance > 500 THEN 2.5 ELSE 3 + BY IF Customer.SalesRep = "John" THEN 1 ELSE + IF Customer.SalesRep = "Jane" THEN 2 ELSE + IF Customer.SalesRep = "Doe" THEN 3 ELSE 4 NO-LOCK: + DISPLAY Customer.SalesRep Customer.Balance Customer.Name. + END. + \ No newline at end of file diff --git a/resources/functionalTests/ifFunction/12else-new-line-in-for/target.p b/resources/functionalTests/ifFunction/12else-new-line-in-for/target.p new file mode 100644 index 0000000..8874b16 --- /dev/null +++ b/resources/functionalTests/ifFunction/12else-new-line-in-for/target.p @@ -0,0 +1,18 @@ +/* formatterSettingsOverride */ +/* { "AblFormatter.ifFunctionFormatting": true, + "AblFormatter.ifFunctionFormattingAddParentheses": "No", + "AblFormatter.ifFunctionFormattingElseLocation": "New"}*/ + +FOR EACH Customer + BY IF Customer.Balance > 10000 THEN 1 + ELSE IF Customer.Balance > 5000 THEN 1.5 + ELSE IF Customer.Balance > 1000 THEN 2 + ELSE IF Customer.Balance > 500 THEN 2.5 + ELSE 3 + BY IF Customer.SalesRep = "John" THEN 1 + ELSE IF Customer.SalesRep = "Jane" THEN 2 + ELSE IF Customer.SalesRep = "Doe" THEN 3 + ELSE 4 NO-LOCK: + DISPLAY Customer.SalesRep Customer.Balance Customer.Name. +END. + \ No newline at end of file diff --git a/resources/functionalTests/ifFunction/7parentheses-else-new-line-in-for/target.p b/resources/functionalTests/ifFunction/7parentheses-else-new-line-in-for/target.p index 44acdd9..c3afc5d 100644 --- a/resources/functionalTests/ifFunction/7parentheses-else-new-line-in-for/target.p +++ b/resources/functionalTests/ifFunction/7parentheses-else-new-line-in-for/target.p @@ -3,8 +3,10 @@ "AblFormatter.ifFunctionFormattingAddParentheses": "Yes", "AblFormatter.ifFunctionFormattingElseLocation": "New"}*/ -FOR EACH Customer NO-LOCK BY (IF Customer.Balance > 10000 THEN 1 - ELSE (IF Customer.Balance > 1000 THEN 2 - ELSE 3)) BY Customer.SalesRep: +FOR EACH Customer NO-LOCK + BY (IF Customer.Balance > 10000 THEN 1 + ELSE (IF Customer.Balance > 1000 THEN 2 + ELSE 3)) + BY Customer.SalesRep: DISPLAY Customer.SalesRep Customer.Balance Customer.Name. END. \ No newline at end of file diff --git a/src/model/SyntaxNodeType.ts b/src/model/SyntaxNodeType.ts index d9bce30..c67cd04 100644 --- a/src/model/SyntaxNodeType.ts +++ b/src/model/SyntaxNodeType.ts @@ -37,6 +37,7 @@ export enum SyntaxNodeType { ForStatement = "for_statement", QueryTuning = "query_tuning", SortClause = "sort_clause", + SortColumn = "sort_column", ComparisonExpression = "comparison_expression", TernaryExpression = "ternary_expression", ParenthesizedExpression = "parenthesized_expression", @@ -59,6 +60,7 @@ export enum SyntaxNodeType { // keywords WhenKeyword = "WHEN", + ByKeyword = "BY", ThenKeyword = "THEN", ElseKeyword = "ELSE", AndKeyword = "AND", diff --git a/src/v2/formatters/for/ForFormatter.ts b/src/v2/formatters/for/ForFormatter.ts index 7df9505..d50efe2 100644 --- a/src/v2/formatters/for/ForFormatter.ts +++ b/src/v2/formatters/for/ForFormatter.ts @@ -111,8 +111,8 @@ export class ForFormatter extends AFormatter implements IFormatter { case SyntaxNodeType.SortClause: newString = fullText.eolDelimiter + - " ".repeat(alignColumn + 1) + - FormatterHelper.getCurrentText(node, fullText).trim(); + " ".repeat(alignColumn) + + this.getSortClauseText(node, fullText, alignColumn); break; case SyntaxNodeType.ForPhrase: newString = this.getSortClauseBlock( @@ -171,6 +171,91 @@ export class ForFormatter extends AFormatter implements IFormatter { return resultString; } + private getSortClauseText( + node: SyntaxNode, + fullText: Readonly, + alignColumn: number + ): string { + let resultString = ""; + + node.children.forEach((child) => { + resultString = resultString.concat( + this.getSortClauseChildText(child, fullText, alignColumn) + ); + }); + + console.log("sortClauseText:\n" + resultString); + console.log( + "sortClauseNode:\n" + FormatterHelper.getCurrentText(node, fullText) + ); + return resultString; + } + + private getSortClauseChildText( + node: SyntaxNode, + fullText: Readonly, + alignColumn: number + ): string { + let newString = ""; + + switch (node.type) { + case SyntaxNodeType.SortColumn: + newString = ""; + node.children.forEach((child) => { + newString = newString.concat( + this.getSortColumnChildText( + child, + fullText, + alignColumn + ) + ); + }); + break; + default: + const text = FormatterHelper.getCurrentText( + node, + fullText + ).trim(); + newString = text.length === 0 ? "" : " " + text; + break; + } + + console.log("sortClauseType:\n" + node.type); + console.log("sortClausePart:\n" + newString); + return newString; + } + + private getSortColumnChildText( + node: SyntaxNode, + fullText: Readonly, + alignColumn: number + ): string { + let newString = ""; + + switch (node.type) { + case SyntaxNodeType.TernaryExpression: + newString = + " " + + FormatterHelper.addIndentation( + FormatterHelper.getCurrentText(node, fullText).trim(), + -node.startPosition.column + alignColumn + 3, // this assumes that if the sort column contains a ternary expression, then it does not contain anything else + fullText.eolDelimiter + ).trim(); + break; + default: + const text = FormatterHelper.getCurrentText( + node, + fullText + ).trim(); + newString = text.length === 0 ? "" : " " + text; + break; + } + + console.log("sortColumnType:\n" + node.type); + console.log("sortColumnPart:\n" + newString); + return newString; + } + private getSortClauseBlock( node: SyntaxNode, fullText: Readonly, @@ -190,8 +275,8 @@ export class ForFormatter extends AFormatter implements IFormatter { case SyntaxNodeType.SortClause: resultString = resultString.concat( fullText.eolDelimiter, - " ".repeat(alignColumn + 1), - FormatterHelper.getCurrentText(child, fullText).trim() + " ".repeat(alignColumn), + this.getSortClauseText(child, fullText, alignColumn) ); break; default: From 92baa5a56855e98dd3c1443b29933b6b66ac8004 Mon Sep 17 00:00:00 2001 From: gmickus Date: Mon, 9 Sep 2024 17:04:34 +0300 Subject: [PATCH 13/13] Refactor empty block formatter to body formatter --- package.json | 4 ++-- .../block/12for-block-end2-disabled/input.p | 2 +- .../block/12for-block-end2-disabled/target.p | 2 +- .../block/15for-block-inner2-disabled/input.p | 2 +- .../15for-block-inner2-disabled/target.p | 2 +- .../block/17for-block-inner4-disabled/input.p | 2 +- .../17for-block-inner4-disabled/target.p | 2 +- .../emptyBlock/10finally/input.p | 2 +- .../emptyBlock/10finally/target.p | 2 +- .../emptyBlock/12function/input.p | 2 +- .../emptyBlock/12function/target.p | 2 +- .../emptyBlock/13method/input.p | 2 +- .../emptyBlock/13method/target.p | 2 +- .../emptyBlock/14on_do/input.p | 2 +- .../emptyBlock/14on_do/target.p | 2 +- .../functionalTests/emptyBlock/15on1/input.p | 2 +- .../functionalTests/emptyBlock/15on1/target.p | 2 +- .../emptyBlock/16procedure/input.p | 2 +- .../emptyBlock/16procedure/target.p | 2 +- .../emptyBlock/17repeat/input.p | 2 +- .../emptyBlock/17repeat/target.p | 2 +- .../functionalTests/emptyBlock/1catch/input.p | 2 +- .../emptyBlock/1catch/target.p | 2 +- .../functionalTests/emptyBlock/2class/input.p | 2 +- .../emptyBlock/2class/target.p | 2 +- .../emptyBlock/3constructor/input.p | 2 +- .../emptyBlock/3constructor/target.p | 2 +- .../emptyBlock/4destructor/input.p | 2 +- .../emptyBlock/4destructor/target.p | 2 +- .../functionalTests/emptyBlock/5do/input.p | 2 +- .../functionalTests/emptyBlock/5do/target.p | 2 +- .../functionalTests/emptyBlock/6do_on/input.p | 2 +- .../emptyBlock/6do_on/target.p | 2 +- .../functionalTests/emptyBlock/7do_to/input.p | 2 +- .../emptyBlock/7do_to/target.p | 2 +- .../emptyBlock/8do_transaction/input.p | 2 +- .../emptyBlock/8do_transaction/target.p | 2 +- .../emptyBlock/9do_while/input.p | 2 +- .../emptyBlock/9do_while/target.p | 2 +- .../functionalTests/for/3each-where/input.p | 4 +--- .../functionalTests/for/3each-where/target.p | 4 +--- .../functionalTests/for/3first-by/input.p | 4 +--- .../functionalTests/for/3first-by/target.p | 4 +--- .../for/5each-break-by/input.p | 6 ++++++ .../for/5each-break-by/target.p | 8 ++++++++ .../functionalTests/for/6each-nested/input.p | 15 ++++++++++++++ .../functionalTests/for/6each-nested/target.p | 18 +++++++++++++++++ .../functionalTests/for/7each-complex/input.p | 20 +++++++++++++++++++ .../for/7each-complex/target.p | 20 +++++++++++++++++++ .../enableFormatterDecorators.ts | 4 ++-- .../BodyFormatter.ts} | 10 +++++----- src/v2/formatters/body/BodySettings.ts | 8 ++++++++ .../emptyblock/EmptyBlockSettings.ts | 8 -------- src/v2/formatters/for/ForFormatter.ts | 12 ----------- 54 files changed, 146 insertions(+), 79 deletions(-) create mode 100644 resources/functionalTests/for/5each-break-by/input.p create mode 100644 resources/functionalTests/for/5each-break-by/target.p create mode 100644 resources/functionalTests/for/6each-nested/input.p create mode 100644 resources/functionalTests/for/6each-nested/target.p create mode 100644 resources/functionalTests/for/7each-complex/input.p create mode 100644 resources/functionalTests/for/7each-complex/target.p rename src/v2/formatters/{emptyblock/EmptyBlockFormatter.ts => body/BodyFormatter.ts} (92%) create mode 100644 src/v2/formatters/body/BodySettings.ts delete mode 100644 src/v2/formatters/emptyblock/EmptyBlockSettings.ts diff --git a/package.json b/package.json index f904192..e4a5f50 100644 --- a/package.json +++ b/package.json @@ -180,11 +180,11 @@ "default": "true", "description": "Enable USING formatting" }, - "AblFormatter.emptyBlockFormatting": { + "AblFormatter.bodyFormatting": { "order": 1000, "type": "boolean", "default": "true", - "description": "Enable EMPTY BLOCK formatting" + "description": "Enable BODY formatting" }, "AblFormatter.propertyFormatting": { "order": 1100, diff --git a/resources/functionalTests/block/12for-block-end2-disabled/input.p b/resources/functionalTests/block/12for-block-end2-disabled/input.p index fb0925b..37f0082 100644 --- a/resources/functionalTests/block/12for-block-end2-disabled/input.p +++ b/resources/functionalTests/block/12for-block-end2-disabled/input.p @@ -1,5 +1,5 @@ /* formatterSettingsOverride */ -/* { "AblFormatter.emptyBlockFormatting": false, +/* { "AblFormatter.bodyFormatting": false, "AblFormatter.forFormatting": false}*/ for each Customer no-lock: diff --git a/resources/functionalTests/block/12for-block-end2-disabled/target.p b/resources/functionalTests/block/12for-block-end2-disabled/target.p index fb0925b..37f0082 100644 --- a/resources/functionalTests/block/12for-block-end2-disabled/target.p +++ b/resources/functionalTests/block/12for-block-end2-disabled/target.p @@ -1,5 +1,5 @@ /* formatterSettingsOverride */ -/* { "AblFormatter.emptyBlockFormatting": false, +/* { "AblFormatter.bodyFormatting": false, "AblFormatter.forFormatting": false}*/ for each Customer no-lock: diff --git a/resources/functionalTests/block/15for-block-inner2-disabled/input.p b/resources/functionalTests/block/15for-block-inner2-disabled/input.p index 70700ee..cc20aad 100644 --- a/resources/functionalTests/block/15for-block-inner2-disabled/input.p +++ b/resources/functionalTests/block/15for-block-inner2-disabled/input.p @@ -1,5 +1,5 @@ /* formatterSettingsOverride */ -/* { "AblFormatter.emptyBlockFormatting": false, +/* { "AblFormatter.bodyFormatting": false, "AblFormatter.forFormatting": false}*/ for each Customer no-lock: diff --git a/resources/functionalTests/block/15for-block-inner2-disabled/target.p b/resources/functionalTests/block/15for-block-inner2-disabled/target.p index 70700ee..cc20aad 100644 --- a/resources/functionalTests/block/15for-block-inner2-disabled/target.p +++ b/resources/functionalTests/block/15for-block-inner2-disabled/target.p @@ -1,5 +1,5 @@ /* formatterSettingsOverride */ -/* { "AblFormatter.emptyBlockFormatting": false, +/* { "AblFormatter.bodyFormatting": false, "AblFormatter.forFormatting": false}*/ for each Customer no-lock: diff --git a/resources/functionalTests/block/17for-block-inner4-disabled/input.p b/resources/functionalTests/block/17for-block-inner4-disabled/input.p index d0e8b53..28b0aee 100644 --- a/resources/functionalTests/block/17for-block-inner4-disabled/input.p +++ b/resources/functionalTests/block/17for-block-inner4-disabled/input.p @@ -1,5 +1,5 @@ /* formatterSettingsOverride */ -/* { "AblFormatter.emptyBlockFormatting": false, +/* { "AblFormatter.bodyFormatting": false, "AblFormatter.forFormatting": false}*/ for each Customer no-lock: diff --git a/resources/functionalTests/block/17for-block-inner4-disabled/target.p b/resources/functionalTests/block/17for-block-inner4-disabled/target.p index d0e8b53..28b0aee 100644 --- a/resources/functionalTests/block/17for-block-inner4-disabled/target.p +++ b/resources/functionalTests/block/17for-block-inner4-disabled/target.p @@ -1,5 +1,5 @@ /* formatterSettingsOverride */ -/* { "AblFormatter.emptyBlockFormatting": false, +/* { "AblFormatter.bodyFormatting": false, "AblFormatter.forFormatting": false}*/ for each Customer no-lock: diff --git a/resources/functionalTests/emptyBlock/10finally/input.p b/resources/functionalTests/emptyBlock/10finally/input.p index 64c1449..325f871 100644 --- a/resources/functionalTests/emptyBlock/10finally/input.p +++ b/resources/functionalTests/emptyBlock/10finally/input.p @@ -1,5 +1,5 @@ /* formatterSettingsOverride */ -/* { "AblFormatter.emptyBlockFormatting": true}*/ +/* { "AblFormatter.blockFormatting": true}*/ finally: end finally. \ No newline at end of file diff --git a/resources/functionalTests/emptyBlock/10finally/target.p b/resources/functionalTests/emptyBlock/10finally/target.p index c7ec459..2e4fe78 100644 --- a/resources/functionalTests/emptyBlock/10finally/target.p +++ b/resources/functionalTests/emptyBlock/10finally/target.p @@ -1,5 +1,5 @@ /* formatterSettingsOverride */ -/* { "AblFormatter.emptyBlockFormatting": true}*/ +/* { "AblFormatter.blockFormatting": true}*/ finally: end finally. \ No newline at end of file diff --git a/resources/functionalTests/emptyBlock/12function/input.p b/resources/functionalTests/emptyBlock/12function/input.p index 2ffe1fd..f00916f 100644 --- a/resources/functionalTests/emptyBlock/12function/input.p +++ b/resources/functionalTests/emptyBlock/12function/input.p @@ -1,5 +1,5 @@ /* formatterSettingsOverride */ -/* { "AblFormatter.emptyBlockFormatting": true}*/ +/* { "AblFormatter.blockFormatting": true}*/ function fun returns integer (): diff --git a/resources/functionalTests/emptyBlock/12function/target.p b/resources/functionalTests/emptyBlock/12function/target.p index 499c361..604749c 100644 --- a/resources/functionalTests/emptyBlock/12function/target.p +++ b/resources/functionalTests/emptyBlock/12function/target.p @@ -1,5 +1,5 @@ /* formatterSettingsOverride */ -/* { "AblFormatter.emptyBlockFormatting": true}*/ +/* { "AblFormatter.blockFormatting": true}*/ function fun returns integer (): diff --git a/resources/functionalTests/emptyBlock/13method/input.p b/resources/functionalTests/emptyBlock/13method/input.p index 90f78bd..220ad1a 100644 --- a/resources/functionalTests/emptyBlock/13method/input.p +++ b/resources/functionalTests/emptyBlock/13method/input.p @@ -1,5 +1,5 @@ /* formatterSettingsOverride */ -/* { "AblFormatter.emptyBlockFormatting": true}*/ +/* { "AblFormatter.blockFormatting": true}*/ class Class final: method public void mt(): diff --git a/resources/functionalTests/emptyBlock/13method/target.p b/resources/functionalTests/emptyBlock/13method/target.p index 7f8170c..35f1604 100644 --- a/resources/functionalTests/emptyBlock/13method/target.p +++ b/resources/functionalTests/emptyBlock/13method/target.p @@ -1,5 +1,5 @@ /* formatterSettingsOverride */ -/* { "AblFormatter.emptyBlockFormatting": true}*/ +/* { "AblFormatter.blockFormatting": true}*/ class Class final: method public void mt(): diff --git a/resources/functionalTests/emptyBlock/14on_do/input.p b/resources/functionalTests/emptyBlock/14on_do/input.p index ba76ea1..50709d3 100644 --- a/resources/functionalTests/emptyBlock/14on_do/input.p +++ b/resources/functionalTests/emptyBlock/14on_do/input.p @@ -1,5 +1,5 @@ /* formatterSettingsOverride */ -/* { "AblFormatter.emptyBlockFormatting": true}*/ +/* { "AblFormatter.blockFormatting": true}*/ on choose of b_next, b_prev do: end. \ No newline at end of file diff --git a/resources/functionalTests/emptyBlock/14on_do/target.p b/resources/functionalTests/emptyBlock/14on_do/target.p index 218d2f0..b59acec 100644 --- a/resources/functionalTests/emptyBlock/14on_do/target.p +++ b/resources/functionalTests/emptyBlock/14on_do/target.p @@ -1,5 +1,5 @@ /* formatterSettingsOverride */ -/* { "AblFormatter.emptyBlockFormatting": true}*/ +/* { "AblFormatter.blockFormatting": true}*/ on choose of b_next, b_prev do: end. \ No newline at end of file diff --git a/resources/functionalTests/emptyBlock/15on1/input.p b/resources/functionalTests/emptyBlock/15on1/input.p index d111088..84fd9cf 100644 --- a/resources/functionalTests/emptyBlock/15on1/input.p +++ b/resources/functionalTests/emptyBlock/15on1/input.p @@ -1,5 +1,5 @@ /* formatterSettingsOverride */ -/* { "AblFormatter.emptyBlockFormatting": true}*/ +/* { "AblFormatter.blockFormatting": true}*/ ON F1 GO. /* F1 will now perform the GO function */ ON F2 HELP. /* F2 will now perform the HELP function */ diff --git a/resources/functionalTests/emptyBlock/15on1/target.p b/resources/functionalTests/emptyBlock/15on1/target.p index d111088..84fd9cf 100644 --- a/resources/functionalTests/emptyBlock/15on1/target.p +++ b/resources/functionalTests/emptyBlock/15on1/target.p @@ -1,5 +1,5 @@ /* formatterSettingsOverride */ -/* { "AblFormatter.emptyBlockFormatting": true}*/ +/* { "AblFormatter.blockFormatting": true}*/ ON F1 GO. /* F1 will now perform the GO function */ ON F2 HELP. /* F2 will now perform the HELP function */ diff --git a/resources/functionalTests/emptyBlock/16procedure/input.p b/resources/functionalTests/emptyBlock/16procedure/input.p index 5cd9706..6da2b3a 100644 --- a/resources/functionalTests/emptyBlock/16procedure/input.p +++ b/resources/functionalTests/emptyBlock/16procedure/input.p @@ -1,5 +1,5 @@ /* formatterSettingsOverride */ -/* { "AblFormatter.emptyBlockFormatting": true}*/ +/* { "AblFormatter.blockFormatting": true}*/ procedure proc: end procedure. \ No newline at end of file diff --git a/resources/functionalTests/emptyBlock/16procedure/target.p b/resources/functionalTests/emptyBlock/16procedure/target.p index ccf684d..f7f199e 100644 --- a/resources/functionalTests/emptyBlock/16procedure/target.p +++ b/resources/functionalTests/emptyBlock/16procedure/target.p @@ -1,5 +1,5 @@ /* formatterSettingsOverride */ -/* { "AblFormatter.emptyBlockFormatting": true}*/ +/* { "AblFormatter.blockFormatting": true}*/ procedure proc: end procedure. \ No newline at end of file diff --git a/resources/functionalTests/emptyBlock/17repeat/input.p b/resources/functionalTests/emptyBlock/17repeat/input.p index 0fa9768..dc8e954 100644 --- a/resources/functionalTests/emptyBlock/17repeat/input.p +++ b/resources/functionalTests/emptyBlock/17repeat/input.p @@ -1,5 +1,5 @@ /* formatterSettingsOverride */ -/* { "AblFormatter.emptyBlockFormatting": true}*/ +/* { "AblFormatter.blockFormatting": true}*/ repeat: end. \ No newline at end of file diff --git a/resources/functionalTests/emptyBlock/17repeat/target.p b/resources/functionalTests/emptyBlock/17repeat/target.p index 4bdeeea..fdd94d5 100644 --- a/resources/functionalTests/emptyBlock/17repeat/target.p +++ b/resources/functionalTests/emptyBlock/17repeat/target.p @@ -1,5 +1,5 @@ /* formatterSettingsOverride */ -/* { "AblFormatter.emptyBlockFormatting": true}*/ +/* { "AblFormatter.blockFormatting": true}*/ repeat: end. \ No newline at end of file diff --git a/resources/functionalTests/emptyBlock/1catch/input.p b/resources/functionalTests/emptyBlock/1catch/input.p index f091a4b..13fce1c 100644 --- a/resources/functionalTests/emptyBlock/1catch/input.p +++ b/resources/functionalTests/emptyBlock/1catch/input.p @@ -1,5 +1,5 @@ /* formatterSettingsOverride */ -/* { "AblFormatter.emptyBlockFormatting": true}*/ +/* { "AblFormatter.blockFormatting": true}*/ catch oErr as MyErrorType: end catch. \ No newline at end of file diff --git a/resources/functionalTests/emptyBlock/1catch/target.p b/resources/functionalTests/emptyBlock/1catch/target.p index 7e7fe38..547d1ac 100644 --- a/resources/functionalTests/emptyBlock/1catch/target.p +++ b/resources/functionalTests/emptyBlock/1catch/target.p @@ -1,5 +1,5 @@ /* formatterSettingsOverride */ -/* { "AblFormatter.emptyBlockFormatting": true}*/ +/* { "AblFormatter.blockFormatting": true}*/ catch oErr as MyErrorType: end catch. \ No newline at end of file diff --git a/resources/functionalTests/emptyBlock/2class/input.p b/resources/functionalTests/emptyBlock/2class/input.p index 4088a4f..760088c 100644 --- a/resources/functionalTests/emptyBlock/2class/input.p +++ b/resources/functionalTests/emptyBlock/2class/input.p @@ -1,5 +1,5 @@ /* formatterSettingsOverride */ -/* { "AblFormatter.emptyBlockFormatting": true}*/ +/* { "AblFormatter.blockFormatting": true}*/ class A inherits B: end class. \ No newline at end of file diff --git a/resources/functionalTests/emptyBlock/2class/target.p b/resources/functionalTests/emptyBlock/2class/target.p index 5ca6f69..a8a4fa6 100644 --- a/resources/functionalTests/emptyBlock/2class/target.p +++ b/resources/functionalTests/emptyBlock/2class/target.p @@ -1,5 +1,5 @@ /* formatterSettingsOverride */ -/* { "AblFormatter.emptyBlockFormatting": true}*/ +/* { "AblFormatter.blockFormatting": true}*/ class A inherits B: end class. \ No newline at end of file diff --git a/resources/functionalTests/emptyBlock/3constructor/input.p b/resources/functionalTests/emptyBlock/3constructor/input.p index a74777c..bf298b2 100644 --- a/resources/functionalTests/emptyBlock/3constructor/input.p +++ b/resources/functionalTests/emptyBlock/3constructor/input.p @@ -1,5 +1,5 @@ /* formatterSettingsOverride */ -/* { "AblFormatter.emptyBlockFormatting": true}*/ +/* { "AblFormatter.blockFormatting": true}*/ class A inherits B: constructor private A(): diff --git a/resources/functionalTests/emptyBlock/3constructor/target.p b/resources/functionalTests/emptyBlock/3constructor/target.p index b91843f..b1217a0 100644 --- a/resources/functionalTests/emptyBlock/3constructor/target.p +++ b/resources/functionalTests/emptyBlock/3constructor/target.p @@ -1,5 +1,5 @@ /* formatterSettingsOverride */ -/* { "AblFormatter.emptyBlockFormatting": true}*/ +/* { "AblFormatter.blockFormatting": true}*/ class A inherits B: constructor private A(): diff --git a/resources/functionalTests/emptyBlock/4destructor/input.p b/resources/functionalTests/emptyBlock/4destructor/input.p index a0af791..1f94d8e 100644 --- a/resources/functionalTests/emptyBlock/4destructor/input.p +++ b/resources/functionalTests/emptyBlock/4destructor/input.p @@ -1,5 +1,5 @@ /* formatterSettingsOverride */ -/* { "AblFormatter.emptyBlockFormatting": true}*/ +/* { "AblFormatter.blockFormatting": true}*/ class A inherits B: destructor public A(): diff --git a/resources/functionalTests/emptyBlock/4destructor/target.p b/resources/functionalTests/emptyBlock/4destructor/target.p index 521236e..5429d8e 100644 --- a/resources/functionalTests/emptyBlock/4destructor/target.p +++ b/resources/functionalTests/emptyBlock/4destructor/target.p @@ -1,5 +1,5 @@ /* formatterSettingsOverride */ -/* { "AblFormatter.emptyBlockFormatting": true}*/ +/* { "AblFormatter.blockFormatting": true}*/ class A inherits B: destructor public A(): diff --git a/resources/functionalTests/emptyBlock/5do/input.p b/resources/functionalTests/emptyBlock/5do/input.p index 51fdf74..1325d31 100644 --- a/resources/functionalTests/emptyBlock/5do/input.p +++ b/resources/functionalTests/emptyBlock/5do/input.p @@ -1,5 +1,5 @@ /* formatterSettingsOverride */ -/* { "AblFormatter.emptyBlockFormatting": true}*/ +/* { "AblFormatter.blockFormatting": true}*/ do: end. \ No newline at end of file diff --git a/resources/functionalTests/emptyBlock/5do/target.p b/resources/functionalTests/emptyBlock/5do/target.p index 513adc0..823c964 100644 --- a/resources/functionalTests/emptyBlock/5do/target.p +++ b/resources/functionalTests/emptyBlock/5do/target.p @@ -1,5 +1,5 @@ /* formatterSettingsOverride */ -/* { "AblFormatter.emptyBlockFormatting": true}*/ +/* { "AblFormatter.blockFormatting": true}*/ do: end. \ No newline at end of file diff --git a/resources/functionalTests/emptyBlock/6do_on/input.p b/resources/functionalTests/emptyBlock/6do_on/input.p index 969ec08..f2542a4 100644 --- a/resources/functionalTests/emptyBlock/6do_on/input.p +++ b/resources/functionalTests/emptyBlock/6do_on/input.p @@ -1,5 +1,5 @@ /* formatterSettingsOverride */ -/* { "AblFormatter.emptyBlockFormatting": true}*/ +/* { "AblFormatter.blockFormatting": true}*/ do on error undo, throw: diff --git a/resources/functionalTests/emptyBlock/6do_on/target.p b/resources/functionalTests/emptyBlock/6do_on/target.p index 177b1b4..6dc8627 100644 --- a/resources/functionalTests/emptyBlock/6do_on/target.p +++ b/resources/functionalTests/emptyBlock/6do_on/target.p @@ -1,5 +1,5 @@ /* formatterSettingsOverride */ -/* { "AblFormatter.emptyBlockFormatting": true}*/ +/* { "AblFormatter.blockFormatting": true}*/ do on error undo, throw: diff --git a/resources/functionalTests/emptyBlock/7do_to/input.p b/resources/functionalTests/emptyBlock/7do_to/input.p index b4b5ae7..c637924 100644 --- a/resources/functionalTests/emptyBlock/7do_to/input.p +++ b/resources/functionalTests/emptyBlock/7do_to/input.p @@ -1,5 +1,5 @@ /* formatterSettingsOverride */ -/* { "AblFormatter.emptyBlockFormatting": true}*/ +/* { "AblFormatter.blockFormatting": true}*/ do iCount = iTotal to 5: end. \ No newline at end of file diff --git a/resources/functionalTests/emptyBlock/7do_to/target.p b/resources/functionalTests/emptyBlock/7do_to/target.p index 835282d..285b123 100644 --- a/resources/functionalTests/emptyBlock/7do_to/target.p +++ b/resources/functionalTests/emptyBlock/7do_to/target.p @@ -1,5 +1,5 @@ /* formatterSettingsOverride */ -/* { "AblFormatter.emptyBlockFormatting": true}*/ +/* { "AblFormatter.blockFormatting": true}*/ do iCount = iTotal to 5: end. \ No newline at end of file diff --git a/resources/functionalTests/emptyBlock/8do_transaction/input.p b/resources/functionalTests/emptyBlock/8do_transaction/input.p index e63b26f..0bc58ec 100644 --- a/resources/functionalTests/emptyBlock/8do_transaction/input.p +++ b/resources/functionalTests/emptyBlock/8do_transaction/input.p @@ -1,5 +1,5 @@ /* formatterSettingsOverride */ -/* { "AblFormatter.emptyBlockFormatting": true}*/ +/* { "AblFormatter.blockFormatting": true}*/ do transaction: end. \ No newline at end of file diff --git a/resources/functionalTests/emptyBlock/8do_transaction/target.p b/resources/functionalTests/emptyBlock/8do_transaction/target.p index 8210da3..84706a5 100644 --- a/resources/functionalTests/emptyBlock/8do_transaction/target.p +++ b/resources/functionalTests/emptyBlock/8do_transaction/target.p @@ -1,5 +1,5 @@ /* formatterSettingsOverride */ -/* { "AblFormatter.emptyBlockFormatting": true}*/ +/* { "AblFormatter.blockFormatting": true}*/ do transaction: end. \ No newline at end of file diff --git a/resources/functionalTests/emptyBlock/9do_while/input.p b/resources/functionalTests/emptyBlock/9do_while/input.p index fef9498..c3d09d3 100644 --- a/resources/functionalTests/emptyBlock/9do_while/input.p +++ b/resources/functionalTests/emptyBlock/9do_while/input.p @@ -1,5 +1,5 @@ /* formatterSettingsOverride */ -/* { "AblFormatter.emptyBlockFormatting": true}*/ +/* { "AblFormatter.blockFormatting": true}*/ do while i < 5: end. \ No newline at end of file diff --git a/resources/functionalTests/emptyBlock/9do_while/target.p b/resources/functionalTests/emptyBlock/9do_while/target.p index d13a096..1e32821 100644 --- a/resources/functionalTests/emptyBlock/9do_while/target.p +++ b/resources/functionalTests/emptyBlock/9do_while/target.p @@ -1,5 +1,5 @@ /* formatterSettingsOverride */ -/* { "AblFormatter.emptyBlockFormatting": true}*/ +/* { "AblFormatter.blockFormatting": true}*/ do while i < 5: end. \ No newline at end of file diff --git a/resources/functionalTests/for/3each-where/input.p b/resources/functionalTests/for/3each-where/input.p index 3ea0e31..7ddeb59 100644 --- a/resources/functionalTests/for/3each-where/input.p +++ b/resources/functionalTests/for/3each-where/input.p @@ -1,7 +1,5 @@ /* formatterSettingsOverride */ -/* { "AblFormatter.blockFormatting": true, -"AblFormatter.forFormatting": true -}*/ +/* { "AblFormatter.forFormatting": true}*/ for each Customer where Customer.var = 1: diff --git a/resources/functionalTests/for/3each-where/target.p b/resources/functionalTests/for/3each-where/target.p index 4ce7747..e46f0b5 100644 --- a/resources/functionalTests/for/3each-where/target.p +++ b/resources/functionalTests/for/3each-where/target.p @@ -1,7 +1,5 @@ /* formatterSettingsOverride */ -/* { "AblFormatter.blockFormatting": true, -"AblFormatter.forFormatting": true -}*/ +/* { "AblFormatter.forFormatting": true}*/ for each Customer where Customer.var = 1: diff --git a/resources/functionalTests/for/3first-by/input.p b/resources/functionalTests/for/3first-by/input.p index 4a7983a..06702de 100644 --- a/resources/functionalTests/for/3first-by/input.p +++ b/resources/functionalTests/for/3first-by/input.p @@ -1,7 +1,5 @@ /* formatterSettingsOverride */ -/* { "AblFormatter.blockFormatting": true, -"AblFormatter.forFormatting": true -}*/ +/* { "AblFormatter.forFormatting": true}*/ FOR FIRST Customer NO-LOCK BY Customer.CreditLimit: diff --git a/resources/functionalTests/for/3first-by/target.p b/resources/functionalTests/for/3first-by/target.p index 02ca12e..4b15a9b 100644 --- a/resources/functionalTests/for/3first-by/target.p +++ b/resources/functionalTests/for/3first-by/target.p @@ -1,7 +1,5 @@ /* formatterSettingsOverride */ -/* { "AblFormatter.blockFormatting": true, -"AblFormatter.forFormatting": true -}*/ +/* { "AblFormatter.forFormatting": true}*/ FOR FIRST Customer NO-LOCK BY Customer.CreditLimit: diff --git a/resources/functionalTests/for/5each-break-by/input.p b/resources/functionalTests/for/5each-break-by/input.p new file mode 100644 index 0000000..90dbf9b --- /dev/null +++ b/resources/functionalTests/for/5each-break-by/input.p @@ -0,0 +1,6 @@ +/* formatterSettingsOverride */ +/* { "AblFormatter.forFormatting": true}*/ + +FOR EACH Employee NO-LOCK WHERE Employee.Salary > 50000 BREAK BY Employee.Department: + DISPLAY Employee.Department Employee.Name Employee.Salary. +END. diff --git a/resources/functionalTests/for/5each-break-by/target.p b/resources/functionalTests/for/5each-break-by/target.p new file mode 100644 index 0000000..2fcf9b2 --- /dev/null +++ b/resources/functionalTests/for/5each-break-by/target.p @@ -0,0 +1,8 @@ +/* formatterSettingsOverride */ +/* { "AblFormatter.forFormatting": true}*/ + +FOR EACH Employee NO-LOCK WHERE + Employee.Salary > 50000 + BREAK BY Employee.Department: + DISPLAY Employee.Department Employee.Name Employee.Salary. +END. diff --git a/resources/functionalTests/for/6each-nested/input.p b/resources/functionalTests/for/6each-nested/input.p new file mode 100644 index 0000000..a0d2d32 --- /dev/null +++ b/resources/functionalTests/for/6each-nested/input.p @@ -0,0 +1,15 @@ +/* formatterSettingsOverride */ +/* { "AblFormatter.forFormatting": true}*/ + +FOR EACH Department NO-LOCK: + DISPLAY Department.Name. + + FOR EACH Employee NO-LOCK WHERE Employee.Department = Department.DepartmentID BREAK BY Employee.JobTitle: + DISPLAY Employee.JobTitle Employee.Name Employee.Salary. + + FOR EACH Project NO-LOCK WHERE Project.EmployeeID = Employee.EmployeeID: + DISPLAY Project.ProjectName Project.StartDate Project.EndDate. + END. + END. +END. + diff --git a/resources/functionalTests/for/6each-nested/target.p b/resources/functionalTests/for/6each-nested/target.p new file mode 100644 index 0000000..ca26682 --- /dev/null +++ b/resources/functionalTests/for/6each-nested/target.p @@ -0,0 +1,18 @@ +/* formatterSettingsOverride */ +/* { "AblFormatter.forFormatting": true}*/ + +FOR EACH Department NO-LOCK: + DISPLAY Department.Name. + + FOR EACH Employee NO-LOCK WHERE + Employee.Department = Department.DepartmentID + BREAK BY Employee.JobTitle: + DISPLAY Employee.JobTitle Employee.Name Employee.Salary. + + FOR EACH Project NO-LOCK WHERE + Project.EmployeeID = Employee.EmployeeID: + DISPLAY Project.ProjectName Project.StartDate Project.EndDate. + END. + END. +END. + diff --git a/resources/functionalTests/for/7each-complex/input.p b/resources/functionalTests/for/7each-complex/input.p new file mode 100644 index 0000000..1cacc72 --- /dev/null +++ b/resources/functionalTests/for/7each-complex/input.p @@ -0,0 +1,20 @@ +/* formatterSettingsOverride */ +/* { "AblFormatter.forFormatting": true}*/ + +FOR EACH Customer NO-LOCK + WHERE (Customer.Balance > 10000 AND Customer.Region = "North") OR (Customer.Region = "South" AND Customer.Status = "Active") + BY Customer.LastName + BY Customer.FirstName + BREAK BY Customer.City + BY Customer.State: + DISPLAY Customer.CustomerID + Customer.Name + Customer.Balance + Customer.Region + Customer.Status + Customer.City + Customer.State + Customer.LastOrderDate + Customer.CreditLimit + Customer.SalesRep. +END. diff --git a/resources/functionalTests/for/7each-complex/target.p b/resources/functionalTests/for/7each-complex/target.p new file mode 100644 index 0000000..b296aa1 --- /dev/null +++ b/resources/functionalTests/for/7each-complex/target.p @@ -0,0 +1,20 @@ +/* formatterSettingsOverride */ +/* { "AblFormatter.forFormatting": true}*/ + +FOR EACH Customer NO-LOCK WHERE + (Customer.Balance > 10000 AND Customer.Region = "North") OR (Customer.Region = "South" AND Customer.Status = "Active") + BY Customer.LastName + BY Customer.FirstName + BREAK BY Customer.City + BY Customer.State: + DISPLAY Customer.CustomerID + Customer.Name + Customer.Balance + Customer.Region + Customer.Status + Customer.City + Customer.State + Customer.LastOrderDate + Customer.CreditLimit + Customer.SalesRep. +END. diff --git a/src/v2/formatterFramework/enableFormatterDecorators.ts b/src/v2/formatterFramework/enableFormatterDecorators.ts index 1ef5bd5..65a36d9 100644 --- a/src/v2/formatterFramework/enableFormatterDecorators.ts +++ b/src/v2/formatterFramework/enableFormatterDecorators.ts @@ -6,15 +6,15 @@ import { UsingFormatter } from "../formatters/using/UsingFormatter"; import { CaseFormatter } from "../formatters/case/CaseFormatter"; import { ForFormatter } from "../formatters/for/ForFormatter"; import { IfFunctionFormatter } from "../formatters/ifFunction/IfFunctionFormatter"; -import { EmptyBlockFormatter } from "../formatters/emptyblock/EmptyBlockFormatter"; import { TempTableFormatter } from "../formatters/tempTable/TempTableFormatter"; import { PropertyFormatter } from "../formatters/property/PropertyFormatter"; +import { BodyFormatter } from "../formatters/body/BodyFormatter"; // needed just for enabling decorators. Decorators does not work if there is no usage of a class in the reachable code export function enableFormatterDecorators(): void { AssignFormatter; - EmptyBlockFormatter; BlockFormater; + BodyFormatter; IfFormatter; DefineFormatter; UsingFormatter; diff --git a/src/v2/formatters/emptyblock/EmptyBlockFormatter.ts b/src/v2/formatters/body/BodyFormatter.ts similarity index 92% rename from src/v2/formatters/emptyblock/EmptyBlockFormatter.ts rename to src/v2/formatters/body/BodyFormatter.ts index b134dcc..7a13bc2 100644 --- a/src/v2/formatters/emptyblock/EmptyBlockFormatter.ts +++ b/src/v2/formatters/body/BodyFormatter.ts @@ -4,7 +4,7 @@ import { IFormatter } from "../../formatterFramework/IFormatter"; import { CodeEdit } from "../../model/CodeEdit"; import { FullText } from "../../model/FullText"; import { AFormatter } from "../AFormatter"; -import { EmptyBlockSettings } from "./EmptyBlockSettings"; +import { BodySettings } from "./BodySettings"; import { IConfigurationManager } from "../../../utils/IConfigurationManager"; import { bodyBlockKeywords, @@ -13,13 +13,13 @@ import { import { FormatterHelper } from "../../formatterFramework/FormatterHelper"; @RegisterFormatter -export class EmptyBlockFormatter extends AFormatter implements IFormatter { - public static readonly formatterLabel = "emptyBlockFormatting"; - private readonly settings: EmptyBlockSettings; +export class BodyFormatter extends AFormatter implements IFormatter { + public static readonly formatterLabel = "bodyFormatting"; + private readonly settings: BodySettings; public constructor(configurationManager: IConfigurationManager) { super(configurationManager); - this.settings = new EmptyBlockSettings(configurationManager); + this.settings = new BodySettings(configurationManager); } public match(node: Readonly): boolean { diff --git a/src/v2/formatters/body/BodySettings.ts b/src/v2/formatters/body/BodySettings.ts new file mode 100644 index 0000000..4423a0c --- /dev/null +++ b/src/v2/formatters/body/BodySettings.ts @@ -0,0 +1,8 @@ +import { ASettings } from "../ASettings"; + +export class BodySettings extends ASettings { + //empty block settings + public BodyFormatting() { + return !!this.configurationManager.get("bodyFormatting"); + } +} diff --git a/src/v2/formatters/emptyblock/EmptyBlockSettings.ts b/src/v2/formatters/emptyblock/EmptyBlockSettings.ts deleted file mode 100644 index 4d8b3b6..0000000 --- a/src/v2/formatters/emptyblock/EmptyBlockSettings.ts +++ /dev/null @@ -1,8 +0,0 @@ -import { ASettings } from "../ASettings"; - -export class EmptyBlockSettings extends ASettings { - //empty block settings - public emptyBlockFormatting() { - return !!this.configurationManager.get("emptyBlockFormatting"); - } -} diff --git a/src/v2/formatters/for/ForFormatter.ts b/src/v2/formatters/for/ForFormatter.ts index d50efe2..9b5346a 100644 --- a/src/v2/formatters/for/ForFormatter.ts +++ b/src/v2/formatters/for/ForFormatter.ts @@ -66,10 +66,6 @@ export class ForFormatter extends AFormatter implements IFormatter { if (child.type === SyntaxNodeType.Identifier) { alignColumn = this.startColumn + resultString.length; } - console.log("child: " + child.type); - console.log( - "childText: " + FormatterHelper.getCurrentText(child, fullText) - ); resultString = resultString.concat( this.getForExpressionString(child, fullText, alignColumn) ); @@ -184,10 +180,6 @@ export class ForFormatter extends AFormatter implements IFormatter { ); }); - console.log("sortClauseText:\n" + resultString); - console.log( - "sortClauseNode:\n" + FormatterHelper.getCurrentText(node, fullText) - ); return resultString; } @@ -220,8 +212,6 @@ export class ForFormatter extends AFormatter implements IFormatter { break; } - console.log("sortClauseType:\n" + node.type); - console.log("sortClausePart:\n" + newString); return newString; } @@ -251,8 +241,6 @@ export class ForFormatter extends AFormatter implements IFormatter { break; } - console.log("sortColumnType:\n" + node.type); - console.log("sortColumnPart:\n" + newString); return newString; }