From 8b834d0f890f62ce25684f6b371559b4f4f6a223 Mon Sep 17 00:00:00 2001 From: Benjamin Pasero Date: Wed, 4 Jan 2017 11:01:22 +0100 Subject: [PATCH] Search relevance in Quick Pick control (fixes #18003) --- .../parts/quickopen/browser/quickOpenModel.ts | 10 +++++----- .../parts/quickopen/quickOpenController.ts | 18 ++++++++++++++++-- .../quickopen/browser/gotoSymbolHandler.ts | 8 ++++---- 3 files changed, 25 insertions(+), 11 deletions(-) diff --git a/src/vs/base/parts/quickopen/browser/quickOpenModel.ts b/src/vs/base/parts/quickopen/browser/quickOpenModel.ts index 18e6deabcbefe..4c36e669bd36f 100644 --- a/src/vs/base/parts/quickopen/browser/quickOpenModel.ts +++ b/src/vs/base/parts/quickopen/browser/quickOpenModel.ts @@ -689,15 +689,15 @@ export class QuickOpenModel implements return this._entries; } - getId(entry: QuickOpenEntry): string { + public getId(entry: QuickOpenEntry): string { return entry.getId(); } - getLabel(entry: QuickOpenEntry): string { + public getLabel(entry: QuickOpenEntry): string { return entry.getLabel(); } - getAriaLabel(entry: QuickOpenEntry): string { + public getAriaLabel(entry: QuickOpenEntry): string { const ariaLabel = entry.getAriaLabel(); if (ariaLabel) { return nls.localize('quickOpenAriaLabelEntry', "{0}, picker", entry.getAriaLabel()); @@ -706,11 +706,11 @@ export class QuickOpenModel implements return nls.localize('quickOpenAriaLabel', "picker"); } - isVisible(entry: QuickOpenEntry): boolean { + public isVisible(entry: QuickOpenEntry): boolean { return !entry.isHidden(); } - run(entry: QuickOpenEntry, mode: Mode, context: IContext): boolean { + public run(entry: QuickOpenEntry, mode: Mode, context: IContext): boolean { return entry.run(mode, context); } } diff --git a/src/vs/workbench/browser/parts/quickopen/quickOpenController.ts b/src/vs/workbench/browser/parts/quickopen/quickOpenController.ts index c0ff71dd522fd..c99ab5c20ac68 100644 --- a/src/vs/workbench/browser/parts/quickopen/quickOpenController.ts +++ b/src/vs/workbench/browser/parts/quickopen/quickOpenController.ts @@ -342,9 +342,9 @@ export class QuickOpenController extends WorkbenchComponent implements IQuickOpe // Model const model = new QuickOpenModel(); - const entries = picks.map(e => this.instantiationService.createInstance(PickOpenEntry, e, () => progress(e))); + const entries = picks.map((e, index) => this.instantiationService.createInstance(PickOpenEntry, e, index, () => progress(e))); if (picks.length === 0) { - entries.push(this.instantiationService.createInstance(PickOpenEntry, { label: nls.localize('emptyPicks', "There are no entries to pick from") }, null)); + entries.push(this.instantiationService.createInstance(PickOpenEntry, { label: nls.localize('emptyPicks', "There are no entries to pick from") }, 0, null)); } model.setEntries(entries); @@ -414,6 +414,15 @@ export class QuickOpenController extends WorkbenchComponent implements IQuickOpe }); } + // Sort by value + model.entries.sort((pickA: PickOpenEntry, pickB: PickOpenEntry) => { + if (!value) { + return pickA.index - pickB.index; // restore natural order + } + + return QuickOpenEntry.compare(pickA, pickB, value); + }); + this.pickOpenWidget.refresh(model, value ? { autoFocusFirstEntry: true } : autoFocus); }, onShow: () => this.handleOnShow(true), @@ -1001,6 +1010,7 @@ class PickOpenEntry extends PlaceholderQuickOpenEntry { constructor( item: IPickOpenEntry, + private _index: number, private onPreview: () => void, @IModeService private modeService: IModeService, @IModelService private modelService: IModelService @@ -1018,6 +1028,10 @@ class PickOpenEntry extends PlaceholderQuickOpenEntry { this.isFolder = fileItem.isFolder; } + public get index(): number { + return this._index; + } + public getLabelOptions(): IIconLabelOptions { return { extraClasses: this.resource ? getIconClasses(this.modelService, this.modeService, this.resource, this.isFolder) : [] diff --git a/src/vs/workbench/parts/quickopen/browser/gotoSymbolHandler.ts b/src/vs/workbench/parts/quickopen/browser/gotoSymbolHandler.ts index 501d38dedd4e8..18eabe4b04d61 100644 --- a/src/vs/workbench/parts/quickopen/browser/gotoSymbolHandler.ts +++ b/src/vs/workbench/parts/quickopen/browser/gotoSymbolHandler.ts @@ -46,7 +46,7 @@ class OutlineModel extends QuickOpenModel { this.outline = outline; } - public dofilter(searchValue: string): void { + public applyFilter(searchValue: string): void { // Normalize search let normalizedSearchValue = searchValue; @@ -389,10 +389,10 @@ export class GotoSymbolHandler extends QuickOpenHandler { } // Resolve Outline Model - return this.getActiveOutline().then((outline) => { + return this.getActiveOutline().then(outline => { // Filter by search - outline.dofilter(searchValue); + outline.applyFilter(searchValue); return outline; }); @@ -548,7 +548,7 @@ export class GotoSymbolHandler extends QuickOpenHandler { public clearDecorations(): void { if (this.rangeHighlightDecorationId) { - this.editorService.getVisibleEditors().forEach((editor) => { + this.editorService.getVisibleEditors().forEach(editor => { if (editor.position === this.rangeHighlightDecorationId.position) { const editorControl = editor.getControl(); editorControl.changeDecorations((changeAccessor: IModelDecorationsChangeAccessor) => {