completers/expr_completer/expr_completer.ps1
|
# expr tab completion for PowerShell # Static option completion for expr.exe and expr. Set-StrictMode -Version 2.0 function Get-ExprCompletionOptions { $cache = Get-Variable -Name 'ExprCompletionOptions' -Scope Script -ErrorAction Ignore if ($null -ne $cache -and $null -ne $cache.Value) { return $cache.Value } $fallbackOptions = @('--help', '--version') $commandCandidates = @('expr.exe', 'expr') foreach ($candidate in $commandCandidates) { $command = Get-Command -Name $candidate -ErrorAction SilentlyContinue if ($null -eq $command) { continue } try { $helpOutput = $null | & $command.Source --help 2>&1 | ForEach-Object { $_ -replace '\e\[[0-9;?]*[ -/]*[@-~]', '' } | Out-String } catch { continue } if ([string]::IsNullOrWhiteSpace($helpOutput)) { continue } $options = [System.Collections.Generic.HashSet[string]]::new([System.StringComparer]::Ordinal) $descriptions = [System.Collections.Hashtable]::new([System.StringComparer]::Ordinal) foreach ($line in ([regex]::Split($helpOutput, '\r?\n'))) { foreach ($match in [regex]::Matches($line, '(?<!\S)(--?[A-Za-z0-9][A-Za-z0-9-]*)(?=(\s|,|=|\[|$))')) { $rawOption = $match.Groups[1].Value $normalized = $rawOption.Trim() if ($normalized.StartsWith('--')) { $normalized = $normalized -replace '\[.*$', '' $normalized = $normalized -replace '=.*$', '' } if ($normalized -match '^-{1,2}[A-Za-z0-9][A-Za-z0-9-]*$') { [void]$options.Add($normalized) if (-not $descriptions.ContainsKey($normalized)) { $description = ($line -replace '^\s*(?:-{1,2}[A-Za-z0-9][A-Za-z0-9-]*(?:[=\[]\S*)?[,\s]+)+', '').Trim() if ($description -and $description -ne $line.Trim()) { $descriptions[$normalized] = $description } } } } } if ($options.Count -gt 0) { Set-Variable -Name 'ExprCompletionOptions' -Value (@($options | Sort-Object)) -Scope Script Set-Variable -Name 'ExprCompletionDescriptions' -Value $descriptions -Scope Script return (Get-Variable -Name 'ExprCompletionOptions' -Scope Script).Value } } Set-Variable -Name 'ExprCompletionOptions' -Value $fallbackOptions -Scope Script return (Get-Variable -Name 'ExprCompletionOptions' -Scope Script).Value } function New-ExprCompletionResult { param( [string]$CompletionText, [string]$ResultType, [string]$ToolTip, [string]$ListItemText ) if ([string]::IsNullOrWhiteSpace($ListItemText)) { $ListItemText = $CompletionText } if ([string]::IsNullOrWhiteSpace($ToolTip)) { $ToolTip = $CompletionText } [System.Management.Automation.CompletionResult]::new( $CompletionText, $ListItemText, $ResultType, $ToolTip ) } function Get-ExprCurrentToken { param( [string]$Line, [int]$CursorPosition, [string]$Fallback ) if ([string]::IsNullOrWhiteSpace($Line)) { return $Fallback } $safeCursor = [Math]::Min([Math]::Max($CursorPosition, 0), $Line.Length) $prefix = $Line.Substring(0, $safeCursor) if ($prefix -match '\s$') { return '' } $parts = @([regex]::Matches($prefix, '"[^"]*"|''[^'']*''|\S+') | ForEach-Object { $_.Value }) if ($parts.Count -gt 0) { return $parts[-1] } $Fallback } function Get-ExprValueCompletions { param([string]$Prefix) $keywords = @( @{ Text = 'match'; Tip = 'match STRING REGEX: anchored pattern match.' } @{ Text = 'substr'; Tip = 'substr STRING POS LENGTH: substring of STRING.' } @{ Text = 'index'; Tip = 'index STRING CHARS: index in STRING of any CHARS.' } @{ Text = 'length'; Tip = 'length STRING: length of STRING.' } ) @( foreach ($keyword in $keywords) { if ($keyword.Text.StartsWith($Prefix, [System.StringComparison]::Ordinal)) { New-ExprCompletionResult -CompletionText $keyword.Text -ListItemText $keyword.Text -ResultType 'ParameterValue' -ToolTip $keyword.Tip } } ) } function Get-ExprOptionDescription { param([string]$Option) $cache = Get-Variable -Name 'ExprCompletionDescriptions' -Scope Script -ErrorAction Ignore if ($null -ne $cache -and $null -ne $cache.Value -and $cache.Value.ContainsKey($Option)) { return $cache.Value[$Option] } 'Option for expr.' } function Complete-Expr { param( [string]$wordToComplete, [System.Management.Automation.Language.CommandAst]$commandAst, [int]$cursorPosition ) $currentWord = if ($cursorPosition -gt $commandAst.Extent.EndOffset) { '' } else { Get-ExprCurrentToken -Line $commandAst.ToString() -CursorPosition ($cursorPosition - $commandAst.Extent.StartOffset) -Fallback $wordToComplete } if ([string]::IsNullOrEmpty($currentWord)) { return Get-ExprValueCompletions -Prefix '' } if ($currentWord.StartsWith('-')) { return @( foreach ($option in Get-ExprCompletionOptions) { if ($option.StartsWith($currentWord, [System.StringComparison]::Ordinal)) { New-ExprCompletionResult -CompletionText $option -ListItemText $option -ResultType 'ParameterName' -ToolTip (Get-ExprOptionDescription -Option $option) } } ) } Get-ExprValueCompletions -Prefix $currentWord } Register-ArgumentCompleter -Native -CommandName 'expr', 'expr.exe' -ScriptBlock { param($wordToComplete, $commandAst, $cursorPosition) Complete-Expr -wordToComplete $wordToComplete -commandAst $commandAst -cursorPosition $cursorPosition } |