Skip to content

Commit

Permalink
Merge pull request #99 from BalticAmadeus/40-find-formatting
Browse files Browse the repository at this point in the history
40 find formatting
  • Loading branch information
PauliusKu authored Oct 14, 2024
2 parents d1a7ebf + fb430cc commit f688439
Show file tree
Hide file tree
Showing 22 changed files with 243 additions and 0 deletions.
7 changes: 7 additions & 0 deletions resources/functionalTests/find/1firstLastNextPrev/input.p
Original file line number Diff line number Diff line change
@@ -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.
11 changes: 11 additions & 0 deletions resources/functionalTests/find/1firstLastNextPrev/target.p
Original file line number Diff line number Diff line change
@@ -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.
5 changes: 5 additions & 0 deletions resources/functionalTests/find/2multipleConditions/input.p
Original file line number Diff line number Diff line change
@@ -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.
5 changes: 5 additions & 0 deletions resources/functionalTests/find/2multipleConditions/target.p
Original file line number Diff line number Diff line change
@@ -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.
4 changes: 4 additions & 0 deletions resources/functionalTests/find/3multipleConditions-()/input.p
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/* formatterSettingsOverride */
/* { "AblFormatter.findFormatting": true}*/

FIND Customer WHERE (Customer.CustNum > 50 AND Customer.CustNum < 100) OR Customer.CustNum > 200 NO-ERROR.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/* formatterSettingsOverride */
/* { "AblFormatter.findFormatting": true}*/

FIND Customer WHERE
(Customer.CustNum > 50 AND Customer.CustNum < 100) OR Customer.CustNum > 200 NO-ERROR.
Original file line number Diff line number Diff line change
@@ -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.
Original file line number Diff line number Diff line change
@@ -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.
4 changes: 4 additions & 0 deletions resources/functionalTests/find/5spaces/input.p
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/* formatterSettingsOverride */
/* { "AblFormatter.findFormatting": true}*/

FIND FIRST Customer WHERE Customer.CustNum = 5 NO-LOCK NO-ERROR.
5 changes: 5 additions & 0 deletions resources/functionalTests/find/5spaces/target.p
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/* formatterSettingsOverride */
/* { "AblFormatter.findFormatting": true}*/

FIND FIRST Customer WHERE
Customer.CustNum = 5 NO-LOCK NO-ERROR.
4 changes: 4 additions & 0 deletions resources/functionalTests/find/6useIndex/input.p
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/* formatterSettingsOverride */
/* { "AblFormatter.findFormatting": true}*/

find Order where Order.OrderStatus = "Shipped" and Order.ShipDate = today use-index OrderNum.
5 changes: 5 additions & 0 deletions resources/functionalTests/find/6useIndex/target.p
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/* formatterSettingsOverride */
/* { "AblFormatter.findFormatting": true}*/

find Order where
Order.OrderStatus = "Shipped" and Order.ShipDate = today use-index OrderNum.
4 changes: 4 additions & 0 deletions resources/functionalTests/find/7no-wait/input.p
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/* formatterSettingsOverride */
/* { "AblFormatter.findFormatting": true}*/

FIND Order WHERE Order.CustomerID = Customer.CustomerID NO-WAIT.
5 changes: 5 additions & 0 deletions resources/functionalTests/find/7no-wait/target.p
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/* formatterSettingsOverride */
/* { "AblFormatter.findFormatting": true}*/

FIND Order WHERE
Order.CustomerID = Customer.CustomerID NO-WAIT.
5 changes: 5 additions & 0 deletions resources/functionalTests/find/7noWhere/input.p
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/* formatterSettingsOverride */
/* { "AblFormatter.findFormatting": true}*/

find first b_vac
no-lock no-error.
4 changes: 4 additions & 0 deletions resources/functionalTests/find/7noWhere/target.p
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/* formatterSettingsOverride */
/* { "AblFormatter.findFormatting": true}*/

find first b_vac no-lock no-error.
4 changes: 4 additions & 0 deletions resources/functionalTests/find/7of-table/input.p
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/* formatterSettingsOverride */
/* { "AblFormatter.findFormatting": true}*/

FIND FIRST Order OF Customer WHERE Customer.Country = "USA" AND Customer.Balance > 1000.
5 changes: 5 additions & 0 deletions resources/functionalTests/find/7of-table/target.p
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/* formatterSettingsOverride */
/* { "AblFormatter.findFormatting": true}*/

FIND FIRST Order OF Customer WHERE
Customer.Country = "USA" AND Customer.Balance > 1000.
1 change: 1 addition & 0 deletions src/model/SyntaxNodeType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ export enum SyntaxNodeType {
EachKeyword = "EACH",
EndKeyword = "END",
IfKeyword = "IF",
FindKeyword = "FIND",
ForKeyword = "FOR",
DotKeyword = ".",
ColonKeyword = ":",
Expand Down
2 changes: 2 additions & 0 deletions src/v2/formatterFramework/enableFormatterDecorators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -20,6 +21,7 @@ export function enableFormatterDecorators(): void {
DefineFormatter;
UsingFormatter;
CaseFormatter;
FindFormatter;
ForFormatter;
TempTableFormatter;
PropertyFormatter;
Expand Down
141 changes: 141 additions & 0 deletions src/v2/formatters/find/FindFormatter.ts
Original file line number Diff line number Diff line change
@@ -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<SyntaxNode>): boolean {
return node.type === SyntaxNodeType.FindStatement;
}

parse(
node: Readonly<SyntaxNode>,
fullText: Readonly<FullText>
): CodeEdit | CodeEdit[] | undefined {
this.collectCaseStructure(node, fullText);
return this.getCodeEdit(
node,
FormatterHelper.getCurrentText(node, fullText),
this.findBodyValue,
fullText
);
}

private collectCaseStructure(
node: SyntaxNode,
fullText: Readonly<FullText>
) {
this.startColumn = node.startPosition.column;
this.findBodyValue = this.getFindStatementBlock(node, fullText);
}

private getFindStatementBlock(
node: SyntaxNode,
fullText: Readonly<FullText>
): 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<FullText>,
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<FullText>,
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;
}
}
8 changes: 8 additions & 0 deletions src/v2/formatters/find/FindSettings.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { ASettings } from "../ASettings";

export class FindSettings extends ASettings {
// token settings
public findFormatting() {
return this.configurationManager.get("findFormatting") ? true : false;
}
}

0 comments on commit f688439

Please sign in to comment.