mirror of
https://github.com/ckaczor/vscode-gitlens.git
synced 2026-02-12 11:08:34 -05:00
Refactors commands to use typed args objects
This commit is contained in:
@@ -1,9 +1,10 @@
|
||||
'use strict';
|
||||
import { commands, InputBoxOptions, TextEditor, Uri, window } from 'vscode';
|
||||
import { ActiveEditorCachedCommand, Commands } from './common';
|
||||
import { ActiveEditorCachedCommand, Commands, getCommandUri } from './common';
|
||||
import { GitRepoSearchBy, GitService, GitUri } from '../gitService';
|
||||
import { Logger } from '../logger';
|
||||
import { CommandQuickPickItem, CommitsQuickPick } from '../quickPicks';
|
||||
import { ShowQuickCommitDetailsCommandArgs } from './showQuickCommitDetails';
|
||||
|
||||
const searchByRegex = /^([@:#])/;
|
||||
const searchByMap = new Map<string, GitRepoSearchBy>([
|
||||
@@ -12,68 +13,73 @@ const searchByMap = new Map<string, GitRepoSearchBy>([
|
||||
['#', GitRepoSearchBy.Sha]
|
||||
]);
|
||||
|
||||
export interface ShowCommitSearchCommandArgs {
|
||||
search?: string;
|
||||
searchBy?: GitRepoSearchBy;
|
||||
|
||||
goBackCommand?: CommandQuickPickItem;
|
||||
}
|
||||
|
||||
export class ShowCommitSearchCommand extends ActiveEditorCachedCommand {
|
||||
|
||||
constructor(private git: GitService) {
|
||||
super(Commands.ShowCommitSearch);
|
||||
}
|
||||
|
||||
async execute(editor: TextEditor, uri?: Uri, search?: string, searchBy?: GitRepoSearchBy, goBackCommand?: CommandQuickPickItem) {
|
||||
if (!(uri instanceof Uri)) {
|
||||
if (!editor || !editor.document) return undefined;
|
||||
uri = editor.document.uri;
|
||||
}
|
||||
async execute(editor: TextEditor, uri?: Uri, args: ShowCommitSearchCommandArgs = {}) {
|
||||
uri = getCommandUri(uri, editor);
|
||||
if (uri === undefined) return undefined;
|
||||
|
||||
const gitUri = await GitUri.fromUri(uri, this.git);
|
||||
if (gitUri.repoPath === undefined) return undefined;
|
||||
|
||||
if (!search || searchBy == null) {
|
||||
search = await window.showInputBox({
|
||||
value: search,
|
||||
if (!args.search || args.searchBy == null) {
|
||||
args.search = await window.showInputBox({
|
||||
value: args.search,
|
||||
prompt: `Please enter a search string`,
|
||||
placeHolder: `search by message, author (use @<name>), files (use :<pattern>), or commit id (use #<sha>)`
|
||||
} as InputBoxOptions);
|
||||
if (search === undefined) return goBackCommand && goBackCommand.execute();
|
||||
if (args.search === undefined) return args.goBackCommand === undefined ? undefined : args.goBackCommand.execute();
|
||||
|
||||
const match = searchByRegex.exec(search);
|
||||
const match = searchByRegex.exec(args.search);
|
||||
if (match && match[1]) {
|
||||
searchBy = searchByMap.get(match[1]);
|
||||
search = search.substring((search[1] === ' ') ? 2 : 1);
|
||||
args.searchBy = searchByMap.get(match[1]);
|
||||
args.search = args.search.substring((args.search[1] === ' ') ? 2 : 1);
|
||||
}
|
||||
else if (GitService.isSha(search)) {
|
||||
searchBy = GitRepoSearchBy.Sha;
|
||||
else if (GitService.isSha(args.search)) {
|
||||
args.searchBy = GitRepoSearchBy.Sha;
|
||||
}
|
||||
else {
|
||||
searchBy = GitRepoSearchBy.Message;
|
||||
args.searchBy = GitRepoSearchBy.Message;
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
if (searchBy === undefined) {
|
||||
searchBy = GitRepoSearchBy.Message;
|
||||
if (args.searchBy === undefined) {
|
||||
args.searchBy = GitRepoSearchBy.Message;
|
||||
}
|
||||
|
||||
const log = await this.git.getLogForRepoSearch(gitUri.repoPath, search, searchBy);
|
||||
const log = await this.git.getLogForRepoSearch(gitUri.repoPath, args.search, args.searchBy);
|
||||
if (log === undefined) return undefined;
|
||||
|
||||
let originalSearch: string | undefined = undefined;
|
||||
let placeHolder: string | undefined = undefined;
|
||||
switch (searchBy) {
|
||||
switch (args.searchBy) {
|
||||
case GitRepoSearchBy.Author:
|
||||
originalSearch = `@${search}`;
|
||||
placeHolder = `commits with author matching '${search}'`;
|
||||
originalSearch = `@${args.search}`;
|
||||
placeHolder = `commits with author matching '${args.search}'`;
|
||||
break;
|
||||
case GitRepoSearchBy.Files:
|
||||
originalSearch = `:${search}`;
|
||||
placeHolder = `commits with files matching '${search}'`;
|
||||
originalSearch = `:${args.search}`;
|
||||
placeHolder = `commits with files matching '${args.search}'`;
|
||||
break;
|
||||
case GitRepoSearchBy.Message:
|
||||
originalSearch = search;
|
||||
placeHolder = `commits with message matching '${search}'`;
|
||||
originalSearch = args.search;
|
||||
placeHolder = `commits with message matching '${args.search}'`;
|
||||
break;
|
||||
case GitRepoSearchBy.Sha:
|
||||
originalSearch = `#${search}`;
|
||||
placeHolder = `commits with id matching '${search}'`;
|
||||
originalSearch = `#${args.search}`;
|
||||
placeHolder = `commits with id matching '${args.search}'`;
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -81,20 +87,32 @@ export class ShowCommitSearchCommand extends ActiveEditorCachedCommand {
|
||||
const currentCommand = new CommandQuickPickItem({
|
||||
label: `go back \u21A9`,
|
||||
description: `\u00a0 \u2014 \u00a0\u00a0 to commit search`
|
||||
}, Commands.ShowCommitSearch, [gitUri, originalSearch, undefined, goBackCommand]);
|
||||
}, Commands.ShowCommitSearch, [
|
||||
gitUri,
|
||||
{
|
||||
search: originalSearch,
|
||||
goBackCommand: args.goBackCommand
|
||||
} as ShowCommitSearchCommandArgs
|
||||
]);
|
||||
|
||||
const pick = await CommitsQuickPick.show(this.git, log, placeHolder!, currentCommand);
|
||||
if (!pick) return undefined;
|
||||
if (pick === undefined) return undefined;
|
||||
|
||||
if (pick instanceof CommandQuickPickItem) {
|
||||
return pick.execute();
|
||||
}
|
||||
if (pick instanceof CommandQuickPickItem) return pick.execute();
|
||||
|
||||
return commands.executeCommand(Commands.ShowQuickCommitDetails, new GitUri(pick.commit.uri, pick.commit), pick.commit.sha, pick.commit,
|
||||
new CommandQuickPickItem({
|
||||
label: `go back \u21A9`,
|
||||
description: `\u00a0 \u2014 \u00a0\u00a0 to search for ${placeHolder}`
|
||||
}, Commands.ShowCommitSearch, [gitUri, search, searchBy, goBackCommand]));
|
||||
return commands.executeCommand(Commands.ShowQuickCommitDetails,
|
||||
new GitUri(pick.commit.uri, pick.commit),
|
||||
{
|
||||
sha: pick.commit.sha,
|
||||
commit: pick.commit,
|
||||
goBackCommand: new CommandQuickPickItem({
|
||||
label: `go back \u21A9`,
|
||||
description: `\u00a0 \u2014 \u00a0\u00a0 to search for ${placeHolder}`
|
||||
}, Commands.ShowCommitSearch, [
|
||||
gitUri,
|
||||
args
|
||||
])
|
||||
} as ShowQuickCommitDetailsCommandArgs);
|
||||
}
|
||||
catch (ex) {
|
||||
Logger.error(ex, 'ShowCommitSearchCommand');
|
||||
|
||||
Reference in New Issue
Block a user