Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion completions/just.fish
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
function __fish_just_complete_recipes
if string match -rq '(-f|--justfile)\s*=?(?<justfile>[^\s]+)' -- (string split -- ' -- ' (commandline -pc))[1]
if string match -rq '(^|\s)(--justfile(\s+|=)|-f(=|\s*))(?<justfile>[^\s]+)' -- (string split -- ' -- ' (commandline -pc))[1]
set -fx JUST_JUSTFILE "$justfile"
end
printf "%s\n" (string split " " (just --summary))
Expand Down
47 changes: 46 additions & 1 deletion src/completions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,27 @@ impl Shell {
#[cfg(test)]
mod tests {
use {
snafu::{OptionExt, ResultExt, Whatever},
super::*,
pretty_assertions::assert_eq,
std::io::{Read, Seek},
tempfile::tempfile,
};

const FISH_JUSTFILE_REGEX: &str =
r"(^|\s)(--justfile(\s+|=)|-f(=|\s*))(?<justfile>[^\s]+)";

fn justfile_capture<'a>(regex: &'a Regex, input: &'a str) -> Result<&'a str, Whatever> {
let captures = regex
.captures(input)
.with_whatever_context(|| format!("missing justfile match for `{input}`"))?;

Ok(captures
.name("justfile")
.with_whatever_context(|| format!("missing named justfile capture for `{input}`"))?
.as_str())
}

#[test]
fn scripts() {
fs::create_dir_all("tmp/completions").unwrap();
Expand All @@ -59,6 +74,36 @@ mod tests {
assert_eq!(Shell::Zsh.script(), zsh);
}

#[test]
fn fish_asset_contains_shared_justfile_regex() {
assert!(
Shell::Fish.script().contains(FISH_JUSTFILE_REGEX),
"checked-in fish completion should embed the shared justfile regex"
);
}

#[test]
fn fish_justfile_regex_does_not_match_recipe_names() -> Result<(), Whatever> {
let regex = Regex::new(FISH_JUSTFILE_REGEX)
.with_whatever_context(|_| "failed to compile fish justfile regex")?;

assert_eq!(justfile_capture(&regex, "just -ffoo --summary")?, "foo");
assert_eq!(justfile_capture(&regex, "just -f foo --summary")?, "foo");
assert_eq!(justfile_capture(&regex, "just -f=foo --summary")?, "foo");
assert_eq!(justfile_capture(&regex, "just --justfile=foo --summary")?, "foo");
assert_eq!(justfile_capture(&regex, "just --justfile foo --summary")?, "foo");
assert!(
regex.captures("just update-fast-input").is_none(),
"fish justfile regex should not treat recipe names containing -f as justfile flags"
);
assert!(
regex.captures("just prefix-fake").is_none(),
"fish justfile regex should not match unrelated tokens containing -f"
);

Ok(())
}

fn clap(shell: clap_complete::Shell) -> String {
fn replace(haystack: &mut String, needle: &str, replacement: &str) {
if let Some(index) = haystack.find(needle) {
Expand Down Expand Up @@ -115,7 +160,7 @@ mod tests {
}

const FISH_RECIPE_COMPLETIONS: &str = r#"function __fish_just_complete_recipes
if string match -rq '(-f|--justfile)\s*=?(?<justfile>[^\s]+)' -- (string split -- ' -- ' (commandline -pc))[1]
if string match -rq '(^|\s)(--justfile(\s+|=)|-f(=|\s*))(?<justfile>[^\s]+)' -- (string split -- ' -- ' (commandline -pc))[1]
set -fx JUST_JUSTFILE "$justfile"
end
printf "%s\n" (string split " " (just --summary))
Expand Down