misc: add command to generate method stubs (#168)

This commit is contained in:
Tyler Wilding 2022-12-10 16:32:07 -05:00 committed by GitHub
parent a580517d51
commit f4ff9615ea
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 61 additions and 0 deletions

View file

@ -61,6 +61,7 @@
"onCommand:opengoal.decomp.casts.typeCastSelection",
"onCommand:opengoal.decomp.misc.addToOffsets",
"onCommand:opengoal.decomp.misc.preserveBlock",
"onCommand:opengoal.decomp.misc.genMethodStubs",
"onCommand:opengoal.decomp.typeSearcher.open",
"onCommand:opengoal.lsp.start",
"onCommand:opengoal.lsp.stop",
@ -136,6 +137,10 @@
"command": "opengoal.decomp.misc.convertDecToHex",
"title": "OpenGOAL - Misc - Convert Dec to Hex"
},
{
"command": "opengoal.decomp.misc.genMethodStubs",
"title": "OpenGOAL - Misc - Generate Method Stubs"
},
{
"command": "opengoal.decomp.typeSearcher.open",
"title": "OpenGOAL - Misc - Type Searcher"

View file

@ -156,6 +156,56 @@ async function convertDecToHex() {
});
}
async function genMethodStubs() {
const editor = vscode.window.activeTextEditor;
if (editor === undefined || editor.selection.isEmpty) {
return;
}
const content = editor.document.getText(editor.selection);
const lines = content.split("\n");
// Figure out the types, parent -- and method count
let parentType = "";
let methodCount = 0;
let typeName = "";
for (const line of lines) {
if (line.includes("deftype")) {
parentType = line.replace("(deftype", "").split("(")[1].split(")")[0];
typeName = line.replace("(deftype ", "").split(" ")[0];
}
if (line.includes("method-count-assert")) {
methodCount = parseInt(line.split("method-count-assert")[1].trim());
}
}
// Now, go find the parent type, and figure out it's method count
const fileContents = editor.document.getText();
const fileContentsLines = fileContents.split("\n");
let foundType = false;
let parentTypeMethodCount = 0;
for (const line of fileContentsLines) {
if (line.includes(`(deftype ${parentType}`)) {
foundType = true;
}
if (foundType && line.includes("method-count-assert")) {
parentTypeMethodCount = parseInt(
line.split("method-count-assert")[1].trim()
);
break;
}
}
// Now put it all together!
const methodStubs = [];
for (let i = parentTypeMethodCount; i < methodCount; i++) {
methodStubs.push(` (${typeName}-method-${i} () none ${i})`);
}
vscode.env.clipboard.writeText(`(:methods\n${methodStubs.join("\n")})`);
return;
}
export async function activateMiscDecompTools() {
getExtensionContext().subscriptions.push(
vscode.commands.registerCommand(
@ -181,4 +231,10 @@ export async function activateMiscDecompTools() {
convertDecToHex
)
);
getExtensionContext().subscriptions.push(
vscode.commands.registerCommand(
"opengoal.decomp.misc.genMethodStubs",
genMethodStubs
)
);
}