Skip to content

Commit 4e75a91

Browse files
theaquamarinedaxian-dbw
authored andcommitted
Allow InsertPairedBraces to wrap selected text (#1293)
1 parent 40a0285 commit 4e75a91

File tree

1 file changed

+15
-2
lines changed

1 file changed

+15
-2
lines changed

PSReadLine/SamplePSReadLineProfile.ps1

+15-2
Original file line numberDiff line numberDiff line change
@@ -229,11 +229,24 @@ Set-PSReadLineKeyHandler -Key '(','{','[' `
229229
<#case#> '[' { [char]']'; break }
230230
}
231231

232-
[Microsoft.PowerShell.PSConsoleReadLine]::Insert("$($key.KeyChar)$closeChar")
232+
$selectionStart = $null
233+
$selectionLength = $null
234+
[Microsoft.PowerShell.PSConsoleReadLine]::GetSelectionState([ref]$selectionStart, [ref]$selectionLength)
235+
233236
$line = $null
234237
$cursor = $null
235238
[Microsoft.PowerShell.PSConsoleReadLine]::GetBufferState([ref]$line, [ref]$cursor)
236-
[Microsoft.PowerShell.PSConsoleReadLine]::SetCursorPosition($cursor - 1)
239+
240+
if ($selectionStart -ne -1)
241+
{
242+
# Text is selected, wrap it in brackets
243+
[Microsoft.PowerShell.PSConsoleReadLine]::Replace($selectionStart, $selectionLength, $key.KeyChar + $line.SubString($selectionStart, $selectionLength) + $closeChar)
244+
[Microsoft.PowerShell.PSConsoleReadLine]::SetCursorPosition($selectionStart + $selectionLength + 2)
245+
} else {
246+
# No text is selected, insert a pair
247+
[Microsoft.PowerShell.PSConsoleReadLine]::Insert("$($key.KeyChar)$closeChar")
248+
[Microsoft.PowerShell.PSConsoleReadLine]::SetCursorPosition($cursor + 1)
249+
}
237250
}
238251

239252
Set-PSReadLineKeyHandler -Key ')',']','}' `

0 commit comments

Comments
 (0)