Stop the cursor from jumping when changing prefix in QuickAccess - v2 (#204702)

* Revert "Revert "Stop the cursor from jumping when changing prefix in QuickAcc…"

This reverts commit 348e88dc00.

* Only keep the selection if the value is unchanged

---------

Co-authored-by: Tyler James Leonhardt <me@tylerleonhardt.com>
This commit is contained in:
Anthony Stewart 2024-03-21 01:17:17 +01:00 committed by GitHub
parent 042c0893d9
commit 09d5f4efc5
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 43 additions and 2 deletions

View file

@ -312,6 +312,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');
}

View file

@ -92,6 +92,10 @@ export class QuickAccessController extends Disposable implements IQuickAccessCon
}
}
// Store the existing selection if there was one.
const visibleSelection = visibleQuickAccess?.picker?.valueSelection;
const visibleValue = visibleQuickAccess?.picker?.value;
// 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();
@ -148,6 +152,11 @@ export class QuickAccessController extends Disposable implements IQuickAccessCon
// on the onDidHide event.
picker.show();
// If the previous picker had a selection and the value is unchanged, we should set that in the new picker.
if (visibleSelection && visibleValue === value) {
picker.valueSelection = visibleSelection;
}
// Pick mode: return with promise
if (pick) {
return pickPromise?.p;

View file

@ -723,7 +723,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();
@ -1167,7 +1175,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();

View file

@ -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();
}