Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

211 ENUM Formatter #232

Merged
merged 8 commits into from
Sep 30, 2024
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,21 @@
],
"description": "Add a new line before the ELSE?"
},
"AblFormatter.enumFormatting": {
"order": 1300,
"type": "boolean",
"default": "true",
"description": "Enable ENUM formatting"
},
"AblFormatter.enumFormattingEndDotLocation": {
"order": 1301,
"type": "string",
"default": "Same",
"enum": [
"New",
"Same"
]
},
"AblFormatter.showTreeInfoOnHover": {
"order": 10100,
"type": "boolean",
Expand Down
21 changes: 21 additions & 0 deletions resources/functionalTests/enum/1-enum-with-values/input.p
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/* formatterSettingsOverride */
/* { "AblFormatter.bodyFormatting": true,
"AblFormatter.enumFormatting": true}*/

ENUM Oper:
DEFINE ENUM
Invalid = 0
Create = 1
Delete = 2
BeforeUpdate = 3
AfterUpdate = 4
FieldDelete = 5
PartitionDrop = 6
PartitionTruncate = 7
PartitionDeallocate = 8
MTPartitionDeallocate = 9
MTPartitionDrop = 10
MergePartitionDrop = 11
FieldAdd = 12.
END ENUM.

20 changes: 20 additions & 0 deletions resources/functionalTests/enum/1-enum-with-values/target.p
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/* formatterSettingsOverride */
/* { "AblFormatter.bodyFormatting": true,
"AblFormatter.enumFormatting": true}*/

ENUM Oper:
define ENUM Invalid = 0
Create = 1
Delete = 2
BeforeUpdate = 3
AfterUpdate = 4
FieldDelete = 5
PartitionDrop = 6
PartitionTruncate = 7
PartitionDeallocate = 8
MTPartitionDeallocate = 9
MTPartitionDrop = 10
MergePartitionDrop = 11
FieldAdd = 12.
END ENUM.

14 changes: 14 additions & 0 deletions resources/functionalTests/enum/1-flag/input.p
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/* formatterSettingsOverride */
/* { "AblFormatter.bodyFormatting": true,
"AblFormatter.enumFormatting": true}*/

ENUM LiteraryMood FLAGS:
define ENUM None = 0
Joyful
Melancholy
Heroic
Whimsical
Epic
JoyfulMelancholy = Joyful,Melancholy
Tragic = Epic.
END ENUM.
14 changes: 14 additions & 0 deletions resources/functionalTests/enum/1-flag/target.p
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/* formatterSettingsOverride */
/* { "AblFormatter.bodyFormatting": true,
"AblFormatter.enumFormatting": true}*/

ENUM LiteraryMood FLAGS:
define ENUM None = 0
Joyful
Melancholy
Heroic
Whimsical
Epic
JoyfulMelancholy = Joyful,Melancholy
Tragic = Epic.
END ENUM.
10 changes: 10 additions & 0 deletions resources/functionalTests/enum/1-simple/input.p
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/* formatterSettingsOverride */
/* { "AblFormatter.blockFormatting": true}*/

ENUM Weather:
define ENUM Rainy
Default = Sunny
Cloudy
Snowy
Apocalyptic .
END ENUM.
10 changes: 10 additions & 0 deletions resources/functionalTests/enum/1-simple/target.p
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/* formatterSettingsOverride */
/* { "AblFormatter.blockFormatting": true}*/

ENUM Weather:
define ENUM Rainy
Default = Sunny
Cloudy
Snowy
Apocalyptic.
END ENUM.
29 changes: 29 additions & 0 deletions resources/functionalTests/enum/2-end-dot-new copy/input.p
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/* formatterSettingsOverride */
/* { "AblFormatter.bodyFormatting": true,
"AblFormatter.enumFormatting": true,
"AblFormatter.assignFormattingEndDotLocation": "New"}*/

enum OpenEdge.Mobile.PushNotificationFilterOperandEnum:
define enum

Equals = 1
NotEqual
Includes
NotIncludes
Matches
GreaterThan
GreaterOrEqual LessThan
LessOrEqual
Near
NearSphere
Within
Intersects
All
And
Or
Not
Nor


.
end enum.
26 changes: 26 additions & 0 deletions resources/functionalTests/enum/2-end-dot-new copy/target.p
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/* formatterSettingsOverride */
/* { "AblFormatter.bodyFormatting": true,
"AblFormatter.enumFormatting": true,
"AblFormatter.assignFormattingEndDotLocation": "New"}*/

enum OpenEdge.Mobile.PushNotificationFilterOperandEnum:
define enum Equals = 1
NotEqual
Includes
NotIncludes
Matches
GreaterThan
GreaterOrEqual
LessThan
LessOrEqual
Near
NearSphere
Within
Intersects
All
And
Or
Not
Nor
.
end enum.
11 changes: 11 additions & 0 deletions resources/functionalTests/enum/2-end-dot-new/input.p
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/* formatterSettingsOverride */
/* { "AblFormatter.bodyFormatting": true,
"AblFormatter.enumFormatting": true,
"AblFormatter.assignFormattingEndDotLocation": "New"}*/

enum DatabaseOptionTypeEnum:
define enum Auditing = 1
GeneralSecurity
AuthenticationGateway
.
end enum.
11 changes: 11 additions & 0 deletions resources/functionalTests/enum/2-end-dot-new/target.p
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/* formatterSettingsOverride */
/* { "AblFormatter.bodyFormatting": true,
"AblFormatter.enumFormatting": true,
"AblFormatter.assignFormattingEndDotLocation": "New"}*/

enum DatabaseOptionTypeEnum:
define enum Auditing = 1
GeneralSecurity
AuthenticationGateway
.
end enum.
10 changes: 10 additions & 0 deletions resources/functionalTests/enum/2-multiple-enums/input.p
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/* formatterSettingsOverride */
/* { "AblFormatter.bodyFormatting": true,
"AblFormatter.enumFormatting": true}*/

ENUM Direction:
define ENUM North
South.
define ENUM Dog
Cat.
END ENUM.
10 changes: 10 additions & 0 deletions resources/functionalTests/enum/2-multiple-enums/target.p
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/* formatterSettingsOverride */
/* { "AblFormatter.bodyFormatting": true,
"AblFormatter.enumFormatting": true}*/

ENUM Direction:
define ENUM North
South.
define ENUM Dog
Cat.
END ENUM.
Binary file modified resources/tree-sitter-abl.wasm
Binary file not shown.
3 changes: 3 additions & 0 deletions src/model/SyntaxNodeType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ export enum SyntaxNodeType {
ProcedureStatement = "procedure_statement",
RepeatStatement = "repeat_statement",
OnStatement = "on_statement",
EnumStatement = "enum_statement",
EnumMember = "enum_member",
EnumDefinition = "enum_definition",
Getter = "getter",
Setter = "setter",
LeftParenthesis = "(",
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 @@ -9,6 +9,7 @@ import { IfFunctionFormatter } from "../formatters/ifFunction/IfFunctionFormatte
import { TempTableFormatter } from "../formatters/tempTable/TempTableFormatter";
import { PropertyFormatter } from "../formatters/property/PropertyFormatter";
import { BodyFormatter } from "../formatters/body/BodyFormatter";
import { EnumFormatter } from "../formatters/enum/EnumFormatter";

// 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 @@ -23,4 +24,5 @@ export function enableFormatterDecorators(): void {
TempTableFormatter;
PropertyFormatter;
IfFunctionFormatter;
EnumFormatter;
}
107 changes: 107 additions & 0 deletions src/v2/formatters/enum/EnumFormatter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
import { SyntaxNode } from "web-tree-sitter";
import { RegisterFormatter } from "../../formatterFramework/formatterDecorator";
import { IFormatter } from "../../formatterFramework/IFormatter";
import { CodeEdit } from "../../model/CodeEdit";
import { FullText } from "../../model/FullText";
import { AFormatter } from "../AFormatter";
import { EnumSettings } from "./EnumSettings";
import { IConfigurationManager } from "../../../utils/IConfigurationManager";
import { SyntaxNodeType } from "../../../model/SyntaxNodeType";
import { FormatterHelper } from "../../formatterFramework/FormatterHelper";

@RegisterFormatter
export class EnumFormatter extends AFormatter implements IFormatter {
public static readonly formatterLabel = "enumFormatting";
private readonly settings: EnumSettings;
private startColumn = 0;
private alignColumn = 0;

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

match(node: Readonly<SyntaxNode>): boolean {
return node.type === SyntaxNodeType.EnumDefinition;
}
parse(
node: Readonly<SyntaxNode>,
fullText: Readonly<FullText>
): CodeEdit | CodeEdit[] | undefined {
const oldText = FormatterHelper.getCurrentText(node, fullText);

const newText = this.collectEnumStructure(node, fullText);
return this.getCodeEdit(node, oldText, newText, fullText);
}

private collectEnumStructure(
node: SyntaxNode,
fullText: Readonly<FullText>
): string {
this.startColumn = FormatterHelper.getActualStatementIndentation(
node,
fullText
);
let foundFirstMember = false;
let resultString = "";
node.children.forEach((child) => {
const childString = this.getEnumExpressionString(
child,
fullText,
foundFirstMember
);
if (!foundFirstMember && child.type === SyntaxNodeType.EnumMember) {
foundFirstMember = true;
this.alignColumn =
this.startColumn + resultString.trim().length + 1; // +1 for space between member and DEFINE ENUM
}
resultString = resultString.concat(childString);
});
if (this.settings.endDotNewLine()) {
resultString +=
fullText.eolDelimiter + " ".repeat(this.alignColumn) + ".";
} else {
resultString += ".";
}
return resultString;
}

private getEnumExpressionString(
node: SyntaxNode,
fullText: Readonly<FullText>,
foundFirstMember: boolean
): string {
let newString = "";

switch (node.type) {
case SyntaxNodeType.DefineKeyword:
newString =
fullText.eolDelimiter +
FormatterHelper.getCurrentText(node, fullText).trim();
break;
case SyntaxNodeType.EnumMember:
newString = foundFirstMember
? fullText.eolDelimiter +
" ".repeat(this.alignColumn) +
FormatterHelper.getCurrentText(node, fullText).trim()
: " " +
FormatterHelper.getCurrentText(node, fullText).trim();
break;
case SyntaxNodeType.DotKeyword:
newString = FormatterHelper.getCurrentText(
node,
fullText
).trim();
break;
default:
const text = FormatterHelper.getCurrentText(
node,
fullText
).trim();
newString = text.length === 0 ? "" : " " + text;
break;
}

return newString;
}
}
15 changes: 15 additions & 0 deletions src/v2/formatters/enum/EnumSettings.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { ASettings } from "../ASettings";

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

public endDotNewLine() {
return (
this.configurationManager.get("assignFormattingEndDotLocation") ===
"New"
);
}
}