diff --git a/helix-term/src/commands/typed.rs b/helix-term/src/commands/typed.rs index 0e21d1c713354..3ae50db6b734c 100644 --- a/helix-term/src/commands/typed.rs +++ b/helix-term/src/commands/typed.rs @@ -3076,7 +3076,7 @@ pub const TYPABLE_COMMAND_LIST: &[TypableCommand] = &[ aliases: &[], doc: "Set the diff source for the file. If no argument is provided, show the current source.", fun: diff_source, - signature: CommandSignature::none(), + signature: CommandSignature::all(completers::diff_source), }, TypableCommand { name: "clear-register", diff --git a/helix-term/src/ui/mod.rs b/helix-term/src/ui/mod.rs index b5969818cf563..23794f60ebc25 100644 --- a/helix-term/src/ui/mod.rs +++ b/helix-term/src/ui/mod.rs @@ -259,6 +259,7 @@ pub mod completers { use crate::ui::prompt::Completion; use helix_core::fuzzy::fuzzy_match; use helix_core::syntax::LanguageServerFeature; + use helix_vcs::DiffSource; use helix_view::document::SCRATCH_BUFFER_NAME; use helix_view::theme; use helix_view::{editor::Config, Editor}; @@ -523,4 +524,13 @@ pub mod completers { .map(|(name, _)| ((0..), name.into())) .collect() } + + pub fn diff_source(_editor: &Editor, input: &str) -> Vec { + let iter = DiffSource::all_values(); + + fuzzy_match(input, iter, false) + .into_iter() + .map(|(&name, _)| ((0..), name.into())) + .collect() + } } diff --git a/helix-vcs/src/lib.rs b/helix-vcs/src/lib.rs index 5289ebb3ee9c3..2e8b87fe26f3f 100644 --- a/helix-vcs/src/lib.rs +++ b/helix-vcs/src/lib.rs @@ -71,6 +71,24 @@ impl DiffSource { Self::Git => Some(git::get_current_head_name(file)), } } + + /// Used to offer completion options for the `diff-source` command + pub fn all_values() -> &'static [&'static str] { + // To force a miscompilation and remember to add the value to the list + match Self::None { + Self::None => (), + Self::File => (), + Self::Git => (), + } + + // Keep it sorted alphabetically + &[ + "file", + #[cfg(feature = "git")] + "git", + "none", + ] + } } impl FromStr for DiffSource { @@ -82,7 +100,7 @@ impl FromStr for DiffSource { "file" => Ok(Self::File), #[cfg(feature = "git")] "git" => Ok(Self::Git), - s => bail!("invalid diff source '{s}', pick one of 'none', 'file' or 'git'"), + s => bail!("invalid diff source '{s}'"), } } } @@ -121,4 +139,12 @@ mod tests { #[cfg(feature = "git")] assert_eq!(DiffSource::Git.to_string(), "git"); } + + #[test] + fn test_diff_source_all_values() { + let all = DiffSource::all_values(); + let mut all_sorted = all.to_vec(); + all_sorted.sort_unstable(); + assert_eq!(all, all_sorted); + } }