Skip to content

Commit

Permalink
Improve Fuzzy Search Accuracy
Browse files Browse the repository at this point in the history
- Combine file name and file paths in fuzzy finder
  • Loading branch information
colinkiama committed Dec 20, 2023
1 parent 03686c4 commit a8c119a
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 13 deletions.
36 changes: 25 additions & 11 deletions plugins/fuzzy-search/fuzzy-finder.vala
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ const int LEADING_LETTER_PENALTY = -5; // penalty applied for every letter in st
const int MAX_LEADING_LETTER_PENALTY = -15; // maximum penalty for leading letters
const int UNMATCHED_LETTER_PENALTY = -1;


public class Scratch.Services.FuzzyFinder {
private class RecursiveFinder {
int recursion_limit;
Expand Down Expand Up @@ -140,7 +139,7 @@ public class Scratch.Services.FuzzyFinder {
if (neighbor != neighbor.toupper () && curr != curr.tolower ()) {
out_score += CAMEL_BONUS;
}
var is_neighbour_separator = neighbor == '_' || neighbor == ' ';
var is_neighbour_separator = neighbor == '_' || neighbor == ' ' || neighbor == GLib.Path.DIR_SEPARATOR;
if (is_neighbour_separator) {
out_score += SEPARATOR_BONUS;
}
Expand Down Expand Up @@ -213,27 +212,42 @@ public class Scratch.Services.FuzzyFinder {
}

var path = project.relative_file_paths[j];
SearchResult search_result;
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 = "";

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

string filename = Path.get_basename (path);
filename_search_result = fuzzy_match (search_str, filename, cancellable);

var root_path = project.root_path;
if (filename_search_result.found && path_search_result.found) {
int combined_score = path_search_result.score + filename_search_result.score;
filename_search_result.score = combined_score;
}

if (search_result.found) {
var root_path = project.root_path;
search_result.relative_path = path;
search_result.full_path = @"$root_path/$path";
search_result.project = project_name;
results.add (search_result);
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;
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;
results.add (path_search_result);
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions plugins/fuzzy-search/fuzzy-search.vala
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,13 @@ public class Scratch.Services.SearchProject {
while (name != null) {
debug ("Fuzzy Search - Parsed fuzzy search path: %s\n", name);
var new_search_path = "";
if (path.has_suffix ("/")) {
if (path.has_suffix (GLib.Path.DIR_SEPARATOR_S)) {
new_search_path = path.substring (0, path.length - 1);
} else {
new_search_path = path;
}

parse_async_internal.begin (new_search_path + "/" + name, (obj, res) => {
parse_async_internal.begin (new_search_path + GLib.Path.DIR_SEPARATOR_S + name, (obj, res) => {
parse_async_internal.end (res);
});

Expand Down

0 comments on commit a8c119a

Please sign in to comment.