diff --git a/src/commands/commands.ts b/src/commands/commands.ts index 7ae5dd0..c097dd8 100644 --- a/src/commands/commands.ts +++ b/src/commands/commands.ts @@ -1,5 +1,5 @@ 'use strict'; -import { commands, Disposable, TextEditor, TextEditorEdit } from 'vscode'; +import { commands, Disposable, TextEditor, TextEditorEdit, window } from 'vscode'; import { Commands } from '../constants'; export abstract class Command extends Disposable { @@ -31,4 +31,23 @@ export abstract class EditorCommand extends Disposable { } 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); + } + + dispose() { + this._disposable && this._disposable.dispose(); + } + + _execute(...args: any[]): any { + return this.execute(window.activeTextEditor, ...args); + } + + abstract execute(editor: TextEditor, ...args: any[]): any; } \ No newline at end of file diff --git a/src/commands/showQuickCommitDetails.ts b/src/commands/showQuickCommitDetails.ts index ad0e3d4..5356d7f 100644 --- a/src/commands/showQuickCommitDetails.ts +++ b/src/commands/showQuickCommitDetails.ts @@ -1,22 +1,22 @@ 'use strict'; import { Iterables } from '../system'; -import { TextEditor, TextEditorEdit, Uri, window } from 'vscode'; -import { EditorCommand } from './commands'; +import { TextEditor, Uri, window } from 'vscode'; +import { ActiveEditorCommand } from './commands'; import { Commands } from '../constants'; import GitProvider, { GitCommit, GitUri } from '../gitProvider'; import { Logger } from '../logger'; import { CommandQuickPickItem, FileQuickPickItem } from './quickPickItems'; import { CommitQuickPick, CommitFilesQuickPick } from './quickPicks'; -export default class ShowQuickCommitDetailsCommand extends EditorCommand { +export default class ShowQuickCommitDetailsCommand extends ActiveEditorCommand { constructor(private git: GitProvider) { super(Commands.ShowQuickCommitDetails); } - async execute(editor: TextEditor, edit: TextEditorEdit, uri?: Uri, sha?: string, commit?: GitCommit, goBackCommand?: CommandQuickPickItem, options: { showFileHistory?: boolean } = { showFileHistory: true }) { + async execute(editor: TextEditor, uri?: Uri, sha?: string, commit?: GitCommit, goBackCommand?: CommandQuickPickItem, options: { showFileHistory?: boolean } = { showFileHistory: true }) { if (!(uri instanceof Uri)) { - if (!editor.document) return undefined; + if (!editor || !editor.document) return undefined; uri = editor.document.uri; } @@ -24,9 +24,10 @@ export default class ShowQuickCommitDetailsCommand extends EditorCommand { let repoPath = gitUri.repoPath; - let line = editor.selection.active.line; if (!sha) { - const blameline = line - gitUri.offset; + if (!editor) return undefined; + + const blameline = editor.selection.active.line - gitUri.offset; if (blameline < 0) return undefined; try { diff --git a/src/commands/showQuickFileHistory.ts b/src/commands/showQuickFileHistory.ts index 388c6e1..49402b2 100644 --- a/src/commands/showQuickFileHistory.ts +++ b/src/commands/showQuickFileHistory.ts @@ -1,21 +1,21 @@ 'use strict'; -import { commands, TextEditor, TextEditorEdit, Uri, window } from 'vscode'; -import { EditorCommand } from './commands'; +import { commands, TextEditor, Uri, window } from 'vscode'; +import { ActiveEditorCommand } from './commands'; import { Commands } from '../constants'; import GitProvider, { GitUri } from '../gitProvider'; import { Logger } from '../logger'; import { CommandQuickPickItem } from './quickPickItems'; import { FileCommitsQuickPick } from './quickPicks'; -export default class ShowQuickFileHistoryCommand extends EditorCommand { +export default class ShowQuickFileHistoryCommand extends ActiveEditorCommand { constructor(private git: GitProvider) { super(Commands.ShowQuickFileHistory); } - async execute(editor: TextEditor, edit: TextEditorEdit, uri?: Uri, maxCount?: number, goBackCommand?: CommandQuickPickItem) { + async execute(editor: TextEditor, uri?: Uri, maxCount?: number, goBackCommand?: CommandQuickPickItem) { if (!(uri instanceof Uri)) { - if (!editor.document) return undefined; + if (!editor || !editor.document) return undefined; uri = editor.document.uri; }