Skip to content

Commit

Permalink
Renames isCommandContext* commands
Browse files Browse the repository at this point in the history
  • Loading branch information
eamodio committed Oct 24, 2020
1 parent db2ee7f commit 7f06da0
Show file tree
Hide file tree
Showing 18 changed files with 92 additions and 93 deletions.
83 changes: 41 additions & 42 deletions src/commands/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,82 +216,82 @@ export interface CommandBaseContext {
}

export interface CommandScmGroupsContext extends CommandBaseContext {
type: 'scm-groups';
scmResourceGroups: SourceControlResourceGroup[];
readonly type: 'scm-groups';
readonly scmResourceGroups: SourceControlResourceGroup[];
}

export interface CommandScmStatesContext extends CommandBaseContext {
type: 'scm-states';
scmResourceStates: SourceControlResourceState[];
readonly type: 'scm-states';
readonly scmResourceStates: SourceControlResourceState[];
}

export interface CommandUnknownContext extends CommandBaseContext {
type: 'unknown';
readonly type: 'unknown';
}

export interface CommandUriContext extends CommandBaseContext {
type: 'uri';
readonly type: 'uri';
}

export interface CommandUrisContext extends CommandBaseContext {
type: 'uris';
uris: Uri[];
readonly type: 'uris';
readonly uris: Uri[];
}

// export interface CommandViewContext extends CommandBaseContext {
// type: 'view';
// readonly type: 'view';
// }

