mirror of
https://github.com/ckaczor/vscode-gitlens.git
synced 2026-01-16 09:35:40 -05:00
Refactors commands to use typed args objects
This commit is contained in:
@@ -1,50 +1,73 @@
|
||||
'use strict';
|
||||
import { TextEditor, Uri, window } from 'vscode';
|
||||
import { ActiveEditorCommand, Commands } from './common';
|
||||
import { GitRemote, RemoteOpenType } from '../gitService';
|
||||
import { ActiveEditorCommand, Commands, getCommandUri } from './common';
|
||||
import { GitLogCommit, GitRemote, RemoteResource } from '../gitService';
|
||||
import { Logger } from '../logger';
|
||||
import { CommandQuickPickItem, OpenRemoteCommandQuickPickItem, RemotesQuickPick } from '../quickPicks';
|
||||
|
||||
export interface OpenInRemoteCommandArgs {
|
||||
remotes?: GitRemote[];
|
||||
resource?: RemoteResource;
|
||||
|
||||
goBackCommand?: CommandQuickPickItem;
|
||||
}
|
||||
|
||||
export class OpenInRemoteCommand extends ActiveEditorCommand {
|
||||
|
||||
constructor() {
|
||||
super(Commands.OpenInRemote);
|
||||
}
|
||||
|
||||
async execute(editor: TextEditor, uri?: Uri, remotes?: GitRemote[], type?: RemoteOpenType, args: string[] = [], goBackCommand?: CommandQuickPickItem) {
|
||||
if (!(uri instanceof Uri)) {
|
||||
uri = editor && editor.document && editor.document.uri;
|
||||
}
|
||||
async execute(editor: TextEditor, uri?: Uri, args: OpenInRemoteCommandArgs = {}) {
|
||||
uri = getCommandUri(uri, editor);
|
||||
|
||||
try {
|
||||
if (remotes === undefined) return undefined;
|
||||
if (type === undefined) throw new Error(`Invalid type ${type}`);
|
||||
if (args.remotes === undefined || args.resource === undefined) return undefined;
|
||||
|
||||
if (remotes.length === 1) {
|
||||
const command = new OpenRemoteCommandQuickPickItem(remotes[0], type, ...args);
|
||||
if (args.remotes.length === 1) {
|
||||
const command = new OpenRemoteCommandQuickPickItem(args.remotes[0], args.resource);
|
||||
return command.execute();
|
||||
}
|
||||
|
||||
let placeHolder: string = '';
|
||||
switch (type) {
|
||||
switch (args.resource.type) {
|
||||
case 'branch':
|
||||
placeHolder = `open ${args[0]} branch in\u2026`;
|
||||
placeHolder = `open ${args.resource.branch} branch in\u2026`;
|
||||
break;
|
||||
|
||||
case 'commit':
|
||||
const shortSha = args[0].substring(0, 8);
|
||||
const shortSha = args.resource.sha.substring(0, 8);
|
||||
placeHolder = `open commit ${shortSha} in\u2026`;
|
||||
break;
|
||||
case 'file':
|
||||
case 'working-file':
|
||||
const shortFileSha = (args[2] && args[2].substring(0, 8)) || '';
|
||||
const shaSuffix = shortFileSha ? ` \u00a0\u2022\u00a0 ${shortFileSha}` : '';
|
||||
|
||||
placeHolder = `open ${args[0]}${shaSuffix} in\u2026`;
|
||||
case 'file':
|
||||
if (args.resource.commit !== undefined && args.resource.commit instanceof GitLogCommit) {
|
||||
if (args.resource.commit.status === 'D') {
|
||||
args.resource.sha = args.resource.commit.previousSha;
|
||||
placeHolder = `open ${args.resource.fileName} \u00a0\u2022\u00a0 ${args.resource.commit.previousShortSha} in\u2026`;
|
||||
}
|
||||
else {
|
||||
args.resource.sha = args.resource.commit.sha;
|
||||
placeHolder = `open ${args.resource.fileName} \u00a0\u2022\u00a0 ${args.resource.commit.shortSha} in\u2026`;
|
||||
}
|
||||
}
|
||||
else {
|
||||
const shortFileSha = args.resource.sha === undefined ? '' : args.resource.sha.substring(0, 8);
|
||||
const shaSuffix = shortFileSha ? ` \u00a0\u2022\u00a0 ${shortFileSha}` : '';
|
||||
|
||||
placeHolder = `open ${args.resource.fileName}${shaSuffix} in\u2026`;
|
||||
}
|
||||
break;
|
||||
|
||||
case 'working-file':
|
||||
placeHolder = `open ${args.resource.fileName} in\u2026`;
|
||||
break;
|
||||
}
|
||||
|
||||
const pick = await RemotesQuickPick.show(remotes, placeHolder, type, args, goBackCommand);
|
||||
return pick && pick.execute();
|
||||
const pick = await RemotesQuickPick.show(args.remotes, placeHolder, args.resource, args.goBackCommand);
|
||||
if (pick === undefined) return undefined;
|
||||
|
||||
return pick.execute();
|
||||
|
||||
}
|
||||
catch (ex) {
|
||||
|
||||
Reference in New Issue
Block a user