Skip to content

Commit 911eb30

Browse files
authored
finders/desktop: add support for GenericName fuzzy querying (#139)
* finders/desktop: use m_name instead of m_fuzzable for cache entries Signed-off-by: Sofia Donato Ferreira <flowlnlnln@gmail.com> * fuzzy: support multiple fuzzables per FinderResult This allows us to query multiple information about an entry at the same time, selecting the one with the best match. Signed-off-by: Sofia Donato Ferreira <flowlnlnln@gmail.com> * finders/desktop: add support for GenericName fuzzy querying Signed-off-by: Sofia Donato Ferreira <flowlnlnln@gmail.com> --------- Signed-off-by: Sofia Donato Ferreira <flowlnlnln@gmail.com>
1 parent c682906 commit 911eb30

9 files changed

Lines changed: 81 additions & 54 deletions

File tree

src/finders/Fuzzy.cpp

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,12 @@ static void workerFn(std::vector<SScoreData>& scores, const std::vector<SP<IFind
183183
for (size_t i = start; i < end; ++i) {
184184
auto& ref = scores[i];
185185

186-
ref.score = scoreCandidate(query, in[i]->fuzzable(), in[i]->frequency());
186+
float bestScore = 0.F;
187+
for (auto const& candidate : in[i]->fuzzables()) {
188+
auto score = scoreCandidate(query, candidate, in[i]->frequency());
189+
bestScore = std::max(score, bestScore);
190+
}
191+
ref.score = bestScore;
187192

188193
ref.result = in[i];
189194
ref.idx = i;
@@ -255,4 +260,22 @@ std::vector<SP<IFinderResult>> Fuzzy::getNResults(const std::vector<SP<IFinderRe
255260
workerFn(scores, in, query, 0, in.size());
256261

257262
return getBestResultsStable(scores, results);
258-
}
263+
}
264+
265+
std::vector<std::string> Fuzzy::createFuzzableStrings(std::initializer_list<std::string_view> strings, bool toLowercase) {
266+
std::vector<std::string> fuzzables{strings.size()};
267+
268+
for (auto&& sv : strings) {
269+
std::string fuzzable;
270+
fuzzable.resize(sv.size());
271+
272+
if (toLowercase)
273+
std::ranges::transform(sv, fuzzable.begin(), ::tolower);
274+
else
275+
fuzzable.assign(sv);
276+
277+
fuzzables.emplace_back(std::move(fuzzable));
278+
}
279+
280+
return fuzzables;
281+
}

src/finders/Fuzzy.hpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#pragma once
22

3+
#include <initializer_list>
34
#include <string>
45
#include <vector>
56

@@ -8,4 +9,5 @@
89

910
namespace Fuzzy {
1011
std::vector<SP<IFinderResult>> getNResults(const std::vector<SP<IFinderResult>>& in, const std::string& query, size_t results);
11-
};
12+
std::vector<std::string> createFuzzableStrings(std::initializer_list<std::string_view>, bool toLowercase = true);
13+
};

src/finders/IFinderResult.hpp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
11
#pragma once
22

33
#include <string>
4+
#include <vector>
45
#include "FinderTypes.hpp"
56

67
class IFinderResult {
78
public:
89
virtual ~IFinderResult() = default;
910

10-
virtual eFinderTypes type() = 0;
11-
virtual void run() = 0;
12-
virtual const std::string& fuzzable() = 0;
13-
virtual const std::string& name() = 0;
14-
virtual uint32_t frequency();
11+
virtual eFinderTypes type() = 0;
12+
virtual void run() = 0;
13+
virtual const std::vector<std::string>& fuzzables() = 0;
14+
virtual const std::string& name() = 0;
15+
virtual uint32_t frequency();
1516

1617
protected:
1718
IFinderResult() = default;
18-
};
19+
};

