Skip to content

Commit

Permalink
feat: add autocompletion for diff-source command
Browse files Browse the repository at this point in the history
  • Loading branch information
poliorcetics committed Mar 27, 2024
1 parent 008db36 commit de43777
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 2 deletions.
2 changes: 1 addition & 1 deletion helix-term/src/commands/typed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
10 changes: 10 additions & 0 deletions helix-term/src/ui/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -523,4 +524,13 @@ pub mod completers {
.map(|(name, _)| ((0..), name.into()))
.collect()
}

pub fn diff_source(_editor: &Editor, input: &str) -> Vec<Completion> {
let iter = DiffSource::all_values();

fuzzy_match(input, iter, false)
.into_iter()
.map(|(&name, _)| ((0..), name.into()))
.collect()
}
}
28 changes: 27 additions & 1 deletion helix-vcs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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}'"),
}
}
}
Expand Down Expand Up @@ -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);
}
}

0 comments on commit de43777

Please sign in to comment.