Skip to content

Commit 881a066

Browse files
committed
feat: search by extension titles
1 parent dc3a15f commit 881a066

2 files changed

Lines changed: 59 additions & 21 deletions

File tree

src/Extension.luau

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,42 +12,51 @@ Extension.__index = Extension
1212

1313
Extension.allExtensions = Charm.atom({} :: { [Extension]: true })
1414

15-
Extension.allCommands = Charm.computed(function(): { Command.Command }
15+
Extension.sortedByTitle = Charm.computed(function(): { Extension }
1616
local result = {}
1717
for extension in Extension.allExtensions() do
18-
table.move(extension._commands, 1, #extension._commands, #result + 1, result)
18+
table.insert(result, extension)
1919
end
20-
table.sort(result, Command.sortByTitle)
20+
table.sort(result, function(lhs: Extension, rhs: Extension): boolean
21+
return lhs._metadata.title < rhs._metadata.title
22+
end)
2123
return result
2224
end)
2325

2426
Extension.extensionTitles = Charm.computed(function(): { string }
25-
local allExtensions = Extension.allExtensions()
27+
local allExtensions = Extension.sortedByTitle()
2628
local result = table.create(#allExtensions) :: { string }
27-
for ext in allExtensions do
29+
for _, ext in allExtensions do
2830
table.insert(result, ext._metadata.title)
2931
end
3032
table.sort(result)
3133
return result
3234
end)
3335

34-
Extension.commandTitles = Charm.computed(function(): { string }
36+
Extension.allCommands = Charm.computed(function(): { Command.Command }
37+
local result = {}
38+
for extension in Extension.allExtensions() do
39+
table.move(extension._commands, 1, #extension._commands, #result + 1, result)
40+
end
41+
table.sort(result, Command.sortByTitle)
42+
return result
43+
end)
44+
45+
Extension.commandToIndex = Charm.computed(function(): { [Command.Command]: number }
3546
local allCommands = Extension.allCommands()
36-
local result = table.create(#allCommands) :: { string }
37-
for i, cmd in allCommands do
38-
result[i] = cmd._metadata.title
47+
local result = table.create(#allCommands) :: { [Command.Command]: number }
48+
for index, command in allCommands do
49+
result[command] = index
3950
end
4051
return result
4152
end)
4253

43-
Extension.sortedByTitle = Charm.computed(function(): { Extension }
44-
local result = {}
45-
for extension in Extension.allExtensions() do
46-
table.insert(result, extension)
54+
Extension.commandTitles = Charm.computed(function(): { string }
55+
local allCommands = Extension.allCommands()
56+
local result = table.create(#allCommands) :: { string }
57+
for i, cmd in allCommands do
58+
result[i] = cmd._metadata.title
4759
end
48-
table.sort(result, function(lhs: Extension, rhs: Extension): boolean
49-
return lhs._metadata.title < rhs._metadata.title
50-
end)
5160
return result
5261
end)
5362

src/launcher/LauncherStore.luau

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,46 @@ LauncherStore.commandContext = nil :: CommandContext.CommandContext?
1818
LauncherStore.searchResults = Charm.computed(function(): { Command.Command }
1919
local search = LauncherStore.search()
2020
local allCommands = Extension.allCommands()
21+
local commandToIndex = Extension.commandToIndex()
22+
2123
local haystack = table.clone(allCommands)
2224
if search ~= "" then
23-
local results = fzy.filter(search, Extension.commandTitles(), false)
24-
table.sort(results, function(lhs: { number }, rhs: { number })
25-
return lhs[3] < rhs[3]
25+
local commandResults = fzy.filter(search, Extension.commandTitles(), false)
26+
27+
local extensions = Extension.sortedByTitle()
28+
local extensionResults = fzy.filter(search, Extension.extensionTitles(), false)
29+
30+
local combined: { [number]: { number } } = {}
31+
for _, result in commandResults do
32+
-- command titles has priority over extension titles
33+
result[3] *= 1000
34+
combined[result[1]] = result
35+
end
36+
37+
for _, result in extensionResults do
38+
for _, command in extensions[result[1]]._commands do
39+
local commandIndex = commandToIndex[command]
40+
local commandResult = combined[commandIndex]
41+
if commandResult then
42+
commandResult[3] += result[3]
43+
else
44+
combined[commandIndex] = { commandToIndex[command], 0, result[3] }
45+
end
46+
end
47+
end
48+
49+
table.sort(combined, function(lhs: { number }, rhs: { number })
50+
-- There are going to be gaps because some commands will be
51+
-- filtered.
52+
return if lhs and rhs then lhs[3] < rhs[3] else not not rhs
2653
end)
27-
haystack = table.create(#results) :: { Command.Command }
28-
for _, result in results do
54+
55+
haystack = table.create(#combined) :: { Command.Command }
56+
for _, result in combined do
2957
table.insert(haystack, allCommands[result[1]])
3058
end
3159
end
60+
3261
return haystack
3362
end)
3463

0 commit comments

Comments
 (0)