UI: improve search results by using fuzzy and prefix matching

Blender does string based searching in many different places.
This patch updates four of these places to use the new string
search api in `BLI_string_search.h`.

In the future we probably want to update the other searches as well.

Reviewers: Severin, pablovazquez

Differential Revision: https://developer.blender.org/D8825
This commit is contained in:
2020-09-09 13:44:39 +02:00
parent 45bd8fdc2b
commit 98eb89be5d
4 changed files with 145 additions and 101 deletions

View File

@@ -41,6 +41,7 @@
#include "BLI_math_matrix.h"
#include "BLI_memarena.h"
#include "BLI_string.h"
#include "BLI_string_search.h"
#include "BLI_string_utils.h"
#include "BLI_utildefines.h"
@@ -993,19 +994,24 @@ static void menu_search_update_fn(const bContext *UNUSED(C),
{
struct MenuSearch_Data *data = arg;
/* Prepare BLI_string_all_words_matched. */
const size_t str_len = strlen(str);
const int words_max = BLI_string_max_possible_word_count(str_len);
int(*words)[2] = BLI_array_alloca(words, words_max);
const int words_len = BLI_string_find_split_words(str, str_len, ' ', words, words_max);
StringSearch *search = BLI_string_search_new();
for (struct MenuSearch_Item *item = data->items.first; item; item = item->next) {
if (BLI_string_all_words_matched(item->drawwstr_full, str, words, words_len)) {
if (!UI_search_item_add(items, item->drawwstr_full, item, item->icon, item->state, 0)) {
break;
}
LISTBASE_FOREACH (struct MenuSearch_Item *, item, &data->items) {
BLI_string_search_add(search, item->drawwstr_full, item);
}
struct MenuSearch_Item **filtered_items;
int filtered_amount = BLI_string_search_query(search, str, (void ***)&filtered_items);
for (int i = 0; i < filtered_amount; i++) {
struct MenuSearch_Item *item = filtered_items[i];
if (!UI_search_item_add(items, item->drawwstr_full, item, item->icon, item->state, 0)) {
break;
}
}
MEM_freeN(filtered_items);
BLI_string_search_free(search);
}
/** \} */