Skip to content

Commit

Permalink
Added Cancel Processing Command (#33)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
ObliviousHarmony authored Aug 13, 2021
1 parent 7fe7eab commit 467372b
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 3 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 7 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,13 @@
"scope": "window"
}
}
}
},
"commands": [
{
"command": "phpCodeSniffer.cancelProcessing",
"title": "PHP_CodeSniffer: Cancel Processing"
}
]
},
"devDependencies": {
"@types/jest": "^26.0.23",
Expand Down
34 changes: 34 additions & 0 deletions src/commands/cancel-processing-command.ts
Original file line number Diff line number Diff line change
@@ -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();
}
}
}
6 changes: 5 additions & 1 deletion src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
43 changes: 42 additions & 1 deletion src/providers/command-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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;
}

/**
Expand All @@ -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(
Expand Down
11 changes: 11 additions & 0 deletions src/services/worker-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down

0 comments on commit 467372b

Please sign in to comment.