Skip to content

Commit

Permalink
First draft for Support of multiple Prefixes (#254)
Browse files Browse the repository at this point in the history
* First draft for Prefixes

* Switch to AppSourceCop.json
  • Loading branch information
pri-kise committed Dec 5, 2022
1 parent fa9209d commit 9e63190
Show file tree
Hide file tree
Showing 4 changed files with 138 additions and 9 deletions.
60 changes: 51 additions & 9 deletions src/NAVObject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ export class NAVObject {
var reg = NAVTableField.fieldRegEx();
var result;
while ((result = reg.exec(this.NAVObjectText)) !== null) {
this.tableFields.push(new NAVTableField(result[1], this.objectType, this._workSpaceSettings[Settings.ObjectNamePrefix], this._workSpaceSettings[Settings.ObjectNameSuffix]))
this.tableFields.push(new NAVTableField(result[1], this.objectType, this._workSpaceSettings[Settings.ObjectNamePrefix], this._workSpaceSettings[Settings.ObjectNameSuffix], this._workSpaceSettings[Settings.MandatoryAffixes]))
}

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

this.NAVObjectText = initNAVObjectText;
Expand Down Expand Up @@ -548,17 +548,32 @@ class NAVTableField {
public type: string;
private _objectType: string;
private _prefix: string;
private _affixes: string[];
private _suffix: string;

public static fieldRegEx(): RegExp {
return /.*(field\((\d+); *"?([ 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
let result = this.name;
if (this._prefix && !this.name.startsWith(this._prefix)) {
result = this._prefix + result
}
Expand All @@ -569,15 +584,16 @@ class NAVTableField {
}

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

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

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 @@ -591,6 +607,11 @@ class NAVTableField {
this.name = result[3].trim().toString();
this.type = result[4].trim().toString();
}
}

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

Expand Down Expand Up @@ -704,16 +725,31 @@ class NAVReportColumn {
private _objectType: string;
private _prefix: string;
private _suffix: string;
private _affixes: string[];

public static columnRegEx(): RegExp {
// return /.*(column\( *"?([ a-zA-Z0-9._/&%\/()-]+)"? *; *([" a-zA-Z0-9._/&%\/()-]+) *\))/g;
return /.*(column\( *"?([ 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.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.replace(" ", ""))) {
result = this._prefix + result
Expand All @@ -726,15 +762,16 @@ class NAVReportColumn {
}

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

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

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

this.parseColumnText();
Expand All @@ -748,6 +785,11 @@ class NAVReportColumn {
this.expression = result[3].trim().toString();
}
}

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

}

22 changes: 22 additions & 0 deletions src/Settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ export class Settings {
static readonly DependencyGraphExcludePublishers = 'DependencyGraph.ExcludePublishers';
static readonly DependencyGraphRemovePrefix = 'DependencyGraph.RemovePrefix';

static readonly MandatoryAffixes = 'AppSourceCop.MandatoryAffixes';

private static config: vscode.WorkspaceConfiguration;
private static launchconfig: vscode.WorkspaceConfiguration;
Expand Down Expand Up @@ -148,10 +149,23 @@ export class Settings {
this.SettingCollection[this.SandboxName] = currentLaunchConfig[0].sandboxName;
}

private static getAppSourceCopSettings(ResourceUri: vscode.Uri) {
let appSourceCopSettings = ResourceUri ?
require(join(vscode.workspace.getWorkspaceFolder(ResourceUri).uri.fsPath, "AppSourceCop.json")) :
vscode.window.activeTextEditor ?
require(join(vscode.workspace.getWorkspaceFolder(vscode.window.activeTextEditor.document.uri).uri.fsPath, "AppSourceCop.json")) :
vscode.workspace.workspaceFolders ?
require(join(vscode.workspace.workspaceFolders[0].uri.fsPath, "AppSourceCop.json")): null;
if (appSourceCopSettings) {
this.SettingCollection[this.MandatoryAffixes] = appSourceCopSettings.mandatoryAffixes
}
}

public static GetAllSettings(ResourceUri: vscode.Uri) {
this.getConfigSettings(ResourceUri);
this.getAppSettings(ResourceUri);
this.getLaunchSettings(ResourceUri);
this.getAppSourceCopSettings(ResourceUri);

return this.SettingCollection;
}
Expand All @@ -162,6 +176,12 @@ export class Settings {
return this.SettingCollection;
}

public static GetAppSourceCopSettings(ResourceUri: vscode.Uri) {
this.getAppSourceCopSettings(ResourceUri);

return this.SettingCollection;
}

public static GetLaunchSettings(ResourceUri: vscode.Uri) {
this.getLaunchSettings(ResourceUri);

Expand All @@ -170,6 +190,8 @@ export class Settings {

public static GetConfigSettings(ResourceUri: vscode.Uri) {
this.getConfigSettings(ResourceUri);
//TODO: How to integrate AppSourceCopSettings into NAVObject?
this.getAppSourceCopSettings(ResourceUri);

return this.SettingCollection;
}
Expand Down
46 changes: 46 additions & 0 deletions src/test/suite/NAVObject.PrefexAndSuffix.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,30 @@ suite("NAVObject ObjectNamePrefix Tests", () => {
assert.strictEqual(field.name.endsWith(testSettings[Settings.ObjectNameSuffix]), true)
})
});


test("Tableextension - Don't set double Affix", () => {
let testSettings = Settings.GetConfigSettings(null)
testSettings[Settings.ObjectNamePrefix] = 'waldo';
testSettings[Settings.MandatoryAffixes] = ['waldo'];

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

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

let navObject2 = new NAVObject(navObject.NAVObjectTextFixed, testSettings, navTestObject.ObjectFileName)
navObject2.tableFields.forEach(tableField => {
assert.strictEqual(tableField.name.endsWith(testSettings[Settings.MandatoryAffixes][0]), true);
assert.strictEqual(tableField.name, tableField.nameFixed);
})

for (let i = 0; i < navObject2.tableFields.length; i++) {
assert.strictEqual(navObject2.tableFields[i].name, navObject.tableFields[i].name)
}
});


test("Tableextension - Don't change fieldnumber", () => {
let testSettings = Settings.GetConfigSettings(null)
testSettings[Settings.ObjectNameSuffix] = 'waldo';
Expand Down Expand Up @@ -345,6 +369,28 @@ suite("NAVObject ObjectNamePrefix Tests", () => {
assert.strictEqual(navObject2.reportColumns[i].name, navObject.reportColumns[i].name)
}
});

test("Reportextension - Don't set double Affix", () => {
let testSettings = Settings.GetConfigSettings(null)
testSettings[Settings.ObjectNamePrefix] = 'waldo';
testSettings[Settings.MandatoryAffixes] = ['waldo'];

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

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

let navObject2 = new NAVObject(navObject.NAVObjectTextFixed, testSettings, navTestObject.ObjectFileName)
navObject2.reportColumns.forEach(column => {
assert.strictEqual(column.name.endsWith(testSettings[Settings.MandatoryAffixes][0]), true);
assert.strictEqual(column.name, column.nameFixed);
})

for (let i = 0; i < navObject2.reportColumns.length; i++) {
assert.strictEqual(navObject2.reportColumns[i].name, navObject.reportColumns[i].name)
}
});

test("Reportextension - Fields on request page", () => {
let testSettings = Settings.GetConfigSettings(null)
testSettings[Settings.ObjectNameSuffix] = 'waldo';
Expand Down
19 changes: 19 additions & 0 deletions src/test/suite/NAVTestObjectLibrary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,25 @@ export function getTableExtension(): NAVTestObject {
return object;
}


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

object.ObjectFileName = 'SomeTableExt.al'
object.ObjectText = `tableextension 50006 "Sales Cr.Memo Header waldo" extends "Sales Cr.Memo Header"
{
fields
{
field(50021; "Test Fieldwaldo "; Code[20])
{
Caption = 'Test Field';
DataClassification = CustomerContent;
}
}
`
return object;
}

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

Expand Down

0 comments on commit 9e63190

Please sign in to comment.