From 0a9559f5a52b261ba5187f468e28d35d48d68c8d Mon Sep 17 00:00:00 2001 From: Eric Amodio Date: Tue, 27 Jun 2017 19:28:14 -0400 Subject: [PATCH] Reworks commanding structure for less redundancy Adds command args copying when needed --- src/commands/closeUnchangedFiles.ts | 22 +--- src/commands/common.ts | 151 ++++++++++++++------------ src/commands/copyMessageToClipboard.ts | 4 +- src/commands/copyShaToClipboard.ts | 4 +- src/commands/diffDirectory.ts | 4 +- src/commands/diffLineWithPrevious.ts | 8 +- src/commands/diffLineWithWorking.ts | 8 +- src/commands/diffWithBranch.ts | 20 +--- src/commands/diffWithNext.ts | 3 +- src/commands/diffWithPrevious.ts | 20 +--- src/commands/diffWithRevision.ts | 20 +--- src/commands/diffWithWorking.ts | 3 +- src/commands/openBranchInRemote.ts | 4 +- src/commands/openChangedFiles.ts | 22 +--- src/commands/openCommitInRemote.ts | 2 +- src/commands/openFileInRemote.ts | 18 +-- src/commands/openInRemote.ts | 5 +- src/commands/openRepoInRemote.ts | 2 +- src/commands/showBlameHistory.ts | 2 + src/commands/showCommitSearch.ts | 6 +- src/commands/showFileBlame.ts | 2 + src/commands/showFileHistory.ts | 2 + src/commands/showLastQuickPick.ts | 2 +- src/commands/showLineBlame.ts | 2 + src/commands/showQuickBranchHistory.ts | 3 +- src/commands/showQuickCommitDetails.ts | 3 +- src/commands/showQuickCommitFileDetails.ts | 3 +- src/commands/showQuickCurrentBranchHistory.ts | 2 +- src/commands/showQuickFileHistory.ts | 20 +--- src/commands/showQuickRepoStatus.ts | 2 +- src/commands/showQuickStashList.ts | 2 +- src/commands/stashApply.ts | 1 + src/commands/stashDelete.ts | 1 + src/commands/stashSave.ts | 1 + src/commands/toggleFileBlame.ts | 2 + src/commands/toggleLineBlame.ts | 2 + 36 files changed, 169 insertions(+), 209 deletions(-) diff --git a/src/commands/closeUnchangedFiles.ts b/src/commands/closeUnchangedFiles.ts index f1b21fd..046917b 100644 --- a/src/commands/closeUnchangedFiles.ts +++ b/src/commands/closeUnchangedFiles.ts @@ -1,7 +1,7 @@ 'use strict'; import { commands, TextEditor, Uri, window } from 'vscode'; import { ActiveEditorTracker } from '../activeEditorTracker'; -import { ActiveEditorCommand, CommandContext, Commands, getCommandUri } from './common'; +import { ActiveEditorCommand, Commands, getCommandUri } from './common'; import { TextEditorComparer, UriComparer } from '../comparers'; import { BuiltInCommands } from '../constants'; import { GitService } from '../gitService'; @@ -18,27 +18,13 @@ export class CloseUnchangedFilesCommand extends ActiveEditorCommand { super(Commands.CloseUnchangedFiles); } - async run(context: CommandContext, args: CloseUnchangedFilesCommandArgs = {}): Promise { - // Since we can change the args and they could be cached -- make a copy - switch (context.type) { - case 'uri': - return this.execute(context.editor, context.uri, { ...args }); - case 'scm-states': - return undefined; - case 'scm-groups': - // const group = context.scmResourceGroups[0]; - // args.uris = group.resourceStates.map(_ => _.resourceUri); - return this.execute(undefined, undefined, { ...args }); - default: - return this.execute(context.editor, undefined, { ...args }); - } - } - - async execute(editor: TextEditor | undefined, uri?: Uri, args: CloseUnchangedFilesCommandArgs = {}) { + async execute(editor?: TextEditor, uri?: Uri, args: CloseUnchangedFilesCommandArgs = {}) { uri = getCommandUri(uri, editor); try { if (args.uris === undefined) { + args = { ...args }; + const repoPath = await this.git.getRepoPathFromUri(uri); if (!repoPath) return Messages.showNoRepositoryWarningMessage(`Unable to close unchanged files`); diff --git a/src/commands/common.ts b/src/commands/common.ts index 7ba146d..7970052 100644 --- a/src/commands/common.ts +++ b/src/commands/common.ts @@ -88,28 +88,35 @@ export function getCommandUri(uri?: Uri, editor?: TextEditor): Uri | undefined { return editor.document.uri; } -export interface ScmGroupsCommandContext { +export interface CommandContextParsingOptions { + editor: boolean; + uri: boolean; +} + +export interface CommandBaseContext { + editor?: TextEditor; + uri?: Uri; +} + +export interface CommandScmGroupsContext extends CommandBaseContext { type: 'scm-groups'; scmResourceGroups: SourceControlResourceGroup[]; } -export interface ScmStatesCommandContext { +export interface CommandScmStatesContext extends CommandBaseContext { type: 'scm-states'; scmResourceStates: SourceControlResourceState[]; } -export interface UnknownCommandContext { +export interface CommandUnknownContext extends CommandBaseContext { type: 'unknown'; - editor?: TextEditor; } -export interface UriCommandContext { +export interface CommandUriContext extends CommandBaseContext { type: 'uri'; - editor?: TextEditor; - uri: Uri; } -export type CommandContext = ScmGroupsCommandContext | ScmStatesCommandContext | UnknownCommandContext | UriCommandContext; +export type CommandContext = CommandScmGroupsContext | CommandScmStatesContext | CommandUnknownContext | CommandUriContext; function isScmResourceGroup(group: any): group is SourceControlResourceGroup { if (group === undefined) return false; @@ -129,16 +136,15 @@ function isTextEditor(editor: any): editor is TextEditor { return editor.id !== undefined && ((editor as TextEditor).edit !== undefined || (editor as TextEditor).document !== undefined); } -export interface Command { - run?(context: CommandContext, ...args: any[]): any; -} - export abstract class Command extends Disposable { + protected readonly contextParsingOptions: CommandContextParsingOptions = { editor: false, uri: false }; + private _disposable: Disposable; constructor(protected command: Commands) { super(() => this.dispose()); + this._disposable = commands.registerCommand(command, this._execute, this); } @@ -146,91 +152,81 @@ export abstract class Command extends Disposable { this._disposable && this._disposable.dispose(); } + protected async preExecute(context: CommandContext, ...args: any[]): Promise { + return this.execute(...args); + } + + abstract execute(...args: any[]): any; + protected _execute(...args: any[]): any { Telemetry.trackEvent(this.command); - if (typeof this.run === 'function') { - let editor: TextEditor | undefined = undefined; + const [context, rest] = Command._parseContext(this.contextParsingOptions, ...args); + return this.preExecute(context, ...rest); + } - let firstArg = args[0]; - if (firstArg === undefined || isTextEditor(firstArg)) { - editor = firstArg; - args = args.slice(1); - firstArg = args[0]; - } + private static _parseContext(options: CommandContextParsingOptions, ...args: any[]): [CommandContext, any[]] { + let editor: TextEditor | undefined = undefined; - if (firstArg instanceof Uri) { - const [uri, ...rest] = args; - return this.run({ type: 'uri', editor: editor, uri: uri }, ...rest); - } + let firstArg = args[0]; + if (options.editor && (firstArg === undefined || isTextEditor(firstArg))) { + editor = firstArg; + args = args.slice(1); + firstArg = args[0]; + } - if (isScmResourceState(firstArg)) { - const states = []; - let count = 0; - for (const arg of args) { - if (!isScmResourceState(arg)) break; + if (options.uri && (firstArg === undefined || firstArg instanceof Uri)) { + const [uri, ...rest] = args as [Uri, any]; + return [{ type: 'uri', editor: editor, uri: uri }, rest]; + } - count++; - states.push(arg); - } + if (isScmResourceState(firstArg)) { + const states = []; + let count = 0; + for (const arg of args) { + if (!isScmResourceState(arg)) break; - return this.run({ type: 'scm-states', scmResourceStates: states }, ...args.slice(count)); + count++; + states.push(arg); } - if (isScmResourceGroup(firstArg)) { - const groups = []; - let count = 0; - for (const arg of args) { - if (!isScmResourceGroup(arg)) break; + return [{ type: 'scm-states', scmResourceStates: states, uri: states[0].resourceUri }, args.slice(count)]; + } - count++; - groups.push(arg); - } + if (isScmResourceGroup(firstArg)) { + const groups = []; + let count = 0; + for (const arg of args) { + if (!isScmResourceGroup(arg)) break; - return this.run({ type: 'scm-groups', scmResourceGroups: groups }, ...args.slice(count)); + count++; + groups.push(arg); } - return this.run({ type: 'unknown', editor: editor }, ...args); + return [{ type: 'scm-groups', scmResourceGroups: groups }, args.slice(count)]; } - return this.execute(...args); + return [{ type: 'unknown', editor: editor }, args]; } - - abstract execute(...args: any[]): any; } -export abstract class EditorCommand extends Disposable { +export abstract class ActiveEditorCommand extends Command { - private _disposable: Disposable; + protected readonly contextParsingOptions: CommandContextParsingOptions = { editor: true, uri: true }; constructor(public readonly command: Commands) { - super(() => this.dispose()); - this._disposable = commands.registerTextEditorCommand(command, this._execute, this); - } - - dispose() { - this._disposable && this._disposable.dispose(); - } - - private _execute(editor: TextEditor, edit: TextEditorEdit, ...args: any[]): any { - Telemetry.trackEvent(this.command); - return this.execute(editor, edit, ...args); + super(command); } - abstract execute(editor: TextEditor, edit: TextEditorEdit, ...args: any[]): any; -} - -export abstract class ActiveEditorCommand extends Command { - - constructor(public readonly command: Commands) { - super(command); + protected async preExecute(context: CommandContext, ...args: any[]): Promise { + return this.execute(context.editor, context.uri, ...args); } protected _execute(...args: any[]): any { return super._execute(window.activeTextEditor, ...args); } - abstract execute(editor: TextEditor, ...args: any[]): any; + abstract execute(editor?: TextEditor, ...args: any[]): any; } let lastCommand: { command: string, args: any[] } | undefined = undefined; @@ -255,6 +251,27 @@ export abstract class ActiveEditorCachedCommand extends ActiveEditorCommand { abstract execute(editor: TextEditor, ...args: any[]): any; } +export abstract class EditorCommand extends Disposable { + + private _disposable: Disposable; + + constructor(public readonly command: Commands) { + super(() => this.dispose()); + this._disposable = commands.registerTextEditorCommand(command, this._execute, this); + } + + dispose() { + this._disposable && this._disposable.dispose(); + } + + private _execute(editor: TextEditor, edit: TextEditorEdit, ...args: any[]): any { + Telemetry.trackEvent(this.command); + return this.execute(editor, edit, ...args); + } + + abstract execute(editor: TextEditor, edit: TextEditorEdit, ...args: any[]): any; +} + export async function openEditor(uri: Uri, options?: TextDocumentShowOptions): Promise { try { const defaults: TextDocumentShowOptions = { diff --git a/src/commands/copyMessageToClipboard.ts b/src/commands/copyMessageToClipboard.ts index 83fe955..141c607 100644 --- a/src/commands/copyMessageToClipboard.ts +++ b/src/commands/copyMessageToClipboard.ts @@ -17,10 +17,12 @@ export class CopyMessageToClipboardCommand extends ActiveEditorCommand { super(Commands.CopyMessageToClipboard); } - async execute(editor: TextEditor, uri?: Uri, args: CopyMessageToClipboardCommandArgs = {}): Promise { + async execute(editor?: TextEditor, uri?: Uri, args: CopyMessageToClipboardCommandArgs = {}): Promise { uri = getCommandUri(uri, editor); try { + args = { ...args }; + // If we don't have an editor then get the message of the last commit to the branch if (uri === undefined) { if (!this.git.repoPath) return undefined; diff --git a/src/commands/copyShaToClipboard.ts b/src/commands/copyShaToClipboard.ts index ed6ec42..a47aba7 100644 --- a/src/commands/copyShaToClipboard.ts +++ b/src/commands/copyShaToClipboard.ts @@ -16,10 +16,12 @@ export class CopyShaToClipboardCommand extends ActiveEditorCommand { super(Commands.CopyShaToClipboard); } - async execute(editor: TextEditor, uri?: Uri, args: CopyShaToClipboardCommandArgs = {}): Promise { + async execute(editor?: TextEditor, uri?: Uri, args: CopyShaToClipboardCommandArgs = {}): Promise { uri = getCommandUri(uri, editor); try { + args = { ...args }; + // If we don't have an editor then get the sha of the last commit to the branch if (uri === undefined) { if (!this.git.repoPath) return undefined; diff --git a/src/commands/diffDirectory.ts b/src/commands/diffDirectory.ts index 89f4815..cff4e4a 100644 --- a/src/commands/diffDirectory.ts +++ b/src/commands/diffDirectory.ts @@ -19,7 +19,7 @@ export class DiffDirectoryCommand extends ActiveEditorCommand { super(Commands.DiffDirectory); } - async execute(editor: TextEditor, uri?: Uri, args: DiffDirectoryCommandCommandArgs = {}): Promise { + async execute(editor?: TextEditor, uri?: Uri, args: DiffDirectoryCommandCommandArgs = {}): Promise { const diffTool = await this.git.getConfig('diff.tool'); if (!diffTool) { const result = await window.showWarningMessage(`Unable to open directory compare because there is no Git diff tool configured`, 'View Git Docs'); @@ -35,6 +35,8 @@ export class DiffDirectoryCommand extends ActiveEditorCommand { if (!repoPath) return Messages.showNoRepositoryWarningMessage(`Unable to open directory compare`); if (!args.shaOrBranch1) { + args = { ...args }; + const branches = await this.git.getBranches(repoPath); const current = Iterables.find(branches, _ => _.current); if (current == null) return window.showWarningMessage(`Unable to open directory compare`); diff --git a/src/commands/diffLineWithPrevious.ts b/src/commands/diffLineWithPrevious.ts index 7c650a6..255e6ed 100644 --- a/src/commands/diffLineWithPrevious.ts +++ b/src/commands/diffLineWithPrevious.ts @@ -21,12 +21,16 @@ export class DiffLineWithPreviousCommand extends ActiveEditorCommand { super(Commands.DiffLineWithPrevious); } - async execute(editor: TextEditor, uri?: Uri, args: DiffLineWithPreviousCommandArgs = {}): Promise { + async execute(editor?: TextEditor, uri?: Uri, args: DiffLineWithPreviousCommandArgs = {}): Promise { uri = getCommandUri(uri, editor); if (uri === undefined) return undefined; const gitUri = await GitUri.fromUri(uri, this.git); - args.line = args.line || (editor === undefined ? gitUri.offset : editor.selection.active.line); + + args = { ...args }; + if (args.line === undefined) { + args.line = editor === undefined ? gitUri.offset : editor.selection.active.line; + } if (args.commit === undefined || GitService.isUncommitted(args.commit.sha)) { if (editor !== undefined && editor.document !== undefined && editor.document.isDirty) return undefined; diff --git a/src/commands/diffLineWithWorking.ts b/src/commands/diffLineWithWorking.ts index 4626a4a..2869361 100644 --- a/src/commands/diffLineWithWorking.ts +++ b/src/commands/diffLineWithWorking.ts @@ -18,12 +18,16 @@ export class DiffLineWithWorkingCommand extends ActiveEditorCommand { super(Commands.DiffLineWithWorking); } - async execute(editor: TextEditor, uri?: Uri, args: DiffLineWithWorkingCommandArgs = {}): Promise { + async execute(editor?: TextEditor, uri?: Uri, args: DiffLineWithWorkingCommandArgs = {}): Promise { uri = getCommandUri(uri, editor); if (uri === undefined) return undefined; const gitUri = await GitUri.fromUri(uri, this.git); - args.line = args.line || (editor === undefined ? gitUri.offset : editor.selection.active.line); + + args = { ...args }; + if (args.line === undefined) { + args.line = editor === undefined ? gitUri.offset : editor.selection.active.line; + } if (args.commit === undefined || GitService.isUncommitted(args.commit.sha)) { if (editor !== undefined && editor.document !== undefined && editor.document.isDirty) return undefined; diff --git a/src/commands/diffWithBranch.ts b/src/commands/diffWithBranch.ts index 82395e7..94a7e34 100644 --- a/src/commands/diffWithBranch.ts +++ b/src/commands/diffWithBranch.ts @@ -1,6 +1,6 @@ 'use strict'; import { commands, TextDocumentShowOptions, TextEditor, Uri, window } from 'vscode'; -import { ActiveEditorCommand, CommandContext, Commands, getCommandUri } from './common'; +import { ActiveEditorCommand, Commands, getCommandUri } from './common'; import { BuiltInCommands, GlyphChars } from '../constants'; import { GitService, GitUri } from '../gitService'; import { Logger } from '../logger'; @@ -21,25 +21,11 @@ export class DiffWithBranchCommand extends ActiveEditorCommand { super(Commands.DiffWithBranch); } - async run(context: CommandContext, args: DiffWithBranchCommandArgs = {}): Promise { - // Since we can change the args and they could be cached -- make a copy - switch (context.type) { - case 'uri': - return this.execute(context.editor, context.uri, { ...args }); - case 'scm-states': - const resource = context.scmResourceStates[0]; - return this.execute(undefined, resource.resourceUri, { ...args }); - case 'scm-groups': - return undefined; - default: - return this.execute(context.editor, undefined, { ...args }); - } - } - - async execute(editor: TextEditor | undefined, uri?: Uri, args: DiffWithBranchCommandArgs = {}): Promise { + async execute(editor?: TextEditor, uri?: Uri, args: DiffWithBranchCommandArgs = {}): Promise { uri = getCommandUri(uri, editor); if (uri === undefined) return undefined; + args = { ...args }; if (args.line === undefined) { args.line = editor === undefined ? 0 : editor.selection.active.line; } diff --git a/src/commands/diffWithNext.ts b/src/commands/diffWithNext.ts index 7b3b09a..edcbd81 100644 --- a/src/commands/diffWithNext.ts +++ b/src/commands/diffWithNext.ts @@ -21,10 +21,11 @@ export class DiffWithNextCommand extends ActiveEditorCommand { super(Commands.DiffWithNext); } - async execute(editor: TextEditor, uri?: Uri, args: DiffWithNextCommandArgs = {}): Promise { + async execute(editor?: TextEditor, uri?: Uri, args: DiffWithNextCommandArgs = {}): Promise { uri = getCommandUri(uri, editor); if (uri === undefined) return undefined; + args = { ...args }; if (args.line === undefined) { args.line = editor === undefined ? 0 : editor.selection.active.line; } diff --git a/src/commands/diffWithPrevious.ts b/src/commands/diffWithPrevious.ts index 0fd5e14..cb0af49 100644 --- a/src/commands/diffWithPrevious.ts +++ b/src/commands/diffWithPrevious.ts @@ -1,7 +1,7 @@ 'use strict'; import { Iterables } from '../system'; import { commands, Range, TextDocumentShowOptions, TextEditor, Uri, window } from 'vscode'; -import { ActiveEditorCommand, CommandContext, Commands, getCommandUri } from './common'; +import { ActiveEditorCommand, Commands, getCommandUri } from './common'; import { BuiltInCommands, GlyphChars } from '../constants'; import { DiffWithWorkingCommandArgs } from './diffWithWorking'; import { GitCommit, GitService, GitUri } from '../gitService'; @@ -22,25 +22,11 @@ export class DiffWithPreviousCommand extends ActiveEditorCommand { super(Commands.DiffWithPrevious); } - async run(context: CommandContext, args: DiffWithPreviousCommandArgs = {}): Promise { - // Since we can change the args and they could be cached -- make a copy - switch (context.type) { - case 'uri': - return this.execute(context.editor, context.uri, { ...args }); - case 'scm-states': - const resource = context.scmResourceStates[0]; - return this.execute(undefined, resource.resourceUri, { ...args }); - case 'scm-groups': - return undefined; - default: - return this.execute(context.editor, undefined, { ...args }); - } - } - - async execute(editor: TextEditor | undefined, uri?: Uri, args: DiffWithPreviousCommandArgs = {}): Promise { + async execute(editor?: TextEditor, uri?: Uri, args: DiffWithPreviousCommandArgs = {}): Promise { uri = getCommandUri(uri, editor); if (uri === undefined) return undefined; + args = { ...args }; if (args.line === undefined) { args.line = editor === undefined ? 0 : editor.selection.active.line; } diff --git a/src/commands/diffWithRevision.ts b/src/commands/diffWithRevision.ts index d934ef1..1310702 100644 --- a/src/commands/diffWithRevision.ts +++ b/src/commands/diffWithRevision.ts @@ -1,6 +1,6 @@ 'use strict'; import { commands, TextDocumentShowOptions, TextEditor, Uri, window } from 'vscode'; -import { ActiveEditorCommand, CommandContext, Commands, getCommandUri } from './common'; +import { ActiveEditorCommand, Commands, getCommandUri } from './common'; import { BuiltInCommands, GlyphChars } from '../constants'; import { GitService, GitUri } from '../gitService'; import { Logger } from '../logger'; @@ -20,25 +20,11 @@ export class DiffWithRevisionCommand extends ActiveEditorCommand { super(Commands.DiffWithRevision); } - async run(context: CommandContext, args: DiffWithRevisionCommandArgs = {}): Promise { - // Since we can change the args and they could be cached -- make a copy - switch (context.type) { - case 'uri': - return this.execute(context.editor, context.uri, { ...args }); - case 'scm-states': - const resource = context.scmResourceStates[0]; - return this.execute(undefined, resource.resourceUri, { ...args }); - case 'scm-groups': - return undefined; - default: - return this.execute(context.editor, undefined, { ...args }); - } - } - - async execute(editor: TextEditor | undefined, uri?: Uri, args: DiffWithRevisionCommandArgs = {}): Promise { + async execute(editor?: TextEditor, uri?: Uri, args: DiffWithRevisionCommandArgs = {}): Promise { uri = getCommandUri(uri, editor); if (uri === undefined) return undefined; + args = { ...args }; if (args.line === undefined) { args.line = editor === undefined ? 0 : editor.selection.active.line; } diff --git a/src/commands/diffWithWorking.ts b/src/commands/diffWithWorking.ts index ef6373f..ab849cc 100644 --- a/src/commands/diffWithWorking.ts +++ b/src/commands/diffWithWorking.ts @@ -19,10 +19,11 @@ export class DiffWithWorkingCommand extends ActiveEditorCommand { super(Commands.DiffWithWorking); } - async execute(editor: TextEditor, uri?: Uri, args: DiffWithWorkingCommandArgs = {}): Promise { + async execute(editor?: TextEditor, uri?: Uri, args: DiffWithWorkingCommandArgs = {}): Promise { uri = getCommandUri(uri, editor); if (uri === undefined) return undefined; + args = { ...args }; if (args.line === undefined) { args.line = editor === undefined ? 0 : editor.selection.active.line; } diff --git a/src/commands/openBranchInRemote.ts b/src/commands/openBranchInRemote.ts index 796ef83..98b6834 100644 --- a/src/commands/openBranchInRemote.ts +++ b/src/commands/openBranchInRemote.ts @@ -18,7 +18,7 @@ export class OpenBranchInRemoteCommand extends ActiveEditorCommand { super(Commands.OpenBranchInRemote); } - async execute(editor: TextEditor, uri?: Uri, args: OpenBranchInRemoteCommandArgs = {}) { + async execute(editor?: TextEditor, uri?: Uri, args: OpenBranchInRemoteCommandArgs = {}) { uri = getCommandUri(uri, editor); const gitUri = uri && await GitUri.fromUri(uri, this.git); @@ -28,6 +28,8 @@ export class OpenBranchInRemoteCommand extends ActiveEditorCommand { try { if (args.branch === undefined) { + args = { ...args }; + const branches = await this.git.getBranches(repoPath); const pick = await BranchesQuickPick.show(branches, `Show history for branch${GlyphChars.Ellipsis}`); diff --git a/src/commands/openChangedFiles.ts b/src/commands/openChangedFiles.ts index 62dfc3a..cd6355e 100644 --- a/src/commands/openChangedFiles.ts +++ b/src/commands/openChangedFiles.ts @@ -1,6 +1,6 @@ 'use strict'; import { TextDocumentShowOptions, TextEditor, Uri, window } from 'vscode'; -import { ActiveEditorCommand, CommandContext, Commands, getCommandUri, openEditor } from './common'; +import { ActiveEditorCommand, Commands, getCommandUri, openEditor } from './common'; import { GitService } from '../gitService'; import { Logger } from '../logger'; import { Messages } from '../messages'; @@ -15,27 +15,13 @@ export class OpenChangedFilesCommand extends ActiveEditorCommand { super(Commands.OpenChangedFiles); } - async run(context: CommandContext, args: OpenChangedFilesCommandArgs = {}): Promise { - // Since we can change the args and they could be cached -- make a copy - switch (context.type) { - case 'uri': - return this.execute(context.editor, context.uri, { ...args }); - case 'scm-states': - return undefined; - case 'scm-groups': - // const group = context.scmResourceGroups[0]; - // args.uris = group.resourceStates.map(_ => _.resourceUri); - return this.execute(undefined, undefined, { ...args }); - default: - return this.execute(context.editor, undefined, { ...args }); - } - } - - async execute(editor: TextEditor | undefined, uri?: Uri, args: OpenChangedFilesCommandArgs = {}) { + async execute(editor?: TextEditor, uri?: Uri, args: OpenChangedFilesCommandArgs = {}) { uri = getCommandUri(uri, editor); try { if (args.uris === undefined) { + args = { ...args }; + const repoPath = await this.git.getRepoPathFromUri(uri); if (!repoPath) return Messages.showNoRepositoryWarningMessage(`Unable to open changed files`); diff --git a/src/commands/openCommitInRemote.ts b/src/commands/openCommitInRemote.ts index c3c1ce3..17a600e 100644 --- a/src/commands/openCommitInRemote.ts +++ b/src/commands/openCommitInRemote.ts @@ -13,7 +13,7 @@ export class OpenCommitInRemoteCommand extends ActiveEditorCommand { super(Commands.OpenCommitInRemote); } - async execute(editor: TextEditor, uri?: Uri) { + async execute(editor?: TextEditor, uri?: Uri) { uri = getCommandUri(uri, editor); if (uri === undefined) return undefined; if (editor !== undefined && editor.document !== undefined && editor.document.isDirty) return undefined; diff --git a/src/commands/openFileInRemote.ts b/src/commands/openFileInRemote.ts index 3d70b93..84e88c5 100644 --- a/src/commands/openFileInRemote.ts +++ b/src/commands/openFileInRemote.ts @@ -1,7 +1,7 @@ 'use strict'; import { Arrays } from '../system'; import { commands, Range, TextEditor, Uri, window } from 'vscode'; -import { ActiveEditorCommand, CommandContext, Commands, getCommandUri } from './common'; +import { ActiveEditorCommand, Commands, getCommandUri } from './common'; import { GitService, GitUri } from '../gitService'; import { Logger } from '../logger'; import { OpenInRemoteCommandArgs } from './openInRemote'; @@ -12,21 +12,7 @@ export class OpenFileInRemoteCommand extends ActiveEditorCommand { super(Commands.OpenFileInRemote); } - async run(context: CommandContext): Promise { - switch (context.type) { - case 'uri': - return this.execute(context.editor, context.uri); - case 'scm-states': - const resource = context.scmResourceStates[0]; - return this.execute(undefined, resource.resourceUri); - case 'scm-groups': - return undefined; - default: - return this.execute(context.editor, undefined); - } - } - - async execute(editor: TextEditor | undefined, uri?: Uri) { + async execute(editor?: TextEditor, uri?: Uri) { uri = getCommandUri(uri, editor); if (uri === undefined) return undefined; diff --git a/src/commands/openInRemote.ts b/src/commands/openInRemote.ts index b4eda1f..8c279fe 100644 --- a/src/commands/openInRemote.ts +++ b/src/commands/openInRemote.ts @@ -23,9 +23,10 @@ export class OpenInRemoteCommand extends ActiveEditorCommand { async execute(editor: TextEditor, uri?: Uri, args: OpenInRemoteCommandArgs = {}) { uri = getCommandUri(uri, editor); - try { - if (args.remotes === undefined || args.resource === undefined) return undefined; + args = { ...args }; + if (args.remotes === undefined || args.resource === undefined) return undefined; + try { if (args.remotes.length === 1) { const command = new OpenRemoteCommandQuickPickItem(args.remotes[0], args.resource); return command.execute(); diff --git a/src/commands/openRepoInRemote.ts b/src/commands/openRepoInRemote.ts index e4e02d2..c301c25 100644 --- a/src/commands/openRepoInRemote.ts +++ b/src/commands/openRepoInRemote.ts @@ -12,7 +12,7 @@ export class OpenRepoInRemoteCommand extends ActiveEditorCommand { super(Commands.OpenRepoInRemote); } - async execute(editor: TextEditor, uri?: Uri) { + async execute(editor?: TextEditor, uri?: Uri) { uri = getCommandUri(uri, editor); const gitUri = uri && await GitUri.fromUri(uri, this.git); diff --git a/src/commands/showBlameHistory.ts b/src/commands/showBlameHistory.ts index d55c41b..686d344 100644 --- a/src/commands/showBlameHistory.ts +++ b/src/commands/showBlameHistory.ts @@ -24,6 +24,8 @@ export class ShowBlameHistoryCommand extends EditorCommand { if (uri === undefined) return undefined; if (args.range == null || args.position == null) { + args = { ...args }; + // If the command is executed manually -- treat it as a click on the root lens (i.e. show blame for the whole file) args.range = editor.document.validateRange(new Range(0, 0, 1000000, 1000000)); args.position = editor.document.validateRange(new Range(0, 0, 0, 1000000)).start; diff --git a/src/commands/showCommitSearch.ts b/src/commands/showCommitSearch.ts index 564fb61..8ded360 100644 --- a/src/commands/showCommitSearch.ts +++ b/src/commands/showCommitSearch.ts @@ -30,7 +30,7 @@ export class ShowCommitSearchCommand extends ActiveEditorCachedCommand { super(Commands.ShowCommitSearch); } - async execute(editor: TextEditor, uri?: Uri, args: ShowCommitSearchCommandArgs = {}) { + async execute(editor?: TextEditor, uri?: Uri, args: ShowCommitSearchCommandArgs = {}) { uri = getCommandUri(uri, editor); const gitUri = uri === undefined ? undefined : await GitUri.fromUri(uri, this.git); @@ -38,6 +38,7 @@ export class ShowCommitSearchCommand extends ActiveEditorCachedCommand { const repoPath = gitUri === undefined ? this.git.repoPath : gitUri.repoPath; if (!repoPath) return Messages.showNoRepositoryWarningMessage(`Unable to show commit search`); + args = { ...args }; if (!args.search || args.searchBy == null) { try { if (!args.search) { @@ -95,14 +96,17 @@ export class ShowCommitSearchCommand extends ActiveEditorCachedCommand { originalSearch = `@${args.search}`; placeHolder = `commits with author matching '${args.search}'`; break; + case GitRepoSearchBy.Files: originalSearch = `:${args.search}`; placeHolder = `commits with files matching '${args.search}'`; break; + case GitRepoSearchBy.Message: originalSearch = args.search; placeHolder = `commits with message matching '${args.search}'`; break; + case GitRepoSearchBy.Sha: originalSearch = `#${args.search}`; placeHolder = `commits with id matching '${args.search}'`; diff --git a/src/commands/showFileBlame.ts b/src/commands/showFileBlame.ts index 1da2dbe..7029afc 100644 --- a/src/commands/showFileBlame.ts +++ b/src/commands/showFileBlame.ts @@ -21,6 +21,8 @@ export class ShowFileBlameCommand extends EditorCommand { try { if (args.type === undefined) { + args = { ...args }; + const cfg = workspace.getConfiguration().get(ExtensionKey)!; args.type = cfg.blame.file.annotationType; } diff --git a/src/commands/showFileHistory.ts b/src/commands/showFileHistory.ts index 5627b08..eeee559 100644 --- a/src/commands/showFileHistory.ts +++ b/src/commands/showFileHistory.ts @@ -23,6 +23,8 @@ export class ShowFileHistoryCommand extends EditorCommand { if (uri === undefined) return undefined; if (args.position == null) { + args = { ...args }; + // If the command is executed manually -- treat it as a click on the root lens (i.e. show blame for the whole file) args.position = editor.document.validateRange(new Range(0, 0, 0, 1000000)).start; } diff --git a/src/commands/showLastQuickPick.ts b/src/commands/showLastQuickPick.ts index 43ea723..977c205 100644 --- a/src/commands/showLastQuickPick.ts +++ b/src/commands/showLastQuickPick.ts @@ -11,7 +11,7 @@ export class ShowLastQuickPickCommand extends Command { async execute() { const command = getLastCommand(); - if (!command) return undefined; + if (command === undefined) return undefined; try { return commands.executeCommand(command.command, ...command.args); diff --git a/src/commands/showLineBlame.ts b/src/commands/showLineBlame.ts index 911c148..5adc408 100644 --- a/src/commands/showLineBlame.ts +++ b/src/commands/showLineBlame.ts @@ -20,6 +20,8 @@ export class ShowLineBlameCommand extends EditorCommand { try { if (args.type === undefined) { + args = { ...args }; + const cfg = workspace.getConfiguration().get(ExtensionKey)!; args.type = cfg.blame.line.annotationType; } diff --git a/src/commands/showQuickBranchHistory.ts b/src/commands/showQuickBranchHistory.ts index 159c857..ccf1546 100644 --- a/src/commands/showQuickBranchHistory.ts +++ b/src/commands/showQuickBranchHistory.ts @@ -24,11 +24,12 @@ export class ShowQuickBranchHistoryCommand extends ActiveEditorCachedCommand { super(Commands.ShowQuickBranchHistory); } - async execute(editor: TextEditor, uri?: Uri, args: ShowQuickBranchHistoryCommandArgs = {}) { + async execute(editor?: TextEditor, uri?: Uri, args: ShowQuickBranchHistoryCommandArgs = {}) { uri = getCommandUri(uri, editor); const gitUri = uri && await GitUri.fromUri(uri, this.git); + args = { ...args }; if (args.maxCount == null) { args.maxCount = this.git.config.advanced.maxQuickHistory; } diff --git a/src/commands/showQuickCommitDetails.ts b/src/commands/showQuickCommitDetails.ts index bb748e9..37d7336 100644 --- a/src/commands/showQuickCommitDetails.ts +++ b/src/commands/showQuickCommitDetails.ts @@ -24,7 +24,7 @@ export class ShowQuickCommitDetailsCommand extends ActiveEditorCachedCommand { super(Commands.ShowQuickCommitDetails); } - async execute(editor: TextEditor, uri?: Uri, args: ShowQuickCommitDetailsCommandArgs = {}) { + async execute(editor?: TextEditor, uri?: Uri, args: ShowQuickCommitDetailsCommandArgs = {}) { uri = getCommandUri(uri, editor); if (uri === undefined) return undefined; @@ -33,6 +33,7 @@ export class ShowQuickCommitDetailsCommand extends ActiveEditorCachedCommand { let repoPath = gitUri.repoPath; let workingFileName = path.relative(repoPath || '', gitUri.fsPath); + args = { ...args }; if (args.sha === undefined) { if (editor === undefined) return undefined; diff --git a/src/commands/showQuickCommitFileDetails.ts b/src/commands/showQuickCommitFileDetails.ts index 92d1832..1261c78 100644 --- a/src/commands/showQuickCommitFileDetails.ts +++ b/src/commands/showQuickCommitFileDetails.ts @@ -24,7 +24,7 @@ export class ShowQuickCommitFileDetailsCommand extends ActiveEditorCachedCommand super(Commands.ShowQuickCommitFileDetails); } - async execute(editor: TextEditor, uri?: Uri, args: ShowQuickCommitFileDetailsCommandArgs = {}) { + async execute(editor?: TextEditor, uri?: Uri, args: ShowQuickCommitFileDetailsCommandArgs = {}) { uri = getCommandUri(uri, editor); if (uri === undefined) return undefined; @@ -32,6 +32,7 @@ export class ShowQuickCommitFileDetailsCommand extends ActiveEditorCachedCommand const gitUri = await GitUri.fromUri(uri, this.git); + args = { ...args }; if (args.sha === undefined) { if (editor === undefined) return undefined; diff --git a/src/commands/showQuickCurrentBranchHistory.ts b/src/commands/showQuickCurrentBranchHistory.ts index 9973acf..19053db 100644 --- a/src/commands/showQuickCurrentBranchHistory.ts +++ b/src/commands/showQuickCurrentBranchHistory.ts @@ -17,7 +17,7 @@ export class ShowQuickCurrentBranchHistoryCommand extends ActiveEditorCachedComm super(Commands.ShowQuickCurrentBranchHistory); } - async execute(editor: TextEditor, uri?: Uri, args: ShowQuickCurrentBranchHistoryCommandArgs = {}) { + async execute(editor?: TextEditor, uri?: Uri, args: ShowQuickCurrentBranchHistoryCommandArgs = {}) { uri = getCommandUri(uri, editor); try { diff --git a/src/commands/showQuickFileHistory.ts b/src/commands/showQuickFileHistory.ts index 6ffba39..5ad366f 100644 --- a/src/commands/showQuickFileHistory.ts +++ b/src/commands/showQuickFileHistory.ts @@ -1,7 +1,7 @@ 'use strict'; import { Strings } from '../system'; import { commands, Range, TextEditor, Uri, window } from 'vscode'; -import { ActiveEditorCachedCommand, CommandContext, Commands, getCommandUri } from './common'; +import { ActiveEditorCachedCommand, Commands, getCommandUri } from './common'; import { GlyphChars } from '../constants'; import { GitLog, GitService, GitUri } from '../gitService'; import { Logger } from '../logger'; @@ -25,27 +25,13 @@ export class ShowQuickFileHistoryCommand extends ActiveEditorCachedCommand { super(Commands.ShowQuickFileHistory); } - async run(context: CommandContext, args: ShowQuickFileHistoryCommandArgs = {}): Promise { - // Since we can change the args and they could be cached -- make a copy - switch (context.type) { - case 'uri': - return this.execute(context.editor, context.uri, { ...args }); - case 'scm-states': - const resource = context.scmResourceStates[0]; - return this.execute(undefined, resource.resourceUri, { ...args }); - case 'scm-groups': - return undefined; - default: - return this.execute(context.editor, undefined, { ...args }); - } - } - - async execute(editor: TextEditor | undefined, uri?: Uri, args: ShowQuickFileHistoryCommandArgs = {}) { + async execute(editor?: TextEditor, uri?: Uri, args: ShowQuickFileHistoryCommandArgs = {}) { uri = getCommandUri(uri, editor); if (uri === undefined) return commands.executeCommand(Commands.ShowQuickCurrentBranchHistory); const gitUri = await GitUri.fromUri(uri, this.git); + args = { ...args }; if (args.maxCount == null) { args.maxCount = this.git.config.advanced.maxQuickHistory; } diff --git a/src/commands/showQuickRepoStatus.ts b/src/commands/showQuickRepoStatus.ts index a4cd30d..78b0722 100644 --- a/src/commands/showQuickRepoStatus.ts +++ b/src/commands/showQuickRepoStatus.ts @@ -16,7 +16,7 @@ export class ShowQuickRepoStatusCommand extends ActiveEditorCachedCommand { super(Commands.ShowQuickRepoStatus); } - async execute(editor: TextEditor, uri?: Uri, args: ShowQuickRepoStatusCommandArgs = {}) { + async execute(editor?: TextEditor, uri?: Uri, args: ShowQuickRepoStatusCommandArgs = {}) { uri = getCommandUri(uri, editor); try { diff --git a/src/commands/showQuickStashList.ts b/src/commands/showQuickStashList.ts index 5634ed8..d031a64 100644 --- a/src/commands/showQuickStashList.ts +++ b/src/commands/showQuickStashList.ts @@ -19,7 +19,7 @@ export class ShowQuickStashListCommand extends ActiveEditorCachedCommand { super(Commands.ShowQuickStashList); } - async execute(editor: TextEditor, uri?: Uri, args: ShowQuickStashListCommandArgs = {}) { + async execute(editor?: TextEditor, uri?: Uri, args: ShowQuickStashListCommandArgs = {}) { uri = getCommandUri(uri, editor); try { diff --git a/src/commands/stashApply.ts b/src/commands/stashApply.ts index 3687be0..80dc786 100644 --- a/src/commands/stashApply.ts +++ b/src/commands/stashApply.ts @@ -25,6 +25,7 @@ export class StashApplyCommand extends Command { async execute(args: StashApplyCommandArgs = { confirm: true, deleteAfter: false }) { if (!this.git.repoPath) return undefined; + args = { ...args }; if (args.stashItem === undefined || args.stashItem.stashName === undefined) { const stash = await this.git.getStashList(this.git.repoPath); if (stash === undefined) return window.showInformationMessage(`There are no stashed changes`); diff --git a/src/commands/stashDelete.ts b/src/commands/stashDelete.ts index 5d9b9d1..20c02e1 100644 --- a/src/commands/stashDelete.ts +++ b/src/commands/stashDelete.ts @@ -22,6 +22,7 @@ export class StashDeleteCommand extends Command { async execute(args: StashDeleteCommandArgs = { confirm: true }) { if (!this.git.repoPath) return undefined; + args = { ...args }; if (args.stashItem === undefined || args.stashItem.stashName === undefined) return undefined; if (args.confirm === undefined) { diff --git a/src/commands/stashSave.ts b/src/commands/stashSave.ts index 0e98e47..87849f5 100644 --- a/src/commands/stashSave.ts +++ b/src/commands/stashSave.ts @@ -21,6 +21,7 @@ export class StashSaveCommand extends Command { async execute(args: StashSaveCommandArgs = { unstagedOnly : false }) { if (!this.git.repoPath) return undefined; + args = { ...args }; if (args.unstagedOnly === undefined) { args.unstagedOnly = false; } diff --git a/src/commands/toggleFileBlame.ts b/src/commands/toggleFileBlame.ts index e0e1eb8..f3898dd 100644 --- a/src/commands/toggleFileBlame.ts +++ b/src/commands/toggleFileBlame.ts @@ -21,6 +21,8 @@ export class ToggleFileBlameCommand extends EditorCommand { try { if (args.type === undefined) { + args = { ...args }; + const cfg = workspace.getConfiguration().get(ExtensionKey)!; args.type = cfg.blame.file.annotationType; } diff --git a/src/commands/toggleLineBlame.ts b/src/commands/toggleLineBlame.ts index 45456b0..049d7dc 100644 --- a/src/commands/toggleLineBlame.ts +++ b/src/commands/toggleLineBlame.ts @@ -20,6 +20,8 @@ export class ToggleLineBlameCommand extends EditorCommand { try { if (args.type === undefined) { + args = { ...args }; + const cfg = workspace.getConfiguration().get(ExtensionKey)!; args.type = cfg.blame.line.annotationType; }