1
- import { FlashStyle } from "@cursorless/common" ;
1
+ import { FlashStyle , Selection , TextEditor } from "@cursorless/common" ;
2
2
import { flatten , zip } from "lodash" ;
3
3
import { RangeUpdater } from "../core/updateSelections/RangeUpdater" ;
4
- import { performEditsAndUpdateRanges } from "../core/updateSelections/updateSelections" ;
4
+ import { performEditsAndUpdateSelections } from "../core/updateSelections/updateSelections" ;
5
5
import { RawSelectionTarget } from "../processTargets/targets" ;
6
6
import { ide } from "../singletons/ide.singleton" ;
7
7
import { Target } from "../typings/target.types" ;
8
8
import { flashTargets , runOnTargetsForEachEditor } from "../util/targetUtils" ;
9
9
import { unifyRemovalTargets } from "../util/unifyRanges" ;
10
10
import { SimpleAction , ActionReturnValue } from "./actions.types" ;
11
+ import { setSelectionsWithoutFocusingEditor } from "../util/setSelectionsAndFocusEditor" ;
11
12
12
13
export default class Delete implements SimpleAction {
13
14
constructor ( private rangeUpdater : RangeUpdater ) {
14
15
this . run = this . run . bind ( this ) ;
16
+ this . runForEditor = this . runForEditor . bind ( this ) ;
15
17
}
16
18
17
19
async run (
@@ -28,28 +30,39 @@ export default class Delete implements SimpleAction {
28
30
}
29
31
30
32
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 ) ,
51
34
) ;
52
35
53
36
return { thatTargets } ;
54
37
}
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
+ }
55
68
}
0 commit comments