Skip to content

Commit c2606d1

Browse files
saidelikeCedric Halbronnpokey
authored
feat: make text editor selections setter async in preparation for neovim (#2268)
## Checklist - [ ] I have added [tests](https://www.cursorless.org/docs/contributing/test-case-recorder/) - [ ] I have updated the [docs](https://github.com/cursorless-dev/cursorless/tree/main/docs) and [cheatsheet](https://github.com/cursorless-dev/cursorless/tree/main/cursorless-talon/src/cheatsheet) - [ ] I have not broken the cheatsheet I am making this small pr to be reviewed now. This is to make it easier to merge the neovim branch later. --------- Co-authored-by: Cedric Halbronn <[email protected]> Co-authored-by: Pokey Rule <[email protected]>
1 parent 35ff9da commit c2606d1

File tree

12 files changed

+31
-18
lines changed

12 files changed

+31
-18
lines changed

packages/common/src/types/TextEditor.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ export interface TextEditor {
5454
}
5555

5656
export interface EditableTextEditor extends TextEditor {
57-
selections: Selection[];
57+
setSelections(selections: Selection[]): Promise<void>;
5858

5959
options: TextEditorOptions;
6060

packages/cursorless-engine/src/actions/BringMoveSwap.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,10 @@ abstract class BringMoveSwap {
209209
// NB: We set the selections here because we don't trust vscode to
210210
// properly move the cursor on a bring. Sometimes it will smear an
211211
// empty selection
212-
setSelectionsWithoutFocusingEditor(editableEditor, cursorSelections);
212+
await setSelectionsWithoutFocusingEditor(
213+
editableEditor,
214+
cursorSelections,
215+
);
213216

214217
const marks = [
215218
...this.getMarks(sourceEdits, updatedSourceEditSelections),

packages/cursorless-engine/src/actions/CallbackAction.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ export class CallbackAction {
116116
// very end. This code can run on multiple editors in the course of
117117
// one command, so we want to avoid focusing the editor multiple
118118
// times.
119-
setSelectionsWithoutFocusingEditor(
119+
await setSelectionsWithoutFocusingEditor(
120120
editableEditor,
121121
updatedOriginalSelections,
122122
);

packages/cursorless-engine/src/actions/Deselect.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export default class Deselect implements SimpleAction {
2424
throw new SelectionRequiredError();
2525
}
2626

27-
setSelectionsWithoutFocusingEditor(
27+
await setSelectionsWithoutFocusingEditor(
2828
ide().getEditableTextEditor(editor),
2929
newSelections,
3030
);

packages/cursorless-engine/src/actions/GenerateSnippet/GenerateSnippet.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,9 @@ export default class GenerateSnippet {
222222

223223
if (isTesting()) {
224224
// If we're testing, we just overwrite the current document
225-
editableEditor.selections = [editor.document.range.toSelection(false)];
225+
await editableEditor.setSelections([
226+
editor.document.range.toSelection(false),
227+
]);
226228
} else {
227229
// Otherwise, we create and open a new document for the snippet in the
228230
// user snippets dir

packages/cursorless-engine/src/actions/InsertCopy.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,10 @@ class InsertCopy implements SimpleAction {
8686
([edit, selection]) => edit!.updateRange(selection!),
8787
);
8888

89-
setSelectionsWithoutFocusingEditor(editableEditor, updatedCursorSelections);
89+
await setSelectionsWithoutFocusingEditor(
90+
editableEditor,
91+
updatedCursorSelections,
92+
);
9093
const primarySelection = editor.selections[0];
9194

9295
if (

packages/cursorless-engine/src/actions/InsertEmptyLines.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ class InsertEmptyLines implements SimpleAction {
6161
],
6262
);
6363

64-
setSelectionsWithoutFocusingEditor(
64+
await setSelectionsWithoutFocusingEditor(
6565
editableEditor,
6666
updatedCursorSelections,
6767
);

packages/cursorless-engine/src/actions/PasteFromClipboard.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ export class PasteFromClipboard {
6060
// Reset cursors on the editor where the edits took place.
6161
// NB: We don't focus the editor here because we want to focus the original
6262
// editor, not the one where the edits took place
63-
setSelectionsWithoutFocusingEditor(editor, updatedCursorSelections);
63+
await setSelectionsWithoutFocusingEditor(editor, updatedCursorSelections);
6464

6565
// If necessary focus back original editor
6666
if (originalEditor != null && !originalEditor.isActive) {

packages/cursorless-engine/src/actions/Wrap.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,10 @@ export default class Wrap {
111111
],
112112
);
113113

114-
setSelectionsWithoutFocusingEditor(editableEditor, cursorSelections);
114+
await setSelectionsWithoutFocusingEditor(
115+
editableEditor,
116+
cursorSelections,
117+
);
115118

116119
await ide().flashRanges(
117120
delimiterSelections.map((selection) => ({

packages/cursorless-engine/src/util/setSelectionsAndFocusEditor.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ export async function setSelectionsAndFocusEditor(
99
selections: Selection[],
1010
revealRange: boolean = true,
1111
) {
12-
setSelectionsWithoutFocusingEditor(editor, selections);
12+
await setSelectionsWithoutFocusingEditor(editor, selections);
1313

1414
if (revealRange) {
1515
await editor.revealRange(editor.selections[0]);
@@ -20,13 +20,15 @@ export async function setSelectionsAndFocusEditor(
2020
await editor.focus();
2121
}
2222

23-
export function setSelectionsWithoutFocusingEditor(
23+
export async function setSelectionsWithoutFocusingEditor(
2424
editor: EditableTextEditor,
2525
selections: Selection[],
2626
) {
27-
editor.selections = uniqWithHash(
28-
selections,
29-
(a, b) => a.isEqual(b),
30-
(s) => s.concise(),
27+
await editor.setSelections(
28+
uniqWithHash(
29+
selections,
30+
(a, b) => a.isEqual(b),
31+
(s) => s.concise(),
32+
),
3133
);
3234
}

0 commit comments

Comments
 (0)