From 6ecdb2f81f99ec9f08e4f702ed8e7fdfed054e53 Mon Sep 17 00:00:00 2001 From: Eric Amodio Date: Tue, 20 Oct 2020 23:42:10 -0400 Subject: [PATCH] Adds Show in Side Bar for search git command Removes Show Here/Show in Side Bar toggle from search git command Changes showResultsInSideBar to be an override if not null --- package.json | 13 +++++++---- src/commands/git/search.ts | 37 ++++++++++++++++-------------- src/commands/quickCommand.buttons.ts | 30 ------------------------ src/commands/quickCommand.steps.ts | 41 ++++++++++++++++++++++++--------- src/commands/searchCommits.ts | 4 +++- src/config.ts | 2 +- src/quickpicks/quickPicksItems.ts | 13 +++++++++++ src/views/nodes/compareResultsNode.ts | 28 +++++++++++------------ src/views/nodes/searchResultsNode.ts | 43 +++++++++++++++++++---------------- 9 files changed, 112 insertions(+), 99 deletions(-) diff --git a/package.json b/package.json index e1e9238..a3798ff 100644 --- a/package.json +++ b/package.json @@ -599,9 +599,12 @@ "markdownDeprecationMessage": "Depreciated: This setting has been renamed to `#gitlens.gitCommands.search.showResultsInSideBar#`" }, "gitlens.gitCommands.search.showResultsInSideBar": { - "type": "boolean", - "default": false, - "markdownDescription": "Specifies whether to show the commit search results in the Side Bar or within the quick pick menu", + "type": [ + "boolean", + "null" + ], + "default": null, + "markdownDescription": "Specifies whether to show the commit search results directly in the quick pick menu, in the Side Bar, or will be based on the context", "scope": "window" }, "gitlens.gitCommands.skipConfirmations": { @@ -7200,12 +7203,12 @@ }, { "command": "gitlens.views.searchAndCompare.edit", - "when": "viewItem == gitlens:search:results", + "when": "viewItem =~ /gitlens:search:results\\b/", "group": "inline@1" }, { "command": "gitlens.views.searchAndCompare.edit", - "when": "viewItem == gitlens:search:results", + "when": "viewItem =~ /gitlens:search:results\\b/", "group": "1_gitlens_actions@1" }, { diff --git a/src/commands/git/search.ts b/src/commands/git/search.ts index 540fade..aa7914b 100644 --- a/src/commands/git/search.ts +++ b/src/commands/git/search.ts @@ -17,7 +17,7 @@ import { StepSelection, StepState, } from '../quickCommand'; -import { QuickPickItemOfT } from '../../quickpicks'; +import { ActionQuickPickItem, QuickPickItemOfT } from '../../quickpicks'; import { Strings } from '../../system'; interface Context { @@ -105,7 +105,7 @@ export class SearchGitCommand extends QuickCommand { state.matchRegex = cfg.matchRegex; } if (state.showResultsInSideBar == null) { - state.showResultsInSideBar = cfg.showResultsInSideBar; + state.showResultsInSideBar = cfg.showResultsInSideBar ?? undefined; } let skippedStepOne = false; @@ -188,6 +188,23 @@ export class SearchGitCommand extends QuickCommand { number: log.hasMore ? `${log.count}+` : undefined, })} for ${state.pattern}`, picked: context.commit?.ref, + showInSideBarCommand: new ActionQuickPickItem( + '$(link-external) Show Results in Side Bar', + () => + void Container.searchAndCompareView.search( + repoPath, + search, + { + label: { label: `for ${state.pattern}` }, + reveal: { + select: true, + focus: false, + expand: true, + }, + }, + context.resultsPromise, + ), + ), showInSideBarButton: { button: QuickCommandButtons.ShowResultsInSideBar, onDidClick: () => @@ -266,26 +283,12 @@ export class SearchGitCommand extends QuickCommand { const matchAllButton = new QuickCommandButtons.MatchAllToggle(state.matchAll); const matchRegexButton = new QuickCommandButtons.MatchRegexToggle(state.matchRegex); - const additionalButtons = [matchCaseButton, matchAllButton, matchRegexButton]; - if (!SearchResultsNode.is(state.showResultsInSideBar)) { - const showResultsToggleButton = new QuickCommandButtons.ShowResultsToggle( - state.showResultsInSideBar, - () => { - // eslint-disable-next-line @typescript-eslint/strict-boolean-expressions - state.showResultsInSideBar = !state.showResultsInSideBar; - showResultsToggleButton.on = state.showResultsInSideBar; - }, - ); - - additionalButtons.push(showResultsToggleButton); - } - const step = QuickCommand.createPickStep>({ title: appendReposToTitle(context.title, state, context), placeholder: 'e.g. "Updates dependencies" author:eamodio', matchOnDescription: true, matchOnDetail: true, - additionalButtons: additionalButtons, + additionalButtons: [matchCaseButton, matchAllButton, matchRegexButton], items: items, value: state.pattern, onDidAccept: (quickpick): boolean => { diff --git a/src/commands/quickCommand.buttons.ts b/src/commands/quickCommand.buttons.ts index 828cd92..50792b8 100644 --- a/src/commands/quickCommand.buttons.ts +++ b/src/commands/quickCommand.buttons.ts @@ -112,32 +112,6 @@ export namespace QuickCommandButtons { tooltip: 'Show Results in Side Bar', }; - export const ShowResultsToggle = class extends ToggleQuickInputButton { - constructor(on = false, onDidClick?: (quickInput: QuickInput) => void) { - super( - () => ({ - on: { - tooltip: 'Show Results in Side Bar', - icon: { - dark: Uri.file(Container.context.asAbsolutePath('images/dark/icon-window.svg')), - light: Uri.file(Container.context.asAbsolutePath('images/light/icon-window.svg')), - }, - }, - off: { - tooltip: 'Show Results Here', - icon: { - dark: Uri.file(Container.context.asAbsolutePath('images/dark/icon-window-disabled.svg')), - light: Uri.file(Container.context.asAbsolutePath('images/light/icon-window-disabled.svg')), - }, - }, - }), - on, - ); - - this.onDidClick = onDidClick; - } - }; - export const ShowTagsToggle = class extends SelectableQuickInputButton { constructor(on = false) { super('Show Tags', 'tag', on); @@ -146,10 +120,6 @@ export namespace QuickCommandButtons { export const WillConfirmForced: QuickInputButton = { iconPath: new ThemeIcon('check'), - // iconPath: { - // dark: Uri.file(Container.context.asAbsolutePath('images/dark/icon-check.svg')), - // light: Uri.file(Container.context.asAbsolutePath('images/light/icon-check.svg')), - // }, tooltip: 'Will always confirm', }; diff --git a/src/commands/quickCommand.steps.ts b/src/commands/quickCommand.steps.ts index c594b1a..725db60 100644 --- a/src/commands/quickCommand.steps.ts +++ b/src/commands/quickCommand.steps.ts @@ -728,7 +728,7 @@ export async function* pickBranchOrTagStepMultiRepo< return QuickCommand.canPickStepContinue(step, state, selection) ? selection[0].item : StepResult.Break; } -export function* pickCommitStep< +export async function* pickCommitStep< State extends PartialStepState & { repo: Repository }, Context extends { repos: Repository[]; title: string } >( @@ -740,6 +740,7 @@ export function* pickCommitStep< onDidLoadMore, picked, placeholder, + showInSideBarCommand, showInSideBarButton: showInSideBar, titleContext, }: { @@ -748,6 +749,7 @@ export function* pickCommitStep< onDidLoadMore?: (log: GitLog | undefined) => void; picked?: string | string[] | undefined; placeholder: string | ((context: Context, log: GitLog | undefined) => string); + showInSideBarCommand?: CommandQuickPickItem; showInSideBarButton?: { button: QuickInputButton; onDidClick: (items: Readonly[]>) => void; @@ -771,14 +773,14 @@ export function* pickCommitStep< ]; } - const step = QuickCommand.createPickStep({ + const step = QuickCommand.createPickStep({ title: appendReposToTitle(`${context.title}${titleContext ?? ''}`, state, context), placeholder: typeof placeholder === 'string' ? placeholder : placeholder(context, log), ignoreFocusOut: ignoreFocusOut, matchOnDescription: true, matchOnDetail: true, value: typeof picked === 'string' && log?.count === 0 ? picked : undefined, - items: getItems(log), + items: showInSideBarCommand != null ? [showInSideBarCommand, ...getItems(log)] : getItems(log), onDidLoadMore: async quickpick => { log = await log?.more?.(configuration.get('advanced', 'maxListItems')); onDidLoadMore?.(log); @@ -795,16 +797,20 @@ export function* pickCommitStep< onDidClickButton: (quickpick, button) => { if (log == null) return; + const items = quickpick.activeItems.filter>( + (i): i is CommitQuickPickItem => !CommandQuickPickItem.is(i), + ); + if (button === showInSideBar?.button) { - showInSideBar.onDidClick(quickpick.activeItems); + showInSideBar.onDidClick(items); return; } - if (quickpick.activeItems.length === 0 || log == null) return; + if (items.length === 0 || log == null) return; if (button === QuickCommandButtons.RevealInSideBar) { - void GitActions.Commit.reveal(quickpick.activeItems[0].item, { + void GitActions.Commit.reveal(items[0].item, { select: true, focus: false, expand: true, @@ -816,10 +822,10 @@ export function* pickCommitStep< if (button === QuickCommandButtons.SearchInSideBar) { void Container.searchAndCompareView.search( state.repo.path, - { pattern: SearchPattern.fromCommit(quickpick.activeItems[0].item.ref) }, + { pattern: SearchPattern.fromCommit(items[0].item.ref) }, { label: { - label: `for ${GitReference.toString(quickpick.activeItems[0].item, { icon: false })}`, + label: `for ${GitReference.toString(items[0].item, { icon: false })}`, }, reveal: { select: true, @@ -834,14 +840,18 @@ export function* pickCommitStep< onDidPressKey: async (quickpick, key) => { if (quickpick.activeItems.length === 0) return; + const items = quickpick.activeItems.filter>( + (i): i is CommitQuickPickItem => !CommandQuickPickItem.is(i), + ); + if (key === 'ctrl+right') { - await GitActions.Commit.reveal(quickpick.activeItems[0].item, { + await GitActions.Commit.reveal(items[0].item, { select: true, focus: false, expand: true, }); } else { - const commit = quickpick.activeItems[0].item; + const commit = items[0].item; await Container.searchAndCompareView.search( commit.repoPath, { pattern: SearchPattern.fromCommit(commit) }, @@ -859,7 +869,16 @@ export function* pickCommitStep< onValidateValue: getValidateGitReferenceFn(state.repo), }); const selection: StepSelection = yield step; - return QuickCommand.canPickStepContinue(step, state, selection) ? selection[0].item : StepResult.Break; + if (!QuickCommand.canPickStepContinue(step, state, selection)) return StepResult.Break; + + if (CommandQuickPickItem.is(selection[0])) { + QuickCommand.endSteps(state); + + await selection[0].execute(); + return StepResult.Break; + } + + return selection[0].item; } export function* pickCommitsStep< diff --git a/src/commands/searchCommits.ts b/src/commands/searchCommits.ts index f90f8d6..d19371e 100644 --- a/src/commands/searchCommits.ts +++ b/src/commands/searchCommits.ts @@ -1,6 +1,7 @@ 'use strict'; import { executeGitCommand } from '../commands'; import { Command, command, CommandContext, Commands, isCommandViewContextWithRepo } from './common'; +import { Container } from '../container'; import { SearchPattern } from '../git/git'; import { SearchResultsNode } from '../views/nodes'; @@ -48,7 +49,8 @@ export class SearchCommitsCommand extends Command { state: { repo: args?.repoPath, ...args?.search, - showResultsInSideBar: args?.showResultsInSideBar, + showResultsInSideBar: + Container.config.gitCommands.search.showResultsInSideBar ?? args?.showResultsInSideBar, }, })); } diff --git a/src/config.ts b/src/config.ts index 6705bfd..3f9115c 100644 --- a/src/config.ts +++ b/src/config.ts @@ -52,7 +52,7 @@ export interface Config { matchAll: boolean; matchCase: boolean; matchRegex: boolean; - showResultsInSideBar: boolean; + showResultsInSideBar: boolean | null; }; skipConfirmations: string[]; }; diff --git a/src/quickpicks/quickPicksItems.ts b/src/quickpicks/quickPicksItems.ts index b35993f..6066ac7 100644 --- a/src/quickpicks/quickPicksItems.ts +++ b/src/quickpicks/quickPicksItems.ts @@ -160,6 +160,19 @@ export class CommandQuickPickItem implements Qu } } +export class ActionQuickPickItem extends CommandQuickPickItem { + constructor( + labelOrItem: string | QuickPickItem, + private readonly action: (options?: { preserveFocus?: boolean; preview?: boolean }) => void | Promise, + ) { + super(labelOrItem, undefined, undefined); + } + + async execute(options?: { preserveFocus?: boolean; preview?: boolean }): Promise { + return this.action(options); + } +} + export type FlagsQuickPickItem = QuickPickItemOfT; export namespace FlagsQuickPickItem { export function create(flags: T[], item: T[], options: QuickPickItem) { diff --git a/src/views/nodes/compareResultsNode.ts b/src/views/nodes/compareResultsNode.ts index 410f406..3f3dd38 100644 --- a/src/views/nodes/compareResultsNode.ts +++ b/src/views/nodes/compareResultsNode.ts @@ -145,13 +145,8 @@ export class CompareResultsNode extends ViewNode { if (this.pinned) return; this._pinned = Date.now(); - await this.view.updatePinned(this.getPinnableId(), { - type: 'comparison', - timestamp: this._pinned, - path: this.repoPath, - ref1: this._ref, - ref2: this._compareWith, - }); + await this.updatePinned(); + setImmediate(() => this.view.reveal(this, { focus: true, select: true })); } @@ -175,13 +170,7 @@ export class CompareResultsNode extends ViewNode { // If we were pinned, remove the existing pin and save a new one if (this.pinned) { await this.view.updatePinned(currentId); - await this.view.updatePinned(this.getPinnableId(), { - type: 'comparison', - timestamp: this._pinned, - path: this.repoPath, - ref1: this._ref, - ref2: this._compareWith, - }); + await this.updatePinned(); } this._children = undefined; @@ -195,6 +184,7 @@ export class CompareResultsNode extends ViewNode { this._pinned = 0; await this.view.updatePinned(this.getPinnableId()); + setImmediate(() => this.view.reveal(this, { focus: true, select: true })); } @@ -284,4 +274,14 @@ export class CompareResultsNode extends ViewNode { return results as CommitsQueryResults; }; } + + private updatePinned() { + return this.view.updatePinned(this.getPinnableId(), { + type: 'comparison', + timestamp: this._pinned, + path: this.repoPath, + ref1: this._ref, + ref2: this._compareWith, + }); + } } diff --git a/src/views/nodes/searchResultsNode.ts b/src/views/nodes/searchResultsNode.ts index b5d8958..a9c35e4 100644 --- a/src/views/nodes/searchResultsNode.ts +++ b/src/views/nodes/searchResultsNode.ts @@ -139,19 +139,6 @@ export class SearchResultsNode extends ViewNode implements item.iconPath = new ThemeIcon('pinned'); } - // if (item.collapsibleState === TreeItemCollapsibleState.None) { - // const args: SearchCommitsCommandArgs = { - // search: this.search, - // prefillOnly: true, - // showResultsInSideBar: true, - // }; - // item.command = { - // title: 'Search Commits', - // command: Commands.SearchCommitsInView, - // arguments: [args], - // }; - // } - return item; } @@ -191,12 +178,22 @@ export class SearchResultsNode extends ViewNode implements return; } + // Save the current id so we can update it later + const currentId = this.getPinnableId(); + this._search = search.pattern; this._labels = search.labels; this._searchQueryOrLog = search.log; this._resultsNode = undefined; + // If we were pinned, remove the existing pin and save a new one + if (this.pinned) { + await this.view.updatePinned(currentId); + await this.updatePinned(); + } + void this.triggerChange(false); + setImmediate(() => this.view.reveal(this, { expand: true, focus: true, select: true })); } @gate() @@ -210,13 +207,8 @@ export class SearchResultsNode extends ViewNode implements if (this.pinned) return; this._pinned = Date.now(); - await this.view.updatePinned(this.getPinnableId(), { - type: 'search', - timestamp: this._pinned, - path: this.repoPath, - labels: this._labels, - search: this.search, - }); + await this.updatePinned(); + setImmediate(() => this.view.reveal(this, { focus: true, select: true })); } @@ -226,6 +218,7 @@ export class SearchResultsNode extends ViewNode implements this._pinned = 0; await this.view.updatePinned(this.getPinnableId()); + setImmediate(() => this.view.reveal(this, { focus: true, select: true })); } @@ -294,4 +287,14 @@ export class SearchResultsNode extends ViewNode implements return results; }; } + + private updatePinned() { + return this.view.updatePinned(this.getPinnableId(), { + type: 'search', + timestamp: this._pinned, + path: this.repoPath, + labels: this._labels, + search: this.search, + }); + } }