Skip to content
3 changes: 2 additions & 1 deletion plugins/fuzzy-search/file-item.vala
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
70 changes: 47 additions & 23 deletions plugins/fuzzy-search/fuzzy-search-popover.vala
Original file line number Diff line number Diff line change
Expand Up @@ -19,37 +19,19 @@ public class Scratch.FuzzySearchPopover : Gtk.Popover {
private Gee.LinkedList<GLib.Cancellable> 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);
public signal void close_search ();

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<FileItem> ();
cancellables = new Gee.LinkedList<GLib.Cancellable> ();

// 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) {
Expand Down Expand Up @@ -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"));
Expand Down Expand Up @@ -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 ()) {
Expand Down Expand Up @@ -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<FileItem> ();
cancellables = new Gee.LinkedList<GLib.Cancellable> ();

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) {
Expand Down
14 changes: 10 additions & 4 deletions src/FolderManager/FileView.vala
Original file line number Diff line number Diff line change
Expand Up @@ -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 <http://www.gnu.org/licenses/>.
*
Expand Down Expand Up @@ -138,7 +138,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);
}
}

Expand Down Expand Up @@ -512,7 +512,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;
Expand Down Expand Up @@ -589,7 +589,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;
});
Expand Down