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

Select between offsets #470

Merged
merged 7 commits into from Jan 9, 2024

Conversation

IngilizAdam
Copy link
Contributor

Implements a simple command that enables user to select byte values between 2 offsets.
Useful for extracting embedded files.

closes #469

let toOffset: number | undefined;
let accepted = false;

startingInput.onDidChangeValue(value => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems we duplicate a lot of logic between the starting and ending inputs, can it be pulled into a helper function?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, working on it

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How is it now?

const decimalRe = /^[0-9]+$/i;

export const showSelectBetweenOffsets = (messaging: ExtensionHostMessageHandler): void => {
const startingInput = vscode.window.createInputBox();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It'd be quite nice for the default value in the starting input to be the currently selected byte. The HexDocument has a selectionState that you can read from.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was looking for that but failed to find the selected byte. Thanks, I will change it with that

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I couldn't access the active HexDocument. So I added a static variable holding that reference. Is this okay or is there a better way to access it? @connor4312

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can get it via the activeDocument on the HexEditorRegistry (created inside activate())

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ohh, how did I not see that?? Fixing it now

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, I reverted my change in the HexDocument and get the ActiveDocument from registry. Now it should be good.

Copy link
Member

@connor4312 connor4312 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A couple comments, thanks for the PR :)

@IngilizAdam
Copy link
Contributor Author

@microsoft-github-policy-service agree

Comment on lines 63 to 80
function changeValueHelper(value: string) {
if (!value) {
inputOffset = undefined;
} else if (addressRe.test(value)) {
inputOffset = parseInt(value.slice(2), 16);
} else if (decimalRe.test(value)) {
inputOffset = parseInt(value, 10);
} else {
startingInput.validationMessage = "Offset must be provided as a decimal (12345) or hex (0x12345) address";
return;
}

startingInput.validationMessage = "";
if (inputOffset !== undefined) {
startingInput.validationMessage = "";
messaging.sendEvent({ type: MessageType.GoToOffset, offset: inputOffset });
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function always affects the startingInput, even though it's used for the endingInput as well

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My bad, thanks for noticing


startingInput.show();

function hideHelper() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you'll want to make sure to .dispose() each input after their used.

Reworking this function to use some inner function getOffset(options: { ... }): Promise<number> would probably be easier to deal with

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought onDidHide already disposes the InputBox. In goToOffset.ts file, there isn't a .dispose() for the input. And if I call .dispose() in hideHelper function, hideHelper gets called twice. Should I still call .dispose()?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, we should always call dispose. There are additional resources that get collected.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did what you suggested and I think it turned out great. Let me know if there are any problems.

Copy link
Member

@connor4312 connor4312 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lgtm, thanks for the PR!

@connor4312 connor4312 enabled auto-merge (squash) January 9, 2024 16:02
@VSCodeTriageBot VSCodeTriageBot added this to the December / January 2024 milestone Jan 9, 2024
@connor4312 connor4312 merged commit 35dab02 into microsoft:main Jan 9, 2024
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[feature request] Add select until offset command
4 participants