Skip to content

Commit

Permalink
Improves the commit search experience
Browse files Browse the repository at this point in the history
Remembers and restores the last commit search string
Adds search command to the results inline toolbar
Reopens search when clicking on a result node with no results
Fixes encoded html in empty state
  • Loading branch information
eamodio committed Dec 13, 2018
1 parent 7b85b2e commit 99bf1c5
Show file tree
Hide file tree
Showing 9 changed files with 126 additions and 25 deletions.
13 changes: 13 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,19 @@ All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](http://semver.org/).

## [Unreleased]

### Added

- Improves the commit search experience
- Remembers and restores the last commit search string
- Adds the _Search Commits_ command to the search results inline toolbar
- Reopens the commit search when clicking on a search results without results

### Fixed

- Fixes html encoding issues with the _Search Commits_ view empty state

## [9.1.0] - 2018-12-12

### Added
Expand Down
20 changes: 15 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4127,11 +4127,6 @@
"when": "viewItem == gitlens:repository",
"group": "8_gitlens@1"
},
{
"command": "gitlens.views.compare.swapComparision",
"when": "viewItem == gitlens:compare:results",
"group": "inline@1"
},
{
"command": "gitlens.views.dismissNode",
"when": "viewItem =~ /gitlens:(compare:picker:ref|compare|search)\\b(?!:(commits|files))/",
Expand All @@ -4142,6 +4137,11 @@
"when": "viewItem =~ /gitlens:(compare:picker:ref|compare|search)\\b(?!:(commits|files))/",
"group": "1_gitlens@1"
},
{
"command": "gitlens.views.compare.swapComparision",
"when": "viewItem == gitlens:compare:results",
"group": "inline@1"
},
{
"command": "gitlens.views.compare.swapComparision",
"when": "viewItem == gitlens:compare:results",
Expand All @@ -4152,6 +4152,16 @@
"when": "viewItem == gitlens:compare:results",
"group": "7_gitlens@1"
},
{
"command": "gitlens.views.search.searchCommits",
"when": "viewItem == gitlens:search:results",
"group": "inline@1"
},
{
"command": "gitlens.views.search.searchCommits",
"when": "viewItem == gitlens:search:results",
"group": "2_gitlens@1"
},
{
"command": "gitlens.stashApply",
"when": "!gitlens:readonly && viewItem == gitlens:stashes",
Expand Down
32 changes: 27 additions & 5 deletions src/commands/searchCommits.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { Logger } from '../logger';
import { Messages } from '../messages';
import { CommandQuickPickItem, CommitsQuickPick, ShowCommitSearchResultsInViewQuickPickItem } from '../quickpicks';
import { Iterables, Strings } from '../system';
import { SearchResultsCommitsNode } from '../views/nodes';
import {
ActiveEditorCachedCommand,
command,
Expand Down Expand Up @@ -39,13 +40,16 @@ export interface SearchCommitsCommandArgs {
search?: string;
searchBy?: GitRepoSearchBy;
maxCount?: number;
prefillOnly?: boolean;
showInView?: boolean;

goBackCommand?: CommandQuickPickItem;
}

@command()
export class SearchCommitsCommand extends ActiveEditorCachedCommand {
private _lastSearch: string | undefined;

constructor() {
super([Commands.SearchCommits, Commands.SearchCommitsInView]);
}
Expand All @@ -55,6 +59,12 @@ export class SearchCommitsCommand extends ActiveEditorCachedCommand {
args = { ...args };
args.showInView = true;

if (context.node instanceof SearchResultsCommitsNode) {
args.search = context.node.search;
args.searchBy = context.node.searchBy;
args.prefillOnly = true;
}

if (isCommandViewContextWithRepo(context)) {
return this.execute(context.editor, context.node.uri, args);
}
Expand Down Expand Up @@ -86,11 +96,21 @@ export class SearchCommitsCommand extends ActiveEditorCachedCommand {
args = { ...args };
const originalArgs = { ...args };

if (args.prefillOnly && args.search && args.searchBy) {
args.search = `${searchByToSymbolMap.get(args.searchBy) || ''}${args.search}`;
args.searchBy = undefined;
}

if (!args.search || args.searchBy == null) {
let selection;
if (!args.search && args.searchBy != null) {
args.search = searchByToSymbolMap.get(args.searchBy);
selection = [1, 1];
if (!args.search) {
if (args.searchBy != null) {
args.search = searchByToSymbolMap.get(args.searchBy);
selection = [1, 1];
}
else {
args.search = this._lastSearch;
}
}

if (args.showInView) {
Expand All @@ -107,7 +127,7 @@ export class SearchCommitsCommand extends ActiveEditorCachedCommand {
return args.goBackCommand === undefined ? undefined : args.goBackCommand.execute();
}

originalArgs.search = args.search;
this._lastSearch = originalArgs.search = args.search;

const match = searchByRegex.exec(args.search);
if (match && match[1]) {
Expand Down Expand Up @@ -198,7 +218,9 @@ export class SearchCommitsCommand extends ActiveEditorCachedCommand {
: undefined,
showInViewCommand:
log !== undefined
? new ShowCommitSearchResultsInViewQuickPickItem(log, { label: searchLabel! })
? new ShowCommitSearchResultsInViewQuickPickItem(args.search, args.searchBy, log, {
label: searchLabel!
})
: undefined
});
if (pick === undefined) return undefined;
Expand Down
8 changes: 6 additions & 2 deletions src/quickpicks/commonQuickPicks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { GlyphChars } from '../constants';
import { Container } from '../container';
import { GitLog, GitLogCommit, GitRepoSearchBy, GitStashCommit, GitUri } from '../git/gitService';
import { KeyMapping, Keys } from '../keyboard';
import { Functions, Strings } from '../system';
import { Strings } from '../system';
import {
BranchesAndTagsQuickPick,
BranchQuickPickItem,
Expand Down Expand Up @@ -243,6 +243,8 @@ export class ShowCommitInViewQuickPickItem extends CommandQuickPickItem {

export class ShowCommitSearchResultsInViewQuickPickItem extends CommandQuickPickItem {
constructor(
public readonly search: string,
public readonly searchBy: GitRepoSearchBy,
public readonly results: GitLog,
public readonly resultsLabel: string | { label: string; resultsType?: { singular: string; plural: string } },
item: QuickPickItem = {
Expand All @@ -258,7 +260,9 @@ export class ShowCommitSearchResultsInViewQuickPickItem extends CommandQuickPick
}

async execute(): Promise<{} | undefined> {
await Container.searchView.showSearchResults(this.results.repoPath, this.results, { label: this.resultsLabel });
await Container.searchView.showSearchResults(this.results.repoPath, this.search, this.searchBy, this.results, {
label: this.resultsLabel
});
return undefined;
}
}
Expand Down
1 change: 1 addition & 0 deletions src/views/nodes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export * from './nodes/resultsCommitsNode';
export * from './nodes/resultsFileNode';
export * from './nodes/resultsFilesNode';
export * from './nodes/searchNode';
export * from './nodes/searchResultsCommitsNode';
export * from './nodes/stashesNode';
export * from './nodes/stashFileNode';
export * from './nodes/stashNode';
Expand Down
11 changes: 7 additions & 4 deletions src/views/nodes/resultsCommitsNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,15 @@ export class ResultsCommitsNode extends ViewNode implements PageableViewNode {
view: View,
parent: ViewNode,
public readonly repoPath: string,
private readonly _commitsQuery: (maxCount: number | undefined) => Promise<CommitsQueryResults>,
private readonly _contextValue: ResourceType = ResourceType.ResultsCommits
private readonly _commitsQuery: (maxCount: number | undefined) => Promise<CommitsQueryResults>
) {
super(GitUri.fromRepoPath(repoPath), view, parent);
}

get type(): ResourceType {
return ResourceType.ResultsCommits;
}

async getChildren(): Promise<ViewNode[]> {
const { log } = await this.getCommitsQueryResults();
if (log === undefined) return [];
Expand Down Expand Up @@ -56,7 +59,7 @@ export class ResultsCommitsNode extends ViewNode implements PageableViewNode {
label,
log && log.count > 0 ? TreeItemCollapsibleState.Collapsed : TreeItemCollapsibleState.None
);
item.contextValue = this._contextValue;
item.contextValue = this.type;

return item;
}
Expand All @@ -67,7 +70,7 @@ export class ResultsCommitsNode extends ViewNode implements PageableViewNode {

private _commitsQueryResults: Promise<CommitsQueryResults> | undefined;

private getCommitsQueryResults() {
protected getCommitsQueryResults() {
if (this._commitsQueryResults === undefined) {
this._commitsQueryResults = this._commitsQuery(this.maxCount);
}
Expand Down
12 changes: 6 additions & 6 deletions src/views/nodes/searchNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export class SearchNode extends ViewNode {
...command,
arguments: [this, { searchBy: GitRepoSearchBy.Message } as SearchCommitsCommandArgs]
},
`Search commits by message (use &lt;message-pattern&gt;)`,
`Search commits by message (use <message-pattern>)`,
'Click to search commits by message'
),
new CommandMessageNode(
Expand All @@ -40,7 +40,7 @@ export class SearchNode extends ViewNode {
...command,
arguments: [this, { searchBy: GitRepoSearchBy.Author } as SearchCommitsCommandArgs]
},
`${GlyphChars.Space.repeat(4)} or, by author (use @&lt;author-pattern&gt;)`,
`${GlyphChars.Space.repeat(4)} or, by author (use @<author-pattern>)`,
'Click to search commits by author'
),
new CommandMessageNode(
Expand All @@ -50,7 +50,7 @@ export class SearchNode extends ViewNode {
...command,
arguments: [this, { searchBy: GitRepoSearchBy.Sha } as SearchCommitsCommandArgs]
},
`${GlyphChars.Space.repeat(4)} or, by commit id (use #&lt;sha&gt;)`,
`${GlyphChars.Space.repeat(4)} or, by commit id (use #<sha>)`,
'Click to search commits by commit id'
),
new CommandMessageNode(
Expand All @@ -60,7 +60,7 @@ export class SearchNode extends ViewNode {
...command,
arguments: [this, { searchBy: GitRepoSearchBy.Files } as SearchCommitsCommandArgs]
},
`${GlyphChars.Space.repeat(4)} or, by files (use :&lt;file-path/glob&gt;)`,
`${GlyphChars.Space.repeat(4)} or, by files (use :<file-path/glob>)`,
'Click to search commits by files'
),
new CommandMessageNode(
Expand All @@ -70,7 +70,7 @@ export class SearchNode extends ViewNode {
...command,
arguments: [this, { searchBy: GitRepoSearchBy.Changes } as SearchCommitsCommandArgs]
},
`${GlyphChars.Space.repeat(4)} or, by changes (use =&lt;pattern&gt;)`,
`${GlyphChars.Space.repeat(4)} or, by changes (use =<pattern>)`,
'Click to search commits by changes'
),
new CommandMessageNode(
Expand All @@ -80,7 +80,7 @@ export class SearchNode extends ViewNode {
...command,
arguments: [this, { searchBy: GitRepoSearchBy.ChangedLines } as SearchCommitsCommandArgs]
},
`${GlyphChars.Space.repeat(4)} or, by changed lines (use ~&lt;pattern&gt;)`,
`${GlyphChars.Space.repeat(4)} or, by changed lines (use ~<pattern>)`,
'Click to search commits by changed lines'
)
];
Expand Down
46 changes: 46 additions & 0 deletions src/views/nodes/searchResultsCommitsNode.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
'use strict';
import { TreeItem } from 'vscode';
import { SearchCommitsCommandArgs } from '../../commands';
import { Commands } from '../../commands/common';
import { GitRepoSearchBy } from '../../git/gitService';
import { View } from '../viewBase';
import { CommitsQueryResults, ResultsCommitsNode } from './resultsCommitsNode';
import { ResourceType, ViewNode } from './viewNode';

export class SearchResultsCommitsNode extends ResultsCommitsNode {
constructor(
view: View,
parent: ViewNode,
repoPath: string,
public readonly search: string,
public readonly searchBy: GitRepoSearchBy,
commitsQuery: (maxCount: number | undefined) => Promise<CommitsQueryResults>
) {
super(view, parent, repoPath, commitsQuery);
}

get type(): ResourceType {
return ResourceType.SearchResults;
}

async getTreeItem(): Promise<TreeItem> {
const { log } = await super.getCommitsQueryResults();

const item = await super.getTreeItem();

if (log == null || log.count === 0) {
const args: SearchCommitsCommandArgs = {
search: this.search,
searchBy: this.searchBy,
prefillOnly: true
};
item.command = {
title: 'Search Commits',
command: Commands.SearchCommitsInView,
arguments: [args]
};
}

return item;
}
}
8 changes: 5 additions & 3 deletions src/views/searchView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { CommandContext, GlyphChars, setCommandContext, WorkspaceState } from '.
import { Container } from '../container';
import { GitLog, GitRepoSearchBy } from '../git/gitService';
import { Functions, Strings } from '../system';
import { ResourceType, ResultsCommitsNode, SearchNode, ViewNode } from './nodes';
import { SearchNode, SearchResultsCommitsNode, ViewNode } from './nodes';
import { RefreshReason, ViewBase } from './viewBase';

interface SearchQueryResult {
Expand Down Expand Up @@ -118,12 +118,14 @@ export class SearchView extends ViewBase<SearchNode> {
);

return this.addResults(
new ResultsCommitsNode(this, this._root!, repoPath, searchQueryFn, ResourceType.SearchResults)
new SearchResultsCommitsNode(this, this._root!, repoPath, search, searchBy, searchQueryFn)
);
}

async showSearchResults(
repoPath: string,
search: string,
searchBy: GitRepoSearchBy,
results: GitLog,
options: {
label:
Expand All @@ -141,7 +143,7 @@ export class SearchView extends ViewBase<SearchNode> {
});

return this.addResults(
new ResultsCommitsNode(this, this._root!, repoPath, searchQueryFn, ResourceType.SearchResults)
new SearchResultsCommitsNode(this, this._root!, repoPath, search, searchBy, searchQueryFn)
);
}

Expand Down

0 comments on commit 99bf1c5

Please sign in to comment.