From 77901dc80fcd33c39fd70479c3d1e1ddc4919fc9 Mon Sep 17 00:00:00 2001 From: Jason Cassidy <47318351+jcassidyav@users.noreply.github.com> Date: Mon, 11 Mar 2024 19:32:23 +0000 Subject: [PATCH 1/2] fix: autocomplete Fix autocompletion functionality --- .../services/auto-completion-service.ts | 55 ++++++++++++++++++- 1 file changed, 54 insertions(+), 1 deletion(-) diff --git a/lib/common/services/auto-completion-service.ts b/lib/common/services/auto-completion-service.ts index a684fea048..d7bf0e6545 100644 --- a/lib/common/services/auto-completion-service.ts +++ b/lib/common/services/auto-completion-service.ts @@ -21,6 +21,9 @@ export class AutoCompletionService implements IAutoCompletionService { "###-begin-%s-completion-###"; private static TABTAB_COMPLETION_END_REGEX_PATTERN = "###-end-%s-completion-###"; + private static GENERATED_TABTAB_COMPLETION_END = + "###-end-nativescript.js-completions-###"; + private static GENERATED_TABTAB_COMPLETION_START = "#compdef nativescript.js"; constructor( private $fs: IFileSystem, @@ -70,6 +73,16 @@ export class AutoCompletionService implements IAutoCompletionService { return tabTabRegex; } + private getTabTabCompletionsRegex(): RegExp { + return new RegExp( + util.format( + "%s[\\s\\S]*%s", + AutoCompletionService.GENERATED_TABTAB_COMPLETION_START, + AutoCompletionService.GENERATED_TABTAB_COMPLETION_END + ) + ); + } + private removeObsoleteAutoCompletion(): void { // In previous releases we were writing directly in .bash_profile, .bashrc, .zshrc and .profile - remove this old code const shellProfilesToBeCleared = this.shellProfiles; @@ -106,6 +119,18 @@ export class AutoCompletionService implements IAutoCompletionService { } }); } + @cache() + private get completionAliasDefinition() { + const pattern = "compdef _nativescript.js_yargs_completions %s"; + const ns = util.format(pattern, "ns"); + const tns = util.format(pattern, "tns"); + return util.format( + "\n%s\n%s\n%s\n", + ns, + tns, + AutoCompletionService.GENERATED_TABTAB_COMPLETION_END + ); + } @cache() private get completionShellScriptContent() { @@ -316,9 +341,37 @@ export class AutoCompletionService implements IAutoCompletionService { __dirname, `../../../bin/${clientExecutableFileName}.js` ); + + if (this.$fs.exists(filePath)) { + const existingText = this.$fs.readText(filePath); + let newText = existingText.replace( + this.getTabTabCompletionsRegex(), + "" + ); + if (newText !== existingText) { + this.$logger.trace( + "Remove existing AutoCompletion from file %s.", + filePath + ); + this.$fs.writeFile(filePath, newText); + } + } + await this.$childProcess.exec( - `"${process.argv[0]}" "${pathToExecutableFile}" completion >> "${filePath}"` + `"${process.argv[0]}" "${pathToExecutableFile}" completion_generate_script >> "${filePath}"` ); + + const contents = this.$fs.readText(filePath); + const endComment = contents.lastIndexOf( + AutoCompletionService.GENERATED_TABTAB_COMPLETION_END + ); + + this.$fs.writeFile( + filePath, + `${contents.substring(0, endComment)}${ + this.completionAliasDefinition + }` + ); // Add aliases this.$fs.chmod(filePath, "0644"); } } catch (err) { From 4844c6b8f43f785e13d269a190a26608f8430923 Mon Sep 17 00:00:00 2001 From: Jason Cassidy <47318351+jcassidyav@users.noreply.github.com> Date: Tue, 12 Mar 2024 14:03:59 +0000 Subject: [PATCH 2/2] fix: autocomplete removing existing config --- .../services/auto-completion-service.ts | 65 ++++++++++--------- 1 file changed, 36 insertions(+), 29 deletions(-) diff --git a/lib/common/services/auto-completion-service.ts b/lib/common/services/auto-completion-service.ts index d7bf0e6545..3e187ee9fe 100644 --- a/lib/common/services/auto-completion-service.ts +++ b/lib/common/services/auto-completion-service.ts @@ -22,8 +22,9 @@ export class AutoCompletionService implements IAutoCompletionService { private static TABTAB_COMPLETION_END_REGEX_PATTERN = "###-end-%s-completion-###"; private static GENERATED_TABTAB_COMPLETION_END = - "###-end-nativescript.js-completions-###"; - private static GENERATED_TABTAB_COMPLETION_START = "#compdef nativescript.js"; + "###-end-ns-completions-section-###"; + private static GENERATED_TABTAB_COMPLETION_START = + "###-begin-ns-completions-section-###"; constructor( private $fs: IFileSystem, @@ -119,6 +120,31 @@ export class AutoCompletionService implements IAutoCompletionService { } }); } + + private removeOboleteTabTabCompletion(text: string) { + try { + let newText = text.replace(this.getTabTabObsoleteRegex("ns"), ""); + + newText = newText.replace(this.getTabTabObsoleteRegex("nsc"), ""); + + newText = newText.replace( + this.getTabTabObsoleteRegex("nativescript"), + "" + ); + + newText = newText.replace(this.getTabTabObsoleteRegex("tns"), ""); + + return newText; + } catch (error) { + this.$logger.trace( + "Error while trying to disable autocompletion for '%s' file. Error is:\n%s", + error.toString() + ); + + return text; + } + } + @cache() private get completionAliasDefinition() { const pattern = "compdef _nativescript.js_yargs_completions %s"; @@ -309,24 +335,9 @@ export class AutoCompletionService implements IAutoCompletionService { if (this.$fs.exists(filePath)) { const contents = this.$fs.readText(filePath); const regExp = new RegExp( - util.format( - "%s\\s+completion\\s+--\\s+", - this.$staticConfig.CLIENT_NAME.toLowerCase() - ) + AutoCompletionService.GENERATED_TABTAB_COMPLETION_START ); let matchCondition = contents.match(regExp); - if (this.$staticConfig.CLIENT_NAME_ALIAS) { - matchCondition = - matchCondition || - contents.match( - new RegExp( - util.format( - "%s\\s+completion\\s+--\\s+", - this.$staticConfig.CLIENT_NAME_ALIAS.toLowerCase() - ) - ) - ); - } if (matchCondition) { doUpdate = false; @@ -348,6 +359,7 @@ export class AutoCompletionService implements IAutoCompletionService { this.getTabTabCompletionsRegex(), "" ); + newText = this.removeOboleteTabTabCompletion(newText); if (newText !== existingText) { this.$logger.trace( "Remove existing AutoCompletion from file %s.", @@ -356,22 +368,17 @@ export class AutoCompletionService implements IAutoCompletionService { this.$fs.writeFile(filePath, newText); } } + // The generated seems to be inconsistent in it's start/end markers so adding our own. + this.$fs.appendFile( + filePath, + `\n${AutoCompletionService.GENERATED_TABTAB_COMPLETION_START}\n` + ); await this.$childProcess.exec( `"${process.argv[0]}" "${pathToExecutableFile}" completion_generate_script >> "${filePath}"` ); + this.$fs.appendFile(filePath, this.completionAliasDefinition); - const contents = this.$fs.readText(filePath); - const endComment = contents.lastIndexOf( - AutoCompletionService.GENERATED_TABTAB_COMPLETION_END - ); - - this.$fs.writeFile( - filePath, - `${contents.substring(0, endComment)}${ - this.completionAliasDefinition - }` - ); // Add aliases this.$fs.chmod(filePath, "0644"); } } catch (err) {