From bea7bcf72b34b18dc34a16ecafbd13d8bd35f6a6 Mon Sep 17 00:00:00 2001 From: Garrett Bjerkhoel Date: Fri, 1 Nov 2024 11:54:12 -0700 Subject: [PATCH] Add extension commands --- README.md | 5 +++++ package.json | 16 +++++++++++++++- src/extension.ts | 40 +++++++++++++++++++++++++++++++++++++++- 3 files changed, 59 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index a0d9d33..5984234 100644 --- a/README.md +++ b/README.md @@ -32,6 +32,11 @@ The coverage is also displayed as a percentage in the editor's status bar: - `uncoveredBranchHighlightColor`: The highlight color to use for highlighting uncovered branches. - `uncoveredBranchGutterStyle`: The style to use for the gutter indicator for uncovered branches. +## Commands + +- `simplecov.coverage.toggle`: Toggle the display of code coverage. +- `simplecov.coverage.apply`: Apply the coverage to the currently active file. +- `simplecov.coverage.remove`: Remove the coverage from the currently active file. ## Requirements diff --git a/package.json b/package.json index 318e6a6..0f7320b 100644 --- a/package.json +++ b/package.json @@ -138,7 +138,21 @@ "scope": "resource" } } - } + }, + "commands": [ + { + "command": "simplecov.coverage.toggle", + "title": "SimpleCov: Toggle Coverage" + }, + { + "command": "simplecov.coverage.apply", + "title": "SimpleCov: Apply Coverage" + }, + { + "command": "simplecov.coverage.remove", + "title": "SimpleCov: Remove Coverage" + } + ] }, "scripts": { "vscode:prepublish": "npm run compile", diff --git a/src/extension.ts b/src/extension.ts index 7f854da..423caca 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -68,6 +68,7 @@ export function activate(context: vscode.ExtensionContext) { initCoverageDecorators(context); addWorkspaceFileSystemWatchers(context); + addExtensionCommandListeners(context); addOnDidChangeConfigListeners(context); addOnChangeActiveTextEditorListeners(context); addOnSaveTextDocumentListeners(context); @@ -75,6 +76,43 @@ export function activate(context: vscode.ExtensionContext) { console.debug("simplecov extension activated"); } +function addExtensionCommandListeners(context: vscode.ExtensionContext) { + let toggleCoverageCommand = vscode.commands.registerCommand( + "simplecov.coverage.toggle", + toggleCoverage + ); + + let applyCoverageCommand = vscode.commands.registerCommand( + "simplecov.coverage.apply", + applyCoverage + ); + + let removeCoverageCommand = vscode.commands.registerCommand( + "simplecov.coverage.remove", + clearCoverage + ); + + context.subscriptions.push(toggleCoverageCommand); + context.subscriptions.push(applyCoverageCommand); + context.subscriptions.push(removeCoverageCommand); +} + +function toggleCoverage() { + if (isCoverageApplied) { + console.debug(`toggleCoverage: removing coverage`); + clearCoverage(); + } else { + console.debug(`toggleCoverage: applying coverage`); + applyCoverage(); + } +} + +function applyCoverage() { + console.debug(`applyCoverage: applying coverage`); + reloadCoverage(); + vscode.window.visibleTextEditors.forEach(applyCodeCoverage); +} + // This method is called when your extension is deactivated export function deactivate() { statusBarItem.dispose(); @@ -305,7 +343,6 @@ function clearCoverage() { } function reloadCoverage() { - console.debug("clearCoverage"); outputChannel.appendLine(`Reloading coverage`); clearCoverage(); @@ -339,6 +376,7 @@ function removeCodeCoverageOnFileSave(e: vscode.TextDocument) { function applyCodeCoverage(editor: vscode.TextEditor | undefined) { if (!editor) { + console.debug(`applyCodeCoverage: no active text editor`); return; }