Tab-complete command names (open, quit, write, help, finder, list)

This commit is contained in:
Greg Shuflin committed 2026-03-03 04:57:59 -08:00
1 parent 8064b52679
commit 28191314b0
1 file changed
+48 -8
+48 -8
View File
@@ -1,17 +1,57 @@
/// Completion logic for command-mode input.
///
/// Each completer handles one family of commands. Adding support for a new
/// command means adding a `complete_path_arg` call in `complete`.
/// Canonical command names used for name completion. Short aliases (`q`, `w`,
/// `h`, `ls`) are intentionally excluded — they're already minimal.
const COMMAND_NAMES: &[&str] = &["finder", "help", "list", "open", "quit", "write"];
/// Complete a partial command string, returning the completed version or `None`
/// if no completion is possible.
///
/// Currently supports path completion for:
/// - `open <path>`
/// - `w <path>` / `write <path>`
/// Dispatches on whether a space is present:
/// - No space yet → complete the command name.
/// - Space present → complete the path argument (for commands that take one).
pub(crate) fn complete(command_text: &str) -> Option<String> {
complete_path_arg(command_text, &["open"])
.or_else(|| complete_path_arg(command_text, &["w", "write"]))
if command_text.contains(' ') {
complete_path_arg(command_text, &["open"])
.or_else(|| complete_path_arg(command_text, &["w", "write"]))
} else {
complete_command_name(command_text)
}
}
/// Complete a partial command name against `COMMAND_NAMES`.
///
/// A single match is returned with a trailing space (ready to type an
/// argument). Multiple matches are reduced to their longest common prefix.
/// Returns `None` if there are no matches or no further progress is possible.
fn complete_command_name(partial: &str) -> Option<String> {
if partial.is_empty() {
return None;
}
let candidates: Vec<&str> = COMMAND_NAMES
.iter()
.copied()
.filter(|name| name.starts_with(partial))
.collect();
match candidates.len() {
0 => None,
1 => Some(format!("{} ", candidates[0])),
_ => {
let first = candidates[0];
let last = candidates[candidates.len() - 1];
let common_len = first
.chars()
.zip(last.chars())
.take_while(|(a, b)| a == b)
.count();
if common_len > partial.len() {
Some(first.chars().take(common_len).collect())
} else {
None // already at the longest common prefix
}
}
}
}
/// Complete the path argument of any command that takes a single path.