@@ -21,6 +21,10 @@ export class AutoCompletionService implements IAutoCompletionService {
21
21
"###-begin-%s-completion-###" ;
22
22
private static TABTAB_COMPLETION_END_REGEX_PATTERN =
23
23
"###-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-###" ;
24
28
25
29
constructor (
26
30
private $fs : IFileSystem ,
@@ -70,6 +74,16 @@ export class AutoCompletionService implements IAutoCompletionService {
70
74
return tabTabRegex ;
71
75
}
72
76
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
+
73
87
private removeObsoleteAutoCompletion ( ) : void {
74
88
// In previous releases we were writing directly in .bash_profile, .bashrc, .zshrc and .profile - remove this old code
75
89
const shellProfilesToBeCleared = this . shellProfiles ;
@@ -107,6 +121,43 @@ export class AutoCompletionService implements IAutoCompletionService {
107
121
} ) ;
108
122
}
109
123
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
+
110
161
@cache ( )
111
162
private get completionShellScriptContent ( ) {
112
163
const startText = util . format (
@@ -284,24 +335,9 @@ export class AutoCompletionService implements IAutoCompletionService {
284
335
if ( this . $fs . exists ( filePath ) ) {
285
336
const contents = this . $fs . readText ( filePath ) ;
286
337
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
291
339
) ;
292
340
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
- }
305
341
306
342
if ( matchCondition ) {
307
343
doUpdate = false ;
@@ -316,9 +352,33 @@ export class AutoCompletionService implements IAutoCompletionService {
316
352
__dirname ,
317
353
`../../../bin/${ clientExecutableFileName } .js`
318
354
) ;
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
+ ) ;
319
377
await this . $childProcess . exec (
320
- `"${ process . argv [ 0 ] } " "${ pathToExecutableFile } " completion >> "${ filePath } "`
378
+ `"${ process . argv [ 0 ] } " "${ pathToExecutableFile } " completion_generate_script >> "${ filePath } "`
321
379
) ;
380
+ this . $fs . appendFile ( filePath , this . completionAliasDefinition ) ;
381
+
322
382
this . $fs . chmod ( filePath , "0644" ) ;
323
383
}
324
384
} catch ( err ) {
0 commit comments