Skip to content

Commit

Permalink
Merge pull request #213 from rayshader/feat/212-format-cet-luadoc
Browse files Browse the repository at this point in the history
Format CET with luadoc types
  • Loading branch information
poirierlouis authored Nov 18, 2024
2 parents 2bb8bbb + 1b68c5a commit fc21f96
Show file tree
Hide file tree
Showing 3 changed files with 134 additions and 51 deletions.
68 changes: 38 additions & 30 deletions src/shared/formatters/lua.formatter.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -237,10 +237,11 @@ describe('LuaFormatter', () => {
const code: string = fmt.formatSpecial('Observe', func, memberOf);

// THEN
expect(code).toBe(`Observe("VehicleComponent", "PlayHonkForDuration", function(this, honkTime)
-- method has just been called with:
-- this: VehicleComponent
-- honkTime: Float
expect(code).toBe(`Observe("VehicleComponent", "PlayHonkForDuration",
---@param this VehicleComponent
---@param honkTime Float
function(this, honkTime)
-- method has just been called
end)
`);
});
Expand All @@ -256,7 +257,7 @@ end)
[
AstHelper.buildArg('vehicle', AstHelper.buildWeakRef('vehicleBaseObject', 'VehicleObject')),
AstHelper.buildArg('vehicleSlotID', AstHelper.buildType('gamemountingMountingSlotId', 'MountingSlotId')),
AstHelper.buildArg('delay', AstHelper.Float),
AstHelper.buildArg('delay', AstHelper.Float, true),
],
'OpenDoor;VehicleObjectMountingSlotIdFloat'
);
Expand All @@ -265,11 +266,12 @@ end)
const code: string = fmt.formatSpecial('Observe', func, memberOf);

// THEN
expect(code).toBe(`Observe("VehicleComponent", "OpenDoor;VehicleObjectMountingSlotIdFloat", function(vehicle, vehicleSlotID, delay)
-- method has just been called with:
-- vehicle: wref<VehicleObject>
-- vehicleSlotID: MountingSlotId
-- delay: Float
expect(code).toBe(`Observe("VehicleComponent", "OpenDoor;VehicleObjectMountingSlotIdFloat",
---@param vehicle VehicleObject
---@param vehicleSlotID MountingSlotId
---@param delay? Float
function(vehicle, vehicleSlotID, delay)
-- method has just been called
end)
`);
});
Expand All @@ -293,10 +295,11 @@ end)
const code: string = fmt.formatSpecial('ObserveAfter', func, memberOf);

// THEN
expect(code).toBe(`ObserveAfter("VehicleComponent", "PlayHonkForDuration", function(this, honkTime)
-- method has been called and fully executed with:
-- this: VehicleComponent
-- honkTime: Float
expect(code).toBe(`ObserveAfter("VehicleComponent", "PlayHonkForDuration",
---@param this VehicleComponent
---@param honkTime Float
function(this, honkTime)
-- method has been called and fully executed
end)
`);
});
Expand All @@ -312,7 +315,7 @@ end)
[
AstHelper.buildArg('vehicle', AstHelper.buildWeakRef('vehicleBaseObject', 'VehicleObject')),
AstHelper.buildArg('vehicleSlotID', AstHelper.buildType('gamemountingMountingSlotId', 'MountingSlotId')),
AstHelper.buildArg('delay', AstHelper.Float),
AstHelper.buildArg('delay', AstHelper.Float, true),
],
'OpenDoor;VehicleObjectMountingSlotIdFloat'
);
Expand All @@ -321,11 +324,12 @@ end)
const code: string = fmt.formatSpecial('ObserveAfter', func, memberOf);

// THEN
expect(code).toBe(`ObserveAfter("VehicleComponent", "OpenDoor;VehicleObjectMountingSlotIdFloat", function(vehicle, vehicleSlotID, delay)
-- method has been called and fully executed with:
-- vehicle: wref<VehicleObject>
-- vehicleSlotID: MountingSlotId
-- delay: Float
expect(code).toBe(`ObserveAfter("VehicleComponent", "OpenDoor;VehicleObjectMountingSlotIdFloat",
---@param vehicle VehicleObject
---@param vehicleSlotID MountingSlotId
---@param delay? Float
function(vehicle, vehicleSlotID, delay)
-- method has been called and fully executed
end)
`);
});
Expand All @@ -344,10 +348,12 @@ end)
const code: string = fmt.formatSpecial('Override', func, memberOf);

