@@ -35,6 +35,7 @@ export class CommandDetectionCapability extends Disposable implements ICommandDe
3535 private _handleCommandStartOptions ?: IHandleCommandOptions ;
3636 private _hasRichCommandDetection : boolean = false ;
3737 get hasRichCommandDetection ( ) { return this . _hasRichCommandDetection ; }
38+ private _nextCommandId : { command : string ; commandId : string | undefined } | undefined ;
3839
3940 private _ptyHeuristicsHooks : ICommandDetectionHeuristicsHooks ;
4041 private readonly _ptyHeuristics : MandatoryMutableDisposable < IPtyHeuristics > ;
@@ -342,6 +343,14 @@ export class CommandDetectionCapability extends Disposable implements ICommandDe
342343 this . _ptyHeuristics . value . handleCommandStart ( options ) ;
343344 }
344345
346+ /**
347+ * Sets the command ID to use for the next command that starts.
348+ * This is useful when you want to pre-assign an ID before the shell sends the command start sequence.
349+ */
350+ setNextCommandId ( command : string , commandId : string ) : void {
351+ this . _nextCommandId = { command, commandId } ;
352+ }
353+
345354 handleCommandExecuted ( options ?: IHandleCommandOptions ) : void {
346355 this . _ptyHeuristics . value . handleCommandExecuted ( options ) ;
347356 this . _currentCommand . markExecutedTime ( ) ;
@@ -355,7 +364,20 @@ export class CommandDetectionCapability extends Disposable implements ICommandDe
355364 if ( ! this . _currentCommand . commandExecutedMarker ) {
356365 this . handleCommandExecuted ( ) ;
357366 }
358-
367+ // If a custom command ID is provided, use it for the current command
368+ // Otherwise, check if there's a pending next command ID
369+ if ( options ?. commandId ) {
370+ this . _currentCommand . id = options . commandId ;
371+ this . _nextCommandId = undefined ; // Clear the pending ID
372+ } else if (
373+ this . _nextCommandId &&
374+ typeof this . currentCommand . command === 'string' &&
375+ typeof this . _nextCommandId . command === 'string' &&
376+ this . currentCommand . command . trim ( ) === this . _nextCommandId . command . trim ( )
377+ ) {
378+ this . _currentCommand . id = this . _nextCommandId . commandId ;
379+ this . _nextCommandId = undefined ; // Clear after use
380+ }
359381 this . _currentCommand . markFinishedTime ( ) ;
360382 this . _ptyHeuristics . value . preHandleCommandFinished ?.( ) ;
361383
@@ -392,7 +414,9 @@ export class CommandDetectionCapability extends Disposable implements ICommandDe
392414 this . _logService . debug ( 'CommandDetectionCapability#onCommandFinished' , newCommand ) ;
393415 this . _onCommandFinished . fire ( newCommand ) ;
394416 }
395- this . _currentCommand = new PartialTerminalCommand ( this . _terminal ) ;
417+ // Create new command for next execution, preserving command ID if one was specified
418+ const nextCommandId = this . _handleCommandStartOptions ?. commandId ;
419+ this . _currentCommand = new PartialTerminalCommand ( this . _terminal , nextCommandId ) ;
396420 this . _handleCommandStartOptions = undefined ;
397421 }
398422
0 commit comments