Skip to content

Commit 6c08b04

Browse files
committed
feat: add autocompletion for diff-source command
1 parent 2477079 commit 6c08b04

File tree

3 files changed

+39
-2
lines changed

3 files changed

+39
-2
lines changed

helix-term/src/commands/typed.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3076,7 +3076,7 @@ pub const TYPABLE_COMMAND_LIST: &[TypableCommand] = &[
30763076
aliases: &[],
30773077
doc: "Set the diff source for the file. If no argument is provided, show the current source.",
30783078
fun: diff_source,
3079-
signature: CommandSignature::none(),
3079+
signature: CommandSignature::all(completers::diff_source),
30803080
},
30813081
TypableCommand {
30823082
name: "clear-register",

helix-term/src/ui/mod.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,7 @@ pub mod completers {
259259
use crate::ui::prompt::Completion;
260260
use helix_core::fuzzy::fuzzy_match;
261261
use helix_core::syntax::LanguageServerFeature;
262+
use helix_vcs::DiffSource;
262263
use helix_view::document::SCRATCH_BUFFER_NAME;
263264
use helix_view::theme;
264265
use helix_view::{editor::Config, Editor};
@@ -399,6 +400,15 @@ pub mod completers {
399400
})
400401
}
401402

403+
pub fn diff_source(_editor: &Editor, input: &str) -> Vec<Completion> {
404+
let iter = DiffSource::all_values();
405+
406+
fuzzy_match(input, iter, false)
407+
.into_iter()
408+
.map(|(&name, _)| ((0..), name.into()))
409+
.collect()
410+
}
411+
402412
#[derive(Copy, Clone, PartialEq, Eq)]
403413
enum FileMatch {
404414
/// Entry should be ignored

helix-vcs/src/lib.rs

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,25 @@ impl DiffSource {
125125
}
126126
});
127127
}
128+
129+
/// Used to offer completion options for the `diff-source` command
130+
pub fn all_values() -> &'static [&'static str] {
131+
// To force a miscompilation and remember to add the value to the list
132+
match Self::None {
133+
Self::None => (),
134+
#[cfg(feature = "git")]
135+
Self::File => (),
136+
Self::Git => (),
137+
}
138+
139+
// Keep it sorted alphabetically
140+
&[
141+
"file",
142+
#[cfg(feature = "git")]
143+
"git",
144+
"none",
145+
]
146+
}
128147
}
129148

130149
impl FromStr for DiffSource {
@@ -136,7 +155,7 @@ impl FromStr for DiffSource {
136155
"file" => Ok(Self::File),
137156
#[cfg(feature = "git")]
138157
"git" => Ok(Self::Git),
139-
s => bail!("invalid diff source '{s}', pick one of 'none', 'file' or 'git'"),
158+
s => bail!("invalid diff source '{s}'"),
140159
}
141160
}
142161
}
@@ -175,4 +194,12 @@ mod tests {
175194
#[cfg(feature = "git")]
176195
assert_eq!(DiffSource::Git.to_string(), "git");
177196
}
197+
198+
#[test]
199+
fn test_diff_source_all_values() {
200+
let all = DiffSource::all_values();
201+
let mut all_sorted = all.to_vec();
202+
all_sorted.sort_unstable();
203+
assert_eq!(all, all_sorted);
204+
}
178205
}

0 commit comments

Comments
 (0)