Skip to content
Open
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
3 changes: 3 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ base64 = "0.23.0"
cargo_toml = "1.0.0"
chrono = "0.4.40"
clap = { version = "4.6.1", default-features = false }
clap_complete = "4.6.3"
clap_complete = { version = "4.6.3", features = ["unstable-dynamic"] }
clap_complete_nushell = "4.6.0"
clap-verbosity-flag = "3.0.2"
cmp_any = "0.8.1"
Expand Down
120 changes: 117 additions & 3 deletions crates/pixi_cli/src/completion.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
use crate::Args as CommandArgs;
use clap::{Arg, Command, CommandFactory, Parser, ValueEnum};
use clap_complete::{Generator, shells};
use clap_complete::{
Generator,
engine::{ArgValueCompleter, CompletionCandidate, ValueCompleter},
shells,
};
use clap_complete_nushell::Nushell;
use miette::IntoDiagnostic;
use pixi_core::WorkspaceLocator;
use regex::{Captures, Regex};
use std::collections::HashMap;
use std::collections::{BTreeSet, HashMap};
use std::ffi::OsStr;
use std::io::Write;
use std::iter;
use std::sync::LazyLock;
Expand Down Expand Up @@ -93,6 +99,76 @@ impl Generator for Shell {
}
}

/// Handle native, runtime completion requests made through `PIXI_COMPLETE`.
pub fn complete() {
clap_complete::CompleteEnv::with_factory(dynamic_completion_command)
.var("PIXI_COMPLETE")
.bin("pixi")
.complete();
}

fn dynamic_completion_command() -> Command {
dynamic_completion_command_with_tasks(current_workspace_task_names())
}

fn dynamic_completion_command_with_tasks(task_names: Vec<String>) -> Command {
CommandArgs::command().mut_subcommand("run", move |command| {
command.trailing_var_arg(false).mut_arg("task", move |arg| {
// The runtime parser stores the task and its arguments in one
// trailing Vec. Narrow only the completion model to its first
// value, otherwise clap offers task names for task arguments too.
arg.action(clap::ArgAction::Set)
.num_args(1)
.trailing_var_arg(false)
.add(ArgValueCompleter::new(TaskNameCompleter { task_names }))
})
})
}

fn current_workspace_task_names() -> Vec<String> {
// Workspace-aware candidates are best-effort. A missing or invalid
// workspace must not disable clap's normal command and option completion.
let Ok(workspace) = WorkspaceLocator::for_cli()
.with_emit_warnings(false)
.locate()
else {
return Vec::new();
};

workspace
.environments()
.into_iter()
.flat_map(|environment| environment.get_filtered_tasks())
.map(|task| task.as_str().to_owned())
.collect::<BTreeSet<_>>()
.into_iter()
.collect()
}

struct TaskNameCompleter {
task_names: Vec<String>,
}

impl TaskNameCompleter {
fn candidates(&self, current: &OsStr) -> Vec<CompletionCandidate> {
let Some(prefix) = current.to_str() else {
return Vec::new();
};

self.task_names
.iter()
.filter(|task| task.starts_with(prefix))
.map(CompletionCandidate::new)
.collect()
}
}

impl ValueCompleter for TaskNameCompleter {
fn complete(&self, current: &OsStr) -> Vec<CompletionCandidate> {
self.candidates(current)
}
}

/// Generate completions for the pixi cli, and print those to the stdout
pub fn execute(args: Args) -> miette::Result<()> {
let cli = Cli::new();
Expand Down Expand Up @@ -708,7 +784,7 @@ fn nushell_completion(cli: &Cli, cmd: &str, long: &str) -> Option<String> {

#[cfg(test)]
mod tests {
use std::collections::HashSet;
use std::{collections::HashSet, ffi::OsString};

use clap::ValueHint;

Expand All @@ -724,6 +800,44 @@ mod tests {
Cli::with_bin_name("pixi")
}

fn dynamic_candidates(args: &[&str], index: usize) -> HashSet<String> {
let mut command = dynamic_completion_command_with_tasks(vec![
"build-docs".to_owned(),
"lint".to_owned(),
"test".to_owned(),
]);
clap_complete::engine::complete(
&mut command,
args.iter().map(OsString::from).collect(),
index,
None,
)
.expect("dynamic completion should succeed")
.into_iter()
.map(|candidate| candidate.get_value().to_string_lossy().into_owned())
.collect()
}

#[test]
fn test_dynamic_completion_uses_the_full_clap_command_tree() {
let root = dynamic_candidates(&["pixi", "work"], 1);
assert!(root.contains("workspace"));

let nested = dynamic_candidates(&["pixi", "workspace", "plat"], 2);
assert!(nested.contains("platform"));
}

#[test]
fn test_dynamic_completion_adds_current_workspace_tasks_to_run() {
let tasks = dynamic_candidates(&["pixi", "run", "bu"], 2);
assert_eq!(tasks, HashSet::from(["build-docs".to_owned()]));

let task_arguments = dynamic_candidates(&["pixi", "run", "build-docs", ""], 3);
assert!(!task_arguments.contains("build-docs"));
assert!(!task_arguments.contains("lint"));
assert!(!task_arguments.contains("test"));
}

#[test]
pub(crate) fn test_zsh_completion() {
let script = r#"
Expand Down
2 changes: 2 additions & 0 deletions crates/pixi_cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,8 @@ impl LockFileUsageConfig {
}

pub async fn execute() -> miette::Result<()> {
completion::complete();

let args = Args::parse();

// Extract values we need before moving args
Expand Down
Loading