From b95ce954c93b619b825f11c15635c88bbc4be3f0 Mon Sep 17 00:00:00 2001 From: Sergio Martins Date: Tue, 23 Apr 2024 11:50:01 +0100 Subject: [PATCH] feat: Added right-click -> context menu -> debug slot --- README.md | 1 + package-lock.json | 8 ++-- package.json | 28 +++++++++++- run_manual_test.sh | 2 +- src/extension.ts | 69 +++++++++++++++++++++++++++++- test/qt_test/vscode.code-workspace | 20 +++++++++ 6 files changed, 121 insertions(+), 7 deletions(-) create mode 100644 test/qt_test/vscode.code-workspace diff --git a/README.md b/README.md index 4626730..924d675 100644 --- a/README.md +++ b/README.md @@ -11,6 +11,7 @@ the build directory and `ctest` is invoked to list the available tests, which we ## Features - Listing and running Qt tests - Listing and running individual QTest test slots +- Context-menu entry to run selected slot in the text editor ## Future plans diff --git a/package-lock.json b/package-lock.json index ce4336f..86e9261 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9,7 +9,7 @@ "version": "1.1.0", "license": "MIT", "dependencies": { - "@iamsergio/qttest-utils": "1.1.2", + "@iamsergio/qttest-utils": "1.2.0", "@types/node": "18.x", "@types/vscode": "^1.75.0", "vscode-cmake-tools": "^1.0.0" @@ -167,9 +167,9 @@ "dev": true }, "node_modules/@iamsergio/qttest-utils": { - "version": "1.1.2", - "resolved": "https://registry.npmjs.org/@iamsergio/qttest-utils/-/qttest-utils-1.1.2.tgz", - "integrity": "sha512-AcxqTA5BdR3/31NRHtLUtoGd9qfE5exZZJyWM6RFcXdfczqC1o0LOK43Hq5U1uauisqCQCJfhgWcX5HYPf9NAg==" + "version": "1.2.0", + "resolved": "https://registry.npmjs.org/@iamsergio/qttest-utils/-/qttest-utils-1.2.0.tgz", + "integrity": "sha512-rWD5Rt3/S+m0UKeDOzLM+MKRd2vLv3yX5cPjNWj/yndC3buXFER5sIqg1EgULuoULdMfWvpZIan1nRL26f5LAg==" }, "node_modules/@nodelib/fs.scandir": { "version": "2.1.5", diff --git a/package.json b/package.json index c1c9962..f21c282 100644 --- a/package.json +++ b/package.json @@ -35,7 +35,7 @@ "typescript": "^5.4.4" }, "dependencies": { - "@iamsergio/qttest-utils": "1.1.2", + "@iamsergio/qttest-utils": "1.2.0", "@types/node": "18.x", "@types/vscode": "^1.75.0", "vscode-cmake-tools": "^1.0.0" @@ -78,6 +78,32 @@ "description": "Which debugger to use" } } + }, + "commands": [ + { + "command": "sergiokdab.qttest.debugTest", + "title": "Debug selection..." + } + ], + "submenus": [ + { + "id": "kdab.qttest.menu", + "label": "QtTest" + } + ], + "menus": { + "editor/context": [ + { + "when": "resourceLangId == cpp && !inDebugMode && editorHasSelection && debuggersAvailable", + "group": "qttest", + "submenu": "kdab.qttest.menu" + } + ], + "kdab.qttest.menu": [ + { + "command": "sergiokdab.qttest.debugTest" + } + ] } } } diff --git a/run_manual_test.sh b/run_manual_test.sh index 321d1a1..8bfe337 100644 --- a/run_manual_test.sh +++ b/run_manual_test.sh @@ -14,4 +14,4 @@ cmake --build $QT_BUILD_DIR/ && \ code --install-extension qttest-*.vsix \ --install-extension ms-vscode.cmake-tools \ --install-extension vadimcn.vscode-lldb && \ -code test/qt_test/ +code test/qt_test/vscode.code-workspace diff --git a/src/extension.ts b/src/extension.ts index 1511d26..a0fb116 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -35,12 +35,21 @@ class KDABQtTest { this.channel.appendLine(message); }; - /// If true, means we check if tests link to QtTestLib + /// If true, means we check if tests link to QtTestLib public checkTestLinksToQtTestLib(): boolean { let conf = vscode.workspace.getConfiguration(); return conf.get("KDAB.QtTest.CheckTestLinksToQtTestLib") ?? false; } + /// returns whether executables exist + public hasExecutables(): boolean { + if (!this.qttests) { + return false; + } + + return this.qttests.qtTestExecutables.length > 0; + } + /// Unused for now. We require cmake. public usesCMakeIntegration(): boolean { let conf = vscode.workspace.getConfiguration(); @@ -154,6 +163,8 @@ class KDABQtTest { this.log("INFO: File changed: " + e.fsPath); this.parseTestsInExecutable(item, controller); }); + + this.parseTestsInExecutable(item, controller); } public async parseTestsInExecutable(item: vscode.TestItem, controller: vscode.TestController) { @@ -379,6 +390,62 @@ export function activate(context: vscode.ExtensionContext) { runHandler(true, request, token, controller); } ); + + // register a command, triggered on context menu: + context.subscriptions.push(vscode.commands.registerCommand('sergiokdab.qttest.debugTest', async () => { + + // get the text currently selected: + let editor = vscode.window.activeTextEditor; + if (!editor) { + thisExtension.log("INFO: No editor found."); + return; + } + + let selection = editor.selection; + + let text = editor.document.getText(selection).trim(); + + if (text.endsWith("()")) { + // trim trailing "()" + text = text.slice(0, -2); + } + + thisExtension.log("INFO: Selected text: " + text); + + if (!thisExtension.hasExecutables()) { + // Probably the 1st load wasn't run yet. + thisExtension.log("INFO: Refreshing tests"); + await vscode.commands.executeCommand("testing.refreshTests"); + thisExtension.log("INFO: Refreshed tests."); + } + + let executables = thisExtension.qttests?.executablesContainingSlot(text); + + if (!executables || executables.length === 0) { + vscode.window.showWarningMessage("No executables found for selection"); + thisExtension.log("INFO: No executables found for selection: " + text); + return; + } + + if (executables.length > 1) { + vscode.window.showWarningMessage("More than one executable contains the slot, please run explicitly from the text explorer."); + thisExtension.log("INFO: More than one executable contains the slot"); + return; + } + + // start debug session: + let executable = executables[0]; + let slot = executable.slotByName(text); + if (!slot) { + vscode.window.showWarningMessage("Slot not found in executable"); + thisExtension.log("INFO: Slot not found in executable"); + return; + } + + thisExtension.log("INFO: Running slot: " + slot.name); + let command = slot.command(); + await thisExtension.debugTest(command.label, command.executablePath, command.args); + })); } export function deactivate() { } diff --git a/test/qt_test/vscode.code-workspace b/test/qt_test/vscode.code-workspace new file mode 100644 index 0000000..678c918 --- /dev/null +++ b/test/qt_test/vscode.code-workspace @@ -0,0 +1,20 @@ +{ + "folders": [ + { + "path": "." + } + ], + "launch": { + "version": "0.2.0", + "configurations": [ + { + "name": "Current Target (LLDB Linux)", + "type": "lldb", + "request": "launch", + "program": "${command:cmake.launchTargetPath}", + "args": [], + "cwd": "${command:cmake.buildDirectory}" + } + ] + } +}