Skip to content

Commit

Permalink
Store relative file paths for fuzzy finder in sets (#1420)
Browse files Browse the repository at this point in the history
* Store relative file paths for fuzzy finder in sets

* Move unreachable early return code in fuzzy finder

* Use var where type can be inferred

* Remove unused variable

---------

Co-authored-by: Jeremy Wootten <[email protected]>
  • Loading branch information
colinkiama and jeremypw authored Jan 28, 2024
1 parent 7d4785c commit 7e6b523
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 63 deletions.
109 changes: 55 additions & 54 deletions plugins/fuzzy-search/fuzzy-finder.vala
Original file line number Diff line number Diff line change
Expand Up @@ -241,60 +241,61 @@ public class Scratch.Services.FuzzyFinder {
}

var project = projects[i];

for (int j = 0; j < project.relative_file_paths.size; j++) {
if (cancellable.is_cancelled ()) {
return results;
}

var path = project.relative_file_paths[j];
SearchResult path_search_result;
SearchResult filename_search_result;

// If there is more than one project prepend the project name
// to the front of the path
// This helps to search for specific files only in one project, e.g.
// "code/fuzfind" will probably only return fuzzy_finder.vala from this project
// even if their is a "fuzzy_finder" file in another project
string project_name = "";

project_name = project_paths.size > 1 ? Path.get_basename (project.root_path) : "";
if (dir_length > 0 || project_paths.size == 1) {
path_search_result = fuzzy_match (search_str, dir_length, path, cancellable);
} else {
path_search_result = fuzzy_match (search_str, dir_length, @"$project_name/$path", cancellable);
}

if (dir_length == 0) {
string filename = Path.get_basename (path);
filename_search_result = fuzzy_match (search_str, dir_length, filename, cancellable);
} else {
filename_search_result = new SearchResult (false, 0);
}

var root_path = project.root_path;

if (filename_search_result.found) {
filename_search_result.relative_path = path;
filename_search_result.full_path = @"$root_path/$path";
filename_search_result.project = project_name;
filename_search_result.score += project.root_path == current_project
? CURRENT_PROJECT_PRIORITY_BONUS
: 0;

results.add (filename_search_result);
} else if (path_search_result.found) {
path_search_result.relative_path = path;
path_search_result.full_path = @"$root_path/$path";
path_search_result.project = project_name;
path_search_result.score = (int) (path_search_result.score * 0.2) +
(project.root_path == current_project
? CURRENT_PROJECT_PRIORITY_BONUS
: 0);

results.add (path_search_result);
}
}
var iter = project.relative_file_paths.iterator ();

while (iter.next () && !cancellable.is_cancelled ()) {
var path = iter.get ();
SearchResult path_search_result;
SearchResult filename_search_result;

// If there is more than one project prepend the project name
// to the front of the path
// This helps to search for specific files only in one project, e.g.
// "code/fuzfind" will probably only return fuzzy_finder.vala from this project
// even if their is a "fuzzy_finder" file in another project
var project_name = "";

project_name = project_paths.size > 1 ? Path.get_basename (project.root_path) : "";
if (dir_length > 0 || project_paths.size == 1) {
path_search_result = fuzzy_match (search_str, dir_length, path, cancellable);
} else {
path_search_result = fuzzy_match (search_str, dir_length, @"$project_name/$path", cancellable);
}

if (dir_length == 0) {
var filename = Path.get_basename (path);
filename_search_result = fuzzy_match (search_str, dir_length, filename, cancellable);
} else {
filename_search_result = new SearchResult (false, 0);
}

var root_path = project.root_path;

if (filename_search_result.found) {
filename_search_result.relative_path = path;
filename_search_result.full_path = @"$root_path/$path";
filename_search_result.project = project_name;
filename_search_result.score += project.root_path == current_project
? CURRENT_PROJECT_PRIORITY_BONUS
: 0;

results.add (filename_search_result);
} else if (path_search_result.found) {
path_search_result.relative_path = path;
path_search_result.full_path = @"$root_path/$path";
path_search_result.project = project_name;
path_search_result.score = (int) (path_search_result.score * 0.2) +
(project.root_path == current_project
? CURRENT_PROJECT_PRIORITY_BONUS
: 0);

results.add (path_search_result);
}
}

if (cancellable.is_cancelled ()) {
return results;
}
}

results.sort ((a, b) => {
Expand Down
19 changes: 10 additions & 9 deletions plugins/fuzzy-search/fuzzy-search-project.vala
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@

public class Scratch.Services.SearchProject {
public string root_path { get; private set; }
public Gee.ArrayList<string> relative_file_paths { get; private set; }
public Gee.HashSet<string> relative_file_paths { get; private set; }

private MonitoredRepository? monitored_repo;

public SearchProject (string root, MonitoredRepository? repo) {
root_path = root;
monitored_repo = repo;
relative_file_paths = new Gee.ArrayList<string> ();
relative_file_paths = new Gee.HashSet<string> ();
}

public async void parse_async (string path, GLib.Cancellable cancellable) {
Expand Down Expand Up @@ -43,15 +44,15 @@ public class Scratch.Services.SearchProject {
return;
}

int start_length = relative_file_paths.size;
Gee.Iterator<string> iter = relative_file_paths.iterator ();

// Remove directory
for (int i = start_length - 1; i > -1; i--) {
string relative_path = relative_file_paths[i];
if (relative_path.has_prefix (deleted_path)) {
relative_file_paths.remove (relative_path);
}
while (iter.next ()) {
var relative_path = iter.get ();
if (relative_path.has_prefix (deleted_path)) {
iter.remove ();
}
}

}

public void add_file (string path, GLib.Cancellable cancellable) {
Expand Down

0 comments on commit 7e6b523

Please sign in to comment.