mirror of
https://github.com/godotengine/godot
synced 2024-10-14 11:58:36 +00:00
Search ranking: favor filenames over extensions.
This commit is contained in:
parent
5054e27aa0
commit
7f6e8d25d4
|
@ -62,7 +62,6 @@ Vector<String> EditorQuickOpen::get_selected_files() const {
|
||||||
TreeItem *item = search_options->get_next_selected(search_options->get_root());
|
TreeItem *item = search_options->get_next_selected(search_options->get_root());
|
||||||
while (item) {
|
while (item) {
|
||||||
files.push_back("res://" + item->get_text(0));
|
files.push_back("res://" + item->get_text(0));
|
||||||
|
|
||||||
item = search_options->get_next_selected(item);
|
item = search_options->get_next_selected(item);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -90,7 +89,6 @@ void EditorQuickOpen::_sbox_input(const Ref<InputEvent> &p_ie) {
|
||||||
}
|
}
|
||||||
|
|
||||||
TreeItem *current = search_options->get_selected();
|
TreeItem *current = search_options->get_selected();
|
||||||
|
|
||||||
TreeItem *item = search_options->get_next_selected(root);
|
TreeItem *item = search_options->get_next_selected(root);
|
||||||
while (item) {
|
while (item) {
|
||||||
item->deselect(0);
|
item->deselect(0);
|
||||||
|
@ -98,25 +96,29 @@ void EditorQuickOpen::_sbox_input(const Ref<InputEvent> &p_ie) {
|
||||||
}
|
}
|
||||||
|
|
||||||
current->select(0);
|
current->select(0);
|
||||||
|
current->set_as_cursor(0);
|
||||||
} break;
|
} break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
float EditorQuickOpen::_path_cmp(String search, String path) const {
|
float EditorQuickOpen::_score_path(String search, String path) const {
|
||||||
// Exact match.
|
// Positive bias for matches close to the _beginning of the file name_.
|
||||||
if (search == path) {
|
String file = path.get_file();
|
||||||
return 1.2f;
|
int pos = file.findn(search);
|
||||||
}
|
|
||||||
|
|
||||||
// Substring match, with positive bias for matches close to the end of the path.
|
|
||||||
int pos = path.rfindn(search);
|
|
||||||
if (pos != -1) {
|
if (pos != -1) {
|
||||||
return 1.1f + 0.09 / (path.length() - pos + 1);
|
return 1.0f - 0.1f * (float(pos) / file.length());
|
||||||
}
|
}
|
||||||
|
|
||||||
// Similarity.
|
// Positive bias for matches close to the _end of the path_.
|
||||||
return path.to_lower().similarity(search.to_lower());
|
String base = path.get_base_dir();
|
||||||
|
pos = base.rfindn(search);
|
||||||
|
if (pos != -1) {
|
||||||
|
return 0.9f - 0.1f * (float(base.length() - pos) / base.length());
|
||||||
|
}
|
||||||
|
|
||||||
|
// Results that contain all characters but not the string.
|
||||||
|
return path.similarity(search) * 0.8f;
|
||||||
}
|
}
|
||||||
|
|
||||||
void EditorQuickOpen::_parse_fs(EditorFileSystemDirectory *efsd, Vector<Pair<String, Ref<Texture2D>>> &list) {
|
void EditorQuickOpen::_parse_fs(EditorFileSystemDirectory *efsd, Vector<Pair<String, Ref<Texture2D>>> &list) {
|
||||||
|
@ -124,25 +126,25 @@ void EditorQuickOpen::_parse_fs(EditorFileSystemDirectory *efsd, Vector<Pair<Str
|
||||||
_parse_fs(efsd->get_subdir(i), list);
|
_parse_fs(efsd->get_subdir(i), list);
|
||||||
}
|
}
|
||||||
|
|
||||||
String search_text = search_box->get_text();
|
|
||||||
|
|
||||||
for (int i = 0; i < efsd->get_file_count(); i++) {
|
for (int i = 0; i < efsd->get_file_count(); i++) {
|
||||||
String file = efsd->get_file_path(i);
|
|
||||||
file = file.substr(6, file.length());
|
|
||||||
|
|
||||||
StringName file_type = efsd->get_file_type(i);
|
StringName file_type = efsd->get_file_type(i);
|
||||||
if (ClassDB::is_parent_class(file_type, base_type) && search_text.is_subsequence_ofi(file)) {
|
|
||||||
Pair<String, Ref<Texture2D>> pair;
|
if (ClassDB::is_parent_class(file_type, base_type)) {
|
||||||
pair.first = file;
|
String file = efsd->get_file_path(i);
|
||||||
StringName icon_name = search_options->has_theme_icon(file_type, ei) ? file_type : ot;
|
file = file.substr(6, file.length());
|
||||||
pair.second = search_options->get_theme_icon(icon_name, ei);
|
|
||||||
list.push_back(pair);
|
if (search_box->get_text().is_subsequence_ofi(file)) {
|
||||||
|
Pair<String, Ref<Texture2D>> pair;
|
||||||
|
pair.first = file;
|
||||||
|
pair.second = search_options->get_theme_icon(search_options->has_theme_icon(file_type, ei) ? file_type : ot, ei);
|
||||||
|
list.push_back(pair);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Vector<Pair<String, Ref<Texture2D>>> EditorQuickOpen::_sort_fs(Vector<Pair<String, Ref<Texture2D>>> &list) {
|
Vector<Pair<String, Ref<Texture2D>>> EditorQuickOpen::_sort_fs(Vector<Pair<String, Ref<Texture2D>>> &list) {
|
||||||
String search_text = search_box->get_text();
|
String search_text = search_box->get_text().to_lower();
|
||||||
Vector<Pair<String, Ref<Texture2D>>> sorted_list;
|
Vector<Pair<String, Ref<Texture2D>>> sorted_list;
|
||||||
|
|
||||||
if (search_text == String() || list.size() == 0) {
|
if (search_text == String() || list.size() == 0) {
|
||||||
|
@ -152,7 +154,7 @@ Vector<Pair<String, Ref<Texture2D>>> EditorQuickOpen::_sort_fs(Vector<Pair<Strin
|
||||||
Vector<float> scores;
|
Vector<float> scores;
|
||||||
scores.resize(list.size());
|
scores.resize(list.size());
|
||||||
for (int i = 0; i < list.size(); i++) {
|
for (int i = 0; i < list.size(); i++) {
|
||||||
scores.write[i] = _path_cmp(search_text, list[i].first);
|
scores.write[i] = _score_path(search_text, list[i].first.to_lower());
|
||||||
}
|
}
|
||||||
|
|
||||||
while (list.size() > 0) {
|
while (list.size() > 0) {
|
||||||
|
@ -190,19 +192,17 @@ void EditorQuickOpen::_update_search() {
|
||||||
ti->set_icon(0, list[i].second);
|
ti->set_icon(0, list[i].second);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (root->get_children()) {
|
TreeItem *result = root->get_children();
|
||||||
TreeItem *ti = root->get_children();
|
if (result) {
|
||||||
|
result->select(0);
|
||||||
ti->select(0);
|
result->set_as_cursor(0);
|
||||||
ti->set_as_cursor(0);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
get_ok()->set_disabled(root->get_children() == nullptr);
|
get_ok()->set_disabled(!result);
|
||||||
}
|
}
|
||||||
|
|
||||||
void EditorQuickOpen::_confirmed() {
|
void EditorQuickOpen::_confirmed() {
|
||||||
TreeItem *ti = search_options->get_selected();
|
if (!search_options->get_selected()) {
|
||||||
if (!ti) {
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
emit_signal("quick_open");
|
emit_signal("quick_open");
|
||||||
|
@ -252,7 +252,6 @@ EditorQuickOpen::EditorQuickOpen() {
|
||||||
vbc->add_margin_child(TTR("Matches:"), search_options, true);
|
vbc->add_margin_child(TTR("Matches:"), search_options, true);
|
||||||
|
|
||||||
get_ok()->set_text(TTR("Open"));
|
get_ok()->set_text(TTR("Open"));
|
||||||
get_ok()->set_disabled(true);
|
|
||||||
register_text_enter(search_box);
|
register_text_enter(search_box);
|
||||||
set_hide_on_ok(false);
|
set_hide_on_ok(false);
|
||||||
|
|
||||||
|
|
|
@ -41,6 +41,7 @@ class EditorQuickOpen : public ConfirmationDialog {
|
||||||
|
|
||||||
LineEdit *search_box;
|
LineEdit *search_box;
|
||||||
Tree *search_options;
|
Tree *search_options;
|
||||||
|
|
||||||
StringName base_type;
|
StringName base_type;
|
||||||
StringName ei;
|
StringName ei;
|
||||||
StringName ot;
|
StringName ot;
|
||||||
|
@ -50,7 +51,7 @@ class EditorQuickOpen : public ConfirmationDialog {
|
||||||
void _sbox_input(const Ref<InputEvent> &p_ie);
|
void _sbox_input(const Ref<InputEvent> &p_ie);
|
||||||
void _parse_fs(EditorFileSystemDirectory *efsd, Vector<Pair<String, Ref<Texture2D>>> &list);
|
void _parse_fs(EditorFileSystemDirectory *efsd, Vector<Pair<String, Ref<Texture2D>>> &list);
|
||||||
Vector<Pair<String, Ref<Texture2D>>> _sort_fs(Vector<Pair<String, Ref<Texture2D>>> &list);
|
Vector<Pair<String, Ref<Texture2D>>> _sort_fs(Vector<Pair<String, Ref<Texture2D>>> &list);
|
||||||
float _path_cmp(String search, String path) const;
|
float _score_path(String search, String path) const;
|
||||||
|
|
||||||
void _confirmed();
|
void _confirmed();
|
||||||
void _text_changed(const String &p_newtext);
|
void _text_changed(const String &p_newtext);
|
||||||
|
|
Loading…
Reference in a new issue