// THEN
expect(code).toBe(`Override("VehicleComponent", "GetVehicle", function(this, wrappedMethod)
-- rewrite method with:
-- this: VehicleComponent
\u0020\u0020\u0020\u0020
expect(code).toBe(`Override("VehicleComponent", "GetVehicle",
---@param this VehicleComponent
---@param wrappedMethod function
---@return VehicleObject
function(this, wrappedMethod)
-- rewrite method
local result = wrappedMethod()
\u0020\u0020\u0020\u0020
return result
Expand All @@ -374,11 +380,13 @@ end)
const code: string = fmt.formatSpecial('Override', func, memberOf);

// THEN
expect(code).toBe(`Override("VehicleComponent", "CloseDoor;VehicleObjectMountingSlotId", function(vehicle, vehicleSlotID, wrappedMethod)
-- rewrite method with:
-- vehicle: wref<VehicleObject>
-- vehicleSlotID: MountingSlotId
\u0020\u0020\u0020\u0020
expect(code).toBe(`Override("VehicleComponent", "CloseDoor;VehicleObjectMountingSlotId",
---@param vehicle VehicleObject
---@param vehicleSlotID MountingSlotId
---@param wrappedMethod function
---@return Bool
function(vehicle, vehicleSlotID, wrappedMethod)
-- rewrite method
local result = wrappedMethod(vehicle, vehicleSlotID)
\u0020\u0020\u0020\u0020
return result
Expand Down Expand Up @@ -412,7 +420,7 @@ end)
OnGodModeChanged = {
args = {"entEntityID", "gameGodModeType"},
callback = function(ownerID, newType)
-- Do stuff
-- do stuff
end
}
})
Expand Down
87 changes: 66 additions & 21 deletions src/shared/formatters/lua.formatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,38 @@ import {RedTypeAst} from "../red-ast/red-type.ast";
import {RedArgumentAst} from "../red-ast/red-argument.ast";
import {CodeSyntax} from "../services/settings.service";

export enum LuaPrimitiveDef {
Void = 'void',
Bool = 'Bool',
Int8 = 'Int8',
Uint8 = 'Uint8',
Int16 = 'Int16',
Uint16 = 'Uint16',
Int32 = 'Int32',
Uint32 = 'Uint32',
Int64 = 'Int64',
Uint64 = 'Uint64',
Float = 'Float',
Double = 'Double',
String = 'String',
LocalizationString = 'LocalizationString',
CName = 'string | CName',
TweakDBID = 'TweakDBID',
NodeRef = 'NodeRef',
DataBuffer = 'DataBuffer',
serializationDeferredDataBuffer = 'serializationDeferredDataBuffer',
SharedDataBuffer = 'SharedDataBuffer',
CDateTime = 'CDateTime',
CGUID = 'CGUID',
CRUID = 'CRUID',
//CRUIDRef = 'any',
EditorObjectID = 'EditorObjectID',
//GamedataLocKeyWrapper = 'any',
MessageResourcePath = 'MessageResourcePath',
//RuntimeEntityRef = 'RuntimeEntityRef',
Variant = 'Variant'
}

