Skip to content

Commit 4a5e48d

Browse files
authored
Merge pull request microsoft#43393 from shobhitchittora/keyBindingsEditor-filter-by-source
Allow to filter keyBindings by 'Source'.
2 parents 366c8eb + 84e2708 commit 4a5e48d

4 files changed

Lines changed: 80 additions & 1 deletion

File tree

src/vs/workbench/parts/preferences/browser/keybindingsEditor.ts

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ import { SearchWidget } from 'vs/workbench/parts/preferences/browser/preferences
2727
import { DefineKeybindingWidget } from 'vs/workbench/parts/preferences/browser/keybindingWidgets';
2828
import {
2929
IPreferencesService, IKeybindingsEditor, CONTEXT_KEYBINDING_FOCUS, CONTEXT_KEYBINDINGS_EDITOR, CONTEXT_KEYBINDINGS_SEARCH_FOCUS, KEYBINDINGS_EDITOR_COMMAND_REMOVE, KEYBINDINGS_EDITOR_COMMAND_COPY,
30-
KEYBINDINGS_EDITOR_COMMAND_RESET, KEYBINDINGS_EDITOR_COMMAND_COPY_COMMAND, KEYBINDINGS_EDITOR_COMMAND_DEFINE, KEYBINDINGS_EDITOR_COMMAND_SHOW_SIMILAR
30+
KEYBINDINGS_EDITOR_COMMAND_RESET, KEYBINDINGS_EDITOR_COMMAND_COPY_COMMAND, KEYBINDINGS_EDITOR_COMMAND_DEFINE, KEYBINDINGS_EDITOR_COMMAND_SHOW_SIMILAR, KEYBINDINGS_EDITOR_SHOW_DEFAULT_KEYBINDINGS,
31+
KEYBINDINGS_EDITOR_SHOW_USER_KEYBINDINGS
3132
} from 'vs/workbench/parts/preferences/common/preferences';
3233
import { IContextMenuService } from 'vs/platform/contextview/browser/contextView';
3334
import { IKeybindingEditingService } from 'vs/workbench/services/keybinding/common/keybindingEditing';
@@ -176,6 +177,29 @@ export class KeybindingsEditor extends BaseEditor implements IKeybindingsEditor
176177
return focusedElement && focusedElement.templateId === KEYBINDING_ENTRY_TEMPLATE_ID ? <IKeybindingItemEntry>focusedElement : null;
177178
}
178179

