@@ -21,6 +21,10 @@ export class AutoCompletionService implements IAutoCompletionService {
2121 "###-begin-%s-completion-###" ;
2222 private static TABTAB_COMPLETION_END_REGEX_PATTERN =
2323 "###-end-%s-completion-###" ;
24+ private static GENERATED_TABTAB_COMPLETION_END =
25+ "###-end-ns-completions-section-###" ;
26+ private static GENERATED_TABTAB_COMPLETION_START =
27+ "###-begin-ns-completions-section-###" ;
2428
2529 constructor (
2630 private $fs : IFileSystem ,
@@ -70,6 +74,16 @@ export class AutoCompletionService implements IAutoCompletionService {
7074 return tabTabRegex ;
7175 }
7276
77+ private getTabTabCompletionsRegex ( ) : RegExp {
78+ return new RegExp (
79+ util . format (
80+ "%s[\\s\\S]*%s" ,
81+ AutoCompletionService . GENERATED_TABTAB_COMPLETION_START ,
82+ AutoCompletionService . GENERATED_TABTAB_COMPLETION_END
83+ )
84+ ) ;
85+ }
86+
7387 private removeObsoleteAutoCompletion ( ) : void {
7488 // In previous releases we were writing directly in .bash_profile, .bashrc, .zshrc and .profile - remove this old code
7589 const shellProfilesToBeCleared = this . shellProfiles ;
@@ -107,6 +121,43 @@ export class AutoCompletionService implements IAutoCompletionService {
107121 } ) ;
108122 }
109123
124+ private removeOboleteTabTabCompletion ( text : string ) {
125+ try {
126+ let newText = text . replace ( this . getTabTabObsoleteRegex ( "ns" ) , "" ) ;
127+
128+ newText = newText . replace ( this . getTabTabObsoleteRegex ( "nsc" ) , "" ) ;
129+
130+ newText = newText . replace (
131+ this . getTabTabObsoleteRegex ( "nativescript" ) ,
132+ ""
133+ ) ;
134+
135+ newText = newText . replace ( this . getTabTabObsoleteRegex ( "tns" ) , "" ) ;
136+
137+ return newText ;
138+ } catch ( error ) {
139+ this . $logger . trace (
140+ "Error while trying to disable autocompletion for '%s' file. Error is:\n%s" ,
141+ error . toString ( )
142+ ) ;
143+
144+ return text ;
145+ }
146+ }
147+
148+ @cache ( )
149+ private get completionAliasDefinition ( ) {
150+ const pattern = "compdef _nativescript.js_yargs_completions %s" ;
151+ const ns = util . format ( pattern , "ns" ) ;
152+ const tns = util . format ( pattern , "tns" ) ;
153+ return util . format (
154+ "\n%s\n%s\n%s\n" ,
155+ ns ,
156+ tns ,
157+ AutoCompletionService . GENERATED_TABTAB_COMPLETION_END
158+ ) ;
159+ }
160+
110161 @cache ( )
111162 private get completionShellScriptContent ( ) {
112163 const startText = util . format (
@@ -284,24 +335,9 @@ export class AutoCompletionService implements IAutoCompletionService {
284335 if ( this . $fs . exists ( filePath ) ) {
285336 const contents = this . $fs . readText ( filePath ) ;
286337 const regExp = new RegExp (
287- util . format (
288- "%s\\s+completion\\s+--\\s+" ,
289- this . $staticConfig . CLIENT_NAME . toLowerCase ( )
290- )
338+ AutoCompletionService . GENERATED_TABTAB_COMPLETION_START
291339 ) ;
292340 let matchCondition = contents . match ( regExp ) ;
293- if ( this . $staticConfig . CLIENT_NAME_ALIAS ) {
294- matchCondition =
295- matchCondition ||
296- contents . match (
297- new RegExp (
298- util . format (
299- "%s\\s+completion\\s+--\\s+" ,
300- this . $staticConfig . CLIENT_NAME_ALIAS . toLowerCase ( )
301- )
302- )
303- ) ;
304- }
305341
306342 if ( matchCondition ) {
307343 doUpdate = false ;
@@ -316,9 +352,33 @@ export class AutoCompletionService implements IAutoCompletionService {
316352 __dirname ,
317353 `../../../bin/${ clientExecutableFileName } .js`
318354 ) ;
355+
356+ if ( this . $fs . exists ( filePath ) ) {
357+ const existingText = this . $fs . readText ( filePath ) ;
358+ let newText = existingText . replace (
359+ this . getTabTabCompletionsRegex ( ) ,
360+ ""
361+ ) ;
362+ newText = this . removeOboleteTabTabCompletion ( newText ) ;
363+ if ( newText !== existingText ) {
364+ this . $logger . trace (
365+ "Remove existing AutoCompletion from file %s." ,
366+ filePath
367+ ) ;
368+ this . $fs . writeFile ( filePath , newText ) ;
369+ }
370+ }
371+ // The generated seems to be inconsistent in it's start/end markers so adding our own.
372+
373+ this . $fs . appendFile (
374+ filePath ,
375+ `\n${ AutoCompletionService . GENERATED_TABTAB_COMPLETION_START } \n`
376+ ) ;
319377 await this . $childProcess . exec (
320- `"${ process . argv [ 0 ] } " "${ pathToExecutableFile } " completion >> "${ filePath } "`
378+ `"${ process . argv [ 0 ] } " "${ pathToExecutableFile } " completion_generate_script >> "${ filePath } "`
321379 ) ;
380+ this . $fs . appendFile ( filePath , this . completionAliasDefinition ) ;
381+
322382 this . $fs . chmod ( filePath , "0644" ) ;
323383 }
324384 } catch ( err ) {
0 commit comments