Skip to content

Commit 35ff9da

Browse files
saidelikeCedric Halbronn
andauthored
enforce new selection for "remove" action (#2285)
## 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 From debugging the vscode cursorless extension, it seems the selection is automatically updated by vscode itself when dealing with the "remove" action while calling editBuilder.delete() (TextEditorEdit). However it is not the case for neovim so it was using the old selection. I fixed it so cursorless always enforce selection for that "remove" action. Tested on neovim and vscode. --------- Co-authored-by: Cedric Halbronn <[email protected]>
1 parent a9e37f6 commit 35ff9da

File tree

2 files changed

+37
-24
lines changed

2 files changed

+37
-24
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ class InsertCopy implements SimpleAction {
7272
const editableEditor = ide().getEditableTextEditor(editor);
7373

7474
const [
75-
updatedEditorSelections,
75+
updatedCursorSelections,
7676
updatedContentSelections,
7777
updatedEditSelections,
7878
]: Selection[][] = await performEditsAndUpdateSelectionsWithBehavior(
@@ -86,7 +86,7 @@ class InsertCopy implements SimpleAction {
8686
([edit, selection]) => edit!.updateRange(selection!),
8787
);
8888

89-
setSelectionsWithoutFocusingEditor(editableEditor, updatedEditorSelections);
89+
setSelectionsWithoutFocusingEditor(editableEditor, updatedCursorSelections);
9090
const primarySelection = editor.selections[0];
9191

9292
if (
Lines changed: 35 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
1-
import { FlashStyle } from "@cursorless/common";
1+
import { FlashStyle, Selection, TextEditor } from "@cursorless/common";
22
import { flatten, zip } from "lodash";
33
import { RangeUpdater } from "../core/updateSelections/RangeUpdater";
4-
import { performEditsAndUpdateRanges } from "../core/updateSelections/updateSelections";
4+
import { performEditsAndUpdateSelections } from "../core/updateSelections/updateSelections";
55
import { RawSelectionTarget } from "../processTargets/targets";
66
import { ide } from "../singletons/ide.singleton";
77
import { Target } from "../typings/target.types";
88
import { flashTargets, runOnTargetsForEachEditor } from "../util/targetUtils";
99
import { unifyRemovalTargets } from "../util/unifyRanges";
1010
import { SimpleAction, ActionReturnValue } from "./actions.types";
11+
import { setSelectionsWithoutFocusingEditor } from "../util/setSelectionsAndFocusEditor";
1112

1213
export 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

Comments
 (0)