Skip to content

Commit df8b27b

Browse files
committed
fix(launcher): Improve search algorithm speed and quality
1 parent 01916fc commit df8b27b

File tree

1 file changed

+26
-19
lines changed

1 file changed

+26
-19
lines changed

src/dialog_launcher.ts

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,16 @@ export class Launcher extends search.Search {
5151
this.stop_services(ext)
5252
};
5353

54-
let search = (pattern: string): Array<launch.SearchOption> | null => {
54+
let search = (pat: string): Array<launch.SearchOption> | null => {
5555
this.options.splice(0)
5656

57-
if (pattern.length == 0) {
57+
if (pat.length == 0) {
5858
this.list_workspace(ext);
5959
return this.options
6060
}
6161

62+
const pattern = pat.toLowerCase()
63+
6264
this.last_plugin = null
6365

6466
let windows = new Array()
@@ -87,7 +89,7 @@ export class Launcher extends search.Search {
8789
}
8890
})
8991

90-
const needles = pattern.toLowerCase().split(' ');
92+
const needles = pattern.split(' ');
9193

9294
const contains_pattern = (haystack: string, needles: Array<string>): boolean => {
9395
const hay = haystack.toLowerCase();
@@ -106,17 +108,17 @@ export class Launcher extends search.Search {
106108

107109
// Filter matching desktop apps
108110
for (const [where, app] of this.desktop_apps) {
109-
const retain = contains_pattern(app.name(), needles)
110-
|| contains_pattern(app.desktop_name, needles)
111-
|| lib.ok(app.generic_name(), (s) => contains_pattern(s, needles))
112-
|| lib.ok(app.comment(), (s) => contains_pattern(s, needles))
113-
|| lib.ok(app.categories(), (s) => contains_pattern(s, needles));
111+
const name = app.name()
112+
const name_match = name.toLowerCase()
113+
const retain = name_match.startsWith(pattern)
114+
|| name_match.includes(pattern)
115+
|| levenshtein.compare(name_match, pattern) < 3
114116

115117
if (retain) {
116118
const generic = app.generic_name();
117119

118120
const button = new launch.SearchOption(
119-
app.name(),
121+
name,
120122
generic ? generic + " — " + where : where,
121123
'application-default-symbolic',
122124
{ gicon: app.icon() },
@@ -134,21 +136,26 @@ export class Launcher extends search.Search {
134136
const a_name = a.title.toLowerCase()
135137
const b_name = b.title.toLowerCase()
136138

137-
const pattern_lower = pattern.toLowerCase()
138-
139-
let a_name_weight = levenshtein.compare(pattern_lower, a_name)
140-
141-
let b_name_weight = levenshtein.compare(pattern_lower, b_name)
139+
let a_name_weight = 0, b_name_weight = 0;
142140

143-
if (a.description) {
144-
a_name_weight = Math.min(a_name_weight, levenshtein.compare(pattern_lower, a.description.toLowerCase()))
141+
if (!a_name.startsWith(pattern)) {
142+
a_name_weight = levenshtein.compare(a_name, pattern)
143+
if (a.description) {
144+
a_name_weight = Math.min(a_name_weight, levenshtein.compare(pattern, a.description.toLowerCase()))
145+
}
145146
}
146147

147-
if (b.description) {
148-
b_name_weight = Math.min(b_name_weight, levenshtein.compare(pattern_lower, b.description.toLowerCase()))
148+
if (!b_name.startsWith(pattern)) {
149+
b_name_weight = levenshtein.compare(b_name, pattern)
150+
151+
if (b.description) {
152+
b_name_weight = Math.min(b_name_weight, levenshtein.compare(pattern, b.description.toLowerCase()))
153+
}
149154
}
150155

151-
return a_name_weight > b_name_weight ? 1 : 0
156+
return a_name_weight === b_name_weight
157+
? a_name.length > b_name.length ? 1 : 0
158+
: a_name_weight > b_name_weight ? 1 : 0
152159
}
153160

154161
// Sort the list of matched selections

0 commit comments

Comments
 (0)