Skip to content

Commit 92b442b

Browse files
authored
Merge pull request #260855 from microsoft/tyriar/260746
Don't trim whitespace when calculating cursor index
2 parents dafc6c5 + c8716ef commit 92b442b

File tree

2 files changed

+53
-2
lines changed

2 files changed

+53
-2
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ export class PromptInputModel extends Disposable implements IPromptInputModel {
301301
let ghostTextIndex = -1;
302302
if (cursorIndex === undefined) {
303303
if (absoluteCursorY === commandStartY) {
304-
cursorIndex = this._getRelativeCursorIndex(this._commandStartX, buffer, line);
304+
cursorIndex = Math.min(this._getRelativeCursorIndex(this._commandStartX, buffer, line), commandLine.length);
305305
} else {
306306
cursorIndex = commandLine.trimEnd().length;
307307
}
@@ -628,7 +628,7 @@ export class PromptInputModel extends Disposable implements IPromptInputModel {
628628
}
629629

630630
private _getRelativeCursorIndex(startCellX: number, buffer: IBuffer, line: IBufferLine): number {
631-
return line?.translateToString(true, startCellX, buffer.cursorX).length ?? 0;
631+
return line?.translateToString(false, startCellX, buffer.cursorX).length ?? 0;
632632
}
633633

634634
private _isCellStyledLikeGhostText(cell: IBufferCell): boolean {

src/vs/platform/terminal/test/common/capabilities/commandDetection/promptInputModel.test.ts

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -491,6 +491,57 @@ suite('PromptInputModel', () => {
491491
});
492492

493493
suite('trailing whitespace', () => {
494+
test('cursor index calculation with whitespace', async () => {
495+
await writePromise('$ ');
496+
fireCommandStart();
497+
await assertPromptInput('|');
498+
499+
await writePromise('echo ');
500+
await assertPromptInput('echo |');
501+
502+
await writePromise('\x1b[3D');
503+
await assertPromptInput('echo| ');
504+
505+
await writePromise('\x1b[C');
506+
await assertPromptInput('echo | ');
507+
508+
await writePromise('\x1b[C');
509+
await assertPromptInput('echo | ');
510+
511+
await writePromise('\x1b[C');
512+
await assertPromptInput('echo |');
513+
});
514+
515+
test('cursor index should not exceed command line length', async () => {
516+
await writePromise('$ ');
517+
fireCommandStart();
518+
await assertPromptInput('|');
519+
520+
await writePromise('cmd');
521+
await assertPromptInput('cmd|');
522+
523+
await writePromise('\x1b[10C');
524+
await assertPromptInput('cmd|');
525+
});
526+
527+
test('whitespace preservation in cursor calculation', async () => {
528+
await writePromise('$ ');
529+
fireCommandStart();
530+
await assertPromptInput('|');
531+
532+
await writePromise('ls -la');
533+
await assertPromptInput('ls -la|');
534+
535+
await writePromise('\x1b[3D');
536+
await assertPromptInput('ls |-la');
537+
538+
await writePromise('\x1b[3D');
539+
await assertPromptInput('ls| -la');
540+
541+
await writePromise('\x1b[2C');
542+
await assertPromptInput('ls | -la');
543+
});
544+
494545
test('delete whitespace with backspace', async () => {
495546
await writePromise('$ ');
496547
fireCommandStart();

0 commit comments

Comments
 (0)