src/finders/desktop/DesktopFinder.cpp

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ class CDesktopEntry : public IFinderResult {
3737
CDesktopEntry() = default;
3838
virtual ~CDesktopEntry() = default;
3939

40-
virtual const std::string& fuzzable() {
41-
return m_fuzzable;
40+
virtual const std::vector<std::string>& fuzzables() {
41+
return m_fuzzables;
4242
}
4343

4444
virtual eFinderTypes type() {
@@ -64,8 +64,8 @@ class CDesktopEntry : public IFinderResult {
6464

6565
Debug::log(TRACE, "Running {}", toExec);
6666

67-
g_desktopFinder->m_entryFrequencyCache->incrementCachedEntry(m_fuzzable);
68-
m_frequency = g_desktopFinder->m_entryFrequencyCache->getCachedEntry(m_fuzzable);
67+
g_desktopFinder->m_entryFrequencyCache->incrementCachedEntry(m_name);
68+
m_frequency = g_desktopFinder->m_entryFrequencyCache->getCachedEntry(m_name);
6969

7070
// replace all funky codes with nothing
7171
replaceInString(toExec, "%U", "");
@@ -84,10 +84,11 @@ class CDesktopEntry : public IFinderResult {
8484
proc.runAsync();
8585
}
8686

87-
std::string m_name, m_exec, m_icon, m_fuzzable, m_stem;
88-
bool m_terminal = false;
87+
std::string m_name, m_exec, m_icon, m_stem;
88+
std::vector<std::string> m_fuzzables;
89+
bool m_terminal = false;
8990

90-
uint32_t m_frequency = 0;
91+
uint32_t m_frequency = 0;
9192
};
9293

9394
static std::filesystem::path resolvePath(const std::string& p) {
@@ -242,6 +243,7 @@ void CDesktopFinder::cacheEntry(const std::filesystem::path& path) {
242243
};
243244

244245
const auto NAME = extract("Name");
246+
const auto GEN_NAME = extract("GenericName");
245247
const auto ICON = extract("Icon");
246248
const auto EXEC = extract("Exec");
247249
const auto NODISPLAY = extract("NoDisplay") == "true";
@@ -259,15 +261,14 @@ void CDesktopFinder::cacheEntry(const std::filesystem::path& path) {
259261
std::erase_if(m_desktopEntryCache, [&pathStem](const auto& e) { return e->m_stem == pathStem; });
260262
}
261263

262-
auto& e = m_desktopEntryCache.emplace_back(makeShared<CDesktopEntry>());
263-
e->m_exec = EXEC;
264-
e->m_icon = ICON;
265-
e->m_name = NAME;
266-
e->m_fuzzable = NAME;
267-
e->m_stem = std::move(pathStem);
268-
e->m_terminal = TERMINAL;
269-
std::ranges::transform(e->m_fuzzable, e->m_fuzzable.begin(), ::tolower);
270-
e->m_frequency = m_entryFrequencyCache->getCachedEntry(e->m_fuzzable);
264+
auto& e = m_desktopEntryCache.emplace_back(makeShared<CDesktopEntry>());
265+
e->m_exec = EXEC;
266+
e->m_icon = ICON;
267+
e->m_name = NAME;
268+
e->m_fuzzables = Fuzzy::createFuzzableStrings({NAME, GEN_NAME});
269+
e->m_stem = std::move(pathStem);
270+
e->m_terminal = TERMINAL;
271+
e->m_frequency = m_entryFrequencyCache->getCachedEntry(e->m_name);
271272
m_desktopEntryCacheGeneric.emplace_back(e);
272273

273274
Debug::log(TRACE, "desktop: cached {} with icon {} and exec line of \"{}\"", NAME, ICON, EXEC);

src/finders/font/FontFinder.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ class CFontEntry : public IFinderResult {
1414
CFontEntry() = default;
1515
virtual ~CFontEntry() = default;
1616

17-
virtual const std::string& fuzzable() {
18-
return m_fuzzable;
17+
virtual const std::vector<std::string>& fuzzables() {
18+
return m_fuzzables;
1919
}
2020

2121
virtual eFinderTypes type() {
@@ -33,7 +33,8 @@ class CFontEntry : public IFinderResult {
3333
proc.runAsync();
3434
}
3535

36-
std::string m_font, m_fuzzable;
36+
std::string m_font;
37+
std::vector<std::string> m_fuzzables;
3738
};
3839

3940
CFontFinder::CFontFinder() : m_valid(FcInit()) {
@@ -69,10 +70,9 @@ void CFontFinder::refreshFonts() {
6970
if (!family || !style)
7071
continue;
7172

72-
auto e = m_entries.emplace_back(makeShared<CFontEntry>());
73-
e->m_font = std::format("{} {}", (char*)family, (char*)style);
74-
e->m_fuzzable = e->m_font;
75-
std::ranges::transform(e->m_fuzzable, e->m_fuzzable.begin(), ::tolower);
73+
auto e = m_entries.emplace_back(makeShared<CFontEntry>());
74+
e->m_font = std::format("{} {}", (char*)family, (char*)style);
75+
e->m_fuzzables = Fuzzy::createFuzzableStrings({e->m_font});
7676
m_entriesGeneric.emplace_back(std::move(e));
7777
}
7878
}

src/finders/ipc/IPCFinder.cpp

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ class CIPCEntry : public IFinderResult {
2020
CIPCEntry() = default;
2121
virtual ~CIPCEntry() = default;
2222

23-
virtual const std::string& fuzzable() {
24-
return m_fuzzable;
23+
virtual const std::vector<std::string>& fuzzables() {
24+
return m_fuzzables;
2525
}
2626

2727
virtual eFinderTypes type() {
@@ -40,7 +40,8 @@ class CIPCEntry : public IFinderResult {
4040
Debug::log(TRACE, "Selected {}", m_entry);
4141
}
4242

43-
std::string m_entry, m_fuzzable;
43+
std::string m_entry;
44+
std::vector<std::string> m_fuzzables;
4445
};
4546

4647
CIPCFinder::CIPCFinder() = default;
@@ -55,9 +56,8 @@ void CIPCFinder::setData(const std::vector<const char*>& data) {
5556
for (const auto& s : data) {
5657
auto e = m_entries.emplace_back(makeShared<CIPCEntry>());
5758
m_entriesGeneric.emplace_back(e);
58-
e->m_entry = s;
59-
e->m_fuzzable = s;
60-
std::ranges::transform(e->m_fuzzable, e->m_fuzzable.begin(), ::tolower);
59+
e->m_entry = s;
60+
e->m_fuzzables = Fuzzy::createFuzzableStrings({s});
6161
}
6262
}
6363

@@ -67,9 +67,8 @@ void CIPCFinder::setData(const std::vector<std::string>& data) {
6767
for (const auto& s : data) {
6868
auto e = m_entries.emplace_back(makeShared<CIPCEntry>());
6969
m_entriesGeneric.emplace_back(e);
70-
e->m_entry = s;
71-
e->m_fuzzable = s;
72-
std::ranges::transform(e->m_fuzzable, e->m_fuzzable.begin(), ::tolower);
70+
e->m_entry = s;
71+
e->m_fuzzables = Fuzzy::createFuzzableStrings({s});
7372
}
7473
}
7574

src/finders/math/MathFinder.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ class CMathEntry : public IFinderResult {
1313
CMathEntry() = default;
1414
virtual ~CMathEntry() = default;
1515

16-
virtual const std::string& fuzzable() {
17-
return m_fuzzable;
16+
virtual const std::vector<std::string>& fuzzables() {
17+
return m_fuzzables;
1818
}
1919

2020
virtual eFinderTypes type() {
@@ -32,7 +32,8 @@ class CMathEntry : public IFinderResult {
3232
proc.runAsync();
3333
}
3434

35-
std::string m_expr, m_result, m_fuzzable;
35+
std::string m_expr, m_result;
36+
std::vector<std::string> m_fuzzables;
3637
};
3738

3839
CMathFinder::CMathFinder() = default;

src/finders/unicode/UnicodeFinder.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ class CUnicodeEntry : public IFinderResult {
1717
CUnicodeEntry() = default;
1818
virtual ~CUnicodeEntry() = default;
1919

20-
virtual const std::string& fuzzable() {
21-
return m_fuzzable;
20+
virtual const std::vector<std::string>& fuzzables() {
21+
return m_fuzzables;
2222
}
2323

2424
virtual eFinderTypes type() {
@@ -43,9 +43,10 @@ class CUnicodeEntry : public IFinderResult {
4343
proc.runAsync();
4444
}
4545

46-
std::string m_name, m_unicode, m_fuzzable;
46+
std::string m_name, m_unicode;
47+
std::vector<std::string> m_fuzzables;
4748

48-
uint32_t m_frequency = 0;
49+
uint32_t m_frequency = 0;
4950
};
5051

5152
static bool isSurrogate(UChar32 cp) {
@@ -86,11 +87,10 @@ void CUnicodeFinder::init() {
8687
std::string utf8;
8788
us.toUTF8String(utf8);
8889

89-
auto& e = m_unicodeEntryCache.emplace_back(makeShared<CUnicodeEntry>());
90-
e->m_unicode = utf8;
91-
e->m_name = name;
92-
e->m_fuzzable = name;
93-
std::ranges::transform(e->m_fuzzable, e->m_fuzzable.begin(), ::tolower);
90+
auto& e = m_unicodeEntryCache.emplace_back(makeShared<CUnicodeEntry>());
91+
e->m_unicode = utf8;
92+
e->m_name = name;
93+
e->m_fuzzables = Fuzzy::createFuzzableStrings({name});
9494
std::ranges::transform(e->m_name, e->m_name.begin(), ::toupper);
9595
e->m_frequency = g_unicodeFinder->m_entryFrequencyCache->getCachedEntry(e->m_unicode);
9696
m_unicodeEntryCacheGeneric.emplace_back(e);

src/query/QueryProcessor.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,4 @@ class CQueryProcessor {
2828
WP<IFinder> m_overrideFinder;
2929
};
3030

31-
inline UP<CQueryProcessor> g_queryProcessor = makeUnique<CQueryProcessor>();
31+
inline UP<CQueryProcessor> g_queryProcessor = makeUnique<CQueryProcessor>();

0 commit comments

Comments
 (0)