1- import { FlashStyle } from "@cursorless/common" ;
1+ import { FlashStyle , Selection , TextEditor } from "@cursorless/common" ;
22import { flatten , zip } from "lodash" ;
33import { RangeUpdater } from "../core/updateSelections/RangeUpdater" ;
4- import { performEditsAndUpdateRanges } from "../core/updateSelections/updateSelections" ;
4+ import { performEditsAndUpdateSelections } from "../core/updateSelections/updateSelections" ;
55import { RawSelectionTarget } from "../processTargets/targets" ;
66import { ide } from "../singletons/ide.singleton" ;
77import { Target } from "../typings/target.types" ;
88import { flashTargets , runOnTargetsForEachEditor } from "../util/targetUtils" ;
99import { unifyRemovalTargets } from "../util/unifyRanges" ;
1010import { SimpleAction , ActionReturnValue } from "./actions.types" ;
11+ import { setSelectionsWithoutFocusingEditor } from "../util/setSelectionsAndFocusEditor" ;
1112
1213export default class Delete implements SimpleAction {
1314 constructor ( private rangeUpdater : RangeUpdater ) {
1415 this . run = this . run . bind ( this ) ;
16+ this . runForEditor = this . runForEditor . bind ( this ) ;
1517 }
1618
1719 async run (
@@ -28,28 +30,39 @@ export default class Delete implements SimpleAction {
2830 }
2931
3032 const thatTargets = flatten (
31- await runOnTargetsForEachEditor ( targets , async ( editor , targets ) => {
32- const edits = targets . map ( ( target ) => target . constructRemovalEdit ( ) ) ;
33- const ranges = edits . map ( ( edit ) => edit . range ) ;
34-
35- const [ updatedRanges ] = await performEditsAndUpdateRanges (
36- this . rangeUpdater ,
37- ide ( ) . getEditableTextEditor ( editor ) ,
38- edits ,
39- [ ranges ] ,
40- ) ;
41-
42- return zip ( targets , updatedRanges ) . map (
43- ( [ target , range ] ) =>
44- new RawSelectionTarget ( {
45- editor : target ! . editor ,
46- isReversed : target ! . isReversed ,
47- contentRange : range ! ,
48- } ) ,
49- ) ;
50- } ) ,
33+ await runOnTargetsForEachEditor ( targets , this . runForEditor ) ,
5134 ) ;
5235
5336 return { thatTargets } ;
5437 }
38+
39+ private async runForEditor ( editor : TextEditor , targets : Target [ ] ) {
40+ const edits = targets . map ( ( target ) => target . constructRemovalEdit ( ) ) ;
41+
42+ const cursorSelections = editor . selections ;
43+ const editSelections = edits . map ( ( { range } ) => range . toSelection ( false ) ) ;
44+ const editableEditor = ide ( ) . getEditableTextEditor ( editor ) ;
45+
46+ const [ updatedCursorSelections , updatedEditSelections ] : Selection [ ] [ ] =
47+ await performEditsAndUpdateSelections (
48+ this . rangeUpdater ,
49+ editableEditor ,
50+ edits ,
51+ [ cursorSelections , editSelections ] ,
52+ ) ;
53+
54+ await setSelectionsWithoutFocusingEditor (
55+ editableEditor ,
56+ updatedCursorSelections ,
57+ ) ;
58+
59+ return zip ( targets , updatedEditSelections ) . map (
60+ ( [ target , range ] ) =>
61+ new RawSelectionTarget ( {
62+ editor : target ! . editor ,
63+ isReversed : target ! . isReversed ,
64+ contentRange : range ! ,
65+ } ) ,
66+ ) ;
67+ }
5568}
0 commit comments