export class LuaFormatter extends CodeFormatter {

constructor() {
Expand Down Expand Up @@ -100,29 +132,33 @@ export class LuaFormatter extends CodeFormatter {
if (nativePrefix !== -1) {
funcName = funcName.substring(nativePrefix + 2);
}
let luadoc: string = '';
let callback: string = '';

if (!func.isStatic) {
luadoc += `---@param this ${memberOf.aliasName ?? memberOf.name}`;
callback += 'this';
if (func.arguments.length > 0) {
luadoc += '\n';
callback += ', ';
}
}
callback += func.arguments.map((arg) => arg.name).join(', ');
luadoc += func.arguments
.map((arg) => `---@param ${arg.name}${arg.isOptional ? '?' : ''} ${RedTypeAst.toLuadoc(arg.type)}`)
.join('\n');
callback += func.arguments
.map((arg) => arg.name)
.join(', ');
let code: string = '';
let after: string = isAfter ? 'After' : '';

code += `Observe${after}("${memberOf.aliasName ?? memberOf.name}", "${funcName}", function(${callback})\n`;
code += `Observe${after}("${memberOf.aliasName ?? memberOf.name}", "${funcName}",\n`;
code += `${luadoc}\n`;
code += `function(${callback})\n`;
if (!isAfter) {
code += ` -- method has just been called with:\n`;
code += ` -- method has just been called\n`;
} else {
code += ` -- method has been called and fully executed with:\n`;
}
if (!func.isStatic) {
code += ` -- this: ${memberOf.aliasName ?? memberOf.name}\n`;
}
for (const argument of func.arguments) {
code += ` -- ${argument.name}: ${RedTypeAst.toString(argument.type, CodeSyntax.redscript)}\n`;
code += ` -- method has been called and fully executed\n`;
}
code += 'end)\n';
return code;
Expand All @@ -135,31 +171,40 @@ export class LuaFormatter extends CodeFormatter {
if (nativePrefix !== -1) {
funcName = funcName.substring(nativePrefix + 2);
}
let luadoc: string = '';
let callback: string = '';

if (!func.isStatic) {
luadoc += `---@param this ${memberOf.aliasName ?? memberOf.name}`;
callback += 'this';
if (func.arguments.length > 0) {
luadoc += '\n';
callback += ', ';
}
}
callback += func.arguments.map((arg) => arg.name).join(', ');
luadoc += func.arguments
.map((arg) => `---@param ${arg.name}${arg.isOptional ? '?' : ''} ${RedTypeAst.toLuadoc(arg.type)}`)
.join('\n');
callback += func.arguments
.map((arg) => arg.name)
.join(', ');
luadoc += '\n';
luadoc += `---@param wrappedMethod function`;
if (func.returnType) {
luadoc += '\n';
luadoc += `---@return ${RedTypeAst.toLuadoc(func.returnType)}`;
}
if (callback.length !== 0) {
callback += ', ';
}
callback += 'wrappedMethod';
const args: string = func.arguments.map((arg) => arg.name).join(', ');
let code: string = '';

code += `Override("${memberOf.aliasName ?? memberOf.name}", "${funcName}", function(${callback})\n`;
code += ' -- rewrite method with:\n';
if (!func.isStatic) {
code += ` -- this: ${memberOf.aliasName ?? memberOf.name}\n`;
}
for (const argument of func.arguments) {
code += ` -- ${argument.name}: ${RedTypeAst.toString(argument.type, CodeSyntax.redscript)}\n`;
}
code += ' \n';
code += `Override("${memberOf.aliasName ?? memberOf.name}", "${funcName}",\n`;
code += `${luadoc}\n`;
code += `function(${callback})\n`;
code += ' -- rewrite method\n';
if (func.returnType) {
code += ` local result = wrappedMethod(${args})\n`;
code += ' \n';
Expand All @@ -183,7 +228,7 @@ export class LuaFormatter extends CodeFormatter {
code += ` ${func.name} = {\n`;
code += ` args = {${pseudoArgs}},\n`;
code += ` callback = function(${args})\n`;
code += ' -- Do stuff\n';
code += ' -- do stuff\n';
code += ' end\n';
code += ' }\n';
code += '})\n';
Expand Down
30 changes: 30 additions & 0 deletions src/shared/red-ast/red-type.ast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {RedNodeAst, RedNodeKind} from "./red-node.ast";
import {cyrb53} from "../string";
import {RedPrimitiveDef, RedTemplateDef} from "./red-definitions.ast";
import {CodeSyntax} from "../services/settings.service";
import {LuaPrimitiveDef} from "../formatters/lua.formatter";

export interface RedTypeJson {
readonly a?: number; // flag
Expand Down Expand Up @@ -71,6 +72,11 @@ export class RedTypeAst {
}
}

static primitiveToLuadoc(flag: RedPrimitiveDef): string {
// @ts-ignore
return LuaPrimitiveDef[RedPrimitiveDef[flag]];
}

static toString(type: RedTypeAst, syntax?: CodeSyntax): string {
let name: string = type.name;
let str: string = '';
Expand Down Expand Up @@ -110,6 +116,30 @@ export class RedTypeAst {
return str;
}

static toLuadoc(type: RedTypeAst): string {
let name: string = type.name;
let str: string = '';

if (type.aliasName) {
name = type.aliasName;
}
if (this.isPrimitive(type)) {
name = this.primitiveToLuadoc(type.flag as RedPrimitiveDef);
} else if (this.isTemplate(type)) {
name = '';
}
if (type.innerType !== undefined) {
str += name;
str += RedTypeAst.toLuadoc(type.innerType);
if (type.flag === RedTemplateDef.array) {
str += '[]';
}
} else {
str = name;
}
return str;
}

static fromJson(json: RedTypeJson): RedTypeAst {
const flag: RedPrimitiveDef | RedTemplateDef | undefined = json.a;
const name: string = (flag === undefined) ? json.b! : ((flag <= RedPrimitiveDef.Variant) ? RedPrimitiveDef[flag] : RedTemplateDef[flag]);
Expand Down

0 comments on commit fc21f96

Please sign in to comment.