From d7fee60abe3df65ecbb96fcaddb1e1a437489d54 Mon Sep 17 00:00:00 2001 From: Eric Amodio Date: Sat, 29 Sep 2018 17:55:04 -0400 Subject: [PATCH] Adds support for view command detection --- src/commands/common.ts | 55 +++++++++++++++++++----------- src/commands/diffDirectory.ts | 2 +- src/commands/showQuickCommitDetails.ts | 2 +- src/commands/showQuickCommitFileDetails.ts | 2 +- 4 files changed, 38 insertions(+), 23 deletions(-) diff --git a/src/commands/common.ts b/src/commands/common.ts index 8beb7be..7843db7 100644 --- a/src/commands/common.ts +++ b/src/commands/common.ts @@ -174,29 +174,33 @@ export interface CommandUriContext extends CommandBaseContext { export interface CommandViewContext extends CommandBaseContext { type: 'view'; +} + +export interface CommandViewItemContext extends CommandBaseContext { + type: 'viewItem'; node: ExplorerNode; } export function isCommandViewContextWithBranch( context: CommandContext -): context is CommandViewContext & { node: ExplorerNode & { branch: GitBranch } } { - if (context.type !== 'view') return false; +): context is CommandViewItemContext & { node: ExplorerNode & { branch: GitBranch } } { + if (context.type !== 'viewItem') return false; return (context.node as ExplorerNode & { branch: GitBranch }).branch instanceof GitBranch; } export function isCommandViewContextWithCommit( context: CommandContext -): context is CommandViewContext & { node: ExplorerNode & { commit: T } } { - if (context.type !== 'view') return false; +): context is CommandViewItemContext & { node: ExplorerNode & { commit: T } } { + if (context.type !== 'viewItem') return false; return (context.node as ExplorerNode & { commit: GitCommit }).commit instanceof GitCommit; } export function isCommandViewContextWithFile( context: CommandContext -): context is CommandViewContext & { node: ExplorerNode & { file: GitFile; repoPath: string } } { - if (context.type !== 'view') return false; +): context is CommandViewItemContext & { node: ExplorerNode & { file: GitFile; repoPath: string } } { + if (context.type !== 'viewItem') return false; const node = context.node as ExplorerNode & { file: GitFile; repoPath: string }; return node.file !== undefined && (node.file.repoPath !== undefined || node.repoPath !== undefined); @@ -204,8 +208,8 @@ export function isCommandViewContextWithFile( export function isCommandViewContextWithFileCommit( context: CommandContext -): context is CommandViewContext & { node: ExplorerNode & { commit: GitCommit; file: GitFile; repoPath: string } } { - if (context.type !== 'view') return false; +): context is CommandViewItemContext & { node: ExplorerNode & { commit: GitCommit; file: GitFile; repoPath: string } } { + if (context.type !== 'viewItem') return false; const node = context.node as ExplorerNode & { commit: GitCommit; file: GitFile; repoPath: string }; return ( @@ -217,10 +221,10 @@ export function isCommandViewContextWithFileCommit( export function isCommandViewContextWithFileRefs( context: CommandContext -): context is CommandViewContext & { +): context is CommandViewItemContext & { node: ExplorerNode & { file: GitFile; ref1: string; ref2: string; repoPath: string }; } { - if (context.type !== 'view') return false; + if (context.type !== 'viewItem') return false; const node = context.node as ExplorerNode & { file: GitFile; ref1: string; ref2: string; repoPath: string }; return ( @@ -233,22 +237,22 @@ export function isCommandViewContextWithFileRefs( export function isCommandViewContextWithRef( context: CommandContext -): context is CommandViewContext & { node: ExplorerNode & { ref: string } } { - return context.type === 'view' && context.node instanceof ExplorerRefNode; +): context is CommandViewItemContext & { node: ExplorerNode & { ref: string } } { + return context.type === 'viewItem' && context.node instanceof ExplorerRefNode; } export function isCommandViewContextWithRemote( context: CommandContext -): context is CommandViewContext & { node: ExplorerNode & { remote: GitRemote } } { - if (context.type !== 'view') return false; +): context is CommandViewItemContext & { node: ExplorerNode & { remote: GitRemote } } { + if (context.type !== 'viewItem') return false; return (context.node as ExplorerNode & { remote: GitRemote }).remote instanceof GitRemote; } export function isCommandViewContextWithRepo( context: CommandContext -): context is CommandViewContext & { node: ExplorerNode & { repo: Repository } } { - if (context.type !== 'view') return false; +): context is CommandViewItemContext & { node: ExplorerNode & { repo: Repository } } { + if (context.type !== 'viewItem') return false; return (context.node as ExplorerNode & { repo?: Repository }).repo instanceof Repository; } @@ -258,7 +262,8 @@ export type CommandContext = | CommandScmStatesContext | CommandUnknownContext | CommandUriContext - | CommandViewContext; + | CommandViewContext + | CommandViewItemContext; function isScmResourceGroup(group: any): group is SourceControlResourceGroup { if (group == null) return false; @@ -325,7 +330,7 @@ export abstract class Command implements Disposable { protected _execute(command: string, ...args: any[]): any { // Telemetry.trackEvent(command); - const [context, rest] = Command.parseContext(command, this.contextParsingOptions, ...args); + const [context, rest] = Command.parseContext(command, { ...this.contextParsingOptions }, ...args); return this.preExecute(context, ...rest); } @@ -343,14 +348,20 @@ export abstract class Command implements Disposable { firstArg = args[0]; } + let maybeView = false; if (options.uri && (firstArg == null || firstArg instanceof Uri)) { const [uri, ...rest] = args as [Uri, any]; - return [{ command: command, type: 'uri', editor: editor, uri: uri }, rest]; + if (uri !== undefined) { + return [{ command: command, type: 'uri', editor: editor, uri: uri }, rest]; + } + else { + maybeView = args.length === 0; + } } if (firstArg instanceof ExplorerNode) { const [node, ...rest] = args as [ExplorerNode, any]; - return [{ command: command, type: 'view', node: node, uri: node.uri }, rest]; + return [{ command: command, type: 'viewItem', node: node, uri: node.uri }, rest]; } if (isScmResourceState(firstArg)) { @@ -382,6 +393,10 @@ export abstract class Command implements Disposable { return [{ command: command, type: 'scm-groups', scmResourceGroups: groups }, args.slice(count)]; } + if (maybeView) { + return [{ command: command, type: 'view', editor: editor }, args]; + } + return [{ command: command, type: 'unknown', editor: editor }, args]; } } diff --git a/src/commands/diffDirectory.ts b/src/commands/diffDirectory.ts index 1bba73a..d9defb0 100644 --- a/src/commands/diffDirectory.ts +++ b/src/commands/diffDirectory.ts @@ -32,7 +32,7 @@ export class DiffDirectoryCommand extends ActiveEditorCommand { protected async preExecute(context: CommandContext, args: DiffDirectoryCommandArgs = {}): Promise { switch (context.command) { case Commands.ExplorersOpenDirectoryDiff: - if (context.type === 'view' && context.node instanceof ResultsComparisonNode) { + if (context.type === 'viewItem' && context.node instanceof ResultsComparisonNode) { args.ref1 = context.node.ref1.ref; args.ref2 = context.node.ref2.ref; } diff --git a/src/commands/showQuickCommitDetails.ts b/src/commands/showQuickCommitDetails.ts index 3be4e3c..62868f4 100644 --- a/src/commands/showQuickCommitDetails.ts +++ b/src/commands/showQuickCommitDetails.ts @@ -41,7 +41,7 @@ export class ShowQuickCommitDetailsCommand extends ActiveEditorCachedCommand { } protected async preExecute(context: CommandContext, args: ShowQuickCommitDetailsCommandArgs = {}): Promise { - if (context.type === 'view') { + if (context.type === 'viewItem') { args = { ...args }; args.sha = context.node.uri.sha; diff --git a/src/commands/showQuickCommitFileDetails.ts b/src/commands/showQuickCommitFileDetails.ts index 43498bb..cbf2e9a 100644 --- a/src/commands/showQuickCommitFileDetails.ts +++ b/src/commands/showQuickCommitFileDetails.ts @@ -44,7 +44,7 @@ export class ShowQuickCommitFileDetailsCommand extends ActiveEditorCachedCommand context: CommandContext, args: ShowQuickCommitFileDetailsCommandArgs = {} ): Promise { - if (context.type === 'view') { + if (context.type === 'viewItem') { args = { ...args }; args.sha = context.node.uri.sha;