Merge pull request #43393 from shobhitchittora/keyBindingsEditor-filter-by-source

Allow to filter keyBindings by 'Source'.
This commit is contained in:
Sandeep Somavarapu 2018-03-14 10:03:30 +05:30 committed by GitHub
commit 4a5e48de17
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 80 additions and 1 deletions

View file

@ -27,7 +27,8 @@ import { SearchWidget } from 'vs/workbench/parts/preferences/browser/preferences
import { DefineKeybindingWidget } from 'vs/workbench/parts/preferences/browser/keybindingWidgets';
import {
IPreferencesService, IKeybindingsEditor, CONTEXT_KEYBINDING_FOCUS, CONTEXT_KEYBINDINGS_EDITOR, CONTEXT_KEYBINDINGS_SEARCH_FOCUS, KEYBINDINGS_EDITOR_COMMAND_REMOVE, KEYBINDINGS_EDITOR_COMMAND_COPY,
KEYBINDINGS_EDITOR_COMMAND_RESET, KEYBINDINGS_EDITOR_COMMAND_COPY_COMMAND, KEYBINDINGS_EDITOR_COMMAND_DEFINE, KEYBINDINGS_EDITOR_COMMAND_SHOW_SIMILAR
KEYBINDINGS_EDITOR_COMMAND_RESET, KEYBINDINGS_EDITOR_COMMAND_COPY_COMMAND, KEYBINDINGS_EDITOR_COMMAND_DEFINE, KEYBINDINGS_EDITOR_COMMAND_SHOW_SIMILAR, KEYBINDINGS_EDITOR_SHOW_DEFAULT_KEYBINDINGS,
KEYBINDINGS_EDITOR_SHOW_USER_KEYBINDINGS
} from 'vs/workbench/parts/preferences/common/preferences';
import { IContextMenuService } from 'vs/platform/contextview/browser/contextView';
import { IKeybindingEditingService } from 'vs/workbench/services/keybinding/common/keybindingEditing';
@ -176,6 +177,29 @@ export class KeybindingsEditor extends BaseEditor implements IKeybindingsEditor
return focusedElement && focusedElement.templateId === KEYBINDING_ENTRY_TEMPLATE_ID ? <IKeybindingItemEntry>focusedElement : null;
}
getSecondaryActions(): IAction[] {
return <IAction[]>[
<IAction>{
label: localize('showDefaultKeybindings', "Show Default Keybindings"),
enabled: true,
id: KEYBINDINGS_EDITOR_SHOW_DEFAULT_KEYBINDINGS,
run: (): TPromise<any> => {
this.searchWidget.setValue('@source: default');
return TPromise.as(null);
}
},
<IAction>{
label: localize('showUserKeybindings', "Show User Keybindings"),
enabled: true,
id: KEYBINDINGS_EDITOR_SHOW_USER_KEYBINDINGS,
run: (): TPromise<any> => {
this.searchWidget.setValue('@source: user');
return TPromise.as(null);
}
}
];
}
defineKeybinding(keybindingEntry: IKeybindingItemEntry): TPromise<any> {
this.selectEntry(keybindingEntry);
this.showOverlayContainer();

View file

@ -107,6 +107,37 @@ export class KeybindingsEditorModel extends EditorModel {
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() || '';
}
private matchSource(itemSource: string, searchValue: string): boolean {
return itemSource.toLowerCase() === searchValue.toLowerCase();
}
private filterBySource(keybindingItems: IKeybindingItem[], searchValue: string, completeMatch: boolean): IKeybindingItemEntry[] {
return <IKeybindingItemEntry[]>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, completeMatch: boolean): IKeybindingItemEntry[] {
const result: IKeybindingItemEntry[] = [];
const words = searchValue.split(' ');
const keybindingWords = this.splitKeybindingWords(words);

View file

@ -230,6 +230,8 @@ export const KEYBINDINGS_EDITOR_COMMAND_COPY = 'keybindings.editor.copyKeybindin
export const KEYBINDINGS_EDITOR_COMMAND_COPY_COMMAND = 'keybindings.editor.copyCommandKeybindingEntry';
export const KEYBINDINGS_EDITOR_COMMAND_SHOW_SIMILAR = 'keybindings.editor.showConflicts';
export const KEYBINDINGS_EDITOR_COMMAND_FOCUS_KEYBINDINGS = 'keybindings.editor.focusKeybindings';
export const KEYBINDINGS_EDITOR_SHOW_DEFAULT_KEYBINDINGS = 'keybindings.editor.showDefaultKeybindings';
export const KEYBINDINGS_EDITOR_SHOW_USER_KEYBINDINGS = 'keybindings.editor.showUserKeybindings';
export const FOLDER_SETTINGS_PATH = join('.vscode', 'settings.json');
export const DEFAULT_SETTINGS_EDITOR_SETTING = 'workbench.settings.openDefaultSettings';

View file

@ -258,6 +258,28 @@ suite('Keybindings Editor Model test', () => {
});
});
test('filter by default source with "@source: " prefix', () => {
const command = 'a' + uuid.generateUuid();
const expected = aResolvedKeybindingItem({ command, firstPart: { keyCode: KeyCode.Escape }, when: 'context1 && context2', isDefault: true });
prepareKeybindingService(expected);
return testObject.resolve({}).then(() => {
const actual = testObject.fetch('@source: default').filter(element => element.keybindingItem.command === command)[0];
assert.ok(actual);
});
});
test('filter by user source with "@source: " prefix', () => {
const command = 'a' + uuid.generateUuid();
const expected = aResolvedKeybindingItem({ command, firstPart: { keyCode: KeyCode.Escape }, when: 'context1 && context2', isDefault: false });
prepareKeybindingService(expected);
return testObject.resolve({}).then(() => {
const actual = testObject.fetch('@source: user').filter(element => element.keybindingItem.command === command)[0];
assert.ok(actual);
});
});
test('filter by when context', () => {
const command = 'a' + uuid.generateUuid();
const expected = aResolvedKeybindingItem({ command, firstPart: { keyCode: KeyCode.Escape }, when: 'whenContext1 && whenContext2', isDefault: false });