diff --git a/plugins/fuzzy-search/file-item.vala b/plugins/fuzzy-search/file-item.vala index 62d5a6e46..5948684ff 100644 --- a/plugins/fuzzy-search/file-item.vala +++ b/plugins/fuzzy-search/file-item.vala @@ -7,13 +7,14 @@ */ public class FileItem : Gtk.ListBoxRow { - private SearchResult result; + public SearchResult result { get; private set; } public string filepath { get { return result.full_path; } } + public FileItem (SearchResult res, bool should_distinguish_project = false) { this.get_style_context ().add_class ("fuzzy-item"); this.get_style_context ().add_class ("flat"); diff --git a/plugins/fuzzy-search/fuzzy-search-popover.vala b/plugins/fuzzy-search/fuzzy-search-popover.vala index a6e5f8d53..ddb5ef8b5 100644 --- a/plugins/fuzzy-search/fuzzy-search-popover.vala +++ b/plugins/fuzzy-search/fuzzy-search-popover.vala @@ -19,7 +19,9 @@ public class Scratch.FuzzySearchPopover : Gtk.Popover { private Gee.LinkedList cancellables; private Gtk.EventControllerKey search_term_entry_key_controller; private Gtk.Label title_label; + private string current_doc_project; public Scratch.MainWindow current_window { get; construct; } + public Scratch.Services.FuzzySearchIndexer search_indexer { get; construct; } public bool sidebar_is_visible { get; set; } public signal void open_file (string filepath); @@ -27,29 +29,9 @@ public class Scratch.FuzzySearchPopover : Gtk.Popover { public FuzzySearchPopover (Scratch.Services.FuzzySearchIndexer search_indexer, Scratch.MainWindow window) { Object ( - modal: true, - relative_to: window.document_view, - width_request: 500, - current_window: window + current_window: window, + search_indexer: search_indexer ); - - int height; - current_window.get_size (null, out height); - window_height = height; - - fuzzy_finder = new Services.FuzzyFinder (search_indexer.project_paths); - indexer = search_indexer; - items = new Gee.ArrayList (); - cancellables = new Gee.LinkedList (); - - // Limit the shown results if the window height is too small - if (window_height > 400) { - max_items = 5; - } else { - max_items = 3; - } - - scrolled.set_max_content_height (45 /* height */ * max_items); } private void calculate_scroll_offset (int old_position, int new_position) { @@ -81,7 +63,11 @@ public class Scratch.FuzzySearchPopover : Gtk.Popover { } construct { + modal = true; + relative_to = current_window.document_view; + width_request = 500; pointing_to = { 0, 32, 1, 1 }; + this.get_style_context ().add_class ("fuzzy-popover"); title_label = new Gtk.Label (_("Find project files")); @@ -183,7 +169,7 @@ public class Scratch.FuzzySearchPopover : Gtk.Popover { } fuzzy_finder.fuzzy_find_async.begin (term, dir_length, - get_current_project (), + current_doc_project, next_cancellable, (obj, res) => { if (next_cancellable.is_cancelled ()) { @@ -260,6 +246,44 @@ public class Scratch.FuzzySearchPopover : Gtk.Popover { scrolled.hide (); this.add (box); + + fuzzy_finder = new Services.FuzzyFinder (search_indexer.project_paths); + indexer = search_indexer; + items = new Gee.ArrayList (); + cancellables = new Gee.LinkedList (); + + search_term_entry.realize.connect_after (() => { + int height; + current_window.get_size (null, out height); + + // Limit the shown results if the window height is too small + if (height > 400) { + max_items = height / 80; + } else { + max_items = 3; + } + + scrolled.set_max_content_height (45 /* height */ * max_items); + + current_doc_project = get_current_project (); // This will not change while popover is showing + search_result_container.set_sort_func ((a , b) => { + var result_a = ((FileItem)a).result; + var result_b = ((FileItem)b).result; + var project_a_is_current = result_a.project == current_doc_project; + var project_b_is_current = result_b.project == current_doc_project; + if (project_a_is_current && !project_b_is_current) { + return 1; + } else if (project_b_is_current && !project_a_is_current) { + return -1; + } else if (result_a.score > result_b.score) { + return -1; + } else if (result_b.score > result_a.score) { + return 1; + } else { + return strcmp (((FileItem)a).result.full_path, ((FileItem)b).result.full_path); + } + }); + }); } private void handle_item_selection (int index) { diff --git a/src/FolderManager/FileView.vala b/src/FolderManager/FileView.vala index 2bb5035b3..37d8f4085 100644 --- a/src/FolderManager/FileView.vala +++ b/src/FolderManager/FileView.vala @@ -11,7 +11,7 @@ * but WITHOUT ANY WARRANTY; without even the implied warranties of * MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR * PURPOSE. See the GNU General Public License for more details. - * + * You should have received a copy of the GNU General Public License * along with this program. If not, see . * @@ -145,7 +145,7 @@ public class Scratch.FolderManager.FileView : Code.Widgets.SourceList, Code.Pane public async void restore_saved_state () { foreach (unowned string path in settings.get_strv ("opened-folders")) { - yield add_folder (new File (path), false); + yield add_folder (new File (path), false, true); } } @@ -507,7 +507,7 @@ public class Scratch.FolderManager.FileView : Code.Widgets.SourceList, Code.Pane } } - private async void add_folder (File folder, bool expand) { + private async void add_folder (File folder, bool expand, bool restoring = false) { if (is_open (folder)) { warning ("Folder '%s' is already open.", folder.path); return; @@ -584,7 +584,13 @@ public class Scratch.FolderManager.FileView : Code.Widgets.SourceList, Code.Pane write_settings (); }); - write_settings (); + // We do not want to rewrite settings while restoring from settings + // This interferes with fuzzy-finder plugins_manager + // See https://github.com/elementary/code/issues/1533 + if (!restoring) { + write_settings (); + } + add_folder.callback (); return Source.REMOVE; });