Skip to content

Commit

Permalink
Merge pull request #285 from BalticAmadeus/283-arrayaccess-formatter
Browse files Browse the repository at this point in the history
285 Implement ArrayAccessFormatter
  • Loading branch information
PauliusKu authored Jan 20, 2025
2 parents 40a3ffa + 5402c1e commit dac1bcd
Show file tree
Hide file tree
Showing 21 changed files with 192 additions and 2 deletions.
16 changes: 16 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,22 @@
],
"description": "Align parameter types?"
},
"AblFormatter.arrayAccessFormatting": {
"order": 1700,
"type": "boolean",
"default": "true",
"description": "Enable ARRAY ACCESS formatting"
},
"AblFormatter.arrayAccessFormattingAddSpaceAfterComma": {
"order": 1701,
"type": "string",
"default": "No",
"enum": [
"Yes",
"No"
],
"description": "Add space after comma?"
},
"AblFormatter.showTreeInfoOnHover": {
"order": 10100,
"type": "boolean",
Expand Down
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]]
4 changes: 4 additions & 0 deletions resources/functionalTests/arrayAccess/4-array-literal/input.p
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/* formatterSettingsOverride */
/* { "AblFormatter.arrayAccessFormatting": true}*/

VAR INT[4] myArray = [ 10,11,12,13 ].
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/* formatterSettingsOverride */
/* { "AblFormatter.arrayAccessFormatting": true}*/

VAR INT[4] myArray = [10,11,12,13].
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/* formatterSettingsOverride */
/* { "AblFormatter.arrayAccessFormatting": true}*/

DEFINE VARIABLE myArray AS INTEGER EXTENT 5 NO-UNDO INITIAL [ 10 , 11 , 12 , 13 , 14 ].

DISPLAY myArray [ 1 FOR 5 ].
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/* formatterSettingsOverride */
/* { "AblFormatter.arrayAccessFormatting": true}*/

define VARIABLE myArray AS INTEGER EXTENT 5 NO-UNDO INITIAL [10,11,12,13,14].

DISPLAY myArray[1 FOR 5].
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/* formatterSettingsOverride */
/* { "AblFormatter.arrayAccessFormatting": true,
"AblFormatter.arrayAccessFormattingAddSpaceAfterComma": "Yes"}*/

VAR INT[] monthlySalesQuotas = [100500,125000,125000,175000,150000,155000,145000].
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/* formatterSettingsOverride */
/* { "AblFormatter.arrayAccessFormatting": true,
"AblFormatter.arrayAccessFormattingAddSpaceAfterComma": "Yes"}*/

VAR INT[] monthlySalesQuotas = [100500, 125000, 125000, 175000, 150000, 155000, 145000].
6 changes: 6 additions & 0 deletions resources/functionalTests/arrayAccess/5-nested/input.p
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/* formatterSettingsOverride */
/* { "AblFormatter.arrayAccessFormatting": true}*/

DISPLAY myArray [
j + 2 * (k - j) / 2
].
4 changes: 4 additions & 0 deletions resources/functionalTests/arrayAccess/5-nested/target.p
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/* formatterSettingsOverride */
/* { "AblFormatter.arrayAccessFormatting": true}*/

DISPLAY myArray[j + 2 * (k - j) / 2].
2 changes: 1 addition & 1 deletion resources/functionalTests/defineVariable/2no-undo4/input.p
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ DEFINE VARIABLE iAge AS INTEGER NO-UNDO LABEL "Age of Person" INITIAL 0.
DEFINE VARIABLE dBirthDate AS DATE NO-UNDO FORMAT "99/99/9999" INITIAL TODAY.
DEFINE VARIABLE hBuffer AS HANDLE NO-UNDO INITIAL BUFFER Customer:HANDLE.
DEFINE VARIABLE mData AS MEMPTR NO-UNDO INITIAL ?.
DEFINE VARIABLE cJson AS CHARACTER NO-UNDO EXTENT 5 INITIAL ["", "", "", "", ""].
DEFINE VARIABLE cJson AS CHARACTER NO-UNDO EXTENT 5 INITIAL ["","","","",""].
DEFINE VARIABLE rAmount AS DECIMAL NO-UNDO FORMAT "->,>>>,>>9.99" INITIAL 0.
DEFINE VARIABLE lFlag AS LOGICAL NO-UNDO INITIAL FALSE.
DEFINE VARIABLE cXml AS LONGCHAR NO-UNDO INITIAL "".
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ define VARIABLE iAge AS INTEGER NO-UNDO LABEL "Age of Person" INITIAL 0.
define VARIABLE dBirthDate AS DATE NO-UNDO FORMAT "99/99/9999" INITIAL TODAY.
define VARIABLE hBuffer AS HANDLE NO-UNDO INITIAL BUFFER Customer:HANDLE.
define VARIABLE mData AS MEMPTR NO-UNDO INITIAL ?.
define VARIABLE cJson AS CHARACTER NO-UNDO EXTENT 5 INITIAL ["", "", "", "", ""].
define VARIABLE cJson AS CHARACTER NO-UNDO EXTENT 5 INITIAL ["","","","",""].
define VARIABLE rAmount AS DECIMAL NO-UNDO FORMAT "->,>>>,>>9.99" INITIAL 0.
define VARIABLE lFlag AS LOGICAL NO-UNDO INITIAL FALSE.
define VARIABLE cXml AS LONGCHAR NO-UNDO INITIAL "".
4 changes: 4 additions & 0 deletions src/model/SyntaxNodeType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,15 @@ export enum SyntaxNodeType {
EnumDefinition = "enum_definition",
TypeTuning = "type_tuning",
AccessTuning = "access_tuning",
ArrayAccess = "array_access",
ArrayLiteral = "array_literal",
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;
}
89 changes: 89 additions & 0 deletions src/v2/formatters/arrayAccess/ArrayAccessFormatter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
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;
private formattingArrayLiteral: boolean = false;

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

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

return false;
}

parse(
node: Readonly<SyntaxNode>,
fullText: Readonly<FullText>
): CodeEdit | CodeEdit[] | undefined {
this.formattingArrayLiteral = node.type === SyntaxNodeType.ArrayLiteral;
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();
if (this.formattingArrayLiteral) {
newString = " " + newString;
}
} else if (
node.type === SyntaxNodeType.RightBracket ||
(node.previousSibling !== null &&
node.previousSibling.type === SyntaxNodeType.LeftBracket)
) {
newString = FormatterHelper.getCurrentText(
node,
fullText
).trimStart();
} else {
if (this.formattingArrayLiteral) {
newString = FormatterHelper.getCurrentText(
node,
fullText
).trim();
if (
node.type === SyntaxNodeType.CommaKeyword &&
this.settings.addSpaceAfterComma()
) {
newString += " ";
}
} else {
newString = FormatterHelper.getCurrentText(node, fullText);
}
}
return newString;
}
}
15 changes: 15 additions & 0 deletions src/v2/formatters/arrayAccess/ArrayAccessSettings.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { ASettings } from "../ASettings";

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

0 comments on commit dac1bcd

Please sign in to comment.