Skip to content

Switch first and second result in quickOpen if first result is the current editor #159301

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions src/vs/platform/quickinput/browser/pickerQuickAccess.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,10 @@ export abstract class PickerQuickAccessProvider<T extends IPickerQuickAccessItem
}
}

if (items.length > 1) {
items = this.reorderPicks(items);
}

picker.items = items;
if (activeItem) {
picker.activeItems = [activeItem];
Expand Down Expand Up @@ -365,6 +369,17 @@ export abstract class PickerQuickAccessProvider<T extends IPickerQuickAccessItem
return disposables;
}

/**
* If needed, returns the array of picks reordered.
*
* @param picks the original list of picks and separators
* @returns the picks, possibly in a new array in a better order
*/
protected reorderPicks(picks: readonly Pick<T>[]): readonly Pick<T>[] {
// by default picks don't need reordering
return picks;
}

/**
* Returns an array of picks and separators as needed. If the picks are resolved
* long running, the provided cancellation token should be used to cancel the
Expand Down
64 changes: 63 additions & 1 deletion src/vs/workbench/contrib/search/browser/anythingQuickAccess.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import 'vs/css!./media/anythingQuickAccess';
import { IQuickInputButton, IKeyMods, quickPickItemScorerAccessor, QuickPickItemScorerAccessor, IQuickPick, IQuickPickItemWithResource, QuickInputHideReason, IQuickInputService, IQuickPickSeparator } from 'vs/platform/quickinput/common/quickInput';
import { IPickerQuickAccessItem, PickerQuickAccessProvider, TriggerAction, FastAndSlowPicks, Picks, PicksWithActive } from 'vs/platform/quickinput/browser/pickerQuickAccess';
import { IPickerQuickAccessItem, PickerQuickAccessProvider, TriggerAction, FastAndSlowPicks, Picks, Pick, PicksWithActive } from 'vs/platform/quickinput/browser/pickerQuickAccess';
import { prepareQuery, IPreparedQuery, compareItemsByFuzzyScore, scoreItemFuzzy, FuzzyScorerCache } from 'vs/base/common/fuzzyScorer';
import { IFileQueryBuilderOptions, QueryBuilder } from 'vs/workbench/services/search/common/queryBuilder';
import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation';
Expand Down Expand Up @@ -298,6 +298,68 @@ export class AnythingQuickAccessProvider extends PickerQuickAccessProvider<IAnyt
return this.doGetPicks(filter, { enableEditorSymbolSearch: lastWasFiltering, includeHelp: runOptions?.includeHelp, from: runOptions?.from }, disposables, token);
}

// if the first pick is the active editor, it's not a useful first hit, so swap it with the second pick
override reorderPicks(picks: readonly Pick<IAnythingQuickPickItem>[]): readonly Pick<IAnythingQuickPickItem>[] {
const findNonSeparatorItemIndex = (start: number) => {
for (let i = start; i < picks.length; i += 1) {
if (picks[i].type !== 'separator') {
return i;
}
}
return -1;
};

const firstIndex = findNonSeparatorItemIndex(0);
if (firstIndex < 0) {
return picks;
}

const firstPick = picks[firstIndex];
if (!('resource' in firstPick) || !firstPick.resource || firstPick.resource.fsPath !== this.editorService.activeEditor?.resource?.fsPath) {
// first pick is not the active editor
return picks;
}

// the first pick is the current editor so it's not a useful first hit
const secondIndex = findNonSeparatorItemIndex(firstIndex + 1);
if (secondIndex < 0) {
return picks; // nothing to switch with
}

const secondPick = picks[secondIndex];
if (secondIndex === firstIndex + 1) {
// the second item does not have its own separator, so we can simply swap the first and second
const retval = Array.from(picks);
retval[firstIndex] = secondPick;
retval[secondIndex] = firstPick;
return retval;
} else {
// there is a separator before the second item, we need to move the second item together with its seaparator
// maybe something like firstWithSeparator (array)
const secondSeparator = picks[secondIndex - 1];

if (firstIndex === 0) {
// first pick isn't preceded by a separator
return [
secondPick,
firstPick,
secondSeparator,
...picks.slice(secondIndex + 1),
];
} else {
const firstSeparator = picks[firstIndex - 1];
return [
secondSeparator,
secondPick,
firstSeparator,
firstPick,
secondSeparator, // repeated as it applies to the following picks
...picks.slice(secondIndex + 1),
];
}
}
}

private doGetPicks(
filter: string,
options: AnythingQuickAccessProviderRunOptions & { enableEditorSymbolSearch: boolean },
Expand Down