Skip to content

Commit

Permalink
feat: make text editor selections setter async in preparation for neovim
Browse files Browse the repository at this point in the history
  • Loading branch information
Cedric Halbronn committed Mar 19, 2024
1 parent 9e9d874 commit 3b9d9a5
Show file tree
Hide file tree
Showing 12 changed files with 31 additions and 18 deletions.
4 changes: 2 additions & 2 deletions packages/common/src/types/TextEditor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export interface TextEditor {
/**
* The selections in this text editor.
*/
readonly selections: Selection[];
get selections(): Selection[];

/**
* Text editor options.
Expand All @@ -54,7 +54,7 @@ export interface TextEditor {
}

export interface EditableTextEditor extends TextEditor {
selections: Selection[];
setSelections(selections: Selection[]): Promise<void>;

options: TextEditorOptions;

Expand Down
5 changes: 4 additions & 1 deletion packages/cursorless-engine/src/actions/BringMoveSwap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
2 changes: 1 addition & 1 deletion packages/cursorless-engine/src/actions/CallbackAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
);
Expand Down
2 changes: 1 addition & 1 deletion packages/cursorless-engine/src/actions/Deselect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export default class Deselect implements SimpleAction {
throw new SelectionRequiredError();
}

setSelectionsWithoutFocusingEditor(
await setSelectionsWithoutFocusingEditor(
ide().getEditableTextEditor(editor),
newSelections,
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 4 additions & 1 deletion packages/cursorless-engine/src/actions/InsertCopy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down
2 changes: 1 addition & 1 deletion packages/cursorless-engine/src/actions/InsertEmptyLines.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class InsertEmptyLines implements SimpleAction {
],
);

setSelectionsWithoutFocusingEditor(
await setSelectionsWithoutFocusingEditor(
editableEditor,
updatedCursorSelections,
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
5 changes: 4 additions & 1 deletion packages/cursorless-engine/src/actions/Wrap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,10 @@ export default class Wrap {
],
);

setSelectionsWithoutFocusingEditor(editableEditor, cursorSelections);
await setSelectionsWithoutFocusingEditor(
editableEditor,
cursorSelections,
);

await ide().flashRanges(
delimiterSelections.map((selection) => ({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
Expand All @@ -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(),
),
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export async function vscodeInsertSnippet(
ranges: Range[] | undefined,
): Promise<void> {
if (ranges != null) {
editor.selections = ranges.map((range) => range.toSelection(false));
editor.setSelections(ranges.map((range) => range.toSelection(false)));
}

await editor.focus();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export class VscodeTextEditorImpl implements EditableTextEditor {
return this.editor.selections.map(fromVscodeSelection);
}

set selections(selections: Selection[]) {
async setSelections(selections: Selection[]): Promise<void> {
this.editor.selections = selections.map(toVscodeSelection);
}

Expand Down

0 comments on commit 3b9d9a5

Please sign in to comment.