Fix format text entity

This commit is contained in:
barsdeveloper
2024-06-01 16:27:17 +02:00
parent 8258572e56
commit 5314228b33
5 changed files with 47 additions and 56 deletions

34
dist/ueblueprint.js vendored
View File

@@ -2528,7 +2528,7 @@ class IEntity {
/** @type {(k: String) => String} */
static printKey = k => k
/** @type {P<InstanceType<typeof this>>} */
/** @type {P<IEntity>} */
static grammar = /** @type {any} */(Parsernostrum.failure())
/** @type {{ [key: String]: typeof IEntity }} */
@@ -2592,7 +2592,7 @@ class IEntity {
showProperty(key) {
/** @type {IEntity} */
let value = this[key];
const Self = value.Self();
const Self = this.Self();
if (Self.silent && Self.default !== undefined) {
if (Self["#default"] === undefined) {
Self["#default"] = Self.default(Self);
@@ -4525,14 +4525,7 @@ class ArrayEntity extends IEntity {
/** @type {typeof IEntity} */
static type
static #grammar
static get grammar() {
return this.#grammar ?? this.createGrammar()
}
static set grammar(value) {
this.#grammar = value;
}
//static grammar = this.createGrammar()
static grammar = this.createGrammar()
/** @param {ExtractType<T>[]} values */
constructor(values = []) {
@@ -4852,9 +4845,10 @@ class StringEntity extends IPrintableEntity {
class FormatTextEntity extends IPrintableEntity {
static lookbehind = ["LOCGEN_FORMAT_NAMED", "LOCGEN_FORMAT_ORDERED"]
static grammar = Parsernostrum.seq(
/** @type {P<FormatTextEntity>} */
static grammar = Parsernostrum.lazy(() => Parsernostrum.seq(
// Resulting regex: /(LOCGEN_FORMAT_NAMED|LOCGEN_FORMAT_ORDERED)\s*/
Parsernostrum.reg(new RegExp(String.raw`(${this.lookbehind.reduce((acc, cur) => acc + "|" + cur)})\s*`), 1),
Parsernostrum.reg(new RegExp(String.raw`(${this.lookbehind.join("|")})\s*\(\s*`), 1),
Parsernostrum.alt(
...[StringEntity, LocalizedTextEntity, InvariantTextEntity, FormatTextEntity].map(type => type.grammar)
).sepBy(Parsernostrum.reg(/\s*\,\s*/)),
@@ -4864,7 +4858,7 @@ class FormatTextEntity extends IPrintableEntity {
const result = new this(values);
result.lookbehind = lookbehind;
return result
})
}))
.label("FormatTextEntity")
/** @param {(StringEntity | LocalizedTextEntity | InvariantTextEntity | FormatTextEntity)[]} values */
@@ -4874,20 +4868,20 @@ class FormatTextEntity extends IPrintableEntity {
}
print() {
const pattern = this.values?.[0]?.toString(); // The pattern is always the first element of the array
const pattern = this.values?.[0]?.print(); // The pattern is always the first element of the array
if (!pattern) {
return ""
}
const values = this.values.slice(1).map(v => v.toString());
let result = this.Self().lookbehind == "LOCGEN_FORMAT_NAMED"
const values = this.values.slice(1).map(v => v.print());
let result = this.lookbehind == "LOCGEN_FORMAT_NAMED"
? pattern.replaceAll(/\{([a-zA-Z]\w*)\}/g, (substring, arg) => {
const argLocation = values.indexOf(arg) + 1;
return argLocation > 0 && argLocation < values.length
? values[argLocation]
: substring
})
: this.Self().lookbehind == "LOCGEN_FORMAT_ORDERED"
: this.lookbehind == "LOCGEN_FORMAT_ORDERED"
? pattern.replaceAll(/\{(\d+)\}/g, (substring, arg) => {
const argValue = Number(arg);
return argValue < values.length
@@ -4895,7 +4889,7 @@ class FormatTextEntity extends IPrintableEntity {
: substring
})
: "";
result = this.Self().lookbehind + "(" + result + ")";
return result
}
}
@@ -13237,10 +13231,6 @@ class UnknownKeysEntity extends IEntity {
attributes.forEach(attributeSetter => attributeSetter(values));
return new this(values)
}).label("UnknownKeysEntity")
constructor(values = {}) {
super(values);
}
}
/**

File diff suppressed because one or more lines are too long

View File

@@ -7,9 +7,10 @@ import StringEntity from "./StringEntity.js"
export default class FormatTextEntity extends IPrintableEntity {
static lookbehind = ["LOCGEN_FORMAT_NAMED", "LOCGEN_FORMAT_ORDERED"]
static grammar = P.seq(
/** @type {P<FormatTextEntity>} */
static grammar = P.lazy(() => P.seq(
// Resulting regex: /(LOCGEN_FORMAT_NAMED|LOCGEN_FORMAT_ORDERED)\s*/
P.reg(new RegExp(String.raw`(${this.lookbehind.reduce((acc, cur) => acc + "|" + cur)})\s*`), 1),
P.reg(new RegExp(String.raw`(${this.lookbehind.join("|")})\s*\(\s*`), 1),
P.alt(
...[StringEntity, LocalizedTextEntity, InvariantTextEntity, FormatTextEntity].map(type => type.grammar)
).sepBy(P.reg(/\s*\,\s*/)),
@@ -19,7 +20,7 @@ export default class FormatTextEntity extends IPrintableEntity {
const result = new this(values)
result.lookbehind = lookbehind
return result
})
}))
.label("FormatTextEntity")
/** @param {(StringEntity | LocalizedTextEntity | InvariantTextEntity | FormatTextEntity)[]} values */
@@ -29,20 +30,20 @@ export default class FormatTextEntity extends IPrintableEntity {
}
print() {
const pattern = this.values?.[0]?.toString() // The pattern is always the first element of the array
const pattern = this.values?.[0]?.print() // The pattern is always the first element of the array
if (!pattern) {
return ""
}
const values = this.values.slice(1).map(v => v.toString())
let result = this.Self().lookbehind == "LOCGEN_FORMAT_NAMED"
const values = this.values.slice(1).map(v => v.print())
let result = this.lookbehind == "LOCGEN_FORMAT_NAMED"
? pattern.replaceAll(/\{([a-zA-Z]\w*)\}/g, (substring, arg) => {
const argLocation = values.indexOf(arg) + 1
return argLocation > 0 && argLocation < values.length
? values[argLocation]
: substring
})
: this.Self().lookbehind == "LOCGEN_FORMAT_ORDERED"
: this.lookbehind == "LOCGEN_FORMAT_ORDERED"
? pattern.replaceAll(/\{(\d+)\}/g, (substring, arg) => {
const argValue = Number(arg)
return argValue < values.length
@@ -50,6 +51,6 @@ export default class FormatTextEntity extends IPrintableEntity {
: substring
})
: ""
result = this.Self().lookbehind + "(" + result + ")"
return result
}
}

View File

@@ -20,7 +20,7 @@ export default class IEntity {
/** @type {(k: String) => String} */
static printKey = k => k
/** @type {P<InstanceType<typeof this>>} */
/** @type {P<IEntity>} */
static grammar = /** @type {any} */(P.failure())
/** @type {{ [key: String]: typeof IEntity }} */
@@ -57,7 +57,6 @@ export default class IEntity {
this.#keys = [... new Set(value)]
}
/** @param {{ [key: String]: IEntity }} values */
constructor(values = {}) {
const keys = Utility.mergeArrays(Object.keys(values), Object.keys(this.Self().attributes))
for (const key of keys) {
@@ -85,7 +84,7 @@ export default class IEntity {
showProperty(key) {
/** @type {IEntity} */
let value = this[key]
const Self = value.Self()
const Self = this.Self()
if (Self.silent && Self.default !== undefined) {
if (Self["#default"] === undefined) {
Self["#default"] = Self.default(Self)

View File

@@ -1,6 +1,7 @@
import { expect, test } from "@playwright/test"
import Utility from "../js/Utility.js"
import ArrayEntity from "../js/entity/ArrayEntity.js"
import BooleanEntity from "../js/entity/BooleanEntity.js"
import FormatTextEntity from "../js/entity/FormatTextEntity.js"
import GuidEntity from "../js/entity/GuidEntity.js"
import IntegerEntity from "../js/entity/IntegerEntity.js"
@@ -123,35 +124,35 @@ test("ArrayEntity", () => {
})
test("Boolean", () => {
let serializer = SerializerFactory.getSerializer(Boolean)
expect(serializer.read("true")).toStrictEqual(true)
expect(serializer.read("True")).toStrictEqual(true)
expect(serializer.read("false")).toStrictEqual(false)
expect(serializer.read("False")).toStrictEqual(false)
let grammar = BooleanEntity.grammar
expect(grammar.parse("true")).toEqual(new BooleanEntity(true))
expect(grammar.parse("True")).toEqual(new BooleanEntity(true))
expect(grammar.parse("false")).toEqual(new BooleanEntity(false))
expect(grammar.parse("False")).toEqual(new BooleanEntity(false))
})
test("FormatTextEntity", () => {
let serializer = SerializerFactory.getSerializer(FormatTextEntity)
let grammar = FormatTextEntity.grammar
expect(
serializer.read(`LOCGEN_FORMAT_NAMED(NSLOCTEXT("KismetSchema", "SplitPinFriendlyNameFormat", "{PinDisplayName} {ProtoPinDisplayName}"), "PinDisplayName", "Out Hit", "ProtoPinDisplayName", "Blocking Hit")`)
.toString()
grammar.parse(`LOCGEN_FORMAT_NAMED(NSLOCTEXT("KismetSchema", "SplitPinFriendlyNameFormat", "{PinDisplayName} {ProtoPinDisplayName}"), "PinDisplayName", "Out Hit", "ProtoPinDisplayName", "Blocking Hit")`)
.print()
).toBe("Out Hit Blocking Hit")
expect(
serializer.read(`LOCGEN_FORMAT_NAMED(NSLOCTEXT("KismetSchema", "SplitPinFriendlyNameFormat", "{PinDisplayName} {ProtoPinDisplayName}"), "PinDisplayName", "Out Hit", "ProtoPinDisplayName", "Hit Bone Name")`)
.toString()
grammar.parse(`LOCGEN_FORMAT_NAMED(NSLOCTEXT("KismetSchema", "SplitPinFriendlyNameFormat", "{PinDisplayName} {ProtoPinDisplayName}"), "PinDisplayName", "Out Hit", "ProtoPinDisplayName", "Hit Bone Name")`)
.print()
).toBe("Out Hit Hit Bone Name")
expect(
serializer.read(String.raw`LOCGEN_FORMAT_ORDERED(
NSLOCTEXT(
"PCGSettings",
"OverridableParamPinTooltip",
"{0}Attribute type is \"{1}\" and its exact name is \"{2}\""
),
"If InRangeMin = InRangeMax, then that density value is mapped to the average of OutRangeMin and OutRangeMax\n",
"float",
"InRangeMin"
)`)
.toString()
grammar.parse(String.raw`LOCGEN_FORMAT_ORDERED(
NSLOCTEXT(
"PCGSettings",
"OverridableParamPinTooltip",
"{0}Attribute type is \"{1}\" and its exact name is \"{2}\""
),
"If InRangeMin = InRangeMax, then that density value is mapped to the average of OutRangeMin and OutRangeMax\n",
"float",
"InRangeMin"
)`)
.print()
).toBe(`If InRangeMin = InRangeMax, then that density value is mapped to the average of OutRangeMin and OutRangeMax\nAttribute type is "float" and its exact name is "InRangeMin"`)
})