Skip to content
Merged
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 src/alias.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ impl<'src> Alias<'src> {
}
}

impl RecipeAlias<'_> {
impl<T> Alias<'_, T> {
pub(crate) fn is_public(&self) -> bool {
!self.name.lexeme().starts_with('_') && !self.attributes.private()
}
Expand Down
2 changes: 2 additions & 0 deletions src/arguments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,7 @@ pub(crate) struct Subcommand {
)]
pub(crate) choose: bool,
#[arg(
add = ArgValueCompleter::new(Completer::complete_recipe_or_module),
conflicts_with = "arguments",
help = "Clear recipe cache, optionally restricted to recipes whose path begins with <RECIPE_PATH>",
help_heading = Self::HEADING,
Expand Down Expand Up @@ -443,6 +444,7 @@ pub(crate) struct Subcommand {
)]
pub(crate) json: bool,
#[arg(
add = ArgValueCompleter::new(Completer::complete_module),
conflicts_with = "arguments",
help = "List available recipes in <MODULE> or root if omitted",
help_heading = Self::HEADING,
Expand Down
64 changes: 64 additions & 0 deletions src/completer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,37 @@ pub(crate) struct Completer<'run, 'src> {
}

impl<'run, 'src> Completer<'run, 'src> {
fn candidate_modules(&self) -> Vec<CompletionCandidate> {
let mut candidates = Vec::new();

for module in self.justfile.public_modules_recursive(&self.config) {
let path = module.module_path.to_string();

if path.starts_with(self.current) {
candidates.push(CompletionCandidate::new(path).help(module.doc.as_ref().map(Into::into)));
}
}

if self.config.complete_aliases {
for (alias, modulepath) in self.justfile.public_module_aliases_recursive(&self.config) {
let name = modulepath.join(alias.name.lexeme()).to_string();
if name.starts_with(self.current) {
candidates.push(
CompletionCandidate::new(name).help(
self
.justfile
.submodule(&alias.target)
.and_then(|module| module.doc.as_ref())
.map(Into::into),
),
);
}
}
}

candidates
}

fn candidate_recipes(&self) -> Vec<CompletionCandidate> {
let mut candidates = Vec::new();

Expand Down Expand Up @@ -67,6 +98,16 @@ impl<'run, 'src> Completer<'run, 'src> {
.collect()
}

pub(crate) fn complete_module(current: &OsStr) -> Vec<CompletionCandidate> {
let loader = Loader::new();

let Some(completer) = Completer::new(current, &loader) else {
return Vec::new();
};

completer.candidate_modules()
}

pub(crate) fn complete_recipe(current: &OsStr) -> Vec<CompletionCandidate> {
let loader = Loader::new();

Expand All @@ -77,6 +118,20 @@ impl<'run, 'src> Completer<'run, 'src> {
completer.candidate_recipes()
}

pub(crate) fn complete_recipe_or_module(current: &OsStr) -> Vec<CompletionCandidate> {
let loader = Loader::new();

let Some(completer) = Completer::new(current, &loader) else {
return Vec::new();
};

let mut candidates = completer.candidate_modules();

candidates.extend(completer.candidate_recipes());

candidates
}

pub(crate) fn complete_variable(current: &OsStr) -> Vec<CompletionCandidate> {
let loader = Loader::new();

Expand All @@ -98,6 +153,15 @@ impl<'run, 'src> Completer<'run, 'src> {

args.drain(1..3);

let index = env::var("_CLAP_COMPLETE_INDEX")
.ok()
.and_then(|index| index.parse::<usize>().ok())
.unwrap_or(args.len() - 1);

if (1..args.len()).contains(&index) {
args.remove(index);
}

let matches = Arguments::command()
.ignore_errors(true)
.try_get_matches_from(args)
Expand Down
38 changes: 38 additions & 0 deletions src/justfile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -752,6 +752,44 @@ impl<'src> Justfile<'src> {
aliases
}

pub(crate) fn public_modules_recursive(&self, config: &Config) -> Vec<&Justfile> {
let mut modules = Vec::new();

let mut stack = self.public_modules(config);
stack.reverse();
while let Some(current) = stack.pop() {
modules.push(current);

for module in current.public_modules(config).into_iter().rev() {
stack.push(module);
}
}

modules
}

pub(crate) fn public_module_aliases_recursive(
&self,
config: &Config,
) -> Vec<(&ModuleAlias<'_>, &Modulepath)> {
let mut aliases = Vec::new();

let mut stack = vec![self];
while let Some(current) = stack.pop() {
for alias in current.module_aliases.values() {
if alias.is_public() {
aliases.push((alias, &current.module_path));
}
}

for module in current.public_modules(config).into_iter().rev() {
stack.push(module);
}
}

aliases
}

pub(crate) fn groups(&self) -> Vec<&str> {
self
.groups
Expand Down
65 changes: 65 additions & 0 deletions tests/completions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -453,3 +453,68 @@ fn recipes_with_invalid_config() {
.stdout_regex("bar\nfoo\n.\njustfile\n--.*")
.success();
}

#[test]
fn list_modules() {
Test::new()
.justfile(
"
# doc
mod foo
",
)
.write("foo.just", "mod bar")
.write("bar.just", "baz:")
.shell(false)
.env("JUST_COMPLETE", "fish")
.args(complete_args(&["--list", ""]))
.stdout_regex("foo\tdoc\nfoo::bar\nfoo::bar::baz\n.\nbar.just\nfoo.just\njustfile\n--.*")
.success();
}

#[test]
fn list_module_aliases_not_completed_by_default() {
Test::new()
.justfile(
"
mod foo
alias f := foo
",
)
.write("foo.just", "")
.shell(false)
.env("JUST_COMPLETE", "fish")
.args(complete_args(&["--list", ""]))
.stdout_regex("foo\n.\nfoo.just\njustfile\n--.*")
.success();
}

#[test]
fn list_module_aliases_completed_with_flag() {
Test::new()
.justfile(
"
# doc
mod foo
alias f := foo
",
)
.write("foo.just", "")
.shell(false)
.env("JUST_COMPLETE", "fish")
.args(complete_args(&["--complete-aliases", "--list", ""]))
.stdout_regex("foo\tdoc\nf\tdoc\n.\nfoo.just\njustfile\n--.*")
.success();
}

#[test]
fn clean_recipes_and_modules() {
Test::new()
.justfile("mod foo")
.write("foo.just", "bar:")
.shell(false)
.env("JUST_COMPLETE", "fish")
.args(complete_args(&["--clean", ""]))
.stdout_regex("foo\nfoo::bar\nfoo::bar\n.\nfoo.just\njustfile\n--.*")
.success();
}
Loading