Skip to content

Commit 83b639d

Browse files
committed
fix: improve terminal marker placements on windows
Signed-off-by: Chapman Pendery <[email protected]>
1 parent 9d98da3 commit 83b639d

File tree

2 files changed

+44
-11
lines changed

2 files changed

+44
-11
lines changed

src/vs/platform/terminal/common/capabilities/commandDetection/terminalCommand.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,16 @@ export interface ICurrentPartialCommand {
236236
*/
237237
isInvalid?: boolean;
238238

239+
/**
240+
* Whether the command start marker has been adjusted on Windows.
241+
*/
242+
isAdjusted?: boolean;
243+
244+
/**
245+
* Whether the command start marker adjustment has been attempt on new terminal input.
246+
*/
247+
isInputAdjusted?: boolean;
248+
239249
getPromptRowCount(): number;
240250
getCommandRowCount(): number;
241251
}

src/vs/platform/terminal/common/capabilities/commandDetectionCapability.ts

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ export class CommandDetectionCapability extends Disposable implements ICommandDe
111111
};
112112
this._register(this._terminal.onResize(e => this._handleResize(e)));
113113
this._register(this._terminal.onCursorMove(() => this._handleCursorMove()));
114+
this._register(this._terminal.onData(() => this._handleInput()));
114115
}
115116

116117
private _handleResize(e: { cols: number; rows: number }) {
@@ -233,6 +234,10 @@ export class CommandDetectionCapability extends Disposable implements ICommandDe
233234
return undefined;
234235
}
235236

237+
private _handleInput(): void {
238+
this._ptyHeuristics.value.handleInput();
239+
}
240+
236241
handlePromptStart(options?: IHandleCommandOptions): void {
237242
// Adjust the last command's finished marker when needed. The standard position for the
238243
// finished marker `D` to appear is at the same position as the following prompt started
@@ -447,6 +452,8 @@ class UnixPtyHeuristics extends Disposable {
447452
}));
448453
}
449454

455+
handleInput() { }
456+
450457
async handleCommandStart(options?: IHandleCommandOptions) {
451458
this._hooks.commitCommandFinished();
452459

@@ -600,6 +607,19 @@ class WindowsPtyHeuristics extends Disposable {
600607
}
601608
}
602609

610+
handleInput() {
611+
if (this._capability.currentCommand.isAdjusted === true || this._capability.currentCommand.isInputAdjusted === true) {
612+
return;
613+
}
614+
this._capability.currentCommand.isInputAdjusted = true;
615+
this._logService.debug('CommandDetectionCapability#handleInput attempting start marker adjustment');
616+
617+
this._tryAdjustCommandStartMarkerScannedLineCount = 0;
618+
this._tryAdjustCommandStartMarkerPollCount = 0;
619+
this._tryAdjustCommandStartMarkerScheduler = new RunOnceScheduler(() => this._tryAdjustCommandStartMarker(this._terminal.registerMarker(0)!), AdjustCommandStartMarkerConstants.Interval);
620+
this._tryAdjustCommandStartMarkerScheduler.schedule();
621+
}
622+
603623
async handleCommandStart() {
604624
this._capability.currentCommand.commandStartX = this._terminal.buffer.active.cursorX;
605625

@@ -661,21 +681,24 @@ class WindowsPtyHeuristics extends Disposable {
661681
if (prompt) {
662682
const adjustedPrompt = typeof prompt === 'string' ? prompt : prompt.prompt;
663683
this._capability.currentCommand.commandStartMarker = this._terminal.registerMarker(0)!;
664-
if (typeof prompt === 'object' && prompt.likelySingleLine) {
665-
this._logService.debug('CommandDetectionCapability#_tryAdjustCommandStartMarker adjusted promptStart', `${this._capability.currentCommand.promptStartMarker?.line} -> ${this._capability.currentCommand.commandStartMarker.line}`);
666-
this._capability.currentCommand.promptStartMarker?.dispose();
667-
this._capability.currentCommand.promptStartMarker = cloneMarker(this._terminal, this._capability.currentCommand.commandStartMarker);
668-
// Adjust the last command if it's not in the same position as the following
669-
// prompt start marker
670-
const lastCommand = this._capability.commands.at(-1);
671-
if (lastCommand && this._capability.currentCommand.commandStartMarker.line !== lastCommand.endMarker?.line) {
672-
lastCommand.endMarker?.dispose();
673-
lastCommand.endMarker = cloneMarker(this._terminal, this._capability.currentCommand.commandStartMarker);
674-
}
684+
685+
// Adjust the prompt start marker to the command start marker
686+
this._logService.debug('CommandDetectionCapability#_tryAdjustCommandStartMarker adjusted promptStart', `${this._capability.currentCommand.promptStartMarker?.line} -> ${this._capability.currentCommand.commandStartMarker.line}`);
687+
this._capability.currentCommand.promptStartMarker?.dispose();
688+
this._capability.currentCommand.promptStartMarker = cloneMarker(this._terminal, this._capability.currentCommand.commandStartMarker);
689+
690+
// Adjust the last command if it's not in the same position as the following
691+
// prompt start marker
692+
const lastCommand = this._capability.commands.at(-1);
693+
if (lastCommand && this._capability.currentCommand.commandStartMarker.line !== lastCommand.endMarker?.line) {
694+
lastCommand.endMarker?.dispose();
695+
lastCommand.endMarker = cloneMarker(this._terminal, this._capability.currentCommand.commandStartMarker);
675696
}
697+
676698
// use the regex to set the position as it's possible input has occurred
677699
this._capability.currentCommand.commandStartX = adjustedPrompt.length;
678700
this._logService.debug('CommandDetectionCapability#_tryAdjustCommandStartMarker adjusted commandStart', `${start.line} -> ${this._capability.currentCommand.commandStartMarker.line}:${this._capability.currentCommand.commandStartX}`);
701+
this._capability.currentCommand.isAdjusted = true;
679702
this._flushPendingHandleCommandStartTask();
680703
return;
681704
}

0 commit comments

Comments
 (0)