From 291c53cd19a4fd5f1d7f76ad1342f0ac4547688b Mon Sep 17 00:00:00 2001 From: Eric Amodio Date: Mon, 27 Mar 2017 02:17:09 -0400 Subject: [PATCH] Refactors commands to utilize a common base --- src/commands/commands.ts | 39 ++++++++++++++++++++------------------- 1 file changed, 20 insertions(+), 19 deletions(-) diff --git a/src/commands/commands.ts b/src/commands/commands.ts index cb92e2f..a34b5b0 100644 --- a/src/commands/commands.ts +++ b/src/commands/commands.ts @@ -44,15 +44,19 @@ export abstract class Command extends Disposable { private _disposable: Disposable; - constructor(command: Commands) { + constructor(protected command: Commands) { super(() => this.dispose()); - this._disposable = commands.registerCommand(command, this.execute, this); + this._disposable = commands.registerCommand(command, this._execute, this); } dispose() { this._disposable && this._disposable.dispose(); } + protected _execute(...args: any[]): any { + return this.execute(...args); + } + abstract execute(...args: any[]): any; } @@ -60,33 +64,30 @@ export abstract class EditorCommand extends Disposable { private _disposable: Disposable; - constructor(command: Commands) { + constructor(public readonly command: Commands) { super(() => this.dispose()); - this._disposable = commands.registerTextEditorCommand(command, this.execute, this); + this._disposable = commands.registerTextEditorCommand(command, this._execute, this); } dispose() { this._disposable && this._disposable.dispose(); } + private _execute(editor: TextEditor, edit: TextEditorEdit, ...args: any[]): any { + return this.execute(editor, edit, ...args); + } + abstract execute(editor: TextEditor, edit: TextEditorEdit, ...args: any[]): any; } -export abstract class ActiveEditorCommand extends Disposable { - - private _disposable: Disposable; - - constructor(command: Commands) { - super(() => this.dispose()); - this._disposable = commands.registerCommand(command, this._execute, this); - } +export abstract class ActiveEditorCommand extends Command { - dispose() { - this._disposable && this._disposable.dispose(); + constructor(public readonly command: Commands) { + super(command); } - _execute(...args: any[]): any { - return this.execute(window.activeTextEditor, ...args); + protected _execute(...args: any[]): any { + return super._execute(window.activeTextEditor, ...args); } abstract execute(editor: TextEditor, ...args: any[]): any; @@ -99,16 +100,16 @@ export function getLastCommand() { export abstract class ActiveEditorCachedCommand extends ActiveEditorCommand { - constructor(private command: Commands) { + constructor(public readonly command: Commands) { super(command); } - _execute(...args: any[]): any { + protected _execute(...args: any[]): any { lastCommand = { command: this.command, args: args }; - return this.execute(window.activeTextEditor, ...args); + return super._execute(...args); } abstract execute(editor: TextEditor, ...args: any[]): any;