From 6c08b04b31382ba11fac3c5f0a76ff07c814d7bc Mon Sep 17 00:00:00 2001 From: "Alexis (Poliorcetics) Bourget" Date: Mon, 1 Apr 2024 18:54:45 +0200 Subject: [PATCH] feat: add autocompletion for diff-source command --- helix-term/src/commands/typed.rs | 2 +- helix-term/src/ui/mod.rs | 10 ++++++++++ helix-vcs/src/lib.rs | 29 ++++++++++++++++++++++++++++- 3 files changed, 39 insertions(+), 2 deletions(-) diff --git a/helix-term/src/commands/typed.rs b/helix-term/src/commands/typed.rs index 98856680eb3de..523c483d78e86 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..6e351c8fd67c1 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}; @@ -399,6 +400,15 @@ pub mod completers { }) } + 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() + } + #[derive(Copy, Clone, PartialEq, Eq)] enum FileMatch { /// Entry should be ignored diff --git a/helix-vcs/src/lib.rs b/helix-vcs/src/lib.rs index 50cb787b17f4b..29a0d0335306f 100644 --- a/helix-vcs/src/lib.rs +++ b/helix-vcs/src/lib.rs @@ -125,6 +125,25 @@ impl DiffSource { } }); } + + /// 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 => (), + #[cfg(feature = "git")] + Self::File => (), + Self::Git => (), + } + + // Keep it sorted alphabetically + &[ + "file", + #[cfg(feature = "git")] + "git", + "none", + ] + } } impl FromStr for DiffSource { @@ -136,7 +155,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}'"), } } } @@ -175,4 +194,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); + } }