Skip to content

Commit

Permalink
Stop the cursor from jumping when changing prefix in QuickAccess (mic…
Browse files Browse the repository at this point in the history
  • Loading branch information
a-stewart authored Feb 5, 2024
1 parent ec291c1 commit 3154b5f
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 2 deletions.
12 changes: 12 additions & 0 deletions src/vs/base/browser/ui/inputbox/inputBox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,18 @@ export class InputBox extends Widget {
return this.input.selectionEnd === this.input.value.length && this.input.selectionStart === this.input.selectionEnd;
}

public getSelection(): IRange | null {
const selectionStart = this.input.selectionStart;
if (selectionStart === null) {
return null;
}
const selectionEnd = this.input.selectionEnd ?? selectionStart;
return {
start: selectionStart,
end: selectionEnd,
};
}

public enable(): void {
this.input.removeAttribute('disabled');
}
Expand Down
8 changes: 8 additions & 0 deletions src/vs/platform/quickinput/browser/quickAccess.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,9 @@ export class QuickAccessController extends Disposable implements IQuickAccessCon
}
}

// Store the existing selection if there was one.
const visibleSelection = visibleQuickAccess?.picker?.valueSelection;

// Create a picker for the provider to use with the initial value
// and adjust the filtering to exclude the prefix from filtering
const disposables = new DisposableStore();
Expand Down Expand Up @@ -148,6 +151,11 @@ export class QuickAccessController extends Disposable implements IQuickAccessCon
// on the onDidHide event.
picker.show();

// If the previous picker had a selection, we should set that in the new picker.
if (visibleSelection) {
picker.valueSelection = visibleSelection;
}

// Pick mode: return with promise
if (pick) {
return pickPromise?.p;
Expand Down
20 changes: 18 additions & 2 deletions src/vs/platform/quickinput/browser/quickInput.ts
Original file line number Diff line number Diff line change
Expand Up @@ -725,7 +725,15 @@ export class QuickPick<T extends IQuickPickItem> extends QuickInput implements I
return this.ui.keyMods;
}

set valueSelection(valueSelection: Readonly<[number, number]>) {
get valueSelection() {
const selection = this.ui.inputBox.getSelection();
if (!selection) {
return undefined;
}
return [selection.start, selection.end];
}

set valueSelection(valueSelection: Readonly<[number, number]> | undefined) {
this._valueSelection = valueSelection;
this.valueSelectionUpdated = true;
this.update();
Expand Down Expand Up @@ -1154,7 +1162,15 @@ export class InputBox extends QuickInput implements IInputBox {
this.update();
}

set valueSelection(valueSelection: Readonly<[number, number]>) {
get valueSelection() {
const selection = this.ui.inputBox.getSelection();
if (!selection) {
return undefined;
}
return [selection.start, selection.end];
}

set valueSelection(valueSelection: Readonly<[number, number]> | undefined) {
this._valueSelection = valueSelection;
this.valueSelectionUpdated = true;
this.update();
Expand Down
4 changes: 4 additions & 0 deletions src/vs/platform/quickinput/browser/quickInputBox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ export class QuickInputBox extends Disposable {
this.findInput.inputBox.select(range);
}

getSelection(): IRange | null {
return this.findInput.inputBox.getSelection();
}

isSelectionAtEnd(): boolean {
return this.findInput.inputBox.isSelectionAtEnd();
}
Expand Down

0 comments on commit 3154b5f

Please sign in to comment.