Skip to content

Commit

Permalink
Implement ArrayAccessFormatter; add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
gmickus committed Dec 20, 2024
1 parent 433875f commit 61666b1
Show file tree
Hide file tree
Showing 10 changed files with 105 additions and 0 deletions.
4 changes: 4 additions & 0 deletions resources/functionalTests/arrayAccess/1/input.p
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/* formatterSettingsOverride */
/* { "AblFormatter.arrayAccessFormatting": true}*/

message a[ 3 ].
4 changes: 4 additions & 0 deletions resources/functionalTests/arrayAccess/1/target.p
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/* formatterSettingsOverride */
/* { "AblFormatter.arrayAccessFormatting": true}*/

message a[3].
4 changes: 4 additions & 0 deletions resources/functionalTests/arrayAccess/2-nested/input.p
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/* formatterSettingsOverride */
/* { "AblFormatter.arrayAccessFormatting": true}*/

message a[ b[ i / 3 ] + 1].
4 changes: 4 additions & 0 deletions resources/functionalTests/arrayAccess/2-nested/target.p
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/* formatterSettingsOverride */
/* { "AblFormatter.arrayAccessFormatting": true}*/

message a[b[i / 3] + 1].
4 changes: 4 additions & 0 deletions resources/functionalTests/arrayAccess/3-nested/input.p
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/* formatterSettingsOverride */
/* { "AblFormatter.arrayAccessFormatting": true}*/

a [ b [ myFunc ( i ) ] ] = d [ e [ f [ i ] ] - g [ j ] ]
4 changes: 4 additions & 0 deletions resources/functionalTests/arrayAccess/3-nested/target.p
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/* formatterSettingsOverride */
/* { "AblFormatter.arrayAccessFormatting": true}*/

a[b[myFunc ( i )]] = d[e[f[i]] - g[j]]
3 changes: 3 additions & 0 deletions src/model/SyntaxNodeType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,14 @@ export enum SyntaxNodeType {
EnumDefinition = "enum_definition",
TypeTuning = "type_tuning",
AccessTuning = "access_tuning",
ArrayAccess = "array_access",
Comment = "comment",
Getter = "getter",
Setter = "setter",
LeftParenthesis = "(",
RightParenthesis = ")",
LeftBracket = "[",
RightBracket = "]",
Label = "label",
Parameters = "parameters",
FunctionParameter = "function_parameter",
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 @@ -14,6 +14,7 @@ import { EnumFormatter } from "../formatters/enum/EnumFormatter";
import { VariableDefinitionFormatter } from "../formatters/variableDefinition/VariableDefinitionFormatter";
import { ProcedureParameterFormatter } from "../formatters/procedureParameter/ProcedureParameterFormatter";
import { FunctionParameterFormatter } from "../formatters/functionParameter/FunctionParameterFormatter";
import { ArrayAccessFormatter } from "../formatters/arrayAccess/ArrayAccessFormatter";

// 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 {
Expand All @@ -33,4 +34,5 @@ export function enableFormatterDecorators(): void {
EnumFormatter;
ProcedureParameterFormatter;
FunctionParameterFormatter;
ArrayAccessFormatter;
}
68 changes: 68 additions & 0 deletions src/v2/formatters/arrayAccess/ArrayAccessFormatter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
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 { IConfigurationManager } from "../../../utils/IConfigurationManager";
import { ArrayAccessSettings } from "./ArrayAccessSettings";

@RegisterFormatter
export class ArrayAccessFormatter extends AFormatter implements IFormatter {
public static readonly formatterLabel = "arrayAccessFormatting";
private readonly settings: ArrayAccessSettings;

public constructor(configurationManager: IConfigurationManager) {
super(configurationManager);
this.settings = new ArrayAccessSettings(configurationManager);
}

match(node: Readonly<SyntaxNode>): boolean {
if (node.type === SyntaxNodeType.ArrayAccess) {
return true;
}

return false;
}

parse(
node: Readonly<SyntaxNode>,
fullText: Readonly<FullText>
): CodeEdit | CodeEdit[] | undefined {
const oldText = FormatterHelper.getCurrentText(node, fullText);
const text = this.collectString(node, fullText);
return this.getCodeEdit(node, oldText, text, fullText);
}

private collectString(
node: SyntaxNode,
fullText: Readonly<FullText>
): string {
let resultString = "";
node.children.forEach((child) => {
resultString = resultString.concat(this.getString(child, fullText));
});
return resultString;
}

private getString(node: SyntaxNode, fullText: Readonly<FullText>): string {
let newString = "";
if (node.type === SyntaxNodeType.LeftBracket) {
newString = FormatterHelper.getCurrentText(node, fullText).trim();
} else if (
node.type === SyntaxNodeType.RightBracket ||
(node.previousSibling !== null &&
node.previousSibling.type === SyntaxNodeType.LeftBracket)
) {
newString = FormatterHelper.getCurrentText(
node,
fullText
).trimStart();
} else {
newString = FormatterHelper.getCurrentText(node, fullText);
}
return newString;
}
}
8 changes: 8 additions & 0 deletions src/v2/formatters/arrayAccess/ArrayAccessSettings.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { ASettings } from "../ASettings";

export class ArrayAccessSettings extends ASettings {
//empty block settings
public ArrayAccessFormatting() {
return !!this.configurationManager.get("arrayAccessFormatting");
}
}

0 comments on commit 61666b1

Please sign in to comment.