diff --git a/packages/common/src/types/TextEditor.ts b/packages/common/src/types/TextEditor.ts index 6eb46fb015..2dfd2576bd 100644 --- a/packages/common/src/types/TextEditor.ts +++ b/packages/common/src/types/TextEditor.ts @@ -32,7 +32,7 @@ export interface TextEditor { /** * The selections in this text editor. */ - readonly selections: Selection[]; + get selections(): Selection[]; /** * Text editor options. @@ -54,7 +54,7 @@ export interface TextEditor { } export interface EditableTextEditor extends TextEditor { - selections: Selection[]; + setSelections(selections: Selection[]): Promise; options: TextEditorOptions; diff --git a/packages/cursorless-engine/src/actions/BringMoveSwap.ts b/packages/cursorless-engine/src/actions/BringMoveSwap.ts index 16fb9c939a..1984e5f51f 100644 --- a/packages/cursorless-engine/src/actions/BringMoveSwap.ts +++ b/packages/cursorless-engine/src/actions/BringMoveSwap.ts @@ -209,7 +209,10 @@ abstract class BringMoveSwap { // NB: We set the selections here because we don't trust vscode to // properly move the cursor on a bring. Sometimes it will smear an // empty selection - setSelectionsWithoutFocusingEditor(editableEditor, cursorSelections); + await setSelectionsWithoutFocusingEditor( + editableEditor, + cursorSelections, + ); const marks = [ ...this.getMarks(sourceEdits, updatedSourceEditSelections), diff --git a/packages/cursorless-engine/src/actions/CallbackAction.ts b/packages/cursorless-engine/src/actions/CallbackAction.ts index e3b0810292..6a51ba6002 100644 --- a/packages/cursorless-engine/src/actions/CallbackAction.ts +++ b/packages/cursorless-engine/src/actions/CallbackAction.ts @@ -116,7 +116,7 @@ export class CallbackAction { // very end. This code can run on multiple editors in the course of // one command, so we want to avoid focusing the editor multiple // times. - setSelectionsWithoutFocusingEditor( + await setSelectionsWithoutFocusingEditor( editableEditor, updatedOriginalSelections, ); diff --git a/packages/cursorless-engine/src/actions/Deselect.ts b/packages/cursorless-engine/src/actions/Deselect.ts index 7534e77de0..2767efc9c9 100644 --- a/packages/cursorless-engine/src/actions/Deselect.ts +++ b/packages/cursorless-engine/src/actions/Deselect.ts @@ -24,7 +24,7 @@ export default class Deselect implements SimpleAction { throw new SelectionRequiredError(); } - setSelectionsWithoutFocusingEditor( + await setSelectionsWithoutFocusingEditor( ide().getEditableTextEditor(editor), newSelections, ); diff --git a/packages/cursorless-engine/src/actions/GenerateSnippet/GenerateSnippet.ts b/packages/cursorless-engine/src/actions/GenerateSnippet/GenerateSnippet.ts index e41a35de4b..ad072f7f3f 100644 --- a/packages/cursorless-engine/src/actions/GenerateSnippet/GenerateSnippet.ts +++ b/packages/cursorless-engine/src/actions/GenerateSnippet/GenerateSnippet.ts @@ -222,7 +222,9 @@ export default class GenerateSnippet { if (isTesting()) { // If we're testing, we just overwrite the current document - editableEditor.selections = [editor.document.range.toSelection(false)]; + await editableEditor.setSelections([ + editor.document.range.toSelection(false), + ]); } else { // Otherwise, we create and open a new document for the snippet in the // user snippets dir diff --git a/packages/cursorless-engine/src/actions/InsertCopy.ts b/packages/cursorless-engine/src/actions/InsertCopy.ts index d9caad1366..d8a59af84f 100644 --- a/packages/cursorless-engine/src/actions/InsertCopy.ts +++ b/packages/cursorless-engine/src/actions/InsertCopy.ts @@ -86,7 +86,10 @@ class InsertCopy implements SimpleAction { ([edit, selection]) => edit!.updateRange(selection!), ); - setSelectionsWithoutFocusingEditor(editableEditor, updatedEditorSelections); + await setSelectionsWithoutFocusingEditor( + editableEditor, + updatedEditorSelections, + ); const primarySelection = editor.selections[0]; if ( diff --git a/packages/cursorless-engine/src/actions/InsertEmptyLines.ts b/packages/cursorless-engine/src/actions/InsertEmptyLines.ts index 109673a9a4..b15b8fec42 100644 --- a/packages/cursorless-engine/src/actions/InsertEmptyLines.ts +++ b/packages/cursorless-engine/src/actions/InsertEmptyLines.ts @@ -61,7 +61,7 @@ class InsertEmptyLines implements SimpleAction { ], ); - setSelectionsWithoutFocusingEditor( + await setSelectionsWithoutFocusingEditor( editableEditor, updatedCursorSelections, ); diff --git a/packages/cursorless-engine/src/actions/PasteFromClipboard.ts b/packages/cursorless-engine/src/actions/PasteFromClipboard.ts index d260696e9b..8c1c24854d 100644 --- a/packages/cursorless-engine/src/actions/PasteFromClipboard.ts +++ b/packages/cursorless-engine/src/actions/PasteFromClipboard.ts @@ -60,7 +60,7 @@ export class PasteFromClipboard { // Reset cursors on the editor where the edits took place. // NB: We don't focus the editor here because we want to focus the original // editor, not the one where the edits took place - setSelectionsWithoutFocusingEditor(editor, updatedCursorSelections); + await setSelectionsWithoutFocusingEditor(editor, updatedCursorSelections); // If necessary focus back original editor if (originalEditor != null && !originalEditor.isActive) { diff --git a/packages/cursorless-engine/src/actions/Wrap.ts b/packages/cursorless-engine/src/actions/Wrap.ts index 53f320b436..bd008e3655 100644 --- a/packages/cursorless-engine/src/actions/Wrap.ts +++ b/packages/cursorless-engine/src/actions/Wrap.ts @@ -111,7 +111,10 @@ export default class Wrap { ], ); - setSelectionsWithoutFocusingEditor(editableEditor, cursorSelections); + await setSelectionsWithoutFocusingEditor( + editableEditor, + cursorSelections, + ); await ide().flashRanges( delimiterSelections.map((selection) => ({ diff --git a/packages/cursorless-engine/src/util/setSelectionsAndFocusEditor.ts b/packages/cursorless-engine/src/util/setSelectionsAndFocusEditor.ts index 2f7f845261..2d5c30e7c4 100644 --- a/packages/cursorless-engine/src/util/setSelectionsAndFocusEditor.ts +++ b/packages/cursorless-engine/src/util/setSelectionsAndFocusEditor.ts @@ -9,7 +9,7 @@ export async function setSelectionsAndFocusEditor( selections: Selection[], revealRange: boolean = true, ) { - setSelectionsWithoutFocusingEditor(editor, selections); + await setSelectionsWithoutFocusingEditor(editor, selections); if (revealRange) { await editor.revealRange(editor.selections[0]); @@ -20,13 +20,15 @@ export async function setSelectionsAndFocusEditor( await editor.focus(); } -export function setSelectionsWithoutFocusingEditor( +export async function setSelectionsWithoutFocusingEditor( editor: EditableTextEditor, selections: Selection[], ) { - editor.selections = uniqWithHash( - selections, - (a, b) => a.isEqual(b), - (s) => s.concise(), + await editor.setSelections( + uniqWithHash( + selections, + (a, b) => a.isEqual(b), + (s) => s.concise(), + ), ); } diff --git a/packages/cursorless-vscode/src/ide/vscode/VscodeInsertSnippets.ts b/packages/cursorless-vscode/src/ide/vscode/VscodeInsertSnippets.ts index c58994f9d6..6bcde2e58b 100644 --- a/packages/cursorless-vscode/src/ide/vscode/VscodeInsertSnippets.ts +++ b/packages/cursorless-vscode/src/ide/vscode/VscodeInsertSnippets.ts @@ -8,7 +8,7 @@ export async function vscodeInsertSnippet( ranges: Range[] | undefined, ): Promise { if (ranges != null) { - editor.selections = ranges.map((range) => range.toSelection(false)); + editor.setSelections(ranges.map((range) => range.toSelection(false))); } await editor.focus(); diff --git a/packages/cursorless-vscode/src/ide/vscode/VscodeTextEditorImpl.ts b/packages/cursorless-vscode/src/ide/vscode/VscodeTextEditorImpl.ts index 15733f6969..710e0b8c15 100644 --- a/packages/cursorless-vscode/src/ide/vscode/VscodeTextEditorImpl.ts +++ b/packages/cursorless-vscode/src/ide/vscode/VscodeTextEditorImpl.ts @@ -52,7 +52,7 @@ export class VscodeTextEditorImpl implements EditableTextEditor { return this.editor.selections.map(fromVscodeSelection); } - set selections(selections: Selection[]) { + async setSelections(selections: Selection[]): Promise { this.editor.selections = selections.map(toVscodeSelection); }