diff --git a/resources/functionalTests/find/1firstLastNextPrev/input.p b/resources/functionalTests/find/1firstLastNextPrev/input.p new file mode 100644 index 0000000..699c876 --- /dev/null +++ b/resources/functionalTests/find/1firstLastNextPrev/input.p @@ -0,0 +1,7 @@ +/* formatterSettingsOverride */ +/* { "AblFormatter.findFormatting": true}*/ + +FIND FIRST Customer WHERE Customer.CustNum = 5 NO-LOCK NO-ERROR. +FIND LAST Customer WHERE Customer.CustNum = 5 NO-LOCK NO-ERROR. +FIND NEXT Customer WHERE Customer.CustNum = 5 NO-LOCK NO-ERROR. +FIND PREV Customer WHERE Customer.CustNum = 5 NO-LOCK NO-ERROR. \ No newline at end of file diff --git a/resources/functionalTests/find/1firstLastNextPrev/target.p b/resources/functionalTests/find/1firstLastNextPrev/target.p new file mode 100644 index 0000000..cfc3874 --- /dev/null +++ b/resources/functionalTests/find/1firstLastNextPrev/target.p @@ -0,0 +1,11 @@ +/* formatterSettingsOverride */ +/* { "AblFormatter.findFormatting": true}*/ + +FIND FIRST Customer WHERE + Customer.CustNum = 5 NO-LOCK NO-ERROR. +FIND LAST Customer WHERE + Customer.CustNum = 5 NO-LOCK NO-ERROR. +FIND NEXT Customer WHERE + Customer.CustNum = 5 NO-LOCK NO-ERROR. +FIND PREV Customer WHERE + Customer.CustNum = 5 NO-LOCK NO-ERROR. \ No newline at end of file diff --git a/resources/functionalTests/find/2multipleConditions/input.p b/resources/functionalTests/find/2multipleConditions/input.p new file mode 100644 index 0000000..50c502a --- /dev/null +++ b/resources/functionalTests/find/2multipleConditions/input.p @@ -0,0 +1,5 @@ +/* formatterSettingsOverride */ +/* { "AblFormatter.findFormatting": true}*/ + +FIND Customer WHERE +Customer.CustNum = 5 AND Customer.CustNum = 6 OR Customer.CustNum = 8 no-lock no-error. \ No newline at end of file diff --git a/resources/functionalTests/find/2multipleConditions/target.p b/resources/functionalTests/find/2multipleConditions/target.p new file mode 100644 index 0000000..fe06937 --- /dev/null +++ b/resources/functionalTests/find/2multipleConditions/target.p @@ -0,0 +1,5 @@ +/* formatterSettingsOverride */ +/* { "AblFormatter.findFormatting": true}*/ + +FIND Customer WHERE + Customer.CustNum = 5 AND Customer.CustNum = 6 OR Customer.CustNum = 8 no-lock no-error. \ No newline at end of file diff --git a/resources/functionalTests/find/3multipleConditions-()/input.p b/resources/functionalTests/find/3multipleConditions-()/input.p new file mode 100644 index 0000000..57b76b6 --- /dev/null +++ b/resources/functionalTests/find/3multipleConditions-()/input.p @@ -0,0 +1,4 @@ +/* formatterSettingsOverride */ +/* { "AblFormatter.findFormatting": true}*/ + +FIND Customer WHERE (Customer.CustNum > 50 AND Customer.CustNum < 100) OR Customer.CustNum > 200 NO-ERROR. \ No newline at end of file diff --git a/resources/functionalTests/find/3multipleConditions-()/target.p b/resources/functionalTests/find/3multipleConditions-()/target.p new file mode 100644 index 0000000..2cdc88a --- /dev/null +++ b/resources/functionalTests/find/3multipleConditions-()/target.p @@ -0,0 +1,5 @@ +/* formatterSettingsOverride */ +/* { "AblFormatter.findFormatting": true}*/ + +FIND Customer WHERE + (Customer.CustNum > 50 AND Customer.CustNum < 100) OR Customer.CustNum > 200 NO-ERROR. \ No newline at end of file diff --git a/resources/functionalTests/find/4multipleConditions-()2/input.p b/resources/functionalTests/find/4multipleConditions-()2/input.p new file mode 100644 index 0000000..4eb239d --- /dev/null +++ b/resources/functionalTests/find/4multipleConditions-()2/input.p @@ -0,0 +1,4 @@ +/* formatterSettingsOverride */ +/* { "AblFormatter.findFormatting": true}*/ + +FIND Order WHERE (Order.OrderStatus = "Shipped" AND (Order.ShipDate > TODAY - 7)) OR (Order.OrderStatus = "Pending" AND (Order.OrderDate = TODAY)) no-lock. \ No newline at end of file diff --git a/resources/functionalTests/find/4multipleConditions-()2/target.p b/resources/functionalTests/find/4multipleConditions-()2/target.p new file mode 100644 index 0000000..39b6cdf --- /dev/null +++ b/resources/functionalTests/find/4multipleConditions-()2/target.p @@ -0,0 +1,5 @@ +/* formatterSettingsOverride */ +/* { "AblFormatter.findFormatting": true}*/ + +FIND Order WHERE + (Order.OrderStatus = "Shipped" AND (Order.ShipDate > TODAY - 7)) OR (Order.OrderStatus = "Pending" AND (Order.OrderDate = TODAY)) no-lock. \ No newline at end of file diff --git a/resources/functionalTests/find/5spaces/input.p b/resources/functionalTests/find/5spaces/input.p new file mode 100644 index 0000000..8a056f5 --- /dev/null +++ b/resources/functionalTests/find/5spaces/input.p @@ -0,0 +1,4 @@ +/* formatterSettingsOverride */ +/* { "AblFormatter.findFormatting": true}*/ + + FIND FIRST Customer WHERE Customer.CustNum = 5 NO-LOCK NO-ERROR. \ No newline at end of file diff --git a/resources/functionalTests/find/5spaces/target.p b/resources/functionalTests/find/5spaces/target.p new file mode 100644 index 0000000..e0b3888 --- /dev/null +++ b/resources/functionalTests/find/5spaces/target.p @@ -0,0 +1,5 @@ +/* formatterSettingsOverride */ +/* { "AblFormatter.findFormatting": true}*/ + + FIND FIRST Customer WHERE + Customer.CustNum = 5 NO-LOCK NO-ERROR. \ No newline at end of file diff --git a/resources/functionalTests/find/6useIndex/input.p b/resources/functionalTests/find/6useIndex/input.p new file mode 100644 index 0000000..73cb455 --- /dev/null +++ b/resources/functionalTests/find/6useIndex/input.p @@ -0,0 +1,4 @@ +/* formatterSettingsOverride */ +/* { "AblFormatter.findFormatting": true}*/ + +find Order where Order.OrderStatus = "Shipped" and Order.ShipDate = today use-index OrderNum. \ No newline at end of file diff --git a/resources/functionalTests/find/6useIndex/target.p b/resources/functionalTests/find/6useIndex/target.p new file mode 100644 index 0000000..4b8db54 --- /dev/null +++ b/resources/functionalTests/find/6useIndex/target.p @@ -0,0 +1,5 @@ +/* formatterSettingsOverride */ +/* { "AblFormatter.findFormatting": true}*/ + +find Order where + Order.OrderStatus = "Shipped" and Order.ShipDate = today use-index OrderNum. \ No newline at end of file diff --git a/resources/functionalTests/find/7no-wait/input.p b/resources/functionalTests/find/7no-wait/input.p new file mode 100644 index 0000000..4740485 --- /dev/null +++ b/resources/functionalTests/find/7no-wait/input.p @@ -0,0 +1,4 @@ +/* formatterSettingsOverride */ +/* { "AblFormatter.findFormatting": true}*/ + +FIND Order WHERE Order.CustomerID = Customer.CustomerID NO-WAIT. \ No newline at end of file diff --git a/resources/functionalTests/find/7no-wait/target.p b/resources/functionalTests/find/7no-wait/target.p new file mode 100644 index 0000000..3828ffc --- /dev/null +++ b/resources/functionalTests/find/7no-wait/target.p @@ -0,0 +1,5 @@ +/* formatterSettingsOverride */ +/* { "AblFormatter.findFormatting": true}*/ + +FIND Order WHERE + Order.CustomerID = Customer.CustomerID NO-WAIT. \ No newline at end of file diff --git a/resources/functionalTests/find/7noWhere/input.p b/resources/functionalTests/find/7noWhere/input.p new file mode 100644 index 0000000..b362157 --- /dev/null +++ b/resources/functionalTests/find/7noWhere/input.p @@ -0,0 +1,5 @@ +/* formatterSettingsOverride */ +/* { "AblFormatter.findFormatting": true}*/ + +find first b_vac +no-lock no-error. \ No newline at end of file diff --git a/resources/functionalTests/find/7noWhere/target.p b/resources/functionalTests/find/7noWhere/target.p new file mode 100644 index 0000000..8246220 --- /dev/null +++ b/resources/functionalTests/find/7noWhere/target.p @@ -0,0 +1,4 @@ +/* formatterSettingsOverride */ +/* { "AblFormatter.findFormatting": true}*/ + +find first b_vac no-lock no-error. \ No newline at end of file diff --git a/resources/functionalTests/find/7of-table/input.p b/resources/functionalTests/find/7of-table/input.p new file mode 100644 index 0000000..3022490 --- /dev/null +++ b/resources/functionalTests/find/7of-table/input.p @@ -0,0 +1,4 @@ +/* formatterSettingsOverride */ +/* { "AblFormatter.findFormatting": true}*/ + +FIND FIRST Order OF Customer WHERE Customer.Country = "USA" AND Customer.Balance > 1000. \ No newline at end of file diff --git a/resources/functionalTests/find/7of-table/target.p b/resources/functionalTests/find/7of-table/target.p new file mode 100644 index 0000000..f840fb9 --- /dev/null +++ b/resources/functionalTests/find/7of-table/target.p @@ -0,0 +1,5 @@ +/* formatterSettingsOverride */ +/* { "AblFormatter.findFormatting": true}*/ + +FIND FIRST Order OF Customer WHERE + Customer.Country = "USA" AND Customer.Balance > 1000. \ No newline at end of file diff --git a/src/model/SyntaxNodeType.ts b/src/model/SyntaxNodeType.ts index db272a4..44e1fb1 100644 --- a/src/model/SyntaxNodeType.ts +++ b/src/model/SyntaxNodeType.ts @@ -87,6 +87,7 @@ export enum SyntaxNodeType { EachKeyword = "EACH", EndKeyword = "END", IfKeyword = "IF", + FindKeyword = "FIND", ForKeyword = "FOR", DotKeyword = ".", ColonKeyword = ":", diff --git a/src/v2/formatterFramework/enableFormatterDecorators.ts b/src/v2/formatterFramework/enableFormatterDecorators.ts index e56d8d7..57b074c 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 { FindFormatter } from "../formatters/find/FindFormatter"; import { ForFormatter } from "../formatters/for/ForFormatter"; import { IfFunctionFormatter } from "../formatters/ifFunction/IfFunctionFormatter"; import { TempTableFormatter } from "../formatters/tempTable/TempTableFormatter"; @@ -20,6 +21,7 @@ export function enableFormatterDecorators(): void { DefineFormatter; UsingFormatter; CaseFormatter; + FindFormatter; ForFormatter; TempTableFormatter; PropertyFormatter; diff --git a/src/v2/formatters/find/FindFormatter.ts b/src/v2/formatters/find/FindFormatter.ts new file mode 100644 index 0000000..f48a321 --- /dev/null +++ b/src/v2/formatters/find/FindFormatter.ts @@ -0,0 +1,141 @@ +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 { FindSettings } from "./FindSettings"; +import { IConfigurationManager } from "../../../utils/IConfigurationManager"; + +@RegisterFormatter +export class FindFormatter extends AFormatter implements IFormatter { + private startColumn = 0; + private findBodyValue = ""; + + public static readonly formatterLabel = "findFormatting"; + private readonly settings: FindSettings; + + public constructor(configurationManager: IConfigurationManager) { + super(configurationManager); + this.settings = new FindSettings(configurationManager); + } + + match(node: Readonly): boolean { + return node.type === SyntaxNodeType.FindStatement; + } + + parse( + node: Readonly, + fullText: Readonly + ): CodeEdit | CodeEdit[] | undefined { + this.collectCaseStructure(node, fullText); + return this.getCodeEdit( + node, + FormatterHelper.getCurrentText(node, fullText), + this.findBodyValue, + fullText + ); + } + + private collectCaseStructure( + node: SyntaxNode, + fullText: Readonly + ) { + this.startColumn = node.startPosition.column; + this.findBodyValue = this.getFindStatementBlock(node, fullText); + } + + private getFindStatementBlock( + node: SyntaxNode, + fullText: Readonly + ): string { + let resultString = ""; + let alignColumn = 0; + + node.children.forEach((child) => { + if (child.type === SyntaxNodeType.Identifier) { + alignColumn = this.startColumn + resultString.length; + } + resultString = resultString.concat( + this.getFindExpressionString(child, fullText, alignColumn) + ); + }); + + return resultString + "."; + } + + private getFindExpressionString( + node: SyntaxNode, + fullText: Readonly, + alignColumn: number + ): string { + let newString = ""; + + switch (node.type) { + case SyntaxNodeType.FindKeyword: + newString = FormatterHelper.getCurrentText( + node, + fullText + ).trim(); + break; + case SyntaxNodeType.WhereClause: + newString = this.getWhereClauseBlock( + node, + fullText, + alignColumn + ); + break; + case SyntaxNodeType.Error: + newString = FormatterHelper.getCurrentText(node, fullText); + break; + default: + const text = FormatterHelper.getCurrentText( + node, + fullText + ).trim(); + newString = text.length === 0 ? "" : " " + text; + break; + } + + return newString; + } + + private getWhereClauseBlock( + node: SyntaxNode, + fullText: Readonly, + alignColumn: number + ): string { + let resultString = ""; + + node.children.forEach((child) => { + switch (child.type) { + case SyntaxNodeType.WhereKeyword: + resultString = resultString.concat( + " ", + FormatterHelper.getCurrentText(child, fullText).trim(), + fullText.eolDelimiter, + " ".repeat(alignColumn) + ); + break; + case SyntaxNodeType.Error: + resultString = resultString.concat( + FormatterHelper.getCurrentText(node, fullText) + ); + break; + default: + const text = FormatterHelper.getCurrentText( + child, + fullText + ).trim(); + resultString = resultString.concat( + text.length === 0 ? "" : " " + text + ); + break; + } + }); + + return resultString; + } +} diff --git a/src/v2/formatters/find/FindSettings.ts b/src/v2/formatters/find/FindSettings.ts new file mode 100644 index 0000000..a088786 --- /dev/null +++ b/src/v2/formatters/find/FindSettings.ts @@ -0,0 +1,8 @@ +import { ASettings } from "../ASettings"; + +export class FindSettings extends ASettings { + // token settings + public findFormatting() { + return this.configurationManager.get("findFormatting") ? true : false; + } +}