Skip to content

Commit 7f9c746

Browse files
authored
Support TargetObject position in ParserErrors (#26649)
1 parent f5c290c commit 7f9c746

2 files changed

Lines changed: 410 additions & 0 deletions

File tree

src/System.Management.Automation/FormatAndOutput/DefaultFormatters/PowerShellCore_format_ps1xml.cs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1190,8 +1190,37 @@ function Get-ConciseViewPositionMessage {
11901190
$highlightLine = ''
11911191
if ($useTargetObject) {
11921192
$line = $_.TargetObject.LineText.Trim()
1193+
11931194
$offsetLength = 0
11941195
$offsetInLine = 0
1196+
$startColumn = 0
1197+
if (
1198+
([System.Collections.IDictionary]$_.TargetObject).Contains('StartColumn') -and
1199+
[System.Management.Automation.LanguagePrimitives]::TryConvertTo[int]($_.TargetObject.StartColumn, [ref]$startColumn) -and
1200+
$null -ne $startColumn -and
1201+
$startColumn -gt 0 -and
1202+
$startColumn -le $line.Length
1203+
) {
1204+
$endColumn = 0
1205+
if (-not (
1206+
([System.Collections.IDictionary]$_.TargetObject).Contains('EndColumn') -and
1207+
[System.Management.Automation.LanguagePrimitives]::TryConvertTo[int]($_.TargetObject.EndColumn, [ref]$endColumn) -and
1208+
$null -ne $endColumn -and
1209+
$endColumn -gt $startColumn -and
1210+
$endColumn -le ($line.Length + 1)
1211+
)) {
1212+
$endColumn = $line.Length + 1
1213+
}
1214+
1215+
# Input is expected to be 1-based index to match the extent positioning
1216+
# but we use 0-based indexing below.
1217+
$startColumn -= 1
1218+
$endColumn -= 1
1219+
1220+
$highlightLine = "$(" " * $startColumn)$("~" * ($endColumn - $startColumn))"
1221+
$offsetLength = $endColumn - $startColumn
1222+
$offsetInLine = $startColumn
1223+
}
11951224
}
11961225
else {
11971226
$positionMessage = $myinv.PositionMessage.Split($newline)

0 commit comments

Comments
 (0)