@@ -1108,6 +1108,7 @@ function parseCommandProgramName(
11081108 context : CommandProgramContext ,
11091109 segmentsRemaining : number ,
11101110 labelContext ?: CommandLabelContext ,
1111+ allowDisplayRewrite = false ,
11111112) : string | null {
11121113 if ( depth >= 8 || segmentsRemaining <= 0 ) return null ;
11131114 const commandWithoutComments = commandWithoutLeadingShellComments ( command ) ;
@@ -1235,18 +1236,29 @@ function parseCommandProgramName(
12351236 const targetOffset = tokenOffsets ?. [ targetIndex ] ;
12361237 if ( target && ! target . startsWith ( "-" ) && targetOffset !== undefined ) {
12371238 const remainder = firstCommand . slice ( targetOffset ) ;
1238- const displayCommand = commandSplit . remainingCommand
1239- ? `${ remainder } ${ commandSplit . separator } ${ commandSplit . remainingCommand } `
1240- : remainder ;
1239+ const source = command . trim ( ) ;
1240+ const sourceFirstCommand = commandSplit . firstCommand . trim ( ) ;
1241+ const sourceOffset =
1242+ source . startsWith ( sourceFirstCommand ) && sourceFirstCommand . endsWith ( remainder )
1243+ ? sourceFirstCommand . length - remainder . length
1244+ : null ;
1245+ const displayCommand =
1246+ sourceOffset !== null
1247+ ? source . slice ( sourceOffset )
1248+ : commandSplit . remainingCommand
1249+ ? `${ remainder } ${ commandSplit . separator } ${ commandSplit . remainingCommand } `
1250+ : remainder ;
1251+ const rewriteDisplay = allowDisplayRewrite && sourceOffset !== null ;
12411252 const program = commandProgramNameInternal (
12421253 displayCommand ,
12431254 depth + 1 ,
12441255 "exec" ,
12451256 segmentsRemaining ,
12461257 labelContext ,
1258+ rewriteDisplay ,
12471259 ) ;
12481260 if ( program !== null && ! labelContext . unresolvedPrefix ) {
1249- labelContext . displayCommand ??= displayCommand ;
1261+ if ( rewriteDisplay ) labelContext . displayCommand ??= displayCommand ;
12501262 return program ;
12511263 }
12521264 }
@@ -1444,6 +1456,7 @@ function commandProgramNameInternal(
14441456 context : CommandProgramContext ,
14451457 segmentsRemaining = MAX_COMMAND_SEGMENTS ,
14461458 labelContext ?: CommandLabelContext ,
1459+ allowDisplayRewrite = false ,
14471460) : string | null {
14481461 if ( segmentsRemaining <= 0 ) return null ;
14491462 const calledProgram = powerShellCallOperatorProgramName ( command ) ;
@@ -1456,14 +1469,21 @@ function commandProgramNameInternal(
14561469 "exec" ,
14571470 segmentsRemaining ,
14581471 labelContext ,
1472+ allowDisplayRewrite ,
14591473 ) ?? calledProgram
14601474 ) ;
14611475 }
14621476 return calledProgram ;
14631477 }
14641478 return (
1465- parseCommandProgramName ( command , depth , context , segmentsRemaining , labelContext ) ??
1466- ( context === "shell" ? literalCommandAliasProgramName ( command ) : null )
1479+ parseCommandProgramName (
1480+ command ,
1481+ depth ,
1482+ context ,
1483+ segmentsRemaining ,
1484+ labelContext ,
1485+ allowDisplayRewrite ,
1486+ ) ?? ( context === "shell" ? literalCommandAliasProgramName ( command ) : null )
14671487 ) ;
14681488}
14691489
@@ -1515,7 +1535,8 @@ export function commandDisplayText(
15151535 const display = withoutShellWrapper ( command ) ;
15161536 if ( ignoredCommandPrefixes . length === 0 ) return withVisibleControlCharacters ( display ) ;
15171537 const context = commandLabelContext ( ignoredCommandPrefixes ) ;
1518- commandProgramNameInternal ( display , 0 , "shell" , MAX_COMMAND_SEGMENTS , context ) ;
1538+ // Nested shell payloads and reconstructed tokens cannot replace the whole preview.
1539+ commandProgramNameInternal ( display , 0 , "shell" , MAX_COMMAND_SEGMENTS , context , true ) ;
15191540 return withVisibleControlCharacters ( context . displayCommand ?? display ) ;
15201541}
15211542
0 commit comments