From 467372b28ac41b2fa77efb4bfef3d37889fe7e98 Mon Sep 17 00:00:00 2001 From: Christopher Allford <6451942+ObliviousHarmony@users.noreply.github.com> Date: Fri, 13 Aug 2021 16:39:05 -0700 Subject: [PATCH] Added Cancel Processing Command (#33) This command allows users to stop the extension's active processing. This is helpful if it stalls for some reason, so that VS Code doesn't need to be restarted. --- CHANGELOG.md | 2 ++ package.json | 8 ++++- src/commands/cancel-processing-command.ts | 34 ++++++++++++++++++ src/extension.ts | 6 +++- src/providers/command-provider.ts | 43 ++++++++++++++++++++++- src/services/worker-service.ts | 11 ++++++ 6 files changed, 101 insertions(+), 3 deletions(-) create mode 100644 src/commands/cancel-processing-command.ts diff --git a/CHANGELOG.md b/CHANGELOG.md index 5e5117f..9b193e9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). ## [Unreleased] +### Added +- Command `phpCodeSniffer.cancelProcessing` to cancel all active processing. ## [1.3.0] - 2021-05-24 ### Fixed diff --git a/package.json b/package.json index 80ab630..f690377 100644 --- a/package.json +++ b/package.json @@ -89,7 +89,13 @@ "scope": "window" } } - } + }, + "commands": [ + { + "command": "phpCodeSniffer.cancelProcessing", + "title": "PHP_CodeSniffer: Cancel Processing" + } + ] }, "devDependencies": { "@types/jest": "^26.0.23", diff --git a/src/commands/cancel-processing-command.ts b/src/commands/cancel-processing-command.ts new file mode 100644 index 0000000..61df657 --- /dev/null +++ b/src/commands/cancel-processing-command.ts @@ -0,0 +1,34 @@ +import { WorkerService } from '../services/worker-service'; + +/** + * A class for handling the command that cancels all active document processing. + */ +export class CancelProcessingCommand { + /** + * The identifier for the command. + */ + public static readonly COMMAND = 'phpCodeSniffer.cancelProcessing'; + + /** + * All of the worker services that we should cancel execution for. + */ + private readonly cancellableServices: WorkerService[]; + + /** + * Constructor. + * + * @param {workspace} workspace The VS Code workspace. + */ + public constructor(cancellableServices: WorkerService[]) { + this.cancellableServices = cancellableServices; + } + + /** + * Handles the command. + */ + public handle(): void { + for (const service of this.cancellableServices) { + service.cancelAll(); + } + } +} diff --git a/src/extension.ts b/src/extension.ts index 5fce29d..b0b644b 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -49,7 +49,11 @@ export function activate(context: ExtensionContext): void { configuration, workerPool ); - const commandProvider = new CommandProvider(); + const commandProvider = new CommandProvider( + diagnosticUpdater, + codeActionEditResolver, + documentFormatter + ); const workspaceListener = new WorkspaceListener( configuration, diagnosticUpdater, diff --git a/src/providers/command-provider.ts b/src/providers/command-provider.ts index 9ffeca7..95e5258 100644 --- a/src/providers/command-provider.ts +++ b/src/providers/command-provider.ts @@ -3,7 +3,11 @@ import { commands as vsCodeCommands, workspace as vsCodeWorkspace, } from 'vscode'; +import { CancelProcessingCommand } from '../commands/cancel-processing-command'; import { IgnoreLineCommand } from '../commands/ignore-line-command'; +import { CodeActionEditResolver } from '../services/code-action-edit-resolver'; +import { DiagnosticUpdater } from '../services/diagnostic-updater'; +import { DocumentFormatter } from '../services/document-formatter'; /** * A class for providing command to VS Code. @@ -14,11 +18,33 @@ export class CommandProvider implements Disposable { */ private readonly subscriptions: Disposable[]; + /** + * The service for updating document diagnostics. + */ + private readonly diagnosticUpdater: DiagnosticUpdater; + + /** + * The service for resolving code action edits. + */ + private readonly codeActionEditResolver: CodeActionEditResolver; + + /** + * The service for formatting documents. + */ + private readonly documentFormatter: DocumentFormatter; + /** * Constructor. */ - public constructor() { + public constructor( + diagnosticUpdater: DiagnosticUpdater, + codeActionEditResolver: CodeActionEditResolver, + documentFormatter: DocumentFormatter + ) { this.subscriptions = []; + this.diagnosticUpdater = diagnosticUpdater; + this.codeActionEditResolver = codeActionEditResolver; + this.documentFormatter = documentFormatter; } /** @@ -41,6 +67,21 @@ export class CommandProvider implements Disposable { workspace: typeof vsCodeWorkspace, commands: typeof vsCodeCommands ): void { + // This command allows users to cancel all processing of documents in the extension. + const cancelProcessingCommand = new CancelProcessingCommand([ + this.diagnosticUpdater, + this.codeActionEditResolver, + this.documentFormatter, + ]); + this.subscriptions.push( + commands.registerCommand( + CancelProcessingCommand.COMMAND, + cancelProcessingCommand.handle, + cancelProcessingCommand + ) + ); + + // This command allows users to ignore specific sniffs on a line. const ignoreLineCommand = new IgnoreLineCommand(workspace); this.subscriptions.push( commands.registerCommand( diff --git a/src/services/worker-service.ts b/src/services/worker-service.ts index e98b8e2..a34e626 100644 --- a/src/services/worker-service.ts +++ b/src/services/worker-service.ts @@ -77,6 +77,17 @@ export abstract class WorkerService implements Disposable { } } + /** + * Cancels in-progress updates for all documents. + */ + public cancelAll(): void { + for (const kvp of this.cancellationTokenSourceMap) { + kvp[1].cancel(); + kvp[1].dispose(); + } + this.cancellationTokenSourceMap.clear(); + } + /** * A handler to be called when a document is closed to clean up after it. *