diff --git a/src/vs/workbench/parts/preferences/browser/keybindingsEditor.ts b/src/vs/workbench/parts/preferences/browser/keybindingsEditor.ts index b6719642b21ad8953f00d6f9aeb0c133b311fe91..a19ef06edf63403852779626abd8ec6d2acbf17b 100644 --- a/src/vs/workbench/parts/preferences/browser/keybindingsEditor.ts +++ b/src/vs/workbench/parts/preferences/browser/keybindingsEditor.ts @@ -184,7 +184,7 @@ export class KeybindingsEditor extends BaseEditor implements IKeybindingsEditor enabled: true, id: KEYBINDINGS_EDITOR_SHOW_DEFAULT_KEYBINDINGS, run: (): TPromise => { - this.searchWidget.setValue('@source: default'); + this.searchWidget.setValue('@source:default'); return TPromise.as(null); } }, @@ -193,7 +193,7 @@ export class KeybindingsEditor extends BaseEditor implements IKeybindingsEditor enabled: true, id: KEYBINDINGS_EDITOR_SHOW_USER_KEYBINDINGS, run: (): TPromise => { - this.searchWidget.setValue('@source: user'); + this.searchWidget.setValue('@source:user'); return TPromise.as(null); } } diff --git a/src/vs/workbench/parts/preferences/common/keybindingsEditorModel.ts b/src/vs/workbench/parts/preferences/common/keybindingsEditorModel.ts index b38b5867e43b9b041d128bc1f37cad70f2806cb0..3e4a2a07105bff32a70c6dcb507d7323ddfeee9d 100644 --- a/src/vs/workbench/parts/preferences/common/keybindingsEditorModel.ts +++ b/src/vs/workbench/parts/preferences/common/keybindingsEditorModel.ts @@ -23,6 +23,9 @@ import { KeybindingResolver } from 'vs/platform/keybinding/common/keybindingReso export const KEYBINDING_ENTRY_TEMPLATE_ID = 'keybinding.entry.template'; export const KEYBINDING_HEADER_TEMPLATE_ID = 'keybinding.header.template'; +const SOURCE_DEFAULT = localize('default', "Default"); +const SOURCE_USER = localize('user', "User"); + export interface KeybindingMatch { ctrlKey?: boolean; shiftKey?: boolean; @@ -89,55 +92,43 @@ export class KeybindingsEditorModel extends EditorModel { } public fetch(searchValue: string, sortByPrecedence: boolean = false): IKeybindingItemEntry[] { - searchValue = searchValue.trim(); - const quoteAtFirstChar = searchValue.charAt(0) === '"'; - const quoteAtLastChar = searchValue.charAt(searchValue.length - 1) === '"'; - if (quoteAtFirstChar) { - searchValue = searchValue.substring(1); - } - if (quoteAtLastChar) { - searchValue = searchValue.substring(0, searchValue.length - 1); + let keybindingItems = sortByPrecedence ? this._keybindingItemsSortedByPrecedence : this._keybindingItems; + + if (/@source:\s*(user|default)/i.test(searchValue)) { + keybindingItems = this.filterBySource(keybindingItems, searchValue); + searchValue = searchValue.replace(/@source:\s*(user|default)/i, ''); } - searchValue = searchValue.trim(); - return this.fetchKeybindingItems(sortByPrecedence ? this._keybindingItemsSortedByPrecedence : this._keybindingItems, searchValue, quoteAtFirstChar && quoteAtLastChar); - } - private fetchKeybindingItems(keybindingItems: IKeybindingItem[], searchValue: string, completeMatch: boolean): IKeybindingItemEntry[] { + searchValue = searchValue.trim(); if (!searchValue) { return keybindingItems.map(keybindingItem => ({ id: KeybindingsEditorModel.getId(keybindingItem), keybindingItem, templateId: KEYBINDING_ENTRY_TEMPLATE_ID })); } - if (this.isSourceFilterApplied(searchValue)) { - return this.filterBySource(keybindingItems, this.getSourceFilterValue(searchValue), completeMatch); - } - return this.filterByText(keybindingItems, searchValue, completeMatch); - } - - private isSourceFilterApplied(searchValue: string): boolean { - return /^@source:/i.test(searchValue); - } - - private getSourceFilterValue(searchValue: string): string { - return searchValue.split('@source:')[1].trim() || ''; + return this.filterByText(keybindingItems, searchValue); } - private matchSource(itemSource: string, searchValue: string): boolean { - return itemSource.toLowerCase() === searchValue.toLowerCase(); + private filterBySource(keybindingItems: IKeybindingItem[], searchValue: string): IKeybindingItem[] { + if (/@source:\s*default/i.test(searchValue)) { + return keybindingItems.filter(k => k.source === SOURCE_DEFAULT); + } + if (/@source:\s*user/i.test(searchValue)) { + return keybindingItems.filter(k => k.source === SOURCE_USER); + } + return keybindingItems; } - private filterBySource(keybindingItems: IKeybindingItem[], searchValue: string, completeMatch: boolean): IKeybindingItemEntry[] { - return keybindingItems - .filter((keybindingItem: IKeybindingItem) => this.matchSource(keybindingItem.source, searchValue)) - .map((keybindingItem: IKeybindingItem) => ( - { - id: KeybindingsEditorModel.getId(keybindingItem), - templateId: KEYBINDING_ENTRY_TEMPLATE_ID, - keybindingItem, - } - )); - } + private filterByText(keybindingItems: IKeybindingItem[], searchValue: string): IKeybindingItemEntry[] { + const quoteAtFirstChar = searchValue.charAt(0) === '"'; + const quoteAtLastChar = searchValue.charAt(searchValue.length - 1) === '"'; + const completeMatch = quoteAtFirstChar && quoteAtLastChar; + if (quoteAtFirstChar) { + searchValue = searchValue.substring(1); + } + if (quoteAtLastChar) { + searchValue = searchValue.substring(0, searchValue.length - 1); + } + searchValue = searchValue.trim(); - private filterByText(keybindingItems: IKeybindingItem[], searchValue: string, completeMatch: boolean): IKeybindingItemEntry[] { const result: IKeybindingItemEntry[] = []; const words = searchValue.split(' '); const keybindingWords = this.splitKeybindingWords(words); @@ -235,7 +226,7 @@ export class KeybindingsEditorModel extends EditorModel { commandLabel: KeybindingsEditorModel.getCommandLabel(menuCommand, editorActionLabel), commandDefaultLabel: KeybindingsEditorModel.getCommandDefaultLabel(menuCommand, workbenchActionsRegistry), when: keybindingItem.when ? keybindingItem.when.serialize() : '', - source: keybindingItem.isDefault ? localize('default', "Default") : localize('user', "User") + source: keybindingItem.isDefault ? SOURCE_DEFAULT : SOURCE_USER }; }