Skip to content

Commit

Permalink
Merge pull request #25 from Jomik/search-prefix
Browse files Browse the repository at this point in the history
Add search prefix back again
  • Loading branch information
Jomik authored Nov 25, 2019
2 parents 4d798a1 + 6379f0f commit 0841731
Show file tree
Hide file tree
Showing 4 changed files with 200 additions and 26 deletions.
48 changes: 43 additions & 5 deletions extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,9 @@ const lookupRecentItems = (
*/
const recentItemMatchesTerms = (
item: RecentItem,
terms: ReadonlyArray<string>
terms: ReadonlyArray<string> | null
): boolean =>
!!terms &&
terms.every(t => item.name.includes(t) || item.readablePath.includes(t));

/**
Expand All @@ -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<RecentItem>,
terms: ReadonlyArray<string>,
terms: ReadonlyArray<string> | null,
kinds: ReadonlyArray<RecentItemKind>
): string[] =>
items
Expand All @@ -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<string>,
prefix: string | null | undefined
): ReadonlyArray<string> | 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.
*
Expand Down Expand Up @@ -233,15 +271,15 @@ const createProvider = (
callback(
findMatchingItems(
Array.from(items.values()),
terms,
checkAndRemovePrefix(terms, getPrefix(settings)),
getEnabledKinds(settings)
)
),
getSubsearchResultSet: (current, terms, callback): void =>
callback(
findMatchingItems(
lookupRecentItems(items, current),
terms,
checkAndRemovePrefix(terms, getPrefix(settings)),
getEnabledKinds(settings)
)
),
Expand Down
6 changes: 6 additions & 0 deletions prefs.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
167 changes: 146 additions & 21 deletions prefs.xml
Original file line number Diff line number Diff line change
@@ -1,82 +1,207 @@
<?xml version="1.0"?>
<?xml version="1.0" encoding="UTF-8"?>
<!--
The MIT License (MIT)
Copyright (c) 2019 Sebastian Wiesner <[email protected]>
Copyright (c) 2017 Jonas Damtoft
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
-->
<interface domain="vscode-search-provider">
<requires lib="gtk+" version="3.20"/>
<object class="GtkNotebook" id="prefs_widget">
<property name="expand">true</property>
<property name="can_focus">False</property>
<child>
<object class="GtkGrid">
<property name="margin">18</property>
<property name="row-spacing">18</property>
<property name="row-homogeneous">false</property>
<property name="can_focus">False</property>
<property name="orientation">vertical</property>
<property name="row_spacing">18</property>
<child>
<object class="GtkVBox">
<object class="GtkBox">
<property name="can_focus">False</property>
<property name="orientation">vertical</property>
<property name="spacing">6</property>
<child>
<object class="GtkBox">
<property name="can_focus">False</property>
<property name="spacing">12</property>
<child>
<object class="GtkLabel">
<property name="can_focus">False</property>
<property name="halign">start</property>
<property name="hexpand">True</property>
<property name="label" translatable="yes">Search results</property>
<property name="hexpand">true</property>
<property name="halign">1</property>
<attributes>
<attribute name="weight" value="PANGO_WEIGHT_BOLD"/>
<attribute name="weight" value="bold"/>
</attributes>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">0</property>
</packing>
</child>
<child>
<object class="GtkLabel" id="version_info">
<property name="can_focus">False</property>
<property name="halign">start</property>
<property name="hexpand">False</property>
<property name="label">Version info placeholder</property>
<property name="hexpand">false</property>
<property name="halign">1</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">1</property>
</packing>
</child>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">0</property>
</packing>
</child>
<child>
<object class="GtkBox">
<property name="margin-left">12</property>
<property name="can_focus">False</property>
<property name="margin_left">12</property>
<property name="spacing">12</property>
<child>
<object class="GtkLabel">
<property translatable="no" name="label">Show workspaces</property>
<property name="hexpand">true</property>
<property name="halign">1</property>
<property name="can_focus">False</property>
<property name="halign">start</property>
<property name="hexpand">True</property>
<property name="label">Show workspaces</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">0</property>
</packing>
</child>
<child>
<object class="GtkSwitch" id="field_workspaces">
<property name="active">true</property>
<property name="can_focus">False</property>
<property name="active">True</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">1</property>
</packing>
</child>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">1</property>
</packing>
</child>
<child>
<object class="GtkBox">
<property name="margin-left">12</property>
<property name="can_focus">False</property>
<property name="margin_left">12</property>
<property name="spacing">12</property>
<child>
<object class="GtkLabel">
<property translatable="no" name="label">Show files</property>
<property name="hexpand">true</property>
<property name="halign">1</property>
<property name="can_focus">False</property>
<property name="halign">start</property>
<property name="hexpand">True</property>
<property name="label">Show files</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">0</property>
</packing>
</child>
<child>
<object class="GtkSwitch" id="field_files">
<property name="active">true</property>
<property name="can_focus">False</property>
<property name="active">True</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">1</property>
</packing>
</child>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">2</property>
</packing>
</child>
<child>
<object class="GtkBox">
<property name="can_focus">False</property>
<property name="margin_left">12</property>
<property name="spacing">12</property>
<child>
<object class="GtkLabel">
<property name="can_focus">False</property>
<property name="halign">start</property>
<property name="hexpand">True</property>
<property name="label">Search prefix</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">0</property>
</packing>
</child>
<child>
<object class="GtkEntry" id="search_prefix">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="placeholder_text">search prefix</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">1</property>
</packing>
</child>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">3</property>
</packing>
</child>
</object>
<packing>
<property name="left_attach">0</property>
<property name="top_attach">0</property>
</packing>
</child>
</object>
</child>
<child type="tab">
<object class="GtkLabel">
<property name="can_focus">False</property>
<property name="label" translatable="yes">Basic settings</property>
</object>
<packing>
<property name="tab_fill">False</property>
</packing>
</child>
</object>
</interface>
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,10 @@
<summary>Display files in search results</summary>
<description>Whether to include files that have been opened in VSCode in the search results provided</description>
</key>
<key name="search-prefix" type="s">
<default>""</default>
<summary>Search prefix</summary>
<description>Search prefix used to limit results to VSCode search provider</description>
</key>
</schema>
</schemalist>

0 comments on commit 0841731

Please sign in to comment.