33//! This module defines the CLI structure using clap and routes commands
44//! to their appropriate handlers.
55
6- use std:: process:: ExitStatus ;
6+ use std:: { ffi :: OsStr , process:: ExitStatus } ;
77
88use clap:: { CommandFactory , FromArgMatches , Parser , Subcommand } ;
9- use clap_complete:: ArgValueCandidates ;
9+ use clap_complete:: ArgValueCompleter ;
1010use tokio:: runtime:: Runtime ;
1111use vite_install:: commands:: {
1212 add:: SaveDependencyType , install:: InstallCommandOptions , outdated:: Format ,
@@ -617,7 +617,7 @@ pub enum Commands {
617617 #[ command( disable_help_flag = true ) ]
618618 Run {
619619 /// Additional arguments
620- #[ arg( trailing_var_arg = true , allow_hyphen_values = true , add = ArgValueCandidates :: new( run_tasks_completions) ) ]
620+ #[ arg( trailing_var_arg = true , allow_hyphen_values = true , add = ArgValueCompleter :: new( run_tasks_completions) ) ]
621621 args : Vec < String > ,
622622 } ,
623623
@@ -1487,12 +1487,18 @@ fn should_force_global_delegate(command: &str, args: &[String]) -> bool {
14871487/// Delegates to the local vite-plus CLI to run `vp run` without arguments,
14881488/// which returns a list of available tasks in the format "task_name: description".
14891489
1490- fn run_tasks_completions ( ) -> Vec < clap_complete:: CompletionCandidate > {
1490+ fn run_tasks_completions ( current : & OsStr ) -> Vec < clap_complete:: CompletionCandidate > {
14911491 let cwd = match std:: env:: current_dir ( ) {
14921492 Ok ( cwd) => cwd,
14931493 Err ( _) => return vec ! [ ] ,
14941494 } ;
14951495
1496+ let current = current
1497+ . to_string_lossy ( )
1498+ . replace ( "\\ #" , "#" )
1499+ . trim_matches ( |c| c == '"' || c == '\'' )
1500+ . to_string ( ) ;
1501+
14961502 let output = tokio:: task:: block_in_place ( || {
14971503 Runtime :: new ( ) . ok ( ) . and_then ( |rt| {
14981504 let cwd = AbsolutePathBuf :: new ( cwd) ?;
@@ -1507,6 +1513,7 @@ fn run_tasks_completions() -> Vec<clap_complete::CompletionCandidate> {
15071513 . lines ( )
15081514 . filter_map ( |line| line. split_once ( ": " ) . map ( |( name, _) | name. trim ( ) ) )
15091515 . filter ( |name| !name. is_empty ( ) )
1516+ . filter ( |name| name. starts_with ( & current) || current. is_empty ( ) )
15101517 . map ( |name| clap_complete:: CompletionCandidate :: new ( name. to_string ( ) ) )
15111518 . collect ( )
15121519 } )
0 commit comments