Skip to content

Commit

Permalink
Add more support for affixes (#309)
Browse files Browse the repository at this point in the history
Fixes #279
  • Loading branch information
pri-kise authored Dec 19, 2024
1 parent b1b5b51 commit ea7779c
Show file tree
Hide file tree
Showing 3 changed files with 139 additions and 12 deletions.
81 changes: 69 additions & 12 deletions src/NAVObject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ export class NAVObject {
var reg = NAVObjectAction.actionRegEx();
var result;
while ((result = reg.exec(this.NAVObjectText)) !== null) {
this.objectActions.push(new NAVObjectAction(result[1], this.objectType, this._workSpaceSettings[Settings.ObjectNamePrefix], this._workSpaceSettings[Settings.ObjectNameSuffix]))
this.objectActions.push(new NAVObjectAction(result[1], this.objectType, this._workSpaceSettings[Settings.ObjectNamePrefix], this._workSpaceSettings[Settings.ObjectNameSuffix], this._workSpaceSettings[Settings.MandatoryAffixes]))
}

var reg = NAVTableField.fieldRegEx();
Expand All @@ -320,13 +320,13 @@ export class NAVObject {
var reg = NAVPageField.fieldRegEx();
var result;
while ((result = reg.exec(this.NAVObjectText)) !== null) {
this.pageFields.push(new NAVPageField(result[1], this.objectType, this._workSpaceSettings[Settings.ObjectNamePrefix], this._workSpaceSettings[Settings.ObjectNameSuffix]))
this.pageFields.push(new NAVPageField(result[1], this.objectType, this._workSpaceSettings[Settings.ObjectNamePrefix], this._workSpaceSettings[Settings.ObjectNameSuffix], this._workSpaceSettings[Settings.MandatoryAffixes]))
}

var reg = NAVPageGroup.fieldRegEx();
var result;
while ((result = reg.exec(this.NAVObjectText)) !== null) {
this.pageGroups.push(new NAVPageGroup(result[1], this.objectType, this._workSpaceSettings[Settings.ObjectNamePrefix], this._workSpaceSettings[Settings.ObjectNameSuffix]))
this.pageGroups.push(new NAVPageGroup(result[1], this.objectType, this._workSpaceSettings[Settings.ObjectNamePrefix], this._workSpaceSettings[Settings.ObjectNameSuffix], this._workSpaceSettings[Settings.MandatoryAffixes]))
}
}

Expand Down Expand Up @@ -538,16 +538,30 @@ class NAVObjectAction {
public fullActionText: string;
private _prefix: string;
private _suffix: string;
private _affixes: string[];
private _objectType: string;

public static actionRegEx(): RegExp {
return /.*( (action\("?)([ a-zA-Z0-9._/&-]+)"?\))/g
}

get nameFixed(): string {
if (!this._prefix && !this._suffix) { return this.name }
if (!this._prefix && !this._suffix && !this.hasAffixesDefined()) { return this.name }
if (!this._objectType.toLocaleLowerCase().endsWith('extension')) { return this.name }; //only for extensionobjects

if (this.hasAffixesDefined()) {
var affixNeeded = true;
this._affixes.forEach(affix => {
if (this.name.startsWith(affix) || this.name.endsWith(affix)) {
affixNeeded = false;
return
}
});
if (!affixNeeded) {
return this.name;
}
}

let result = this.name
if (this._prefix && !this.name.startsWith(this._prefix)) {
result = this._prefix + result
Expand All @@ -559,15 +573,16 @@ class NAVObjectAction {
}

get fullActionTextFixed(): string {
if (!this._prefix && !this._suffix) { return this.fullActionText };
if (!this._prefix && !this._suffix && !this.hasAffixesDefined()) { return this.fullActionText };

return " action(" + StringFunctions.encloseInQuotesIfNecessary(this.nameFixed) + ")"
}

constructor(fullActionText: string, objectType: string, prefix?: string, suffix?: string) {
constructor(fullActionText: string, objectType: string, prefix?: string, suffix?: string, affixes?: string[]) {
this.fullActionText = fullActionText;
this._prefix = prefix ? prefix : null;
this._suffix = suffix ? suffix : null;
this._affixes = affixes ? affixes : null;
this._objectType = objectType;

this.parseActionText();
Expand All @@ -580,6 +595,10 @@ class NAVObjectAction {
this.name = result[3];
}
}

private hasAffixesDefined(): boolean {
return (Array.isArray(this._affixes) && this._affixes.length > 0)
}
}

class NAVTableField {
Expand Down Expand Up @@ -661,16 +680,30 @@ class NAVPageField {
private _objectType: string;
private _prefix: string;
private _suffix: string;
private _affixes: string[];

public static fieldRegEx(): RegExp {
return /.*(field\( *"?([ a-zA-Z0-9._/&%\/()-]+)"? *; *([" a-zA-Z0-9._/&%\/()-]+(\[([1-9]\d*)\])?) *\))/g;
}

get nameFixed(): string {
if (!this._prefix && !this._suffix) { return this.name }
if (!this._prefix && !this._suffix && !this.hasAffixesDefined()) { return this.name }
if (!this._objectType.toLocaleLowerCase().endsWith('extension')) { return this.name }; //only for extensionobjects
if (this._objectType.toLocaleLowerCase().startsWith('table')) { return this.name }; //table-fields should not be parsed as pagefields

if (this.hasAffixesDefined()) {
var affixNeeded = true;
this._affixes.forEach(affix => {
if (this.name.startsWith(affix) || this.name.endsWith(affix)) {
affixNeeded = false;
return
}
});
if (!affixNeeded) {
return this.name;
}
}

let result = this.name
if (this._prefix && !this.name.startsWith(this._prefix)) {
result = this._prefix + result
Expand All @@ -682,15 +715,16 @@ class NAVPageField {
}

get fullFieldTextFixed(): string {
if (!this._prefix && !this._suffix) { return this.fullFieldText }
if (!this._prefix && !this._suffix && !this.hasAffixesDefined()) { return this.fullFieldText }

return "field(" + StringFunctions.encloseInQuotesIfNecessary(this.nameFixed) + "; " + this.expression + ")"
}

constructor(fullFieldText: string, objectType: string, prefix?: string, suffix?: string) {
constructor(fullFieldText: string, objectType: string, prefix?: string, suffix?: string, affixes?: string[]) {
this.fullFieldText = fullFieldText;
this._prefix = prefix ? prefix : null;
this._suffix = suffix ? suffix : null;
this._affixes = affixes ? affixes : null;
this._objectType = objectType;

this.parseFieldText();
Expand All @@ -705,6 +739,10 @@ class NAVPageField {
}
}

private hasAffixesDefined(): boolean {
return (Array.isArray(this._affixes) && this._affixes.length > 0)
}

}

class NAVPageGroup {
Expand All @@ -713,15 +751,29 @@ class NAVPageGroup {
private _objectType: string;
private _prefix: string;
private _suffix: string;
private _affixes: string[];

public static fieldRegEx(): RegExp {
return /.*(group\( *"?([ a-zA-Z0-9._/&%\/()-]+)"? *\))/g;
}

get nameFixed(): string {
if (!this._prefix && !this._suffix) { return this.name }
if (!this._prefix && !this._suffix && !this.hasAffixesDefined()) { return this.name }
if (!this._objectType.toLocaleLowerCase().endsWith('extension')) { return this.name }; //only for extensionobjects

if (this.hasAffixesDefined()) {
var affixNeeded = true;
this._affixes.forEach(affix => {
if (this.name.startsWith(affix) || this.name.endsWith(affix)) {
affixNeeded = false;
return
}
});
if (!affixNeeded) {
return this.name;
}
}

let result = this.name
if (this._prefix && !this.name.startsWith(this._prefix)) {
result = this._prefix + result
Expand All @@ -733,15 +785,16 @@ class NAVPageGroup {
}

get fullGroupTextFixed(): string {
if (!this._prefix && !this._suffix) { return this.fullGroupText }
if (!this._prefix && !this._suffix && !this.hasAffixesDefined()) { return this.fullGroupText }

return "group(" + StringFunctions.encloseInQuotesIfNecessary(this.nameFixed) + ")"
}

constructor(fullGroupText: string, objectType: string, prefix?: string, suffix?: string) {
constructor(fullGroupText: string, objectType: string, prefix?: string, suffix?: string, affixes?: string[]) {
this.fullGroupText = fullGroupText;
this._prefix = prefix ? prefix : null;
this._suffix = suffix ? suffix : null;
this._affixes = affixes ? affixes : null;
this._objectType = objectType;

this.parseFieldText();
Expand All @@ -754,6 +807,10 @@ class NAVPageGroup {
this.name = result[2].trim().toString();
}
}

private hasAffixesDefined(): boolean {
return (Array.isArray(this._affixes) && this._affixes.length > 0)
}

}

Expand Down
19 changes: 19 additions & 0 deletions src/test/suite/NAVObject.PrefexAndSuffix.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,25 @@ suite("NAVObject ObjectNamePrefix Tests", () => {
assert.strictEqual(action.name, action.nameFixed);
})
});

test("Pageextension - avoid setting double affixes", () => {
let testSettings = Settings.GetConfigSettings(null)
testSettings[Settings.MandatoryAffixes] = ['waldo'];

let navTestObject = NAVTestObjectLibrary.getPageExtensionWithWaldoSuffix();
let navObject = new NAVObject(navTestObject.ObjectText, testSettings, navTestObject.ObjectFileName)

assert.notStrictEqual(navObject.objectActions.length, 0)

let navObject2 = new NAVObject(navObject.NAVObjectTextFixed, testSettings, navTestObject.ObjectFileName)

assert.strictEqual(navObject2.objectName.endsWith(testSettings[Settings.MandatoryAffixes][0]), true);
assert.strictEqual(navObject2.objectName, navObject2.objectNameFixed)
navObject2.objectActions.forEach(action => {
assert.strictEqual(action.name.endsWith(testSettings[Settings.MandatoryAffixes][0]), true);
assert.strictEqual(action.name, action.nameFixed);
})
});
test("Page - avoid removing prefixes from actions", () => {
let testSettings = Settings.GetConfigSettings(null)
testSettings[Settings.ObjectNamePrefix] = 'waldo';
Expand Down
51 changes: 51 additions & 0 deletions src/test/suite/NAVTestObjectLibrary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,57 @@ export function getPageExtensionWithWaldoPrefixWithActions(): NAVTestObject {
return object;
}

export function getPageExtensionWithWaldoSuffix(): NAVTestObject {
let object = new NAVTestObject;

object.ObjectFileName = 'SomeFile.al'
object.ObjectText = `pageextension 50100 "Some Page Extwaldo" extends "Customer List" //22
{
layout
{
addfirst(Content)
{
field("Telex No. waldo"; "Telex No.")
{
ApplicationArea = All;
}
field("Telex No. waldo"; "Telex No.")
{
ApplicationArea = All;
}
field("Telex No. waldo"; "Telex No.")
{
ApplicationArea = All;
}
}
}
actions
{
addfirst("&Customer")
{
action(SomeActionwaldo)
{
RunObject = page "_Empl. Absences by Cat. Matrix";
}
group(someGroupwaldo)
{
action(SomeAction2waldo)
{
RunObject = page "_Empl. Absences by Cat. Matrix";
}
action("Some Action 3 waldo")
{
RunObject = page "_Empl. Absences by Cat. Matrix";
}
}
}
}
}
`
return object;
}

export function getPageExtensionWithSlashInFileName(): NAVTestObject {
let object = new NAVTestObject;

Expand Down

0 comments on commit ea7779c

Please sign in to comment.