export interface CommandViewItemContext extends CommandBaseContext {
type: 'viewItem';
node: ViewNode;
export interface CommandViewNodeContext extends CommandBaseContext {
readonly type: 'viewItem';
readonly node: ViewNode;
}

export function isCommandViewContextWithBranch(
export function isCommandContextViewNodeHasBranch(
context: CommandContext,
): context is CommandViewItemContext & { node: ViewNode & { branch: GitBranch } } {
): context is CommandViewNodeContext & { node: ViewNode & { branch: GitBranch } } {
if (context.type !== 'viewItem') return false;

return GitBranch.is((context.node as ViewNode & { branch: GitBranch }).branch);
}

export function isCommandViewContextWithCommit<T extends GitCommit>(
export function isCommandContextViewNodeHasCommit<T extends GitCommit>(
context: CommandContext,
): context is CommandViewItemContext & { node: ViewNode & { commit: T } } {
): context is CommandViewNodeContext & { node: ViewNode & { commit: T } } {
if (context.type !== 'viewItem') return false;

return GitCommit.is((context.node as ViewNode & { commit: GitCommit }).commit);
}

export function isCommandViewContextWithContributor(
export function isCommandContextViewNodeHasContributor(
context: CommandContext,
): context is CommandViewItemContext & { node: ViewNode & { contributor: GitContributor } } {
): context is CommandViewNodeContext & { node: ViewNode & { contributor: GitContributor } } {
if (context.type !== 'viewItem') return false;

return GitContributor.is((context.node as ViewNode & { contributor: GitContributor }).contributor);
}

export function isCommandViewContextWithFile(
export function isCommandContextViewNodeHasFile(
context: CommandContext,
): context is CommandViewItemContext & { node: ViewNode & { file: GitFile; repoPath: string } } {
): context is CommandViewNodeContext & { node: ViewNode & { file: GitFile; repoPath: string } } {
if (context.type !== 'viewItem') return false;

const node = context.node as ViewNode & { file: GitFile; repoPath: string };
return node.file != null && (node.file.repoPath != null || node.repoPath != null);
}

export function isCommandViewContextWithFileCommit(
export function isCommandContextViewNodeHasFileCommit(
context: CommandContext,
): context is CommandViewItemContext & { node: ViewNode & { commit: GitCommit; file: GitFile; repoPath: string } } {
): context is CommandViewNodeContext & { node: ViewNode & { commit: GitCommit; file: GitFile; repoPath: string } } {
if (context.type !== 'viewItem') return false;

const node = context.node as ViewNode & { commit: GitCommit; file: GitFile; repoPath: string };
return node.file != null && GitCommit.is(node.commit) && (node.file.repoPath != null || node.repoPath != null);
}

export function isCommandViewContextWithFileRefs(
export function isCommandContextViewNodeHasFileRefs(
context: CommandContext,
): context is CommandViewItemContext & {
): context is CommandViewNodeContext & {
node: ViewNode & { file: GitFile; ref1: string; ref2: string; repoPath: string };
} {
if (context.type !== 'viewItem') return false;
Expand All @@ -305,39 +305,39 @@ export function isCommandViewContextWithFileRefs(
);
}

export function isCommandViewContextWithRef(
export function isCommandContextViewNodeHasRef(
context: CommandContext,
): context is CommandViewItemContext & { node: ViewNode & { ref: string } } {
): context is CommandViewNodeContext & { node: ViewNode & { ref: string } } {
return context.type === 'viewItem' && context.node instanceof ViewRefNode;
}

export function isCommandViewContextWithRemote(
export function isCommandContextViewNodeHasRemote(
context: CommandContext,
): context is CommandViewItemContext & { node: ViewNode & { remote: GitRemote } } {
): context is CommandViewNodeContext & { node: ViewNode & { remote: GitRemote } } {
if (context.type !== 'viewItem') return false;

return GitRemote.is((context.node as ViewNode & { remote: GitRemote }).remote);
}

export function isCommandViewContextWithRepo(
export function isCommandContextViewNodeHasRepository(
context: CommandContext,
): context is CommandViewItemContext & { node: ViewNode & { repo: Repository } } {
): context is CommandViewNodeContext & { node: ViewNode & { repo: Repository } } {
if (context.type !== 'viewItem') return false;

return (context.node as ViewNode & { repo?: Repository }).repo instanceof Repository;
}

export function isCommandViewContextWithRepoPath(
export function isCommandContextViewNodeHasRepoPath(
context: CommandContext,
): context is CommandViewItemContext & { node: ViewNode & { repoPath: string } } {
): context is CommandViewNodeContext & { node: ViewNode & { repoPath: string } } {
if (context.type !== 'viewItem') return false;

return typeof (context.node as ViewNode & { repoPath?: string }).repoPath === 'string';
}

export function isCommandViewContextWithTag(
export function isCommandContextViewNodeHasTag(
context: CommandContext,
): context is CommandViewItemContext & { node: ViewNode & { tag: GitTag } } {
): context is CommandViewNodeContext & { node: ViewNode & { tag: GitTag } } {
if (context.type !== 'viewItem') return false;

return GitTag.is((context.node as ViewNode & { tag: GitTag }).tag);
Expand All @@ -350,24 +350,23 @@ export type CommandContext =
| CommandUriContext
| CommandUrisContext
// | CommandViewContext
| CommandViewItemContext;
| CommandViewNodeContext;

function isScmResourceGroup(group: any): group is SourceControlResourceGroup {
if (group == null) return false;

return (
(group as SourceControl).inputBox == null &&
(group as SourceControlResourceGroup).id != null &&
(group.handle != null ||
(group as SourceControlResourceGroup).label != null ||
(group as SourceControlResourceGroup).resourceStates != null)
(group as SourceControlResourceGroup).label != null &&
(group as SourceControlResourceGroup).resourceStates != null &&
Array.isArray((group as SourceControlResourceGroup).resourceStates)
);
}

function isScmResourceState(state: any): state is SourceControlResourceState {
if (state == null) return false;
function isScmResourceState(resource: any): resource is SourceControlResourceState {
if (resource == null) return false;

return (state as SourceControlResourceState).resourceUri != null;
return (resource as SourceControlResourceState).resourceUri != null;
}

export abstract class Command implements Disposable {
Expand Down
4 changes: 2 additions & 2 deletions src/commands/copyMessageToClipboard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
CommandContext,
Commands,
getCommandUri,
isCommandViewContextWithCommit,
isCommandContextViewNodeHasCommit,
} from './common';

export interface CopyMessageToClipboardCommandArgs {
Expand All @@ -26,7 +26,7 @@ export class CopyMessageToClipboardCommand extends ActiveEditorCommand {
}

protected preExecute(context: CommandContext, args?: CopyMessageToClipboardCommandArgs) {
if (isCommandViewContextWithCommit(context)) {
if (isCommandContextViewNodeHasCommit(context)) {
args = { ...args };
args.sha = context.node.commit.sha;
return this.execute(context.editor, context.node.commit.uri, args);
Expand Down
12 changes: 6 additions & 6 deletions src/commands/copyShaToClipboard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ import {
CommandContext,
Commands,
getCommandUri,
isCommandViewContextWithBranch,
isCommandViewContextWithCommit,
isCommandViewContextWithTag,
isCommandContextViewNodeHasBranch,
isCommandContextViewNodeHasCommit,
isCommandContextViewNodeHasTag,
} from './common';

export interface CopyShaToClipboardCommandArgs {
Expand All @@ -27,15 +27,15 @@ export class CopyShaToClipboardCommand extends ActiveEditorCommand {
}

protected preExecute(context: CommandContext, args?: CopyShaToClipboardCommandArgs) {
if (isCommandViewContextWithCommit(context)) {
if (isCommandContextViewNodeHasCommit(context)) {
args = { ...args };
args.sha = context.node.commit.sha;
return this.execute(context.editor, context.node.commit.uri, args);
} else if (isCommandViewContextWithBranch(context)) {
} else if (isCommandContextViewNodeHasBranch(context)) {
args = { ...args };
args.sha = context.node.branch.sha;
return this.execute(context.editor, context.node.uri, args);
} else if (isCommandViewContextWithTag(context)) {
} else if (isCommandContextViewNodeHasTag(context)) {
args = { ...args };
args.sha = context.node.tag.sha;
return this.execute(context.editor, context.node.uri, args);
Expand Down
8 changes: 4 additions & 4 deletions src/commands/externalDiff.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import {
CommandContext,
Commands,
getRepoPathOrPrompt,
isCommandViewContextWithFileCommit,
isCommandViewContextWithFileRefs,
isCommandContextViewNodeHasFileCommit,
isCommandContextViewNodeHasFileRefs,
} from './common';
import { Container } from '../container';
import { GitRevision } from '../git/git';
Expand Down Expand Up @@ -68,7 +68,7 @@ export class ExternalDiffCommand extends Command {
protected async preExecute(context: CommandContext, args?: ExternalDiffCommandArgs) {
args = { ...args };

if (isCommandViewContextWithFileCommit(context)) {
if (isCommandContextViewNodeHasFileCommit(context)) {
const ref1 = GitRevision.isUncommitted(context.node.commit.previousFileSha)
? ''
: context.node.commit.previousFileSha;
Expand All @@ -86,7 +86,7 @@ export class ExternalDiffCommand extends Command {
return this.execute(args);
}

if (isCommandViewContextWithFileRefs(context)) {
if (isCommandContextViewNodeHasFileRefs(context)) {
args.files = [
{
uri: GitUri.fromFile(context.node.file, context.node.file.repoPath ?? context.node.repoPath),
Expand Down
4 changes: 2 additions & 2 deletions src/commands/inviteToLiveShare.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
'use strict';
import { command, Command, CommandContext, Commands, isCommandViewContextWithContributor } from './common';
import { command, Command, CommandContext, Commands, isCommandContextViewNodeHasContributor } from './common';
import { Container } from '../container';

export interface InviteToLiveShareCommandArgs {
Expand All @@ -21,7 +21,7 @@ export class InviteToLiveShareCommand extends Command {
}

protected preExecute(context: CommandContext, args?: InviteToLiveShareCommandArgs) {
if (isCommandViewContextWithContributor(context)) {
if (isCommandContextViewNodeHasContributor(context)) {
args = { ...args };
args.email = context.node.contributor.email;
return this.execute(args);
Expand Down
4 changes: 2 additions & 2 deletions src/commands/openBranchOnRemote.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
executeCommand,
getCommandUri,
getRepoPathOrActiveOrPrompt,
isCommandViewContextWithBranch,
isCommandContextViewNodeHasBranch,
} from './common';
import { BranchSorting } from '../configuration';
import { RemoteResourceType } from '../git/git';
Expand All @@ -30,7 +30,7 @@ export class OpenBranchOnRemoteCommand extends ActiveEditorCommand {
}

protected preExecute(context: CommandContext, args?: OpenBranchOnRemoteCommandArgs) {
if (isCommandViewContextWithBranch(context)) {
if (isCommandContextViewNodeHasBranch(context)) {
args = {
...args,
branch: context.node.branch.name,
Expand Down
4 changes: 2 additions & 2 deletions src/commands/openBranchesOnRemote.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
executeCommand,
getCommandUri,
getRepoPathOrActiveOrPrompt,
isCommandViewContextWithRemote,
isCommandContextViewNodeHasRemote,
} from './common';
import { OpenOnRemoteCommandArgs } from './openOnRemote';

Expand All @@ -27,7 +27,7 @@ export class OpenBranchesOnRemoteCommand extends ActiveEditorCommand {
}

protected preExecute(context: CommandContext, args?: OpenBranchesOnRemoteCommandArgs) {
if (isCommandViewContextWithRemote(context)) {
if (isCommandContextViewNodeHasRemote(context)) {
args = { ...args, remote: context.node.remote.name };
}

Expand Down
4 changes: 2 additions & 2 deletions src/commands/openCommitOnRemote.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
Commands,
executeCommand,
getCommandUri,
isCommandViewContextWithCommit,
isCommandContextViewNodeHasCommit,
} from './common';
import { Container } from '../container';
import { RemoteResourceType } from '../git/git';
Expand Down Expand Up @@ -37,7 +37,7 @@ export class OpenCommitOnRemoteCommand extends ActiveEditorCommand {
protected preExecute(context: CommandContext, args?: OpenCommitOnRemoteCommandArgs) {
let uri = context.uri;

if (isCommandViewContextWithCommit(context)) {
if (isCommandContextViewNodeHasCommit(context)) {
if (context.node.commit.isUncommitted) return Promise.resolve(undefined);

args = { ...args, sha: context.node.commit.sha };
Expand Down
4 changes: 2 additions & 2 deletions src/commands/openDirectoryCompare.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
Commands,
getCommandUri,
getRepoPathOrActiveOrPrompt,
isCommandViewContextWithRef,
isCommandContextViewNodeHasRef,
} from './common';
import { Container } from '../container';
import { Logger } from '../logger';
Expand Down Expand Up @@ -47,7 +47,7 @@ export class OpenDirectoryCompareCommand extends ActiveEditorCommand {
break;

case Commands.ViewsOpenDirectoryDiffWithWorking:
if (isCommandViewContextWithRef(context)) {
if (isCommandContextViewNodeHasRef(context)) {
args = { ...args };
args.ref1 = context.node.ref;
args.ref2 = undefined;
Expand Down
8 changes: 4 additions & 4 deletions src/commands/openFileOnRemote.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import {
Commands,
executeCommand,
getCommandUri,
isCommandViewContextWithBranch,
isCommandViewContextWithCommit,
isCommandContextViewNodeHasBranch,
isCommandContextViewNodeHasCommit,
} from './common';
import { UriComparer } from '../comparers';
import { BranchSorting } from '../configuration';
Expand Down Expand Up @@ -40,13 +40,13 @@ export class OpenFileOnRemoteCommand extends ActiveEditorCommand {

if (context.type === 'uris' || context.type === 'scm-states') {
args = { ...args, range: false };
} else if (isCommandViewContextWithCommit(context)) {
} else if (isCommandContextViewNodeHasCommit(context)) {
args = { ...args, range: false };

if (context.command === Commands.CopyRemoteFileUrl) {
// If it is a StatusFileNode then don't include the sha, since it hasn't been pushed yet
args.sha = context.node instanceof StatusFileNode ? undefined : context.node.commit.sha;
} else if (isCommandViewContextWithBranch(context)) {
} else if (isCommandContextViewNodeHasBranch(context)) {
args.branch = context.node.branch?.name;
}

Expand Down

0 comments on commit 7f06da0

Please sign in to comment.