From 860b03bb76cefad3b24500b7a28da09f86f991aa Mon Sep 17 00:00:00 2001 From: Tomas Silva <136352470+tomilho@users.noreply.github.com> Date: Mon, 29 Apr 2024 16:32:53 +0100 Subject: [PATCH] Add copyOffsetAsHex/Dec --- media/editor/dataDisplay.tsx | 11 +++++++++++ package.json | 26 ++++++++++++++++++++++++-- package.nls.json | 2 ++ src/extension.ts | 36 ++++++++++++++++++++++++++---------- 4 files changed, 63 insertions(+), 12 deletions(-) diff --git a/media/editor/dataDisplay.tsx b/media/editor/dataDisplay.tsx index 38238e8..b40ffae 100644 --- a/media/editor/dataDisplay.tsx +++ b/media/editor/dataDisplay.tsx @@ -481,6 +481,17 @@ const DataCell: React.FC<{ const onMouseDown = useCallback( (e: React.MouseEvent) => { + if (e.buttons === 2) { + // Sets a new range and focused when the user opens + // the context menu outside the selected range, just + // like the text editor. + if (!ctx.isSelected(focusedElement.byte)) { + ctx.focusedElement = focusedElement; + ctx.isSelecting = undefined; + ctx.setSelectionRanges([Range.single(offset)]); + } + return; + } if (!(e.buttons & 1)) { return; } diff --git a/package.json b/package.json index 01089b8..455d75c 100644 --- a/package.json +++ b/package.json @@ -117,6 +117,16 @@ "command": "hexEditor.switchEditMode", "category": "%name%", "title": "%hexEditor.switchEditMode%" + }, + { + "command": "hexEditor.copyOffsetAsHex", + "category": "%name%", + "title": "%hexEditor.copyOffsetAsHex%" + }, + { + "command": "hexEditor.copyOffsetAsDec", + "category": "%name%", + "title": "%hexEditor.copyOffsetAsDec%" } ], "viewsContainers": { @@ -152,9 +162,21 @@ ], "editor/title": [ { - "when": "activeEditor && config.hexeditor.showOpenFileButton", "command": "hexEditor.openFile", - "group": "navigation@1" + "group": "navigation@1", + "when": "activeEditor && config.hexeditor.showOpenFileButton" + } + ], + "webview/context": [ + { + "command": "hexEditor.copyOffsetAsDec", + "group": "9_cutcopypaste", + "when": "hexEditor:isActive" + }, + { + "command": "hexEditor.copyOffsetAsHex", + "group": "9_cutcopypaste", + "when": "hexEditor:isActive" } ] }, diff --git a/package.nls.json b/package.nls.json index 49ad51d..6496cc4 100644 --- a/package.nls.json +++ b/package.nls.json @@ -13,5 +13,7 @@ "hexEditor.goToOffset": "Go To Offset", "hexEditor.selectBetweenOffsets": "Select Between Offsets", "hexEditor.switchEditMode": "Switch Edit Mode", + "hexEditor.copyOffsetAsDec": "Copy Offset as Dec", + "hexEditor.copyOffsetAsHex": "Copy Offset as Hex", "dataInspectorView": "Data Inspector" } diff --git a/src/extension.ts b/src/extension.ts index 5ab7d7c..57a8ca6 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -73,17 +73,32 @@ export function activate(context: vscode.ExtensionContext): void { }, ); - const switchEditModeCommand = vscode.commands.registerCommand( - "hexEditor.switchEditMode", - () => { - if (registry.activeDocument) { - registry.activeDocument.editMode = - registry.activeDocument.editMode === HexDocumentEditOp.Insert - ? HexDocumentEditOp.Replace - : HexDocumentEditOp.Insert; + const switchEditModeCommand = vscode.commands.registerCommand("hexEditor.switchEditMode", () => { + if (registry.activeDocument) { + registry.activeDocument.editMode = + registry.activeDocument.editMode === HexDocumentEditOp.Insert + ? HexDocumentEditOp.Replace + : HexDocumentEditOp.Insert; + } + }); + + const copyOffsetAsHex = vscode.commands.registerCommand("hexEditor.copyOffsetAsHex", () => { + if (registry.activeDocument) { + const focused = registry.activeDocument.selectionState.focused; + if (focused !== undefined) { + vscode.env.clipboard.writeText(focused.toString(16).toUpperCase()); } - }, - ); + } + }); + + const copyOffsetAsDec = vscode.commands.registerCommand("hexEditor.copyOffsetAsDec", () => { + if (registry.activeDocument) { + const focused = registry.activeDocument.selectionState.focused; + if (focused !== undefined) { + vscode.env.clipboard.writeText(focused.toString()); + } + } + }); context.subscriptions.push(new StatusEditMode(registry)); context.subscriptions.push(new StatusFocus(registry)); @@ -93,6 +108,7 @@ export function activate(context: vscode.ExtensionContext): void { context.subscriptions.push(switchEditModeCommand); context.subscriptions.push(openWithCommand); context.subscriptions.push(telemetryReporter); + context.subscriptions.push(copyOffsetAsDec, copyOffsetAsHex); context.subscriptions.push( HexEditorProvider.register(context, telemetryReporter, dataInspectorProvider, registry), );