Skip to content

Instantly share code, notes, and snippets.

@dlwyatt
Last active September 1, 2017 14:06
Show Gist options
  • Save dlwyatt/666185835efbaf6ab287b9e2b8ecd906 to your computer and use it in GitHub Desktop.
Save dlwyatt/666185835efbaf6ab287b9e2b8ecd906 to your computer and use it in GitHub Desktop.
Stop putting System. at the beginning of type tab completion!
function TabExpansion2
{
<# Options include:
RelativeFilePaths - [bool]
Always resolve file paths using Resolve-Path -Relative.
The default is to use some heuristics to guess if relative or absolute is better.
To customize your own custom options, pass a hashtable to CompleteInput, e.g.
return [System.Management.Automation.CommandCompletion]::CompleteInput($inputScript, $cursorColumn,
@{ RelativeFilePaths=$false }
#>
[CmdletBinding(DefaultParameterSetName = 'ScriptInputSet')]
Param(
[Parameter(ParameterSetName = 'ScriptInputSet', Mandatory = $true, Position = 0)]
[string] $inputScript,
[Parameter(ParameterSetName = 'ScriptInputSet', Mandatory = $true, Position = 1)]
[int] $cursorColumn,
[Parameter(ParameterSetName = 'AstInputSet', Mandatory = $true, Position = 0)]
[System.Management.Automation.Language.Ast] $ast,
[Parameter(ParameterSetName = 'AstInputSet', Mandatory = $true, Position = 1)]
[System.Management.Automation.Language.Token[]] $tokens,
[Parameter(ParameterSetName = 'AstInputSet', Mandatory = $true, Position = 2)]
[System.Management.Automation.Language.IScriptPosition] $positionOfCursor,
[Parameter(ParameterSetName = 'ScriptInputSet', Position = 2)]
[Parameter(ParameterSetName = 'AstInputSet', Position = 3)]
[Hashtable] $options = $null
)
End
{
function StripSystem([Management.Automation.CommandCompletion] $Completion)
{
if ($null -eq $Completion -or $null -eq $Completion.CompletionMatches) { return }
$completionMatches = @($Completion.CompletionMatches)
$Completion.CompletionMatches.Clear()
foreach ($match in $completionMatches)
{
$newMatch = $match
if ($match.ResultType -eq [Management.Automation.CompletionResultType]::Type)
{
$newMatch = [Management.Automation.CompletionResult]::new(($match.CompletionText -replace '^System\.'),
$match.ListItemText,
$match.ResultType,
$match.Tooltip)
}
$null = $Completion.CompletionMatches.Add($newMatch)
}
}
if ($psCmdlet.ParameterSetName -eq 'ScriptInputSet')
{
$completion = [System.Management.Automation.CommandCompletion]::CompleteInput(
<#inputScript#> $inputScript,
<#cursorColumn#> $cursorColumn,
<#options#> $options)
StripSystem $completion
return $completion
}
else
{
$completion = [System.Management.Automation.CommandCompletion]::CompleteInput(
<#ast#> $ast,
<#tokens#> $tokens,
<#positionOfCursor#> $positionOfCursor,
<#options#> $options)
StripSystem $completion
return $completion
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment