From 1bc8d1900fe68510e1e5a9cc08a7241d69690034 Mon Sep 17 00:00:00 2001 From: George Marques Date: Wed, 6 Jul 2016 19:29:15 -0300 Subject: Improve quick open sorting Perfect matches and substrings will be shown first. Similar matches will be at the bottom. When they score is the same they're shown in the natural file system order. --- tools/editor/quick_open.cpp | 38 ++++++++++++++++++++++++-------------- 1 file changed, 24 insertions(+), 14 deletions(-) (limited to 'tools/editor/quick_open.cpp') diff --git a/tools/editor/quick_open.cpp b/tools/editor/quick_open.cpp index adb08b65d6..e18dc584d5 100644 --- a/tools/editor/quick_open.cpp +++ b/tools/editor/quick_open.cpp @@ -109,6 +109,17 @@ void EditorQuickOpen::_sbox_input(const InputEvent& p_ie) { } +float EditorQuickOpen::_path_cmp(String search, String path) const { + + if (search == path) { + return 1.2f; + } + if (path.findn(search) != -1) { + return 1.1f; + } + return path.to_lower().similarity(search.to_lower()); +} + void EditorQuickOpen::_parse_fs(EditorFileSystemDirectory *efsd, Vector< Pair< String, Ref > > &list) { if (!add_directories) { @@ -130,17 +141,18 @@ void EditorQuickOpen::_parse_fs(EditorFileSystemDirectory *efsd, Vector< Pair< S Pair< String, Ref > pair; pair.first = path; pair.second = get_icon("folder", "FileDialog"); - if (list.size() > 0) { - float this_sim = search_text.to_lower().similarity(path.to_lower()); - float other_sim = search_text.to_lower().similarity(list[0].first.to_lower()); + if (search_text != String() && list.size() > 0) { + + float this_sim = _path_cmp(search_text, path); + float other_sim = _path_cmp(list[0].first, path); int pos = 1; - while (pos < list.size() && this_sim < other_sim) { - other_sim = search_text.to_lower().similarity(list[pos++].first.to_lower()); + while (pos < list.size() && this_sim <= other_sim) { + other_sim = _path_cmp(list[pos++].first, path); } - pos = this_sim > other_sim ? pos - 1 : pos; + pos = this_sim >= other_sim ? pos - 1 : pos; list.insert(pos, pair); } else { @@ -159,17 +171,17 @@ void EditorQuickOpen::_parse_fs(EditorFileSystemDirectory *efsd, Vector< Pair< S pair.first = file; pair.second = get_icon((has_icon(efsd->get_file_type(i), ei) ? efsd->get_file_type(i) : ot), ei); - if (list.size() > 0) { + if (search_text != String() && list.size() > 0) { - float this_sim = search_text.to_lower().similarity(file.to_lower()); - float other_sim = search_text.to_lower().similarity(list[0].first.to_lower()); + float this_sim = _path_cmp(search_text, file); + float other_sim = _path_cmp(list[0].first, file); int pos = 1; - while (pos < list.size() && this_sim < other_sim) { - other_sim = search_text.to_lower().similarity(list[pos++].first.to_lower()); + while (pos < list.size() && this_sim <= other_sim) { + other_sim = _path_cmp(list[pos++].first, file); } - pos = this_sim > other_sim ? pos - 1 : pos; + pos = this_sim >= other_sim ? pos - 1 : pos; list.insert(pos, pair); } else { @@ -198,8 +210,6 @@ void EditorQuickOpen::_update_search() { _parse_fs(efsd, list); - //String best_match = list[0]; - for (int i = 0; i < list.size(); i++) { TreeItem *ti = search_options->create_item(root); ti->set_text(0, list[i].first); -- cgit v1.2.3