Skip to content

Commit

Permalink
feat: Added right-click -> context menu -> debug slot
Browse files Browse the repository at this point in the history
  • Loading branch information
iamsergio committed Apr 23, 2024
1 parent cf6a6b4 commit b95ce95
Show file tree
Hide file tree
Showing 6 changed files with 121 additions and 7 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 27 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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"
}
]
}
}
}
2 changes: 1 addition & 1 deletion run_manual_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
69 changes: 68 additions & 1 deletion src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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() { }
20 changes: 20 additions & 0 deletions test/qt_test/vscode.code-workspace
Original file line number Diff line number Diff line change
@@ -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}"
}
]
}
}

0 comments on commit b95ce95

Please sign in to comment.