Skip to content

Commit f4d0359

Browse files
jeremypwdanirabbitzeebok
authored
Improve fuzzy find result order (#1655)
* Do not rewrite opened-folders setting while restoring * Do not overwrite settings while restoring * Set sort func and current project on popup; Move stuff into construct clause * Show closest matches first --------- Co-authored-by: Danielle Foré <danielle@elementary.io> Co-authored-by: Ryan Kornheisl <ryan@skarva.tech>
1 parent 907b56a commit f4d0359

3 files changed

Lines changed: 59 additions & 28 deletions

File tree

plugins/fuzzy-search/file-item.vala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,14 @@
77
*/
88

99
public class FileItem : Gtk.ListBoxRow {
10-
private SearchResult result;
10+
public SearchResult result { get; private set; }
1111

1212
public string filepath {
1313
get {
1414
return result.full_path;
1515
}
1616
}
17+
1718
public FileItem (SearchResult res, bool should_distinguish_project = false) {
1819
this.get_style_context ().add_class ("fuzzy-item");
1920
this.get_style_context ().add_class ("flat");

plugins/fuzzy-search/fuzzy-search-popover.vala

Lines changed: 47 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -19,37 +19,19 @@ public class Scratch.FuzzySearchPopover : Gtk.Popover {
1919
private Gee.LinkedList<GLib.Cancellable> cancellables;
2020
private Gtk.EventControllerKey search_term_entry_key_controller;
2121
private Gtk.Label title_label;
22+
private string current_doc_project;
2223
public Scratch.MainWindow current_window { get; construct; }
24+
public Scratch.Services.FuzzySearchIndexer search_indexer { get; construct; }
2325
public bool sidebar_is_visible { get; set; }
2426

2527
public signal void open_file (string filepath);
2628
public signal void close_search ();
2729

2830
public FuzzySearchPopover (Scratch.Services.FuzzySearchIndexer search_indexer, Scratch.MainWindow window) {
2931
Object (
30-
modal: true,
31-
relative_to: window.document_view,
32-
width_request: 500,
33-
current_window: window
32+
current_window: window,
33+
search_indexer: search_indexer
3434
);
35-
36-
int height;
37-
current_window.get_size (null, out height);
38-
window_height = height;
39-
40-
fuzzy_finder = new Services.FuzzyFinder (search_indexer.project_paths);
41-
indexer = search_indexer;
42-
items = new Gee.ArrayList<FileItem> ();
43-
cancellables = new Gee.LinkedList<GLib.Cancellable> ();
44-
45-
// Limit the shown results if the window height is too small
46-
if (window_height > 400) {
47-
max_items = 5;
48-
} else {
49-
max_items = 3;
50-
}
51-
52-
scrolled.set_max_content_height (45 /* height */ * max_items);
5335
}
5436

5537
private void calculate_scroll_offset (int old_position, int new_position) {
@@ -81,7 +63,11 @@ public class Scratch.FuzzySearchPopover : Gtk.Popover {
8163
}
8264

8365
construct {
66+
modal = true;
67+
relative_to = current_window.document_view;
68+
width_request = 500;
8469
pointing_to = { 0, 32, 1, 1 };
70+
8571
this.get_style_context ().add_class ("fuzzy-popover");
8672

8773
title_label = new Gtk.Label (_("Find project files"));
@@ -183,7 +169,7 @@ public class Scratch.FuzzySearchPopover : Gtk.Popover {
183169
}
184170

185171
fuzzy_finder.fuzzy_find_async.begin (term, dir_length,
186-
get_current_project (),
172+
current_doc_project,
187173
next_cancellable,
188174
(obj, res) => {
189175
if (next_cancellable.is_cancelled ()) {
@@ -260,6 +246,44 @@ public class Scratch.FuzzySearchPopover : Gtk.Popover {
260246

261247
scrolled.hide ();
262248
this.add (box);
249+
250+
fuzzy_finder = new Services.FuzzyFinder (search_indexer.project_paths);
251+
indexer = search_indexer;
252+
items = new Gee.ArrayList<FileItem> ();
253+
cancellables = new Gee.LinkedList<GLib.Cancellable> ();
254+
255+
search_term_entry.realize.connect_after (() => {
256+
int height;
257+
current_window.get_size (null, out height);
258+
259+
// Limit the shown results if the window height is too small
260+
if (height > 400) {
261+
max_items = height / 80;
262+
} else {
263+
max_items = 3;
264+
}
265+
266+
scrolled.set_max_content_height (45 /* height */ * max_items);
267+
268+
current_doc_project = get_current_project (); // This will not change while popover is showing
269+
search_result_container.set_sort_func ((a , b) => {
270+
var result_a = ((FileItem)a).result;
271+
var result_b = ((FileItem)b).result;
272+
var project_a_is_current = result_a.project == current_doc_project;
273+
var project_b_is_current = result_b.project == current_doc_project;
274+
if (project_a_is_current && !project_b_is_current) {
275+
return 1;
276+
} else if (project_b_is_current && !project_a_is_current) {
277+
return -1;
278+
} else if (result_a.score > result_b.score) {
279+
return -1;
280+
} else if (result_b.score > result_a.score) {
281+
return 1;
282+
} else {
283+
return strcmp (((FileItem)a).result.full_path, ((FileItem)b).result.full_path);
284+
}
285+
});
286+
});
263287
}
264288

265289
private void handle_item_selection (int index) {

src/FolderManager/FileView.vala

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
* but WITHOUT ANY WARRANTY; without even the implied warranties of
1212
* MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
1313
* PURPOSE. See the GNU General Public License for more details.
14-
*
14+
1515
* You should have received a copy of the GNU General Public License
1616
* along with this program. If not, see <http://www.gnu.org/licenses/>.
1717
*
@@ -160,7 +160,7 @@ public class Scratch.FolderManager.FileView : Code.Widgets.SourceList, Code.Pane
160160

161161
public async void restore_saved_state () {
162162
foreach (unowned string path in settings.get_strv ("opened-folders")) {
163-
yield add_folder (new File (path), false);
163+
yield add_folder (new File (path), false, true);
164164
}
165165
}
166166

@@ -581,7 +581,7 @@ public class Scratch.FolderManager.FileView : Code.Widgets.SourceList, Code.Pane
581581
}
582582
}
583583

584-
private async void add_folder (File folder, bool expand) {
584+
private async void add_folder (File folder, bool expand, bool restoring = false) {
585585
if (is_open (folder)) {
586586
warning ("Folder '%s' is already open.", folder.path);
587587
return;
@@ -658,7 +658,13 @@ public class Scratch.FolderManager.FileView : Code.Widgets.SourceList, Code.Pane
658658
write_settings ();
659659
});
660660

661-
write_settings ();
661+
// We do not want to rewrite settings while restoring from settings
662+
// This interferes with fuzzy-finder plugins_manager
663+
// See https://github.com/elementary/code/issues/1533
664+
if (!restoring) {
665+
write_settings ();
666+
}
667+
662668
add_folder.callback ();
663669
return Source.REMOVE;
664670
});

0 commit comments

Comments
 (0)