|
| 1 | +function LaunchVim |
| 2 | +{ |
| 3 | + param |
| 4 | + ( |
| 5 | + [System.String] |
| 6 | + $errorFile, |
| 7 | + |
| 8 | + [System.Management.Automation.SwitchParameter] |
| 9 | + $NewInstance |
| 10 | + ) |
| 11 | + |
| 12 | + if ($NewInstance) |
| 13 | + { |
| 14 | + gvim -q $errorfile |
| 15 | + } |
| 16 | + else |
| 17 | + { |
| 18 | + # Make sure we have a running instance to send commands to |
| 19 | + if(!(vim --serverlist | Select-String "$reuseInstanceName" -q)) |
| 20 | + { |
| 21 | + gvim '--servername' $reuseInstanceName |
| 22 | + Start-Sleep -Milliseconds 1000 |
| 23 | + } |
| 24 | + gvim --servername $reuseInstanceName --remote-send "<ESC><ESC><ESC>:cf $errorFile<CR>" |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | + |
| 29 | +function Start-VimErrorFile |
| 30 | +{ |
| 31 | +<# |
| 32 | +.Synopsis |
| 33 | + Starts the Vim editor with an error file. |
| 34 | +.Description |
| 35 | + This command starts creates an errorfile from the pipeline input and starts gvim with that file as an argument. |
| 36 | + This enables easy navigations |
| 37 | +
|
| 38 | +.Example |
| 39 | + PS> Get-ChildItem *.txt | Start-VimWithErrorInfo |
| 40 | + |
| 41 | + PS> Select-String 'Foo' *.txt | Start-VimWithErrorInfo |
| 42 | +
|
| 43 | +.Link |
| 44 | +
|
| 45 | +.Notes |
| 46 | +NAME: Start-VimErrorInfo |
| 47 | +#Requires -Version 2.0 |
| 48 | +#> |
| 49 | + [CmdletBinding( |
| 50 | + DefaultParameterSetName='input')] |
| 51 | + param( |
| 52 | + [Parameter(ParameterSetName='ErrorFile',Position=0, Mandatory=$true, ValueFromPipelineByPropertyName=$true)] |
| 53 | + [string] $errfile, |
| 54 | + [Parameter(ParameterSetName='input',Position=0, Mandatory=$true, ValueFromPipeline=$true)] |
| 55 | + [ValidateNotNull()] |
| 56 | + [PSObject[]] $data, |
| 57 | + [switch] $NewInstance |
| 58 | + ) |
| 59 | + |
| 60 | + begin{ |
| 61 | + $lines = New-Object System.Collections.Generic.list[string] 100 |
| 62 | + } |
| 63 | + process { |
| 64 | + if ($PsCmdLet.ParameterSetName -eq 'ErrorFile') |
| 65 | + { |
| 66 | + LaunchVim (Get-Item $errFile).FullName -forceNewInstance:$NewInstance |
| 67 | + return |
| 68 | + } |
| 69 | + foreach($d in $data) |
| 70 | + { |
| 71 | + if($d -is [Microsoft.PowerShell.Commands.MatchInfo]) |
| 72 | + { |
| 73 | + $lines += '{0}({1}) : {2}' -f $d.path, $d.LineNumber, $d.Line |
| 74 | + continue |
| 75 | + } |
| 76 | + if ($d -is [System.IO.FileInfo]) |
| 77 | + { |
| 78 | + $lines += '{0}(1) : File: {1}' -f $d.FullName, $d.Name |
| 79 | + continue |
| 80 | + } |
| 81 | + if ($d -is [System.IO.DirectoryInfo]) |
| 82 | + { |
| 83 | + continue |
| 84 | + } |
| 85 | + if ($d.PSPath) |
| 86 | + { |
| 87 | + $lines += (Resolve-Path $d.PSPath).ProviderPath | ForEach-Object {'{0}(1) : File: {1}' -f $_, (Split-Path -Leaf $_)} |
| 88 | + continue |
| 89 | + } |
| 90 | + if ($d -is [string]){ |
| 91 | + $lines += $d |
| 92 | + continue |
| 93 | + } |
| 94 | + |
| 95 | + Write-Error -targetobject $data -message 'Invalid type of input' |
| 96 | + |
| 97 | + } |
| 98 | + } |
| 99 | + end |
| 100 | + { |
| 101 | + if($lines) |
| 102 | + { |
| 103 | + $outfile = [io.path]::GetTempFileName() + '.vimerror' |
| 104 | + $output = [string]::join("`n", $lines) |
| 105 | + Set-Content -encoding Ascii -Path $outfile -Value $output |
| 106 | + LaunchVim (Get-Item $outfile).fullname -NewInstance:$NewInstance |
| 107 | + } |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +function Set-VimReuseInstanceName{ |
| 112 | + param |
| 113 | + ( |
| 114 | + [string] $name = 'PsVim' |
| 115 | + ) |
| 116 | + |
| 117 | + $script:reuseInstanceName = $name |
| 118 | +} |
0 commit comments