diff --git a/extension.ts b/extension.ts index 46d0e08..4b76e1c 100644 --- a/extension.ts +++ b/extension.ts @@ -148,8 +148,9 @@ const lookupRecentItems = ( */ const recentItemMatchesTerms = ( item: RecentItem, - terms: ReadonlyArray + terms: ReadonlyArray | null ): boolean => + !!terms && terms.every(t => item.name.includes(t) || item.readablePath.includes(t)); /** @@ -158,11 +159,11 @@ const recentItemMatchesTerms = ( * @param items Recent items * @param terms Terms to look for * @param kinds Item kinds to filter by - * @returns The IDs of all matching items + * @returns The IDs of all matching items, or an empty array if terms is empty */ const findMatchingItems = ( items: ReadonlyArray, - terms: ReadonlyArray, + terms: ReadonlyArray | null, kinds: ReadonlyArray ): string[] => items @@ -187,6 +188,43 @@ const getEnabledKinds = ( return kinds; }; +/** + * Get the search prefix set in the given settings. + */ +const getPrefix = (settings: imports.gi.Gio.GSettings): string => + settings.get_string("search-prefix"); + +/** + * Check search terms against a user-specified search prefix. + * + * `terms` match the given `prefix` if the prefix is empty or if the first term + * starts with `prefix`. If terms match the prefix return `terms` with `prefix` + * removed from the first term, otherwise return `null`. + * + * @param terms + * @param prefix + * @returns `terms` without `prefix`, or null if terms didn't match prefix + */ +const checkAndRemovePrefix = ( + terms: ReadonlyArray, + prefix: string | null | undefined +): ReadonlyArray | null => { + if (!prefix) { + // There's no prefix so just return the terms unchanged + return terms; + } else { + if (0 < terms.length && terms[0].startsWith(prefix)) { + // The prefix matches, so remove it from the first term + const head = terms[0].substring(prefix.length); + const tail = terms.slice(1); + return head ? [head, ...tail] : tail; + } else { + // Prefix doesn't match + return null; + } + } +}; + /** * Create a function to turn a recent item into a search provider result meta object. * @@ -233,7 +271,7 @@ const createProvider = ( callback( findMatchingItems( Array.from(items.values()), - terms, + checkAndRemovePrefix(terms, getPrefix(settings)), getEnabledKinds(settings) ) ), @@ -241,7 +279,7 @@ const createProvider = ( callback( findMatchingItems( lookupRecentItems(items, current), - terms, + checkAndRemovePrefix(terms, getPrefix(settings)), getEnabledKinds(settings) ) ), diff --git a/prefs.js b/prefs.js index bf6a47d..9a34618 100644 --- a/prefs.js +++ b/prefs.js @@ -55,6 +55,12 @@ function buildPrefsWidget() { "active", Gio.SettingsBindFlags.DEFAULT ); + settings.bind( + "search-prefix", + buildable.get_object("search_prefix"), + "text", + Gio.SettingsBindFlags.DEFAULT + ); box.show_all(); return box; diff --git a/prefs.xml b/prefs.xml index 6c1c7b4..895446a 100644 --- a/prefs.xml +++ b/prefs.xml @@ -1,82 +1,207 @@ - + + + - true + False - 18 - 18 - false + False vertical + 18 - + + False + vertical 6 + False 12 + False + start + True Search results - true - 1 - + + + False + True + 0 + + False + start + False Version info placeholder - false - 1 + + False + True + 1 + + + False + True + 0 + - 12 + False + 12 12 - Show workspaces - true - 1 + False + start + True + Show workspaces + + False + True + 0 + - true + False + True + + False + True + 1 + + + False + True + 1 + - 12 + False + 12 12 - Show files - true - 1 + False + start + True + Show files + + False + True + 0 + - true + False + True + + False + True + 1 + + + False + True + 2 + + + + + False + 12 + 12 + + + False + start + True + Search prefix + + + False + True + 0 + + + + + True + True + search prefix + + + False + True + 1 + + + + + False + True + 3 + + + 0 + 0 + + False Basic settings + + False + diff --git a/schemas/org.gnome.shell.extensions.vscode-search-provider.gschema.xml b/schemas/org.gnome.shell.extensions.vscode-search-provider.gschema.xml index f6c46aa..4b575ec 100644 --- a/schemas/org.gnome.shell.extensions.vscode-search-provider.gschema.xml +++ b/schemas/org.gnome.shell.extensions.vscode-search-provider.gschema.xml @@ -11,5 +11,10 @@ Display files in search results Whether to include files that have been opened in VSCode in the search results provided + + "" + Search prefix + Search prefix used to limit results to VSCode search provider +