Skip to content
This repository has been archived by the owner on May 24, 2024. It is now read-only.

Commit

Permalink
Merge pull request #6 from MarkusAmshove/add-run-codelens
Browse files Browse the repository at this point in the history
Add CodeLens to run a test
  • Loading branch information
MarkusAmshove committed Nov 3, 2023
2 parents 02732d7 + c133ac0 commit 89a5fe3
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/extension.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import * as vscode from 'vscode';
import { natunitConfig, reloadConfiguration, setNatparm } from './natunit/config';
import { testData, NatUnitTestCase, NatUnitTest, testToTestCase } from './natunit/testTree';
import { NatUnitCodeLensProvider } from './natunit/codelenses';

let natparmItem: vscode.StatusBarItem;

export async function activate(context: vscode.ExtensionContext) {
const ctrl = vscode.tests.createTestController('natunitTestController', 'NatUnit');
context.subscriptions.push(ctrl);
context.subscriptions.push(vscode.languages.registerCodeLensProvider('natural', new NatUnitCodeLensProvider()));

context.subscriptions.push(vscode.workspace.onDidChangeConfiguration(e => {
if(e.affectsConfiguration("natunit")) {
Expand Down
41 changes: 41 additions & 0 deletions src/natunit/codelenses.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import * as vscode from 'vscode';
import { testRegex } from './parser';
import path = require('path');

export class NatUnitCodeLensProvider implements vscode.CodeLensProvider {
onDidChangeCodeLenses?: vscode.Event<void> | undefined;

private codeLenses : vscode.CodeLens[] = [];

provideCodeLenses(document: vscode.TextDocument, token: vscode.CancellationToken): vscode.ProviderResult<vscode.CodeLens[]> {
this.codeLenses = [];

const filename = path.basename(document.uri.fsPath);
if (!filename.startsWith("TC") && !filename.endsWith(".NSN")) {
return this.codeLenses;
}

for (let lineNo = 0; lineNo < document.lineCount; lineNo++) {
const line = document.lineAt(lineNo);
if (testRegex.exec(line.text)) {
this.codeLenses.push(...this.createCodeLensForRange(line.range));
}
}

return this.codeLenses;
}

resolveCodeLens?(codeLens: vscode.CodeLens, token: vscode.CancellationToken): vscode.ProviderResult<vscode.CodeLens> {
return codeLens;
}

private createCodeLensForRange(range: vscode.Range) : vscode.CodeLens[] {
return [
new vscode.CodeLens(range, {
title: "$(testing-run-icon) Run",
tooltip: "Run the current test case",
command: "testing.runCurrentFile"
}),
]

Check warning on line 39 in src/natunit/codelenses.ts

View workflow job for this annotation

GitHub Actions / build

Missing semicolon
}
};
2 changes: 1 addition & 1 deletion src/natunit/parser.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as vscode from 'vscode';

const testRegex = /^\s*IF NUTESTP.TEST EQ\s+'(.*?)'/g;
export const testRegex = /^\s*IF NUTESTP.TEST EQ\s+'(.*?)'/g;
const testDataRegex = /^\s*\/\*\s*@TESTDATA/g;

export const parseTestCase = (text: string, events: {
Expand Down

0 comments on commit 89a5fe3

Please sign in to comment.