1use std::sync::LazyLock;
2
3use crate::skills::command_skill_specs;
4use crate::terminal_setup::detector::TerminalType;
5use crate::ui::search::{fuzzy_match, normalize_query};
6
7#[derive(Clone, Copy, Debug)]
9pub struct SlashCommandInfo {
10 pub name: &'static str,
12 pub description: &'static str,
14 pub category: &'static str,
16}
17
18pub static SLASH_COMMANDS: LazyLock<Vec<SlashCommandInfo>> = LazyLock::new(|| {
20 command_skill_specs()
21 .iter()
22 .map(|spec| SlashCommandInfo {
23 name: spec.slash_name,
24 description: spec.description,
25 category: spec.category(),
26 })
27 .collect()
28});
29
30fn detected_terminal_for_visibility() -> TerminalType {
31 TerminalType::detect().unwrap_or(TerminalType::Unknown)
32}
33
34fn command_visible_for_terminal(command: &SlashCommandInfo, terminal: TerminalType) -> bool {
35 command.name != "terminal-setup" || terminal.should_offer_terminal_setup()
36}
37
38pub fn visible_commands() -> Vec<&'static SlashCommandInfo> {
40 visible_commands_for_terminal(detected_terminal_for_visibility())
41}
42
43pub fn visible_commands_for_terminal(terminal: TerminalType) -> Vec<&'static SlashCommandInfo> {
45 SLASH_COMMANDS
46 .iter()
47 .filter(|command| command_visible_for_terminal(command, terminal))
48 .collect()
49}
50
51pub fn visible_commands_for_desktop() -> Vec<&'static SlashCommandInfo> {
56 SLASH_COMMANDS.iter().filter(|command| command.category != "terminal").collect()
57}
58
59pub fn find_visible_command(name: &str) -> Option<&'static SlashCommandInfo> {
61 let terminal = detected_terminal_for_visibility();
62 SLASH_COMMANDS
63 .iter()
64 .find(|command| command.name == name && command_visible_for_terminal(command, terminal))
65}
66
67pub fn find_command(name: &str) -> Option<&'static SlashCommandInfo> {
69 SLASH_COMMANDS.iter().find(|command| command.name == name)
70}
71
72pub fn suggestions_for_terminal(prefix: &str, terminal: TerminalType) -> Vec<&'static SlashCommandInfo> {
74 let visible = visible_commands_for_terminal(terminal);
75 suggestions_for_commands(prefix, &visible)
76}
77
78fn suggestions_for_commands(prefix: &str, commands: &[&'static SlashCommandInfo]) -> Vec<&'static SlashCommandInfo> {
79 let trimmed = prefix.trim();
80 if trimmed.is_empty() {
81 return commands.to_vec();
82 }
83
84 let query = trimmed.to_ascii_lowercase();
85
86 let mut prefix_matches: Vec<&SlashCommandInfo> =
87 commands.iter().copied().filter(|info| info.name.starts_with(&query)).collect();
88
89 if !prefix_matches.is_empty() {
90 prefix_matches.sort_by(|a, b| a.name.cmp(b.name));
91 return prefix_matches;
92 }
93
94 let mut substring_matches: Vec<(&SlashCommandInfo, usize)> = commands
95 .iter()
96 .copied()
97 .filter_map(|info| info.name.find(&query).map(|position| (info, position)))
98 .collect();
99
100 if !substring_matches.is_empty() {
101 substring_matches
102 .sort_by(|(a, pos_a), (b, pos_b)| (*pos_a, a.name.len(), a.name).cmp(&(*pos_b, b.name.len(), b.name)));
103 return substring_matches.into_iter().map(|(info, _)| info).collect();
104 }
105
106 let normalized_query = normalize_query(&query);
107 if normalized_query.is_empty() {
108 return commands.to_vec();
109 }
110
111 let mut scored: Vec<(&SlashCommandInfo, usize, usize)> = commands
112 .iter()
113 .copied()
114 .filter_map(|info| {
115 let mut candidate = info.name.to_ascii_lowercase();
123 let name_portion_end = candidate.len();
124 let desc_lower = if info.description.is_empty() {
125 String::new()
126 } else {
127 info.description.to_ascii_lowercase()
128 };
129 if !desc_lower.is_empty() {
130 candidate.push(' ');
131 candidate.push_str(&desc_lower);
132 }
133
134 if !fuzzy_match(&normalized_query, &candidate) {
135 return None;
136 }
137
138 let name_pos = candidate[..name_portion_end].find(&query).unwrap_or(usize::MAX);
139 let desc_pos = desc_lower.find(&query).unwrap_or(usize::MAX);
140
141 Some((info, name_pos, desc_pos))
142 })
143 .collect();
144
145 if scored.is_empty() {
146 return commands.to_vec();
147 }
148
149 scored.sort_by(|(a, name_pos_a, desc_pos_a), (b, name_pos_b, desc_pos_b)| {
150 let score_a = (*name_pos_a == usize::MAX, *name_pos_a, *desc_pos_a, a.name);
151 let score_b = (*name_pos_b == usize::MAX, *name_pos_b, *desc_pos_b, b.name);
152 score_a.cmp(&score_b)
153 });
154
155 scored.into_iter().map(|(info, _, _)| info).collect()
156}
157
158pub fn suggestions_for(prefix: &str) -> Vec<&'static SlashCommandInfo> {
160 suggestions_for_terminal(prefix, detected_terminal_for_visibility())
161}
162
163#[cfg(test)]
164mod tests {
165 use super::*;
166
167 fn names_for(prefix: &str) -> Vec<&'static str> {
168 suggestions_for(prefix).into_iter().map(|info| info.name).collect()
169 }
170
171 #[test]
172 fn prefix_matches_are_sorted_alphabetically() {
173 let names = names_for("c");
174 assert_eq!(names, vec!["clear", "compact", "config", "copy"]);
175 }
176
177 #[test]
178 fn substring_matches_prioritize_earlier_occurrences() {
179 let names = names_for("eme");
180 assert_eq!(names, vec!["theme"]);
181 }
182
183 #[test]
184 fn fuzzy_matches_include_description_keywords() {
185 let names = names_for("check");
186 assert!(names.contains(&"config"));
187 }
188
189 #[test]
190 fn fuzzy_matches_handle_non_contiguous_sequences() {
191 let names = names_for("sts");
192 assert!(names.contains(&"status"));
193 }
194
195 #[test]
196 fn prefix_matches_include_history_command() {
197 let names = names_for("his");
198 assert_eq!(names, vec!["history"]);
199 }
200
201 #[test]
202 fn prefix_matches_include_review_command() {
203 let names = names_for("rev");
204 assert_eq!(names, vec!["review"]);
205 }
206
207 #[test]
208 fn checkpoint_commands_are_visible_and_suggested() {
209 let visible = visible_commands_for_terminal(TerminalType::Unknown);
210 let suggestions = suggestions_for_terminal("re", TerminalType::Unknown);
211 for name in ["rewind", "redo"] {
212 assert!(visible.iter().any(|command| command.name == name), "{name} must be visible");
213 assert!(suggestions.iter().any(|command| command.name == name), "{name} must be suggested");
214 assert_eq!(find_command(name).expect("checkpoint command").name, name);
215 }
216 assert!(find_command("rewind-recover").is_none(), "rewind-recover must be removed; /rewind auto-recovers");
217 assert_eq!(names_for("redo"), vec!["redo"]);
218 }
219
220 #[test]
221 fn consolidated_panel_commands_route_via_config() {
222 assert!(names_for("task").is_empty() || !names_for("task").contains(&"tasks"));
223 assert!(names_for("job").is_empty() || !names_for("job").contains(&"jobs"));
224 assert!(find_command("config").is_some());
225 }
226
227 #[test]
228 fn terminal_setup_hidden_for_native_terminals() {
229 let names: Vec<&str> = visible_commands_for_terminal(TerminalType::WezTerm)
230 .into_iter()
231 .map(|info| info.name)
232 .collect();
233 assert!(!names.contains(&"terminal-setup"));
234
235 let names: Vec<&str> = visible_commands_for_terminal(TerminalType::ITerm2)
236 .into_iter()
237 .map(|info| info.name)
238 .collect();
239 assert!(!names.contains(&"terminal-setup"));
240
241 let names: Vec<&str> = visible_commands_for_terminal(TerminalType::WindowsTerminal)
242 .into_iter()
243 .map(|info| info.name)
244 .collect();
245 assert!(!names.contains(&"terminal-setup"));
246 }
247
248 #[test]
249 fn desktop_catalog_excludes_terminal_only_commands() {
250 let commands = visible_commands_for_desktop();
251 let names = commands.iter().map(|command| command.name).collect::<Vec<_>>();
252 for name in ["terminal-setup", "statusline", "title", "vim"] {
253 assert!(!names.contains(&name), "desktop catalog must not suggest /{name}");
254 }
255 for name in ["config", "model", "files", "review", "webmcp", "resume", "rewind"] {
256 assert!(names.contains(&name), "desktop catalog must retain /{name}");
257 }
258 assert!(commands.iter().all(|command| command.category != "terminal"));
259 }
260
261 #[test]
262 fn terminal_setup_visible_for_supported_setup_terminals() {
263 let names: Vec<&str> = visible_commands_for_terminal(TerminalType::VSCode)
264 .into_iter()
265 .map(|info| info.name)
266 .collect();
267 assert!(names.contains(&"terminal-setup"));
268
269 let names: Vec<&str> = visible_commands_for_terminal(TerminalType::Alacritty)
270 .into_iter()
271 .map(|info| info.name)
272 .collect();
273 assert!(names.contains(&"terminal-setup"));
274
275 let names: Vec<&str> = visible_commands_for_terminal(TerminalType::Zed)
276 .into_iter()
277 .map(|info| info.name)
278 .collect();
279 assert!(names.contains(&"terminal-setup"));
280 }
281
282 #[test]
283 fn support_commands_are_visible_and_suggested() {
284 for name in ["docs", "donate", "feedback"] {
285 assert!(find_command(name).is_some(), "/{name} must be registered");
286 let suggestions = suggestions_for_terminal(name, TerminalType::Unknown);
287 assert!(
288 suggestions.iter().any(|command| command.name == name),
289 "/{name} must be suggested for its exact prefix"
290 );
291 }
292 assert!(
293 visible_commands_for_desktop().iter().any(|command| command.name == "donate"),
294 "desktop catalog must retain /donate"
295 );
296 }
297
298 #[test]
299 fn doctor_is_visible_while_checkup_stays_a_hidden_alias() {
300 assert!(find_command("doctor").is_some(), "/doctor must be registered");
301 assert!(find_command("checkup").is_none(), "/checkup must stay a hidden alias for /doctor");
302 assert!(
303 suggestions_for_terminal("doc", TerminalType::Unknown)
304 .iter()
305 .any(|command| command.name == "doctor"),
306 "/doctor must be suggested for its prefix"
307 );
308 assert!(
309 visible_commands_for_desktop().iter().any(|command| command.name == "doctor"),
310 "desktop catalog must retain /doctor"
311 );
312 }
313
314 #[test]
315 fn config_consolidated_commands_are_not_registered() {
316 for removed in [
317 "checkup",
318 "permissions",
319 "ide",
320 "tasks",
321 "jobs",
322 "log",
323 "subprocess",
324 "notify",
325 ] {
326 assert!(find_command(removed).is_none(), "/{removed} must route via /config");
327 }
328 assert!(find_command("config").is_some());
329 }
330}