From 46fe611c30fd21599992a67ff7bdbd075c7b7caf Mon Sep 17 00:00:00 2001 From: AmirSa12 Date: Sun, 27 Oct 2024 17:48:39 +0330 Subject: [PATCH] fix desc issue in pwsh --- demo.ts | 4 +- powershell.ts | 107 +++++++++++++++++++++++--------------------------- 2 files changed, 52 insertions(+), 59 deletions(-) diff --git a/demo.ts b/demo.ts index d072d73..cc0e364 100644 --- a/demo.ts +++ b/demo.ts @@ -131,7 +131,7 @@ cli .option("--strictPort", `[boolean] exit if specified port is already in use`) .option( "--force", - `[boolean] force the optimizer to ignore the cache and re-bundle`, + `[boolean] force the optimizer to ignore the cache and re-bundle` ) .action((root, options) => { console.log(`Starting dev server at ${root || "."} with options:`, options); @@ -193,7 +193,7 @@ for (const c of [cli.globalCommand, ...cli.commands]) { // Completion for --config flagMap.set(optionKey, async (previousArgs, toComplete) => { const configFiles = ["vite.config.ts", "vite.config.js"].filter( - (file) => file.startsWith(toComplete), + (file) => file.startsWith(toComplete) ); return configFiles.map((file) => ({ action: file })); }); diff --git a/powershell.ts b/powershell.ts index 205780f..91b8a76 100644 --- a/powershell.ts +++ b/powershell.ts @@ -57,7 +57,7 @@ export function generate( # Make sure the $Command is longer then the $CursorPosition before we truncate. # This happens because the $Command does not include the last space. if ($Command.Length -gt $CursorPosition) { - $Command=$Command.Substring(0,$CursorPosition) + $Command = $Command.Substring(0, $CursorPosition) } __${name}_debug "Truncated command: $Command" @@ -68,22 +68,14 @@ export function generate( $ShellCompDirectiveFilterDirs=${ShellCompDirectiveFilterDirs} $ShellCompDirectiveKeepOrder=${ShellCompDirectiveKeepOrder} - # Original exec value - $originalExec = "${exec}" - - # Split the exec string by spaces to handle each part - $execParts = $originalExec -split ' ' - - __${name}_debug "Value of exec: ${exec}" - # Prepare the command to request completions for the program. # Split the command at the first space to separate the program and arguments. - $Program,$Arguments = $Command.Split(" ",2) + $Program, $Arguments = $Command.Split(" ", 2) $RequestComp = "& ${exec} complete -- $Arguments" __${name}_debug "RequestComp: $RequestComp" - # We cannot use $WordToComplete because it + # we cannot use $WordToComplete because it # has the wrong values if the cursor was moved # so use the last argument if ($WordToComplete -ne "" ) { @@ -91,39 +83,40 @@ export function generate( } __${name}_debug "New WordToComplete: $WordToComplete" + # Check for flag with equal sign $IsEqualFlag = ($WordToComplete -Like "--*=*" ) if ( $IsEqualFlag ) { __${name}_debug "Completing equal sign flag" # Remove the flag part - $Flag,$WordToComplete = $WordToComplete.Split("=",2) + $Flag, $WordToComplete = $WordToComplete.Split("=", 2) } if ( $WordToComplete -eq "" -And ( -Not $IsEqualFlag )) { # If the last parameter is complete (there is a space following it) - # We add an extra empty parameter so we can indicate this to the program. + # We add an extra empty parameter so we can indicate this to the go method. __${name}_debug "Adding extra empty parameter" # PowerShell 7.2+ changed the way how the arguments are passed to executables, # so for pre-7.2 or when Legacy argument passing is enabled we need to use - # \`"\`" to pass an empty argument, a "" or '' does not work!!! if ($PSVersionTable.PsVersion -lt [version]'7.2.0' -or ($PSVersionTable.PsVersion -lt [version]'7.3.0' -and -not [ExperimentalFeature]::IsEnabled("PSNativeCommandArgumentPassing")) -or (($PSVersionTable.PsVersion -ge [version]'7.3.0' -or [ExperimentalFeature]::IsEnabled("PSNativeCommandArgumentPassing")) -and $PSNativeCommandArgumentPassing -eq 'Legacy')) { $RequestComp="$RequestComp" + ' \`"\`"' } else { - $RequestComp="$RequestComp" + ' ""' + $RequestComp = "$RequestComp" + ' ""' } } - __${name}_debug "Calling: $RequestComp" - # First disable ActiveHelp which is not supported for PowerShell + __${name}_debug "Calling $RequestComp" + # First disable ActiveHelp which is not supported for Powershell + $env:ActiveHelp = 0 - # Call the command, store the output in $out, and redirect stderr and stdout to null - # $Out is an array containing each line per element + # call the command store the output in $out and redirect stderr and stdout to null + # $Out is an array contains each line per element Invoke-Expression -OutVariable out "$RequestComp" 2>&1 | Out-Null - # Get directive from last line + # get directive from last line [int]$Directive = $Out[-1].TrimStart(':') if ($Directive -eq "") { # There is no directive specified @@ -131,52 +124,53 @@ export function generate( } __${name}_debug "The completion directive is: $Directive" - # Remove directive (last element) from out + # remove directive (last element) from out $Out = $Out | Where-Object { $_ -ne $Out[-1] } __${name}_debug "The completions are: $Out" if (($Directive -band $ShellCompDirectiveError) -ne 0 ) { - # Error code. No completion. - __${name}_debug "Received error from custom completion code" + # Error code. No completion. + __${name}_debug "Received error from custom completion go code" return } $Longest = 0 [Array]$Values = $Out | ForEach-Object { - # Split the output into name and description - $Name, $Description = $_.Split("t",2) + # Split the output in name and description + $Name, $Description = $_.Split("\`t", 2) __${name}_debug "Name: $Name Description: $Description" - # Look for the longest completion to format things nicely + # Look for the longest completion so that we can format things nicely if ($Longest -lt $Name.Length) { $Longest = $Name.Length } - # Set the description to a one-space string if none is set - # This is needed because CompletionResult does not accept an empty string + # Set the description to a one space string if there is none set. + # This is needed because the CompletionResult does not accept an empty string as argument if (-Not $Description) { $Description = " " } - @{Name="$Name";Description="$Description"} + @{ Name = "$Name"; Description = "$Description" } } + $Space = " " if (($Directive -band $ShellCompDirectiveNoSpace) -ne 0 ) { - # Remove the space here + # remove the space here __${name}_debug "ShellCompDirectiveNoSpace is called" $Space = "" } if ((($Directive -band $ShellCompDirectiveFilterFileExt) -ne 0 ) -or (($Directive -band $ShellCompDirectiveFilterDirs) -ne 0 )) { - __${name}_debug "ShellCompDirectiveFilterFileExt and ShellCompDirectiveFilterDirs are not supported" + __${name}_debug "ShellCompDirectiveFilterFileExt ShellCompDirectiveFilterDirs are not supported" - # Return here to prevent the completion of the extensions + # return here to prevent the completion of the extensions return } $Values = $Values | Where-Object { - # Filter the result + # filter the result $_.Name -like "$WordToComplete*" # Join the flag back if we have an equal sign flag @@ -186,7 +180,7 @@ export function generate( } } - # Sort the values in ascending order by name if keep order isn't passed + # we sort the values in ascending order by name if keep order isn't passed if (($Directive -band $ShellCompDirectiveKeepOrder) -eq 0 ) { $Values = $Values | Sort-Object -Property Name } @@ -195,43 +189,45 @@ export function generate( __${name}_debug "ShellCompDirectiveNoFileComp is called" if ($Values.Length -eq 0) { - # Just print an empty string here so the shell does not start to complete paths - # We cannot use CompletionResult here because it does not accept an empty string + # Just print an empty string here so the + # shell does not start to complete paths. + # We cannot use CompletionResult here because + # it does not accept an empty string as argument. "" return } } # Get the current mode - $Mode = (Get-PSReadLineKeyHandler | Where-Object {$_.Key -eq "Tab" }).Function + $Mode = (Get-PSReadLineKeyHandler | Where-Object { $_.Key -eq "Tab" }).Function __${name}_debug "Mode: $Mode" $Values | ForEach-Object { - # Store temporary because switch will overwrite $_ + # store temporary because switch will overwrite $_ $comp = $_ # PowerShell supports three different completion modes - # - TabCompleteNext (default Windows style) + # - TabCompleteNext (default windows style - on each key press the next option is displayed) # - Complete (works like bash) # - MenuComplete (works like zsh) - # Set the mode with Set-PSReadLineKeyHandler -Key Tab -Function + # You set the mode with Set-PSReadLineKeyHandler -Key Tab -Function # CompletionResult Arguments: - # 1) CompletionText: text to be used as the auto-completion result - # 2) ListItemText: text to be displayed in the suggestion list - # 3) ResultType: type of completion result - # 4) ToolTip: text for the tooltip with details about the object + # 1) CompletionText text to be used as the auto completion result + # 2) ListItemText text to be displayed in the suggestion list + # 3) ResultType type of completion result + # 4) ToolTip text for the tooltip with details about the object switch ($Mode) { - # Bash-like + # bash like "Complete" { if ($Values.Length -eq 1) { __${name}_debug "Only one completion left" - # Insert space after value + # insert space after value [System.Management.Automation.CompletionResult]::new($($comp.Name | __${name}_escapeStringWithSpecialChars) + $Space, "$($comp.Name)", 'ParameterValue', "$($comp.Description)") } else { @@ -251,17 +247,19 @@ export function generate( } } - # Zsh-like + # zsh like "MenuComplete" { - # Insert space after value - # MenuComplete will automatically show the ToolTip + # insert space after value + # MenuComplete will automatically show the ToolTip of + # the highlighted value at the bottom of the suggestions. [System.Management.Automation.CompletionResult]::new($($comp.Name | __${name}_escapeStringWithSpecialChars) + $Space, "$($comp.Name)", 'ParameterValue', "$($comp.Description)") } - # Default and TabCompleteNext + # TabCompleteNext and in case we get something unknown Default { - # Like MenuComplete but without adding a space - # Description will not be shown as it's not possible with TabCompleteNext + # Like MenuComplete but we don't want to add a space here because + # the user need to press space anyway to get the completion. + # Description will not be shown because that's not possible with TabCompleteNext [System.Management.Automation.CompletionResult]::new($($comp.Name | __${name}_escapeStringWithSpecialChars), "$($comp.Name)", 'ParameterValue', "$($comp.Description)") } } @@ -269,11 +267,6 @@ export function generate( } } -$Values | ForEach-Object { - __${name}_debug "Completion suggestion: $($_.Name)" -} - - Register-ArgumentCompleter -CommandName '${name}' -ScriptBlock $__${nameForVar}CompleterBlock `; } -- 2.51.2