Created
May 31, 2024 08:04
-
-
Save trackd/f4abd47384d80f14b50111b6d5953263 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using namespace System.Management.Automation | |
using namespace System.Management.Automation.Language | |
using namespace System.Collections | |
class CommandInfoTransform : ArgumentTransformationAttribute { | |
[object] Transform([EngineIntrinsics] $engineIntrinsics, [object] $inputObject) { | |
if ($inputObject -is [CommandInfo]) { | |
return $inputObject | |
} | |
try { | |
$gcm = Get-Command "$inputObject" | Select-Object -First 1 | |
return $gcm | |
} | |
catch { | |
throw | |
} | |
} | |
} | |
Register-ArgumentCompleter -CommandName Edit-VSCode -ParameterName Command -ScriptBlock { | |
param( | |
[String] $commandName, | |
[String] $parameterName, | |
[String] $wordToComplete, | |
[CommandAst] $commandAst, | |
[IDictionary] $fakeBoundParameters | |
) | |
if ([String]::IsNullOrEmpty($wordToComplete)) { | |
$wordToComplete = '*' | |
} | |
return [CompletionCompleters]::CompleteCommand($wordToComplete) | |
} | |
function Edit-VSCode { | |
<# | |
.SYNOPSIS | |
Edit a command in VSCode. | |
.PARAMETER Command | |
CommandName or CommandInfo to edit. | |
.PARAMETER Editor | |
Editor to use, defaults to $env:editor | |
.EXAMPLE | |
Edit-VSCode Edit-VSCode | |
.EXAMPLE | |
Edit-VSCode Edit-VSCode -Editor code | |
#> | |
[Alias('ev')] | |
[CmdletBinding()] | |
param( | |
[Parameter(ValueFromPipeline, Mandatory, Position = 0)] | |
[ValidateNotNullOrEmpty()] | |
[CommandInfoTransform()] | |
[CommandInfo] $Command, | |
[ArgumentCompletions('code', 'code-insiders')] | |
[String] $Editor = $env:editor | |
) | |
if ($Command -is [AliasInfo]) { | |
$Command = $Command.ResolvedCommand | |
} | |
if ($Command -is [CmdletInfo] -or $Command -is [ApplicationInfo]) { | |
$PSCmdlet.ThrowTerminatingError( | |
[ErrorRecord]::new( | |
<# Exception #> [PSNotSupportedException]::new('Editing {0} not supported' -f $Command.CommandType), | |
<# errorId #> 'TypeNotSupported', | |
<# ErrorCategory #> [ErrorCategory]::InvalidType, | |
<# targetObject #> $Command | |
) | |
) | |
} | |
$code = $ExecutionContext.InvokeCommand.GetCommand($Editor, 'Application') | |
if (-Not $code) { | |
$PSCmdlet.ThrowTerminatingError( | |
[ErrorRecord]::new( | |
<# Exception #> [CommandNotFoundException]::new('Editor Not Found: {0}' -f $Editor), | |
<# errorId #> 'EditorNotFound', | |
<# ErrorCategory #> [ErrorCategory]::NotInstalled, | |
<# targetObject #> $Editor | |
) | |
) | |
} | |
if (-Not $Command.ScriptBlock.File) { | |
$PSCmdlet.ThrowTerminatingError( | |
[ErrorRecord]::new( | |
<# Exception #> [ItemNotFoundException]::new('File not found'), | |
<# errorId #> 'FileNotFound', | |
<# ErrorCategory #> [ErrorCategory]::ObjectNotFound, | |
<# targetObject #> $Command | |
) | |
) | |
} | |
$Arguments = @( | |
'--goto' | |
'{0}:{1}:{2}' -f $Command.ScriptBlock.File, | |
$Command.ScriptBlock.StartPosition.StartLine, | |
$Command.ScriptBlock.StartPosition.StartLine.StartColumn | |
'--reuse-window' | |
) | |
& $code $Arguments | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment