Skip to content

Craft Anything: understand how any software works, and craft your own.

Rust

List slash commands and complete them with Tab (#228)

muraa-p694b41929e12
diff --git a/crates/ui-egui/src/i18n/zh-hans.tsv b/crates/ui-egui/src/i18n/zh-hans.tsv
index 314e821c..92046294 100644
--- a/crates/ui-egui/src/i18n/zh-hans.tsv
+++ b/crates/ui-egui/src/i18n/zh-hans.tsv
@@ -250,6 +250,7 @@ Code reconstructed from a program that looks like the C language. Close to what
Code signature 代码签名
Coding agent app 编程智能体应用
Command palette (Ctrl/⌘ K) 命令面板 (Ctrl/⌘ K)
+Commands: 命令:
Compare WebAssembly interfaces 比较 WebAssembly 接口
Compare module exports 比较模块导出
Compare two .NET builds 比较两个 .NET 构建
diff --git a/crates/ui-egui/src/i18n/zh-hant.tsv b/crates/ui-egui/src/i18n/zh-hant.tsv
index 613c41f4..77ca43a2 100644
--- a/crates/ui-egui/src/i18n/zh-hant.tsv
+++ b/crates/ui-egui/src/i18n/zh-hant.tsv
@@ -250,6 +250,7 @@ Code reconstructed from a program that looks like the C language. Close to what
Code signature 程式碼簽章
Coding agent app 程式設計代理應用程式
Command palette (Ctrl/⌘ K) 命令面板 (Ctrl/⌘ K)
+Commands: 指令:
Compare WebAssembly interfaces 比較 WebAssembly 介面
Compare module exports 比較模組匯出
Compare two .NET builds 比較兩個 .NET 建置
diff --git a/crates/ui-egui/src/prompt_box.rs b/crates/ui-egui/src/prompt_box.rs
index d6a86ec6..c4a6653c 100644
--- a/crates/ui-egui/src/prompt_box.rs
+++ b/crates/ui-egui/src/prompt_box.rs
@@ -157,6 +157,17 @@ pub fn show(ui: &mut Ui, text: &mut String, attachments: &[PathBuf], b: PromptBo
} else if let Some(history) = b.history.filter(|_| focused && !b.recall) {
browse(ui, id, text, history);
}
+ // What Tab would complete to, when the box starts with a partial command name. egui
+ // decides keyboard navigation at the start of the pass, so the field keeps Tab only
+ // while there is something to complete; anywhere else Tab moves focus as usual.
+ let completing = completed_command(text);
+ if focused
+ && let Some(completed) = &completing
+ && ui.input_mut(|i| i.consume_key(egui::Modifiers::NONE, egui::Key::Tab))
+ {
+ text.clone_from(completed);
+ cursor_to_end(ui.ctx(), id, text);
+ }
// No "more below" fade: the frame after each keystroke the scroll area briefly
// measures a few points of overflow, and the fade blinked over the line being typed.
ui.spacing_mut().scroll.fade.strength = 0.0;
@@ -170,13 +181,23 @@ pub fn show(ui: &mut Ui, text: &mut String, attachments: &[PathBuf], b: PromptBo
.desired_width(f32::INFINITY)
.font(egui::FontId::proportional(b.font_size))
// Enter sends; Shift+Enter starts a new line.
- .return_key(Some(egui::KeyboardShortcut::new(egui::Modifiers::SHIFT, egui::Key::Enter))),
+ .return_key(Some(egui::KeyboardShortcut::new(egui::Modifiers::SHIFT, egui::Key::Enter)))
+ .event_filter(egui::EventFilter {
+ tab: completing.is_some(),
+ horizontal_arrows: true,
+ vertical_arrows: true,
+ ..egui::EventFilter::default()
+ }),
);
widgets::named(r.clone(), b.name);
if r.has_focus() && ui.input(|i| i.key_pressed(egui::Key::Enter) && !i.modifiers.shift) && !text.trim().is_empty() {
out.send = true;
}
});
+ // Hint: available slash commands when typing one.
+ if text.trim_start().starts_with('/') && !text.trim().contains(' ') {
+ ui.label(RichText::new(format!("{} {}", tr("Commands:"), crate::slash::names())).size(theme::CAPTION).color(t.text_faint));
+ }
ui.add_space(4.0);
ui.horizontal(|ui| {
if let Some(tip) = b.attach
@@ -204,6 +225,23 @@ pub fn show(ui: &mut Ui, text: &mut String, attachments: &[PathBuf], b: PromptBo
out
}
+/// What Tab would complete the box to, when it starts with a partial command name. Only the
+/// name is replaced, so a question after it is kept; `None` when there is nothing to complete.
+fn completed_command(text: &str) -> Option<String> {
+ let trimmed = text.trim_start();
+ let rest = trimmed.strip_prefix('/')?;
+ let name = rest.split_whitespace().next().unwrap_or(rest);
+ if name.is_empty() || !name.chars().all(|c| c.is_ascii_alphanumeric() || c == '-') {
+ return None;
+ }
+ let full = crate::slash::complete(name)?;
+ if name.eq_ignore_ascii_case(full) {
+ return None;
+ }
+ let leading = text.len() - trimmed.len();
+ Some(format!("{}/{full} {}", &text[..leading], rest[name.len()..].trim_start()))
+}
+
/// The cursor is at this character, selecting nothing (or there's no text, or no cursor yet).
fn cursor_at(ctx: &egui::Context, id: egui::Id, text: &str, index: usize) -> bool {
text.is_empty()
diff --git a/crates/ui-egui/src/slash.rs b/crates/ui-egui/src/slash.rs
index c99e4034..67726eef 100644
--- a/crates/ui-egui/src/slash.rs
+++ b/crates/ui-egui/src/slash.rs
@@ -41,6 +41,15 @@ pub fn names() -> String {
COMMANDS.iter().map(|(n, _)| format!("/{n}")).collect::<Vec<_>>().join(", ")
}
+/// Complete a partial command name (without the leading slash) when there's exactly one match.
+pub fn complete(name: &str) -> Option<&'static str> {
+ if name.is_empty() {
+ return None;
+ }
+ let matches: Vec<&str> = COMMANDS.iter().filter(|(n, _)| n.starts_with(name)).map(|(n, _)| *n).collect();
+ if matches.len() == 1 { Some(matches[0]) } else { None }
+}
+
#[cfg(test)]
mod tests {
use super::*;
@@ -59,4 +68,12 @@ mod tests {
assert_eq!(read(text), Typed::Message, "{text}");
}
}
+
+ #[test]
+ fn a_partial_name_completes_only_when_one_command_matches() {
+ assert_eq!(complete("b"), Some("btw"));
+ assert_eq!(complete("bt"), Some("btw"));
+ assert_eq!(complete(""), None);
+ assert_eq!(complete("x"), None);
+ }
}
diff --git a/crates/ui-egui/tests/smoke.rs b/crates/ui-egui/tests/smoke.rs
index 6724cf7c..29b1428e 100644
--- a/crates/ui-egui/tests/smoke.rs
+++ b/crates/ui-egui/tests/smoke.rs
@@ -432,6 +432,27 @@ fn the_prompt_box_sends_on_enter_with_its_pictures() {
);
}
+#[test]
+fn tab_completes_a_slash_command_and_keeps_focus() {
+ use craft_anything_ai::rebuild::StandIns;
+ use craft_anything_engine::workspace::{TargetRef, Workspace};
+ let d = tempfile::tempdir().unwrap();
+ let a = Workspace::create(d.path(), "Alpha", "A notes app", TargetRef::None).unwrap();
+ let (dir, runs) = (d.path().to_path_buf(), StandIns::new());
+ let mut h = harness(move || stand_in_app(dir, &runs));
+ h.run_steps(2);
+ h.state_mut().open_workspace(a);
+ h.run_steps(2);
+ h.get_by_label("Ask for a change").focus();
+ h.run_steps(2);
+ h.get_by_label("Ask for a change").type_text("/b");
+ h.run_steps(2);
+ h.key_press(egui::Key::Tab);
+ h.run_steps(3);
+ assert_eq!(h.state().state.project_input, "/btw ");
+ assert!(h.get_by_label("Ask for a change").is_focused(), "the box keeps focus");
+}
+
#[test]
fn pictures_of_the_original_go_with_the_draft_then_into_the_project() {
use craft_anything_engine::workspace::{TargetRef, Workspace};
diff --git a/docs/ROADMAP.md b/docs/ROADMAP.md
index a07d9912..612a6f62 100644
--- a/docs/ROADMAP.md
+++ b/docs/ROADMAP.md
@@ -758,7 +758,7 @@ Gaps found while using the app for real projects.
- [x] Slash commands in a project's prompt box: `/btw` asks about the build in place of the old
`?` prefix; a new one is a `slash::Command` and a row in `COMMANDS`. A mistyped command stays
in the box rather than starting a build; a path like `/usr/bin` is a plain message
-- [ ] Typing `/` lists the commands, and Tab completes one
+- [x] Typing `/` lists the commands, and Tab completes one
- [x] Questions about a build show in the conversation where they were asked, so a long answer
scrolls with it and leaves the prompt box alone; each folds to one line, and they're kept in
the project (`questions.json`, the newest 200, `rebuild_questions_list`), in their place