diff --git a/src/zigMainCodeLens.ts b/src/zigMainCodeLens.ts index 4e106e2..0699f6b 100644 --- a/src/zigMainCodeLens.ts +++ b/src/zigMainCodeLens.ts @@ -16,11 +16,16 @@ export default class ZigMainCodeLensProvider implements vscode.CodeLensProvider const codeLenses: vscode.CodeLens[] = []; const text = document.getText(); - const mainRegex = /pub\s+fn\s+main\s*\(/g; + const mainRegex = /^(?!\s*\/\/\/?\s*)\s*pub\s+fn\s+main\s*\(/gm; let match; while ((match = mainRegex.exec(text))) { const position = document.positionAt(match.index); - const range = new vscode.Range(position, position); + const line = document.lineAt(position.line); + const nextLine = Math.min(line.lineNumber + 1, document.lineCount - 1); + const range = new vscode.Range( + document.lineAt(nextLine).range.start, + document.lineAt(nextLine).range.start, + ); codeLenses.push( new vscode.CodeLens(range, { title: "Run", command: "zig.run", arguments: [document.uri.fsPath] }), ); diff --git a/src/zigTestRunnerProvider.ts b/src/zigTestRunnerProvider.ts index 2d2abf2..64df8ea 100644 --- a/src/zigTestRunnerProvider.ts +++ b/src/zigTestRunnerProvider.ts @@ -70,7 +70,7 @@ export default class ZigTestRunnerProvider { private _updateTestItems(textDocument: vscode.TextDocument) { if (textDocument.languageId !== "zig") return; - const regex = /\btest\s+(?:"([^"]+)"|([a-zA-Z0-9_][\w]*)|@"([^"]+)")\s*\{/g; + const regex = /^(?!\s*\/\/\/?\s*)\s*\btest\s+(?:"([^"]+)"|([A-Za-z0-9_][\w]*)|@"([^"]+)")\s*\{/gm; const matches = Array.from(textDocument.getText().matchAll(regex)); this.deleteTestForAFile(textDocument.uri); @@ -78,7 +78,13 @@ export default class ZigTestRunnerProvider { const testDesc = match[1] || match[2] || match[3]; const isDocTest = !match[1]; const position = textDocument.positionAt(match.index); - const range = new vscode.Range(position, position.translate(0, match[0].length)); + const line = textDocument.lineAt(position.line); + const nextLine = Math.min(line.lineNumber + 1, textDocument.lineCount - 1); + const range = new vscode.Range( + textDocument.lineAt(nextLine).range.start, + textDocument.lineAt(nextLine).range.start, + ); + const fileName = path.basename(textDocument.uri.fsPath); // Add doctest prefix to handle scenario where test name matches one with non doctest. E.g `test foo` and `test "foo"`