Skip to content

Commit 2cd242c

Browse files
committed
fix: use prompt height to support multiline prompt adjustments
Signed-off-by: Chapman Pendery <[email protected]>
1 parent b86de5e commit 2cd242c

File tree

6 files changed

+28
-4
lines changed

6 files changed

+28
-4
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,7 @@ export interface ICommandDetectionCapability {
179179
readonly onCommandInvalidated: Event<ITerminalCommand[]>;
180180
readonly onCurrentCommandInvalidated: Event<ICommandInvalidationRequest>;
181181
setCwd(value: string): void;
182+
setPromptHeight(value: number): void;
182183
setIsWindowsPty(value: boolean): void;
183184
setIsCommandStorageDisabled(): void;
184185
/**

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,7 @@ export class TerminalCommand implements ITerminalCommand {
213213

214214
export interface ICurrentPartialCommand {
215215
promptStartMarker?: IMarker;
216+
promptHeight?: number;
216217

217218
commandStartMarker?: IMarker;
218219
commandStartX?: number;
@@ -260,6 +261,7 @@ export interface ICurrentPartialCommand {
260261

261262
export class PartialTerminalCommand implements ICurrentPartialCommand {
262263
promptStartMarker?: IMarker;
264+
promptHeight?: number;
263265

264266
commandStartMarker?: IMarker;
265267
commandStartX?: number;

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

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,10 @@ export class CommandDetectionCapability extends Disposable implements ICommandDe
198198
this._cwd = value;
199199
}
200200

201+
setPromptHeight(value: number) {
202+
this._currentCommand.promptHeight = value;
203+
}
204+
201205
setIsWindowsPty(value: boolean) {
202206
if (value && !(this._ptyHeuristics.value instanceof WindowsPtyHeuristics)) {
203207
const that = this;
@@ -646,7 +650,9 @@ class WindowsPtyHeuristics extends Disposable {
646650
}
647651

648652
handleInput() {
649-
if (this._capability.currentCommand.isAdjusted === true || this._capability.currentCommand.isInputAdjusted === true) {
653+
const hasActiveCommand = this._capability.currentCommand.commandStartX !== undefined && this._capability.currentCommand.commandExecutedX === undefined;
654+
const hasAdjusted = this._capability.currentCommand.isAdjusted === true || this._capability.currentCommand.isInputAdjusted === true;
655+
if (!hasActiveCommand || hasAdjusted) {
650656
return;
651657
}
652658
this._capability.currentCommand.isInputAdjusted = true;
@@ -723,14 +729,14 @@ class WindowsPtyHeuristics extends Disposable {
723729
// Adjust the prompt start marker to the command start marker
724730
this._logService.debug('CommandDetectionCapability#_tryAdjustCommandStartMarker adjusted promptStart', `${this._capability.currentCommand.promptStartMarker?.line} -> ${this._capability.currentCommand.commandStartMarker.line}`);
725731
this._capability.currentCommand.promptStartMarker?.dispose();
726-
this._capability.currentCommand.promptStartMarker = cloneMarker(this._terminal, this._capability.currentCommand.commandStartMarker);
732+
this._capability.currentCommand.promptStartMarker = cloneMarker(this._terminal, this._capability.currentCommand.commandStartMarker, -(this._capability.currentCommand.promptHeight ?? 0));
727733

728734
// Adjust the last command if it's not in the same position as the following
729735
// prompt start marker
730736
const lastCommand = this._capability.commands.at(-1);
731737
if (lastCommand && this._capability.currentCommand.commandStartMarker.line !== lastCommand.endMarker?.line) {
732738
lastCommand.endMarker?.dispose();
733-
lastCommand.endMarker = cloneMarker(this._terminal, this._capability.currentCommand.commandStartMarker);
739+
lastCommand.endMarker = cloneMarker(this._terminal, this._capability.currentCommand.commandStartMarker, -(this._capability.currentCommand.promptHeight ?? 0));
734740
}
735741

736742
// use the regex to set the position as it's possible input has occurred

src/vs/platform/terminal/common/xterm/shellIntegrationAddon.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,9 @@ export class ShellIntegrationAddon extends Disposable implements IShellIntegrati
392392
this.capabilities.get(TerminalCapability.CommandDetection)?.setIsCommandStorageDisabled();
393393
return true;
394394
}
395+
case 'PromptHeight': {
396+
this.capabilities.get(TerminalCapability.CommandDetection)?.setPromptHeight(parseInt(value));
397+
}
395398
}
396399
}
397400
case VSCodeOscPt.SetMark: {

src/vs/workbench/contrib/terminal/browser/media/shellIntegration-bash.sh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,11 @@ __vsc_update_cwd() {
171171
builtin printf '\e]633;P;Cwd=%s\a' "$(__vsc_escape_value "$PWD")"
172172
}
173173

174+
__vsc_update_prompt_height() {
175+
__vsc_prompt_height="$(builtin printf "%s" "${PS1@P}" | wc -l)"
176+
builtin printf '\e]633;P;PromptHeight=%s\a' "$(__vsc_escape_value "$__vsc_prompt_height")"
177+
}
178+
174179
__vsc_command_output_start() {
175180
builtin printf '\e]633;E;%s;%s\a' "$(__vsc_escape_value "${__vsc_current_command}")" $__vsc_nonce
176181
builtin printf '\e]633;C\a'
@@ -218,6 +223,7 @@ __vsc_precmd() {
218223
__vsc_command_complete "$__vsc_status"
219224
__vsc_current_command=""
220225
__vsc_update_prompt
226+
__vsc_update_prompt_height
221227
__vsc_first_prompt=1
222228
}
223229

src/vs/workbench/contrib/terminal/browser/media/shellIntegration.ps1

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,13 @@ function Global:Prompt() {
8787
Write-Error "failure" -ea ignore
8888
}
8989
# Run the original prompt
90-
$Result += $Global:__VSCodeOriginalPrompt.Invoke()
90+
$OriginalPrompt += $Global:__VSCodeOriginalPrompt.Invoke()
91+
$Result += $OriginalPrompt
92+
93+
# Prompt height
94+
# OSC 633 ; <Property>=<Value> ST
95+
$Result += "$([char]0x1b)]633;P;PromptHeight=$(__VSCode-Escape-Value ($OriginalPrompt -Split '\n').Count)`a"
96+
9197
# Write command started
9298
$Result += "$([char]0x1b)]633;B`a"
9399
$Global:__LastHistoryId = $LastHistoryEntry.Id

0 commit comments

Comments
 (0)