180+
getSecondaryActions(): IAction[] {
181+
return <IAction[]>[
182+
<IAction>{
183+
label: localize('showDefaultKeybindings', "Show Default Keybindings"),
184+
enabled: true,
185+
id: KEYBINDINGS_EDITOR_SHOW_DEFAULT_KEYBINDINGS,
186+
run: (): TPromise<any> => {
187+
this.searchWidget.setValue('@source: default');
188+
return TPromise.as(null);
189+
}
190+
},
191+
<IAction>{
192+
label: localize('showUserKeybindings', "Show User Keybindings"),
193+
enabled: true,
194+
id: KEYBINDINGS_EDITOR_SHOW_USER_KEYBINDINGS,
195+
run: (): TPromise<any> => {
196+
this.searchWidget.setValue('@source: user');
197+
return TPromise.as(null);
198+
}
199+
}
200+
];
201+
}
202+
179203
defineKeybinding(keybindingEntry: IKeybindingItemEntry): TPromise<any> {
180204
this.selectEntry(keybindingEntry);
181205
this.showOverlayContainer();

src/vs/workbench/parts/preferences/common/keybindingsEditorModel.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,37 @@ export class KeybindingsEditorModel extends EditorModel {
107107
return keybindingItems.map(keybindingItem => ({ id: KeybindingsEditorModel.getId(keybindingItem), keybindingItem, templateId: KEYBINDING_ENTRY_TEMPLATE_ID }));
108108
}
109109

110+
if (this.isSourceFilterApplied(searchValue)) {
111+
return this.filterBySource(keybindingItems, this.getSourceFilterValue(searchValue), completeMatch);
112+
}
113+
return this.filterByText(keybindingItems, searchValue, completeMatch);
114+
}
115+
116+
private isSourceFilterApplied(searchValue: string): boolean {
117+
return /^@source:/i.test(searchValue);
118+
}
119+
120+
private getSourceFilterValue(searchValue: string): string {
121+
return searchValue.split('@source:')[1].trim() || '';
122+
}
123+
124+
private matchSource(itemSource: string, searchValue: string): boolean {
125+
return itemSource.toLowerCase() === searchValue.toLowerCase();
126+
}
127+
128+
private filterBySource(keybindingItems: IKeybindingItem[], searchValue: string, completeMatch: boolean): IKeybindingItemEntry[] {
129+
return <IKeybindingItemEntry[]>keybindingItems
130+
.filter((keybindingItem: IKeybindingItem) => this.matchSource(keybindingItem.source, searchValue))
131+
.map((keybindingItem: IKeybindingItem) => (
132+
{
133+
id: KeybindingsEditorModel.getId(keybindingItem),
134+
templateId: KEYBINDING_ENTRY_TEMPLATE_ID,
135+
keybindingItem,
136+
}
137+
));
138+
}
139+
140+
private filterByText(keybindingItems: IKeybindingItem[], searchValue: string, completeMatch: boolean): IKeybindingItemEntry[] {
110141
const result: IKeybindingItemEntry[] = [];
111142
const words = searchValue.split(' ');
112143
const keybindingWords = this.splitKeybindingWords(words);

src/vs/workbench/parts/preferences/common/preferences.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,8 @@ export const KEYBINDINGS_EDITOR_COMMAND_COPY = 'keybindings.editor.copyKeybindin
230230
export const KEYBINDINGS_EDITOR_COMMAND_COPY_COMMAND = 'keybindings.editor.copyCommandKeybindingEntry';
231231
export const KEYBINDINGS_EDITOR_COMMAND_SHOW_SIMILAR = 'keybindings.editor.showConflicts';
232232
export const KEYBINDINGS_EDITOR_COMMAND_FOCUS_KEYBINDINGS = 'keybindings.editor.focusKeybindings';
233+
export const KEYBINDINGS_EDITOR_SHOW_DEFAULT_KEYBINDINGS = 'keybindings.editor.showDefaultKeybindings';
234+
export const KEYBINDINGS_EDITOR_SHOW_USER_KEYBINDINGS = 'keybindings.editor.showUserKeybindings';
233235

234236
export const FOLDER_SETTINGS_PATH = join('.vscode', 'settings.json');
235237
export const DEFAULT_SETTINGS_EDITOR_SETTING = 'workbench.settings.openDefaultSettings';

src/vs/workbench/parts/preferences/test/common/keybindingsEditorModel.test.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,28 @@ suite('Keybindings Editor Model test', () => {
258258
});
259259
});
260260

261+
test('filter by default source with "@source: " prefix', () => {
262+
const command = 'a' + uuid.generateUuid();
263+
const expected = aResolvedKeybindingItem({ command, firstPart: { keyCode: KeyCode.Escape }, when: 'context1 && context2', isDefault: true });
264+
prepareKeybindingService(expected);
265+
266+
return testObject.resolve({}).then(() => {
267+
const actual = testObject.fetch('@source: default').filter(element => element.keybindingItem.command === command)[0];
268+
assert.ok(actual);
269+
});
270+
});
271+
272+
test('filter by user source with "@source: " prefix', () => {
273+
const command = 'a' + uuid.generateUuid();
274+
const expected = aResolvedKeybindingItem({ command, firstPart: { keyCode: KeyCode.Escape }, when: 'context1 && context2', isDefault: false });
275+
prepareKeybindingService(expected);
276+
277+
return testObject.resolve({}).then(() => {
278+
const actual = testObject.fetch('@source: user').filter(element => element.keybindingItem.command === command)[0];
279+
assert.ok(actual);
280+
});
281+
});
282+
261283
test('filter by when context', () => {
262284
const command = 'a' + uuid.generateUuid();
263285
const expected = aResolvedKeybindingItem({ command, firstPart: { keyCode: KeyCode.Escape }, when: 'whenContext1 && whenContext2', isDefault: false });

0 commit comments

Comments
 (0)