@@ -154,6 +154,7 @@ export class CommandDetectionCapability extends Disposable implements ICommandDe
154
154
} ;
155
155
this . _register ( this . _terminal . onResize ( e => this . _handleResize ( e ) ) ) ;
156
156
this . _register ( this . _terminal . onCursorMove ( ( ) => this . _handleCursorMove ( ) ) ) ;
157
+ this . _register ( this . _terminal . onData ( ( ) => this . _handleInput ( ) ) ) ;
157
158
}
158
159
159
160
private _handleResize ( e : { cols : number ; rows : number } ) {
@@ -207,6 +208,10 @@ export class CommandDetectionCapability extends Disposable implements ICommandDe
207
208
this . _cwd = value ;
208
209
}
209
210
211
+ setPromptHeight ( value : number ) {
212
+ this . _currentCommand . promptHeight = value ;
213
+ }
214
+
210
215
setIsWindowsPty ( value : boolean ) {
211
216
if ( value && ! ( this . _ptyHeuristics . value instanceof WindowsPtyHeuristics ) ) {
212
217
const that = this ;
@@ -280,6 +285,10 @@ export class CommandDetectionCapability extends Disposable implements ICommandDe
280
285
return undefined ;
281
286
}
282
287
288
+ private _handleInput ( ) : void {
289
+ this . _ptyHeuristics . value . handleInput ( ) ;
290
+ }
291
+
283
292
handlePromptStart ( options ?: IHandleCommandOptions ) : void {
284
293
// Adjust the last command's finished marker when needed. The standard position for the
285
294
// finished marker `D` to appear is at the same position as the following prompt started
@@ -499,6 +508,8 @@ class UnixPtyHeuristics extends Disposable {
499
508
} ) ) ;
500
509
}
501
510
511
+ handleInput ( ) { }
512
+
502
513
handleCommandStart ( options ?: IHandleCommandOptions ) {
503
514
this . _hooks . commitCommandFinished ( ) ;
504
515
@@ -652,6 +663,28 @@ class WindowsPtyHeuristics extends Disposable {
652
663
}
653
664
}
654
665
666
+ /**
667
+ * Attempt to adjust the command start marker when input is handled for the first time.
668
+ */
669
+ handleInput ( ) {
670
+ const currentY = this . _terminal . buffer . active . baseY + this . _terminal . buffer . active . cursorY ;
671
+
672
+ const hasWrappingInPrompt = Array . from ( { length : ( this . _capability . currentCommand . promptHeight ?? 0 ) + 1 } , ( _ , i ) => currentY - i ) . find ( y => this . _terminal . buffer . active . getLine ( y ) ?. isWrapped ) !== undefined ;
673
+ const hasActiveCommand = this . _capability . currentCommand . commandStartX !== undefined && this . _capability . currentCommand . commandExecutedX === undefined ;
674
+ const hasAdjusted = this . _capability . currentCommand . isAdjusted === true || this . _capability . currentCommand . isInputAdjusted === true ;
675
+
676
+ if ( ! hasActiveCommand || hasAdjusted || hasWrappingInPrompt ) {
677
+ return ;
678
+ }
679
+ this . _capability . currentCommand . isInputAdjusted = true ;
680
+ this . _logService . debug ( 'CommandDetectionCapability#handleInput attempting start marker adjustment' ) ;
681
+
682
+ this . _tryAdjustCommandStartMarkerScannedLineCount = 0 ;
683
+ this . _tryAdjustCommandStartMarkerPollCount = 0 ;
684
+ this . _tryAdjustCommandStartMarkerScheduler = new RunOnceScheduler ( ( ) => this . _tryAdjustCommandStartMarker ( this . _terminal . registerMarker ( 0 ) ! ) , AdjustCommandStartMarkerConstants . Interval ) ;
685
+ this . _tryAdjustCommandStartMarkerScheduler . schedule ( ) ;
686
+ }
687
+
655
688
handleCommandStart ( ) {
656
689
this . _capability . currentCommand . commandStartX = this . _terminal . buffer . active . cursorX ;
657
690
@@ -713,21 +746,24 @@ class WindowsPtyHeuristics extends Disposable {
713
746
if ( prompt ) {
714
747
const adjustedPrompt = typeof prompt === 'string' ? prompt : prompt . prompt ;
715
748
this . _capability . currentCommand . commandStartMarker = this . _terminal . registerMarker ( 0 ) ! ;
716
- if ( typeof prompt === 'object' && prompt . likelySingleLine ) {
717
- this . _logService . debug ( 'CommandDetectionCapability#_tryAdjustCommandStartMarker adjusted promptStart' , `${ this . _capability . currentCommand . promptStartMarker ?. line } -> ${ this . _capability . currentCommand . commandStartMarker . line } ` ) ;
718
- this . _capability . currentCommand . promptStartMarker ?. dispose ( ) ;
719
- this . _capability . currentCommand . promptStartMarker = cloneMarker ( this . _terminal , this . _capability . currentCommand . commandStartMarker ) ;
720
- // Adjust the last command if it's not in the same position as the following
721
- // prompt start marker
722
- const lastCommand = this . _capability . commands . at ( - 1 ) ;
723
- if ( lastCommand && this . _capability . currentCommand . commandStartMarker . line !== lastCommand . endMarker ?. line ) {
724
- lastCommand . endMarker ?. dispose ( ) ;
725
- lastCommand . endMarker = cloneMarker ( this . _terminal , this . _capability . currentCommand . commandStartMarker ) ;
726
- }
749
+
750
+ // Adjust the prompt start marker to the command start marker
751
+ this . _logService . debug ( 'CommandDetectionCapability#_tryAdjustCommandStartMarker adjusted promptStart' , `${ this . _capability . currentCommand . promptStartMarker ?. line } -> ${ this . _capability . currentCommand . commandStartMarker . line } ` ) ;
752
+ this . _capability . currentCommand . promptStartMarker ?. dispose ( ) ;
753
+ this . _capability . currentCommand . promptStartMarker = cloneMarker ( this . _terminal , this . _capability . currentCommand . commandStartMarker , - ( ( this . _capability . currentCommand . promptHeight ?? 1 ) - 1 ) ) ;
754
+
755
+ // Adjust the last command if it's not in the same position as the following
756
+ // prompt start marker
757
+ const lastCommand = this . _capability . commands . at ( - 1 ) ;
758
+ if ( lastCommand && this . _capability . currentCommand . commandStartMarker . line !== lastCommand . endMarker ?. line ) {
759
+ lastCommand . endMarker ?. dispose ( ) ;
760
+ lastCommand . endMarker = cloneMarker ( this . _terminal , this . _capability . currentCommand . commandStartMarker , - ( ( this . _capability . currentCommand . promptHeight ?? 1 ) - 1 ) ) ;
727
761
}
762
+
728
763
// use the regex to set the position as it's possible input has occurred
729
764
this . _capability . currentCommand . commandStartX = adjustedPrompt . length ;
730
765
this . _logService . debug ( 'CommandDetectionCapability#_tryAdjustCommandStartMarker adjusted commandStart' , `${ start . line } -> ${ this . _capability . currentCommand . commandStartMarker . line } :${ this . _capability . currentCommand . commandStartX } ` ) ;
766
+ this . _capability . currentCommand . isAdjusted = true ;
731
767
this . _flushPendingHandleCommandStartTask ( ) ;
732
768
return ;
733
769
}
@@ -1049,5 +1085,7 @@ function getXtermLineContent(buffer: IBuffer, lineStart: number, lineEnd: number
1049
1085
}
1050
1086
1051
1087
function cloneMarker ( xterm : Terminal , marker : IXtermMarker , offset : number = 0 ) : IXtermMarker | undefined {
1052
- return xterm . registerMarker ( marker . line - ( xterm . buffer . active . baseY + xterm . buffer . active . cursorY ) + offset ) ;
1088
+ const cursorY = xterm . buffer . active . baseY + xterm . buffer . active . cursorY ;
1089
+ const cursorYOffset = marker . line - cursorY + offset ;
1090
+ return xterm . registerMarker ( ( cursorY + cursorYOffset ) < 0 ? - cursorY : cursorYOffset ) ;
1053
1091
}
0 commit comments