Просмотр исходного кода

LibGUI: Use fuzzy matching in CommandPalette

This patch changes the previously used contains method for matching the
user search term with all available commands to use the fuzzy match
algorithm, which makes it more typo tolerant.
faxe1008 3 лет назад
Родитель
Сommit
9e323241f8
1 измененных файлов с 7 добавлено и 2 удалено
  1. 7 2
      Userland/Libraries/LibGUI/CommandPalette.cpp

+ 7 - 2
Userland/Libraries/LibGUI/CommandPalette.cpp

@@ -6,6 +6,7 @@
  * SPDX-License-Identifier: BSD-2-Clause
  */
 
+#include <AK/FuzzyMatch.h>
 #include <AK/QuickSort.h>
 #include <LibGUI/Action.h>
 #include <LibGUI/ActionGroup.h>
@@ -136,8 +137,12 @@ public:
 
     virtual TriState data_matches(GUI::ModelIndex const& index, GUI::Variant const& term) const override
     {
-        auto search = String::formatted("{} {}", menu_name(index), action_text(index));
-        if (search.contains(term.as_string(), CaseSensitivity::CaseInsensitive))
+        auto needle = term.as_string();
+        if (needle.is_empty())
+            return TriState::True;
+
+        auto haystack = String::formatted("{} {}", menu_name(index), action_text(index));
+        if (fuzzy_match(needle, haystack).score > 0)
             return TriState::True;
         return TriState::False;
     }