diff --git a/packages/cursorless-engine/src/actions/InsertCopy.ts b/packages/cursorless-engine/src/actions/InsertCopy.ts index d9caad1366..d87b962c59 100644 --- a/packages/cursorless-engine/src/actions/InsertCopy.ts +++ b/packages/cursorless-engine/src/actions/InsertCopy.ts @@ -72,7 +72,7 @@ class InsertCopy implements SimpleAction { const editableEditor = ide().getEditableTextEditor(editor); const [ - updatedEditorSelections, + updatedCursorSelections, updatedContentSelections, updatedEditSelections, ]: Selection[][] = await performEditsAndUpdateSelectionsWithBehavior( @@ -86,7 +86,7 @@ class InsertCopy implements SimpleAction { ([edit, selection]) => edit!.updateRange(selection!), ); - setSelectionsWithoutFocusingEditor(editableEditor, updatedEditorSelections); + setSelectionsWithoutFocusingEditor(editableEditor, updatedCursorSelections); const primarySelection = editor.selections[0]; if ( diff --git a/packages/cursorless-engine/src/actions/Remove.ts b/packages/cursorless-engine/src/actions/Remove.ts index aeaf93d620..7728e0e44f 100644 --- a/packages/cursorless-engine/src/actions/Remove.ts +++ b/packages/cursorless-engine/src/actions/Remove.ts @@ -1,17 +1,19 @@ -import { FlashStyle } from "@cursorless/common"; +import { FlashStyle, Selection, TextEditor } from "@cursorless/common"; import { flatten, zip } from "lodash"; import { RangeUpdater } from "../core/updateSelections/RangeUpdater"; -import { performEditsAndUpdateRanges } from "../core/updateSelections/updateSelections"; +import { performEditsAndUpdateSelections } from "../core/updateSelections/updateSelections"; import { RawSelectionTarget } from "../processTargets/targets"; import { ide } from "../singletons/ide.singleton"; import { Target } from "../typings/target.types"; import { flashTargets, runOnTargetsForEachEditor } from "../util/targetUtils"; import { unifyRemovalTargets } from "../util/unifyRanges"; import { SimpleAction, ActionReturnValue } from "./actions.types"; +import { setSelectionsWithoutFocusingEditor } from "../util/setSelectionsAndFocusEditor"; export default class Delete implements SimpleAction { constructor(private rangeUpdater: RangeUpdater) { this.run = this.run.bind(this); + this.runForEditor = this.runForEditor.bind(this); } async run( @@ -28,28 +30,39 @@ export default class Delete implements SimpleAction { } const thatTargets = flatten( - await runOnTargetsForEachEditor(targets, async (editor, targets) => { - const edits = targets.map((target) => target.constructRemovalEdit()); - const ranges = edits.map((edit) => edit.range); - - const [updatedRanges] = await performEditsAndUpdateRanges( - this.rangeUpdater, - ide().getEditableTextEditor(editor), - edits, - [ranges], - ); - - return zip(targets, updatedRanges).map( - ([target, range]) => - new RawSelectionTarget({ - editor: target!.editor, - isReversed: target!.isReversed, - contentRange: range!, - }), - ); - }), + await runOnTargetsForEachEditor(targets, this.runForEditor), ); return { thatTargets }; } + + private async runForEditor(editor: TextEditor, targets: Target[]) { + const edits = targets.map((target) => target.constructRemovalEdit()); + + const cursorSelections = editor.selections; + const editSelections = edits.map(({ range }) => range.toSelection(false)); + const editableEditor = ide().getEditableTextEditor(editor); + + const [updatedCursorSelections, updatedEditSelections]: Selection[][] = + await performEditsAndUpdateSelections( + this.rangeUpdater, + editableEditor, + edits, + [cursorSelections, editSelections], + ); + + await setSelectionsWithoutFocusingEditor( + editableEditor, + updatedCursorSelections, + ); + + return zip(targets, updatedEditSelections).map( + ([target, range]) => + new RawSelectionTarget({ + editor: target!.editor, + isReversed: target!.isReversed, + contentRange: range!, + }), + ); + } }