Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

decomp: add a command to increment all deftype's offsets by n #96

Merged
merged 1 commit into from
Sep 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
"onCommand:opengoal.decomp.casts.labelCastSelection",
"onCommand:opengoal.decomp.casts.stackCastSelection",
"onCommand:opengoal.decomp.casts.typeCastSelection",
"onCommand:opengoal.decomp.misc.addToOffsets",
"onCommand:opengoal.lsp.start",
"onCommand:opengoal.lsp.stop",
"onCommand:opengoal.lsp.restart"
Expand Down Expand Up @@ -116,6 +117,10 @@
"command": "opengoal.decomp.casts.typeCastSelection",
"title": "OpenGOAL - Casts - Add Type Cast to Selection"
},
{
"command": "opengoal.decomp.misc.addToOffsets",
"title": "OpenGOAL - Misc - Add to deftype Offsets"
},
{
"command": "opengoal.lsp.start",
"title": "OpenGOAL - LSP - Start"
Expand Down
47 changes: 47 additions & 0 deletions src/decomp/misc-tools.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { getExtensionContext } from "../context";
import * as vscode from "vscode";

async function addToOffsets() {
const editor = vscode.window.activeTextEditor;
if (
editor === undefined ||
editor.selection.isEmpty ||
editor.selection.isSingleLine
) {
return;
}

// Get amount to increment
const incString = await vscode.window.showInputBox({
title: "Increment By?",
});
if (incString === undefined) {
return;
}
const incAmount = parseInt(incString);

editor.edit((selectedText) => {
// Get the content of the selection
const content = editor.document.getText(editor.selection);

// Increment all values after `:offset` or `:offset-assert`
const result = content.replace(
/(?<=offset(?:-assert)?\s+)(\d+)(?=\s+|\))/g,
(match, key) => {
console.log(`${match}-${key}`);
return `${parseInt(key) + incAmount}`;
}
);

selectedText.replace(editor.selection, result);
});
}

export async function activateMiscDecompTools() {
getExtensionContext().subscriptions.push(
vscode.commands.registerCommand(
"opengoal.decomp.misc.addToOffsets",
addToOffsets
)
);
}
38 changes: 0 additions & 38 deletions src/decomp/type-caster.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
// [inclusive, exclusive]

import { getExtensionContext } from "../context";
import * as vscode from "vscode";
import { basename, join } from "path";
Expand Down Expand Up @@ -464,39 +462,3 @@ export async function activateTypeCastTools() {
)
);
}

// TODO - better handling around upserting casts
// this requires properly handling the CommentArray type instead of building raw arrays so comments are preserved
// const finalEntries = [];
// if (relevantJson !== undefined) {
// // prepare the entry for the upcoming update
// // remove any identical casts / range casts that effect it
// for (const entry of relevantJson) {
// if (entry[1] === registerSelection) {
// if (entry[0] instanceof Array) {
// const [start, end] = entry[0];
// if (castContext.endOp === undefined) {
// if (castContext.startOp >= start && castContext.startOp < end) {
// continue;
// }
// } else if (
// (castContext.startOp >= start && castContext.startOp < end) ||
// (castContext.endOp > start && castContext.endOp < end) ||
// (castContext.startOp >= start && castContext.endOp < end)
// ) {
// continue;
// }
// } else if (castContext.startOp == entry[0]) {
// continue;
// }
// finalEntries.push(entry);
// }
// }
// // Add our new entry
// // TODO - sort by op number (annoying because of the ranges...)
// if (castContext.endOp === undefined) {
// finalEntries.push([castContext.startOp, registerSelection, castToType]);
// } else {
// finalEntries.push([[castContext.startOp, castContext.endOp], registerSelection, castToType]);
// }
// }
2 changes: 2 additions & 0 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { IRFoldingRangeProvider } from "./languages/ir2/ir2-folder";
import { activateTypeCastTools } from "./decomp/type-caster";
import { IRInlayHintsProvider } from "./languages/ir2/ir2-inlay-hinter";
import { OpenGOALDisasmRenameProvider } from "./languages/opengoal/disasm/opengoal-disasm-renamer";
import { activateMiscDecompTools } from "./decomp/misc-tools";

export async function activate(context: vscode.ExtensionContext) {
try {
Expand All @@ -32,6 +33,7 @@ export async function activate(context: vscode.ExtensionContext) {

activateDecompTools();
activateTypeCastTools();
activateMiscDecompTools();

// Customized PDF Viewer
const provider = new PdfCustomProvider(
Expand Down