Skip to content

Commit

Permalink
Changes commit search from the views to open in results
Browse files Browse the repository at this point in the history
  • Loading branch information
eamodio committed Sep 30, 2018
1 parent d7fee60 commit af71820
Show file tree
Hide file tree
Showing 6 changed files with 165 additions and 40 deletions.
53 changes: 36 additions & 17 deletions src/commands/showCommitSearch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,27 @@ import {
import { ShowQuickCommitDetailsCommandArgs } from './showQuickCommitDetails';

const searchByRegex = /^([@~=:#])/;
const searchByMap = new Map<string, GitRepoSearchBy>([
const symbolToSearchByMap = new Map<string, GitRepoSearchBy>([
['@', GitRepoSearchBy.Author],
['~', GitRepoSearchBy.ChangedLines],
['=', GitRepoSearchBy.Changes],
[':', GitRepoSearchBy.Files],
['#', GitRepoSearchBy.Sha]
]);

const searchByToSymbolMap = new Map<GitRepoSearchBy, string>([
[GitRepoSearchBy.Author, '@'],
[GitRepoSearchBy.ChangedLines, '~'],
[GitRepoSearchBy.Changes, '='],
[GitRepoSearchBy.Files, ':'],
[GitRepoSearchBy.Sha, '#']
]);

export interface ShowCommitSearchCommandArgs {
search?: string;
searchBy?: GitRepoSearchBy;
maxCount?: number;
showInResults?: boolean;

goBackCommand?: CommandQuickPickItem;
}
Expand All @@ -41,8 +50,13 @@ export class ShowCommitSearchCommand extends ActiveEditorCachedCommand {
}

protected async preExecute(context: CommandContext, args: ShowCommitSearchCommandArgs = {}) {
if (isCommandViewContextWithRepo(context)) {
return this.execute(context.editor, context.node.uri, args);
if (context.type === 'view' || context.type === 'viewItem') {
args = { ...args };
args.showInResults = true;

if (isCommandViewContextWithRepo(context)) {
return this.execute(context.editor, context.node.uri, args);
}
}

return this.execute(context.editor, context.uri, args);
Expand All @@ -65,24 +79,17 @@ export class ShowCommitSearchCommand extends ActiveEditorCachedCommand {
const originalArgs = { ...args };

if (!args.search || args.searchBy == null) {
try {
if (!args.search) {
if (editor != null && gitUri != null) {
const blameLine = await Container.git.getBlameForLine(gitUri, editor.selection.active.line);
if (blameLine !== undefined && !blameLine.commit.isUncommitted) {
args.search = `#${blameLine.commit.shortSha}`;
}
}
}
}
catch (ex) {
Logger.error(ex, 'ShowCommitSearchCommand', 'search prefetch failed');
let selection;
if (!args.search && args.searchBy != null) {
args.search = searchByToSymbolMap.get(args.searchBy);
selection = [1, 1];
}

args.search = await window.showInputBox({
value: args.search,
prompt: `Please enter a search string`,
placeHolder: `search by message, author (@<pattern>), files (:<pattern>), commit id (#<sha>), changes (=<pattern>), changed lines (~<pattern>)`
placeHolder: `search by message, author (@<pattern>), files (:<pattern>), commit id (#<sha>), changes (=<pattern>), changed lines (~<pattern>)`,
valueSelection: selection
} as InputBoxOptions);
if (args.search === undefined) {
return args.goBackCommand === undefined ? undefined : args.goBackCommand.execute();
Expand All @@ -92,7 +99,7 @@ export class ShowCommitSearchCommand extends ActiveEditorCachedCommand {

const match = searchByRegex.exec(args.search);
if (match && match[1]) {
args.searchBy = searchByMap.get(match[1]);
args.searchBy = symbolToSearchByMap.get(match[1]);
args.search = args.search.substring(args.search[1] === ' ' ? 2 : 1);
}
else if (GitService.isShaLike(args.search)) {
Expand Down Expand Up @@ -134,6 +141,18 @@ export class ShowCommitSearchCommand extends ActiveEditorCachedCommand {
break;
}

if (args.showInResults) {
Container.resultsExplorer.addSearchResults(
repoPath,
Container.git.getLogForSearch(repoPath, args.search!, args.searchBy!, {
maxCount: args.maxCount
}),
{ label: searchLabel! }
);

return;
}

const progressCancellation = CommitsQuickPick.showProgress(searchLabel!);
try {
const log = await Container.git.getLogForSearch(repoPath, args.search, args.searchBy, {
Expand Down
2 changes: 1 addition & 1 deletion src/quickpicks/commonQuickPicks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ export class ShowCommitsInResultsQuickPickItem extends CommandQuickPickItem {
async execute(
options: TextDocumentShowOptions = { preserveFocus: false, preview: false }
): Promise<{} | undefined> {
await Container.resultsExplorer.addSearchResults(this.results, this.resultsLabel);
await Container.resultsExplorer.addSearchResults(this.results.repoPath, this.results, this.resultsLabel);
return undefined;
}
}
Expand Down
38 changes: 32 additions & 6 deletions src/views/nodes/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,38 @@ export class MessageNode extends ExplorerNode {
}
}

export class CommandMessageNode extends MessageNode {
constructor(
parent: ExplorerNode,
private readonly _command: Command,
message: string,
tooltip?: string,
iconPath?:
| string
| Uri
| {
light: string | Uri;
dark: string | Uri;
}
| ThemeIcon
) {
super(parent, message, tooltip, iconPath);
}

getTreeItem(): TreeItem | Promise<TreeItem> {
const item = super.getTreeItem();
if (item instanceof TreeItem) {
item.command = this._command;
return item;
}

return item.then(i => {
i.command = this._command;
return i;
});
}
}

export class UpdateableMessageNode extends ExplorerNode {
constructor(
parent: ExplorerNode,
Expand Down Expand Up @@ -150,9 +182,3 @@ export class ShowMoreNode extends PagerNode {
this._args.maxCount = maxCount;
}
}

export class ShowAllNode extends ShowMoreNode {
constructor(type: string, node: ExplorerNode, explorer: Explorer) {
super(type, node, explorer, 0);
}
}
6 changes: 3 additions & 3 deletions src/views/nodes/resultsCommitsNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { GitLog, GitUri } from '../../git/gitService';
import { Iterables } from '../../system';
import { ResultsExplorer } from '../resultsExplorer';
import { CommitNode } from './commitNode';
import { ShowAllNode } from './common';
import { ShowMoreNode } from './common';
import { ExplorerNode, PageableExplorerNode, ResourceType } from './explorerNode';

export interface CommitsQueryResults {
Expand All @@ -30,12 +30,12 @@ export class ResultsCommitsNode extends ExplorerNode implements PageableExplorer
const { log } = await this.getCommitsQueryResults();
if (log === undefined) return [];

const children: (CommitNode | ShowAllNode)[] = [
const children: (CommitNode | ShowMoreNode)[] = [
...Iterables.map(log.commits.values(), c => new CommitNode(c, this, this.explorer))
];

if (log.truncated) {
children.push(new ShowAllNode('Results', this, this.explorer));
children.push(new ShowMoreNode('Results', this, this.explorer));
}

return children;
Expand Down
79 changes: 77 additions & 2 deletions src/views/nodes/resultsNode.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
'use strict';
import { TreeItem, TreeItemCollapsibleState } from 'vscode';
import { ShowCommitSearchCommandArgs } from '../../commands';
import { GlyphChars } from '../../constants';
import { GitRepoSearchBy } from '../../git/gitService';
import { Strings } from '../../system';
import { ResultsExplorer } from '../resultsExplorer';
import { MessageNode } from './common';
import { CommandMessageNode, MessageNode } from './common';
import { ExplorerNode, ResourceType, unknownGitUri } from './explorerNode';

export class ResultsNode extends ExplorerNode {
Expand All @@ -14,7 +18,78 @@ export class ResultsNode extends ExplorerNode {
}

async getChildren(): Promise<ExplorerNode[]> {
if (this._children.length === 0) return [new MessageNode(this, 'No results')];
if (this._children.length === 0) {
const command = {
title: 'Search Commits',
command: 'gitlens.showCommitSearch'
};

return [
new CommandMessageNode(
this,
{
...command,
arguments: [this, { searchBy: GitRepoSearchBy.Message } as ShowCommitSearchCommandArgs]
},
`Start a commit search by`,
'Click to search'
),
new CommandMessageNode(
this,
{
...command,
arguments: [this, { searchBy: GitRepoSearchBy.Message } as ShowCommitSearchCommandArgs]
},
`${GlyphChars.Space.repeat(4)} message ${Strings.pad(GlyphChars.Dash, 1, 1)} use <message-pattern>`,
'Click to search by message'
),
new CommandMessageNode(
this,
{
...command,
arguments: [this, { searchBy: GitRepoSearchBy.Author } as ShowCommitSearchCommandArgs]
},
`${GlyphChars.Space.repeat(4)} author ${Strings.pad(GlyphChars.Dash, 1, 1)} use @<author-pattern>`,
'Click to search by author'
),
new CommandMessageNode(
this,
{
...command,
arguments: [this, { searchBy: GitRepoSearchBy.Sha } as ShowCommitSearchCommandArgs]
},
`${GlyphChars.Space.repeat(4)} commit id ${Strings.pad(GlyphChars.Dash, 1, 1)} use #<sha>`,
'Click to search by commit id'
),
new CommandMessageNode(
this,
{
...command,
arguments: [this, { searchBy: GitRepoSearchBy.Files } as ShowCommitSearchCommandArgs]
},
`${GlyphChars.Space.repeat(4)} files ${Strings.pad(GlyphChars.Dash, 1, 1)} use :<file-pattern>`,
'Click to search by files'
),
new CommandMessageNode(
this,
{
...command,
arguments: [this, { searchBy: GitRepoSearchBy.Changes } as ShowCommitSearchCommandArgs]
},
`${GlyphChars.Space.repeat(4)} changes ${Strings.pad(GlyphChars.Dash, 1, 1)} use =<pattern>`,
'Click to search by changes'
),
new CommandMessageNode(
this,
{
...command,
arguments: [this, { searchBy: GitRepoSearchBy.ChangedLines } as ShowCommitSearchCommandArgs]
},
`${GlyphChars.Space.repeat(4)} changed lines ${Strings.pad(GlyphChars.Dash, 1, 1)} use ~<pattern>`,
'Click to search by changed lines'
)
];
}

return this._children;
}
Expand Down
27 changes: 16 additions & 11 deletions src/views/resultsExplorer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,8 @@ export class ResultsExplorer extends ExplorerBase<ResultsNode> {
}

addSearchResults(
results: GitLog,
repoPath: string,
resultsOrPromise: GitLog | Promise<GitLog | undefined>,
resultsLabel:
| string
| {
Expand All @@ -139,12 +140,17 @@ export class ResultsExplorer extends ExplorerBase<ResultsNode> {
}
) {
const getCommitsQuery = async (maxCount: number | undefined) => {
const log = await Functions.seeded(
results.query === undefined
? (maxCount: number | undefined) => Promise.resolve(results)
: results.query,
results
)(maxCount);
const results = await resultsOrPromise;

let log;
if (results !== undefined) {
log = await Functions.seeded(
results.query === undefined
? (maxCount: number | undefined) => Promise.resolve(results)
: results.query,
results.maxCount === maxCount ? results : undefined
)(maxCount);
}

let label;
if (typeof resultsLabel === 'string') {
Expand All @@ -161,9 +167,8 @@ export class ResultsExplorer extends ExplorerBase<ResultsNode> {

let repository = '';
if ((await Container.git.getRepositoryCount()) > 1) {
const repo = await Container.git.getRepository(results.repoPath);
repository = ` ${Strings.pad(GlyphChars.Dash, 1, 1)} ${(repo && repo.formattedName) ||
results.repoPath}`;
const repo = await Container.git.getRepository(repoPath);
repository = ` ${Strings.pad(GlyphChars.Dash, 1, 1)} ${(repo && repo.formattedName) || repoPath}`;
}

label = `${Strings.pluralize(resultsType.singular, count, {
Expand All @@ -180,7 +185,7 @@ export class ResultsExplorer extends ExplorerBase<ResultsNode> {
};

return this.addResults(
new ResultsCommitsNode(results.repoPath, getCommitsQuery, undefined, this, ResourceType.SearchResults)
new ResultsCommitsNode(repoPath, getCommitsQuery, undefined, this, ResourceType.SearchResults)
);
}

Expand Down

0 comments on commit af71820

Please sign in to comment.