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 Apr 10, 2024
1 parent b6e4762 commit ef87854
Show file tree
Hide file tree
Showing 3 changed files with 39 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 @@ -399,6 +400,15 @@ pub mod completers {
})
}

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()
}

#[derive(Copy, Clone, PartialEq, Eq)]
enum FileMatch {
/// Entry should be ignored
Expand Down
29 changes: 28 additions & 1 deletion helix-vcs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,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 {
Expand All @@ -132,7 +151,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 @@ -171,4 +190,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 ef87854

Please sign in to comment.