@@ -40,6 +40,24 @@ function _bash-it-component-completion-callback-on-init-aliases() {
4040		line=" ${line# alias -- } " 
4141		line=" ${line# alias } " 
4242		alias_name=" ${line%% =* } " 
43+ 
44+ 		#  Skip aliases not added by this script that already have completion functions.
45+ 		#  This allows users to define their own alias completion functions.
46+ 		#  For aliases added by this script, we do want to replace them in case the
47+ 		#  alias getting the completion added has changed.
48+ 		if  complete  -p " $alias_name " & >  /dev/null;  then 
49+ 			#  Get the -F argument from the existing completion for this alias.
50+ 			aliasCommandFunction=$( complete -p " $alias_name " |  rev |  cut -d "  " |  rev) 
51+ 			#  Check if aliasCommandFunction starts with our namespace.
52+ 			if  [[ " $aliasCommandFunction " !=  " _${namespace} ::" *  ]];  then 
53+ 				continue 
54+ 			fi 
55+ 
56+ 			#  Remove existing completion. It will be replaced by the new one. We need to
57+ 			#  delete it in case the new alias does not support having completion added.
58+ 			complete  -r " $alias_name " 
59+ 		fi 
60+ 
4361		alias_defn=" ${line#* =\' } " #  alias definition
4462		alias_defn=" ${alias_defn% \' } " 
4563		alias_cmd=" ${alias_defn%% [[:space:]]* } " #  first word of alias
@@ -71,15 +89,20 @@ function _bash-it-component-completion-callback-on-init-aliases() {
7189		fi 
7290		new_completion=" $( complete -p " $alias_cmd " 2>  /dev/null) " 
7391
74- 		#  create a wrapper inserting the alias arguments if any
75- 		if  [[ -n  $alias_args  ]];  then 
76- 			compl_func=" ${new_completion/#*  -F / } " 
77- 			compl_func=" ${compl_func%%  * } " 
78- 			#  avoid recursive call loops by ignoring our own functions
79- 			if  [[ " ${compl_func# _" $namespace " :: } " ==  " $compl_func " ;  then 
80- 				compl_wrapper=" _${namespace} ::${alias_name} " 
92+ 		compl_func=" ${new_completion/#*  -F / } " 
93+ 		compl_func=" ${compl_func%%  * } " 
94+ 		#  avoid recursive call loops by ignoring our own functions
95+ 		if  [[ " ${compl_func# _" $namespace " :: } " ==  " $compl_func " ;  then 
96+ 			compl_wrapper=" _${namespace} ::${alias_name} " 
8197
82- 				#  Create a wrapper function for the alias
98+ 			if  [[ -z  $alias_args  ]];  then 
99+ 				#  Create a wrapper without arguments.
100+ 				#  This allows identifying the completions added by this script on reload.
101+ 				echo  " function $compl_wrapper  {
102+ 				$compl_func  \"\$ @\"  
103+ 				}"   >>  " $tmp_file " 
104+ 			else 
105+ 				#  Create a wrapper inserting the alias arguments
83106				#  The use of printf on alias_arg_words is needed to ensure each element of
84107				#  the array is quoted. E.X. (one two three) -> ('one' 'two' 'three')
85108				echo  " function $compl_wrapper  {
@@ -99,8 +122,8 @@ function _bash-it-component-completion-callback-on-init-aliases() {
99122                        (( COMP_POINT += \$ {#COMP_LINE} )) 
100123                        \" $compl_func \"  \" $alias_cmd \"  \"\$ compl_word\"  \"\$ prec_word\"  
101124                    }"   >>  " $tmp_file " 
102- 				new_completion=" ${new_completion/  -F $compl_func  /  -F $compl_wrapper  } " 
103125			fi 
126+ 			new_completion=" ${new_completion/  -F $compl_func  /  -F $compl_wrapper  } " 
104127		fi 
105128
106129		#  replace completion trigger by alias
0 commit comments