@@ -2,6 +2,7 @@ use super::super::{
22 directory, script,
33 types:: { CommandType , TargetType } ,
44} ;
5+ use super :: builtins:: BUILTIN_COMMANDS ;
56use std:: { fs, io, path:: PathBuf } ;
67
78pub fn complete ( command_directory_path : & str , args : & [ String ] ) -> Result < String , String > {
@@ -32,30 +33,37 @@ pub fn complete(command_directory_path: &str, args: &[String]) -> Result<String,
3233 let remaining_args: Vec < _ > = args_peekable. collect ( ) ;
3334 return match target_type {
3435 TargetType :: Directory => {
35- let mut result = vec ! [ ] ;
36- let paths_raw: io:: Result < _ > = fs:: read_dir ( target. to_str ( ) . unwrap_or ( "" ) ) ;
37- // TODO(zph) deftly fix panics when this code path is triggered with empty string: ie sc dir_example bar<TAB>
38- // current implementation avoids the panic but is crude.
36+ let paths_raw: io:: Result < _ > = fs:: read_dir ( target. to_str ( ) . unwrap ( ) ) ;
3937 let mut paths: Vec < _ > = match paths_raw {
4038 Err ( _a) => return Err ( "Invalid argument to completion" . to_string ( ) ) ,
4139 Ok ( a) => a,
4240 }
43- . map ( |r| r. unwrap ( ) )
44- . collect ( ) ;
45- paths. sort_by_key ( |f| f. path ( ) ) ;
46- for path_buf in paths {
47- let path = path_buf. path ( ) ;
48- if path. is_dir ( ) && !directory:: is_tome_script_directory ( & path) {
49- continue ;
41+ . filter_map ( |r| match r {
42+ Ok ( path_buf) => {
43+ let path = path_buf. path ( ) ;
44+ if path. is_dir ( ) && !directory:: is_tome_script_directory ( & path) {
45+ return None ;
46+ }
47+ if path. is_file ( )
48+ && !script:: is_tome_script (
49+ path_buf. file_name ( ) . to_str ( ) . unwrap_or_default ( ) ,
50+ )
51+ {
52+ return None ;
53+ }
54+ Some ( path. file_name ( ) . unwrap ( ) . to_str ( ) . unwrap_or ( "" ) . to_owned ( ) )
5055 }
51- if path. is_file ( )
52- && !script:: is_tome_script ( path_buf. file_name ( ) . to_str ( ) . unwrap_or_default ( ) )
53- {
54- continue ;
56+ Err ( _) => None ,
57+ } )
58+ . collect ( ) ;
59+ // if this is the root directory, add the builtin commands
60+ if target. to_str ( ) . unwrap ( ) == command_directory_path {
61+ for command in BUILTIN_COMMANDS . keys ( ) {
62+ paths. push ( command. to_owned ( ) ) ;
5563 }
56- result. push ( path. file_name ( ) . unwrap ( ) . to_str ( ) . unwrap_or ( "" ) . to_owned ( ) ) ;
5764 }
58- Ok ( result. join ( " " ) )
65+ paths. sort_by_key ( |f| f. to_owned ( ) ) ;
66+ Ok ( paths. join ( " " ) )
5967 }
6068 TargetType :: File => match script:: Script :: load ( target. to_str ( ) . unwrap_or_default ( ) ) {
6169 Ok ( script) => script. get_execution_body ( CommandType :: Completion , & remaining_args) ,
0 commit comments