Skip to content

Commit bd7a5c7

Browse files
Refactor methods out to handle -
1. filter By @source: 2. normal text filter 3. @source filter with quotes - @source: "user"
1 parent 303fc92 commit bd7a5c7

1 file changed

Lines changed: 73 additions & 53 deletions

File tree

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

Lines changed: 73 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -90,78 +90,98 @@ export class KeybindingsEditorModel extends EditorModel {
9090

9191
public fetch(searchValue: string, sortByPrecedence: boolean = false): IKeybindingItemEntry[] {
9292
searchValue = searchValue.trim();
93-
const quoteAtFirstChar = searchValue.charAt(0) === '"';
94-
const quoteAtLastChar = searchValue.charAt(searchValue.length - 1) === '"';
93+
94+
if (this.isSourceFilterApplied(searchValue)) {
95+
searchValue = this.getSourceFilterValue(searchValue);
96+
searchValue = this.parseSearchValue(searchValue);
97+
return this.fetchKeybindingItemsBySource(sortByPrecedence ? this._keybindingItemsSortedByPrecedence : this._keybindingItems, searchValue, this.quoteAtFirst(searchValue) && this.quoteAtLast(searchValue));
98+
}
99+
searchValue = this.parseSearchValue(searchValue);
100+
return this.fetchKeybindingItemsByText(sortByPrecedence ? this._keybindingItemsSortedByPrecedence : this._keybindingItems, searchValue, this.quoteAtFirst(searchValue) && this.quoteAtLast(searchValue));
101+
}
102+
103+
private isSourceFilterApplied(searchValue: string): boolean {
104+
return /^@source:/i.test(searchValue);
105+
}
106+
107+
private getSourceFilterValue(searchValue: string): string {
108+
return searchValue.split('@source:')[1].trim() || '';
109+
}
110+
111+
private quoteAtFirst(searchValue: string) {
112+
return searchValue.charAt(0) === '"';
113+
}
114+
115+
private quoteAtLast(searchValue: string) {
116+
return searchValue.charAt(searchValue.length - 1) === '"';
117+
}
118+
119+
private parseSearchValue(searchValue: string) {
120+
const quoteAtFirstChar = this.quoteAtFirst(searchValue);
121+
const quoteAtLastChar = this.quoteAtLast(searchValue);
95122
if (quoteAtFirstChar) {
96123
searchValue = searchValue.substring(1);
97124
}
98125
if (quoteAtLastChar) {
99126
searchValue = searchValue.substring(0, searchValue.length - 1);
100127
}
101-
searchValue = searchValue.trim();
102-
return this.fetchKeybindingItems(sortByPrecedence ? this._keybindingItemsSortedByPrecedence : this._keybindingItems, searchValue, quoteAtFirstChar && quoteAtLastChar);
103-
}
104-
105-
isSourceFilterApplied(words: string[]): boolean {
106-
return (words.length) && (words[0].trim().toLowerCase() === '@source:');
128+
return searchValue.trim();
107129
}
108130

109-
getSourceFilterValue(words: string[]): string {
110-
return words[1] || '';
111-
}
112-
113-
private fetchKeybindingItems(keybindingItems: IKeybindingItem[], searchValue: string, completeMatch: boolean): IKeybindingItemEntry[] {
131+
private fetchKeybindingItemsBySource(keybindingItems: IKeybindingItem[], searchValue: string, completeMatch: boolean): IKeybindingItemEntry[] {
114132
if (!searchValue) {
115133
return keybindingItems.map(keybindingItem => ({ id: KeybindingsEditorModel.getId(keybindingItem), keybindingItem, templateId: KEYBINDING_ENTRY_TEMPLATE_ID }));
116134
}
117135

118136
const result: IKeybindingItemEntry[] = [];
119137
const words = searchValue.split(' ');
138+
const keybindingWords = this.splitKeybindingWords(words);
139+
for (const keybindingItem of keybindingItems) {
140+
let keybindingMatches = new KeybindingItemMatches(this.modifierLabels, keybindingItem, searchValue, words, keybindingWords, completeMatch);
141+
if (keybindingMatches.sourceMatches) {
142+
result.push({
143+
id: KeybindingsEditorModel.getId(keybindingItem),
144+
templateId: KEYBINDING_ENTRY_TEMPLATE_ID,
145+
commandLabelMatches: keybindingMatches.commandLabelMatches,
146+
commandDefaultLabelMatches: keybindingMatches.commandDefaultLabelMatches,
147+
keybindingItem,
148+
keybindingMatches: keybindingMatches.keybindingMatches,
149+
commandIdMatches: keybindingMatches.commandIdMatches,
150+
sourceMatches: keybindingMatches.sourceMatches,
151+
whenMatches: keybindingMatches.whenMatches
152+
});
153+
}
154+
}
155+
return result;
156+
}
120157

121-
const sourceFilterApplied: boolean = this.isSourceFilterApplied(words);
122-
let sourceFilterSearchValue: string;
123-
if (sourceFilterApplied) {
124-
sourceFilterSearchValue = this.getSourceFilterValue(words);
158+
private fetchKeybindingItemsByText(keybindingItems: IKeybindingItem[], searchValue: string, completeMatch: boolean): IKeybindingItemEntry[] {
159+
if (!searchValue) {
160+
return keybindingItems.map(keybindingItem => ({ id: KeybindingsEditorModel.getId(keybindingItem), keybindingItem, templateId: KEYBINDING_ENTRY_TEMPLATE_ID }));
125161
}
126162

163+
const result: IKeybindingItemEntry[] = [];
164+
const words = searchValue.split(' ');
127165
const keybindingWords = this.splitKeybindingWords(words);
128166
for (const keybindingItem of keybindingItems) {
129-
if (sourceFilterApplied) {
130-
let keybindingMatches = new KeybindingItemMatches(this.modifierLabels, keybindingItem, sourceFilterSearchValue, words, keybindingWords, completeMatch);
131-
if (keybindingMatches.sourceMatches) {
132-
result.push({
133-
id: KeybindingsEditorModel.getId(keybindingItem),
134-
templateId: KEYBINDING_ENTRY_TEMPLATE_ID,
135-
commandLabelMatches: keybindingMatches.commandLabelMatches,
136-
commandDefaultLabelMatches: keybindingMatches.commandDefaultLabelMatches,
137-
keybindingItem,
138-
keybindingMatches: keybindingMatches.keybindingMatches,
139-
commandIdMatches: keybindingMatches.commandIdMatches,
140-
sourceMatches: keybindingMatches.sourceMatches,
141-
whenMatches: keybindingMatches.whenMatches
142-
});
143-
}
144-
}
145-
else {
146-
let keybindingMatches = new KeybindingItemMatches(this.modifierLabels, keybindingItem, searchValue, words, keybindingWords, completeMatch);
147-
if (keybindingMatches.commandIdMatches
148-
|| keybindingMatches.commandLabelMatches
149-
|| keybindingMatches.commandDefaultLabelMatches
150-
|| keybindingMatches.sourceMatches
151-
|| keybindingMatches.whenMatches
152-
|| keybindingMatches.keybindingMatches) {
153-
result.push({
154-
id: KeybindingsEditorModel.getId(keybindingItem),
155-
templateId: KEYBINDING_ENTRY_TEMPLATE_ID,
156-
commandLabelMatches: keybindingMatches.commandLabelMatches,
157-
commandDefaultLabelMatches: keybindingMatches.commandDefaultLabelMatches,
158-
keybindingItem,
159-
keybindingMatches: keybindingMatches.keybindingMatches,
160-
commandIdMatches: keybindingMatches.commandIdMatches,
161-
sourceMatches: keybindingMatches.sourceMatches,
162-
whenMatches: keybindingMatches.whenMatches
163-
});
164-
}
167+
let keybindingMatches = new KeybindingItemMatches(this.modifierLabels, keybindingItem, searchValue, words, keybindingWords, completeMatch);
168+
if (keybindingMatches.commandIdMatches
169+
|| keybindingMatches.commandLabelMatches
170+
|| keybindingMatches.commandDefaultLabelMatches
171+
|| keybindingMatches.sourceMatches
172+
|| keybindingMatches.whenMatches
173+
|| keybindingMatches.keybindingMatches) {
174+
result.push({
175+
id: KeybindingsEditorModel.getId(keybindingItem),
176+
templateId: KEYBINDING_ENTRY_TEMPLATE_ID,
177+
commandLabelMatches: keybindingMatches.commandLabelMatches,
178+
commandDefaultLabelMatches: keybindingMatches.commandDefaultLabelMatches,
179+
keybindingItem,
180+
keybindingMatches: keybindingMatches.keybindingMatches,
181+
commandIdMatches: keybindingMatches.commandIdMatches,
182+
sourceMatches: keybindingMatches.sourceMatches,
183+
whenMatches: keybindingMatches.whenMatches
184+
});
165185
}
166186
}
167187
return result;

0 commit comments

Comments
 (0)