Skip to content

Commit 008baa1

Browse files
Ability to focus diff editor (#2388)
Also took a crack at the problem of focusing the search editor Fixes #1722 ## 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 --------- Co-authored-by: Pokey Rule <[email protected]>
1 parent 06eb81c commit 008baa1

File tree

16 files changed

+120
-105
lines changed

16 files changed

+120
-105
lines changed

packages/common/src/types/TextEditor.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,25 @@ export interface TextEditor {
5353
isEqual(other: TextEditor): boolean;
5454
}
5555

56+
export interface SetSelectionsOpts {
57+
focusEditor?: boolean;
58+
}
59+
5660
export interface EditableTextEditor extends TextEditor {
57-
setSelections(selections: Selection[]): Promise<void>;
61+
/**
62+
* Set the selections in this text editor, optionally focusing the editor
63+
* and/or revealing the ranges.
64+
*
65+
* Note that if your editor requires unique selections, you should deduplicate
66+
* them in your implementation of this method.
67+
*
68+
* @param selections The new selections
69+
* @param opts The options for setting the selections
70+
*/
71+
setSelections(
72+
selections: Selection[],
73+
opts?: SetSelectionsOpts,
74+
): Promise<void>;
5875

5976
options: TextEditorOptions;
6077

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

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import {
1414
import { ide } from "../singletons/ide.singleton";
1515
import { EditWithRangeUpdater } from "../typings/Types";
1616
import { Destination, Target } from "../typings/target.types";
17-
import { setSelectionsWithoutFocusingEditor } from "../util/setSelectionsAndFocusEditor";
1817
import {
1918
flashTargets,
2019
getContentRange,
@@ -209,10 +208,7 @@ abstract class BringMoveSwap {
209208
// NB: We set the selections here because we don't trust vscode to
210209
// properly move the cursor on a bring. Sometimes it will smear an
211210
// empty selection
212-
await setSelectionsWithoutFocusingEditor(
213-
editableEditor,
214-
cursorSelections,
215-
);
211+
await editableEditor.setSelections(cursorSelections);
216212

217213
const marks = [
218214
...this.getMarks(sourceEdits, updatedSourceEditSelections),

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

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,6 @@ import { RangeUpdater } from "../core/updateSelections/RangeUpdater";
55
import { callFunctionAndUpdateSelections } from "../core/updateSelections/updateSelections";
66
import { ide } from "../singletons/ide.singleton";
77
import { Target } from "../typings/target.types";
8-
import {
9-
setSelectionsAndFocusEditor,
10-
setSelectionsWithoutFocusingEditor,
11-
} from "../util/setSelectionsAndFocusEditor";
128
import {
139
ensureSingleEditor,
1410
ensureSingleTarget,
@@ -95,11 +91,9 @@ export class CallbackAction {
9591

9692
// For this callback/command to the work we have to have the correct editor focused
9793
if (options.setSelection) {
98-
await setSelectionsAndFocusEditor(
99-
editableEditor,
100-
targetSelections,
101-
false,
102-
);
94+
await editableEditor.setSelections(targetSelections, {
95+
focusEditor: true,
96+
});
10397
}
10498

10599
const [updatedOriginalSelections, updatedTargetSelections] =
@@ -116,10 +110,7 @@ export class CallbackAction {
116110
// very end. This code can run on multiple editors in the course of
117111
// one command, so we want to avoid focusing the editor multiple
118112
// times.
119-
await setSelectionsWithoutFocusingEditor(
120-
editableEditor,
121-
updatedOriginalSelections,
122-
);
113+
await editableEditor.setSelections(updatedOriginalSelections);
123114
}
124115

125116
// If the document hasn't changed then we just return the original targets

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { PlainTarget } from "../processTargets/targets";
22
import { ide } from "../singletons/ide.singleton";
33
import { Target } from "../typings/target.types";
4-
import { setSelectionsAndFocusEditor } from "../util/setSelectionsAndFocusEditor";
54
import { ensureSingleEditor } from "../util/targetUtils";
65
import { Actions } from "./Actions";
76
import { SimpleAction, ActionReturnValue } from "./actions.types";
@@ -27,10 +26,12 @@ export default class Clear implements SimpleAction {
2726
const { thatTargets } = await this.actions.remove.run(plainTargets);
2827

2928
if (thatTargets != null) {
30-
await setSelectionsAndFocusEditor(
31-
ide().getEditableTextEditor(editor),
32-
thatTargets.map(({ contentSelection }) => contentSelection),
33-
);
29+
await ide()
30+
.getEditableTextEditor(editor)
31+
.setSelections(
32+
thatTargets.map(({ contentSelection }) => contentSelection),
33+
{ focusEditor: true },
34+
);
3435
}
3536

3637
return { thatTargets };

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

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { ide } from "../singletons/ide.singleton";
22
import { Target } from "../typings/target.types";
3-
import { setSelectionsWithoutFocusingEditor } from "../util/setSelectionsAndFocusEditor";
43
import { runOnTargetsForEachEditor } from "../util/targetUtils";
54
import { SimpleAction, ActionReturnValue } from "./actions.types";
65

@@ -24,10 +23,7 @@ export default class Deselect implements SimpleAction {
2423
throw new SelectionRequiredError();
2524
}
2625

27-
await setSelectionsWithoutFocusingEditor(
28-
ide().getEditableTextEditor(editor),
29-
newSelections,
30-
);
26+
await ide().getEditableTextEditor(editor).setSelections(newSelections);
3127
});
3228

3329
return {

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { RangeUpdater } from "../../core/updateSelections/RangeUpdater";
22
import { ide } from "../../singletons/ide.singleton";
33
import { Destination } from "../../typings/target.types";
4-
import { setSelectionsAndFocusEditor } from "../../util/setSelectionsAndFocusEditor";
54
import { createThatMark, ensureSingleEditor } from "../../util/targetUtils";
65
import { Actions } from "../Actions";
76
import { ActionReturnValue } from "../actions.types";
@@ -54,7 +53,7 @@ export class EditNew {
5453
const newSelections = state.destinations.map((destination, index) =>
5554
state.cursorRanges[index]!.toSelection(destination.target.isReversed),
5655
);
57-
await setSelectionsAndFocusEditor(editableEditor, newSelections);
56+
await editableEditor.setSelections(newSelections, { focusEditor: true });
5857

5958
return {
6059
thatSelections: createThatMark(

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

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import { ModifierStageFactory } from "../processTargets/ModifierStageFactory";
1212
import { containingLineIfUntypedModifier } from "../processTargets/modifiers/commonContainingScopeIfUntypedModifiers";
1313
import { ide } from "../singletons/ide.singleton";
1414
import { Target } from "../typings/target.types";
15-
import { setSelectionsWithoutFocusingEditor } from "../util/setSelectionsAndFocusEditor";
1615
import { createThatMark, runOnTargetsForEachEditor } from "../util/targetUtils";
1716
import { SimpleAction, ActionReturnValue } from "./actions.types";
1817

@@ -86,10 +85,7 @@ class InsertCopy implements SimpleAction {
8685
([edit, selection]) => edit!.updateRange(selection!),
8786
);
8887

89-
await setSelectionsWithoutFocusingEditor(
90-
editableEditor,
91-
updatedCursorSelections,
92-
);
88+
await editableEditor.setSelections(updatedCursorSelections);
9389
const primarySelection = editor.selections[0];
9490

9591
if (

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

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import { RangeUpdater } from "../core/updateSelections/RangeUpdater";
44
import { performEditsAndUpdateSelections } from "../core/updateSelections/updateSelections";
55
import { ide } from "../singletons/ide.singleton";
66
import { Target } from "../typings/target.types";
7-
import { setSelectionsWithoutFocusingEditor } from "../util/setSelectionsAndFocusEditor";
87
import { runOnTargetsForEachEditor } from "../util/targetUtils";
98
import { SimpleAction, ActionReturnValue } from "./actions.types";
109

@@ -61,10 +60,7 @@ class InsertEmptyLines implements SimpleAction {
6160
],
6261
);
6362

64-
await setSelectionsWithoutFocusingEditor(
65-
editableEditor,
66-
updatedCursorSelections,
67-
);
63+
await editableEditor.setSelections(updatedCursorSelections);
6864

6965
return {
7066
thatMark: updatedThatSelections.map((selection) => ({

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import {
1010
} from "../core/updateSelections/updateSelections";
1111
import { ide } from "../singletons/ide.singleton";
1212
import { Destination } from "../typings/target.types";
13-
import { setSelectionsWithoutFocusingEditor } from "../util/setSelectionsAndFocusEditor";
1413
import { ensureSingleEditor } from "../util/targetUtils";
1514
import { Actions } from "./Actions";
1615
import { ActionReturnValue } from "./actions.types";
@@ -60,7 +59,7 @@ export class PasteFromClipboard {
6059
// Reset cursors on the editor where the edits took place.
6160
// NB: We don't focus the editor here because we want to focus the original
6261
// editor, not the one where the edits took place
63-
await setSelectionsWithoutFocusingEditor(editor, updatedCursorSelections);
62+
await editor.setSelections(updatedCursorSelections);
6463

6564
// If necessary focus back original editor
6665
if (originalEditor != null && !originalEditor.isActive) {

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

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ 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";
1211

1312
export default class Delete implements SimpleAction {
1413
constructor(private rangeUpdater: RangeUpdater) {
@@ -51,10 +50,7 @@ export default class Delete implements SimpleAction {
5150
[cursorSelections, editSelections],
5251
);
5352

54-
await setSelectionsWithoutFocusingEditor(
55-
editableEditor,
56-
updatedCursorSelections,
57-
);
53+
await editableEditor.setSelections(updatedCursorSelections);
5854

5955
return zip(targets, updatedEditSelections).map(
6056
([target, range]) =>

0 commit comments

Comments
 (0)