From d8604116c5d5b0e3bf54fbbbb7d7a094fee1faba Mon Sep 17 00:00:00 2001 From: Thomas <84604286+ThomasOrs@users.noreply.github.com> Date: Thu, 24 Oct 2024 13:24:02 -0300 Subject: [PATCH] feat(diagnostics): Add a reload command and support command keybinds (#245) ## Summary - Adds a vscode command to reload in the command pallete `>Minecraft Diagnostics: Reload MC` - Added a keyboard shortcut for this `ctrl+shift+r` this can be edited by the user in keyboard shortcuts - Added a vscode command which allows users to add custom keyboard shortcuts to run commands from `keybindings.json` - Example `{ "key": "ctrl+shift+t", "command": "minecraft-debugger.runMinecraftCommand", "args": "say hello" },` - Added an error message if you have an active debug session but try and run an empty command from within the home view ## To test: - Launch a debug session of the extension **Reload** - Try and run the reload command without a session active an observe the warning message - Start a debugging session - Run the reload command from the command pallete, confirm it reloads in game - Run the reload command from hot keys `ctrl+shift+r`, confirm it reloads in game **Command Keybinds** - Open the `keybindings.json` (stored in Roaming/Code/User/keybindings.json) or command pallete `>Preferences: Open Keyboard Shortcuts (JSON)` - Add a keybinding such as: - ```[{ "key": "ctrl+shift+t", "command": "minecraft-debugger.runMinecraftCommand", "args": "say hello" }]``` - Save and use the keybind - Confirm Scripting says hello --- package.json | 11 +++++++++++ src/extension.ts | 29 ++++++++++++++++++++++++++++- src/panels/HomeViewProvider.ts | 4 ++++ 3 files changed, 43 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index ac72bde..faba1e7 100644 --- a/package.json +++ b/package.json @@ -56,6 +56,17 @@ { "command": "minecraft-debugger.showMinecraftDiagnostics", "title": "Minecraft Diagnostics: Show" + }, + { + "command": "minecraft-debugger.minecraftReload", + "title": "Minecraft Reload" + } + ], + "keybindings":[ + { + "command": "minecraft-debugger.minecraftReload", + "key": "ctrl+shift+r", + "when": "inDebugMode && editorLangId == typescript || inDebugMode && editorLangId == javascript" } ], "breakpoints": [ diff --git a/src/extension.ts b/src/extension.ts index 4d4b603..c3b1cc0 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -49,8 +49,35 @@ export function activate(context: vscode.ExtensionContext) { } ); + const minecraftReloadCommand = vscode.commands.registerCommand('minecraft-debugger.minecraftReload', () => { + if (!vscode.debug.activeDebugSession) { + vscode.window.showErrorMessage('Error running command reload: No active Minecraft Debugger session.'); + } + eventEmitter.emit('run-minecraft-command', 'reload'); + }); + + // Create a command to allow keyboard shortcuts to run Minecraft commands + const runMinecraftCommand = vscode.commands.registerCommand( + 'minecraft-debugger.runMinecraftCommand', + (...args: any[]) => { + if (args.length === 0) { + vscode.window.showErrorMessage('No command provided.'); + return; + } + const command = args[0]; // Use only the first argument + if (typeof command !== 'string') { + vscode.window.showErrorMessage('Command must be a string.'); + return; + } + if (!vscode.debug.activeDebugSession) { + vscode.window.showErrorMessage('Error running command: No active Minecraft Debugger session.'); + } + eventEmitter.emit('run-minecraft-command', command); + } + ); + // Add command to the extension context - context.subscriptions.push(showDiagnosticsCommand); + context.subscriptions.push(showDiagnosticsCommand, minecraftReloadCommand, runMinecraftCommand); } // called when extension is deactivated diff --git a/src/panels/HomeViewProvider.ts b/src/panels/HomeViewProvider.ts index a7992d9..dfb6271 100644 --- a/src/panels/HomeViewProvider.ts +++ b/src/panels/HomeViewProvider.ts @@ -61,6 +61,10 @@ export class HomeViewProvider implements vscode.WebviewViewProvider { break; } case 'run-minecraft-command': { + if (!message.command || message.command.trim() === '') { + vscode.window.showErrorMessage('Minecraft Command Shortcut can not be empty.'); + return; + } this._eventEmitter.emit('run-minecraft-command', message.command); break; }