Skip to content

Commit 3538503

Browse files
pokeyAndreasArvidssonpre-commit-ci[bot]
authored
Remove VSCode from highlights code (#1221)
## Checklist - [ ] I have added [tests](https://www.cursorless.org/docs/contributing/test-case-recorder/) - [ ] Add new "clear highlights" command that will remove all highlights in active editor - [ ] 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: Andreas Arvidsson <[email protected]> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent bca3c8a commit 3538503

File tree

133 files changed

+1414
-1015
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

133 files changed

+1414
-1015
lines changed

src/actions/BringMoveSwap.ts

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,16 @@ import {
88
getSelectionInfo,
99
performEditsAndUpdateFullSelectionInfos,
1010
} from "../core/updateSelections/updateSelections";
11+
import { FlashStyle } from "../libs/common/ide/types/FlashDescriptor";
1112
import ide from "../libs/cursorless-engine/singletons/ide.singleton";
1213
import { Target } from "../typings/target.types";
1314
import { EditWithRangeUpdater, Graph } from "../typings/Types";
1415
import { setSelectionsWithoutFocusingEditor } from "../util/setSelectionsAndFocusEditor";
15-
import { getContentRange, runForEachEditor } from "../util/targetUtils";
16+
import {
17+
flashTargets,
18+
getContentRange,
19+
runForEachEditor,
20+
} from "../util/targetUtils";
1621
import { unifyRemovalTargets } from "../util/unifyRanges";
1722
import { Action, ActionReturnValue } from "./actions.types";
1823

@@ -47,39 +52,37 @@ class BringMoveSwap implements Action {
4752
}
4853

4954
private getDecorationContext() {
50-
let sourceStyle;
55+
let sourceStyle: FlashStyle;
5156
let getSourceRangeCallback;
5257
if (this.type === "bring") {
53-
sourceStyle = this.graph.editStyles.referenced;
58+
sourceStyle = FlashStyle.referenced;
5459
getSourceRangeCallback = getContentRange;
5560
} else if (this.type === "move") {
56-
sourceStyle = this.graph.editStyles.pendingDelete;
61+
sourceStyle = FlashStyle.pendingDelete;
5762
getSourceRangeCallback = getRemovalHighlightRange;
5863
}
5964
// NB this.type === "swap"
6065
else {
61-
sourceStyle = this.graph.editStyles.pendingModification1;
66+
sourceStyle = FlashStyle.pendingModification1;
6267
getSourceRangeCallback = getContentRange;
6368
}
6469
return {
6570
sourceStyle,
66-
destinationStyle: this.graph.editStyles.pendingModification0,
71+
destinationStyle: FlashStyle.pendingModification0,
6772
getSourceRangeCallback,
6873
};
6974
}
7075

7176
private async decorateTargets(sources: Target[], destinations: Target[]) {
7277
const decorationContext = this.getDecorationContext();
7378
await Promise.all([
74-
this.graph.editStyles.displayPendingEditDecorations(
79+
flashTargets(
80+
ide(),
7581
sources,
7682
decorationContext.sourceStyle,
7783
decorationContext.getSourceRangeCallback,
7884
),
79-
this.graph.editStyles.displayPendingEditDecorations(
80-
destinations,
81-
decorationContext.destinationStyle,
82-
),
85+
flashTargets(ide(), destinations, decorationContext.destinationStyle),
8386
]);
8487
}
8588

@@ -220,12 +223,14 @@ class BringMoveSwap implements Action {
220223
const getRange = (target: Target) =>
221224
thatMark.find((t) => t.target === target)!.selection;
222225
return Promise.all([
223-
this.graph.editStyles.displayPendingEditDecorations(
226+
flashTargets(
227+
ide(),
224228
thatMark.filter(({ isSource }) => isSource).map(({ target }) => target),
225229
decorationContext.sourceStyle,
226230
getRange,
227231
),
228-
this.graph.editStyles.displayPendingEditDecorations(
232+
flashTargets(
233+
ide(),
229234
thatMark
230235
.filter(({ isSource }) => !isSource)
231236
.map(({ target }) => target),

src/actions/CallbackAction.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { EditableTextEditor, TextEditor } from "@cursorless/common";
22
import { flatten } from "lodash";
33
import { selectionToThatTarget } from "../core/commandRunner/selectionToThatTarget";
44
import { callFunctionAndUpdateSelections } from "../core/updateSelections/updateSelections";
5+
import { FlashStyle } from "../libs/common/ide/types/FlashDescriptor";
56
import ide from "../libs/cursorless-engine/singletons/ide.singleton";
67
import { Target } from "../typings/target.types";
78
import { Graph } from "../typings/Types";
@@ -12,6 +13,7 @@ import {
1213
import {
1314
ensureSingleEditor,
1415
ensureSingleTarget,
16+
flashTargets,
1517
runOnTargetsForEachEditor,
1618
runOnTargetsForEachEditorSequentially,
1719
} from "../util/targetUtils";
@@ -41,10 +43,7 @@ export class CallbackAction implements Action {
4143
options: CallbackOptions,
4244
): Promise<ActionReturnValue> {
4345
if (options.showDecorations) {
44-
await this.graph.editStyles.displayPendingEditDecorations(
45-
targets,
46-
this.graph.editStyles.referenced,
47-
);
46+
await flashTargets(ide(), targets, FlashStyle.referenced);
4847
}
4948

5049
if (options.ensureSingleEditor) {

src/actions/CutToClipboard.ts

Lines changed: 60 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
1-
import { PlainTarget } from "../processTargets/targets";
1+
import { Range } from "@cursorless/common";
2+
import {
3+
FlashDescriptor,
4+
FlashStyle,
5+
} from "../libs/common/ide/types/FlashDescriptor";
6+
import {
7+
toCharacterRange,
8+
toLineRange,
9+
} from "../libs/common/types/GeneralizedRange";
10+
import ide from "../libs/cursorless-engine/singletons/ide.singleton";
211
import { Target } from "../typings/target.types";
312
import { Graph } from "../typings/Types";
4-
import { getOutsideOverflow } from "../util/targetUtils";
513
import { Action, ActionReturnValue } from "./actions.types";
614

715
export class CutToClipboard implements Action {
@@ -10,32 +18,42 @@ export class CutToClipboard implements Action {
1018
}
1119

1220
async run([targets]: [Target[]]): Promise<ActionReturnValue> {
13-
const overflowTargets = targets.flatMap((target) => {
14-
const range = target.getRemovalHighlightRange();
15-
if (range == null) {
16-
return [];
17-
}
18-
return getOutsideOverflow(target.editor, target.contentRange, range).map(
19-
(overflow): Target =>
20-
// TODO Instead of creating a new target display decorations by range
21-
new PlainTarget({
22-
editor: target.editor,
23-
contentRange: overflow,
24-
isReversed: target.isReversed,
25-
}),
26-
);
27-
});
28-
29-
await Promise.all([
30-
this.graph.editStyles.displayPendingEditDecorations(
31-
targets,
32-
this.graph.editStyles.referenced,
33-
),
34-
this.graph.editStyles.displayPendingEditDecorations(
35-
overflowTargets,
36-
this.graph.editStyles.pendingDelete,
37-
),
38-
]);
21+
await ide().flashRanges(
22+
targets.flatMap((target) => {
23+
const { editor, contentRange } = target;
24+
const removalHighlightRange = target.getRemovalHighlightRange();
25+
26+
if (target.isLine) {
27+
return [
28+
{
29+
editor,
30+
range: toCharacterRange(contentRange),
31+
style: FlashStyle.referenced,
32+
},
33+
{
34+
editor,
35+
range: toLineRange(removalHighlightRange),
36+
style: FlashStyle.pendingDelete,
37+
},
38+
];
39+
}
40+
41+
return [
42+
{
43+
editor,
44+
range: toCharacterRange(contentRange),
45+
style: FlashStyle.referenced,
46+
},
47+
...getOutsideOverflow(contentRange, removalHighlightRange).map(
48+
(overflow): FlashDescriptor => ({
49+
editor,
50+
range: toCharacterRange(overflow),
51+
style: FlashStyle.pendingDelete,
52+
}),
53+
),
54+
];
55+
}),
56+
);
3957

4058
const options = { showDecorations: false };
4159

@@ -49,3 +67,17 @@ export class CutToClipboard implements Action {
4967
return { thatSelections: thatMark };
5068
}
5169
}
70+
71+
/** Get the possible leading and trailing overflow ranges of the outside range compared to the inside range */
72+
function getOutsideOverflow(insideRange: Range, outsideRange: Range): Range[] {
73+
const { start: insideStart, end: insideEnd } = insideRange;
74+
const { start: outsideStart, end: outsideEnd } = outsideRange;
75+
const result = [];
76+
if (outsideStart.isBefore(insideStart)) {
77+
result.push(new Range(outsideStart, insideStart));
78+
}
79+
if (outsideEnd.isAfter(insideEnd)) {
80+
result.push(new Range(insideEnd, outsideEnd));
81+
}
82+
return result;
83+
}

src/actions/FollowLink.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
1+
import { FlashStyle } from "../libs/common/ide/types/FlashDescriptor";
12
import ide from "../libs/cursorless-engine/singletons/ide.singleton";
23
import { Target } from "../typings/target.types";
34
import { Graph } from "../typings/Types";
4-
import { createThatMark, ensureSingleTarget } from "../util/targetUtils";
5+
import {
6+
createThatMark,
7+
ensureSingleTarget,
8+
flashTargets,
9+
} from "../util/targetUtils";
510
import { Action, ActionReturnValue } from "./actions.types";
611

712
export default class FollowLink implements Action {
@@ -12,10 +17,7 @@ export default class FollowLink implements Action {
1217
async run([targets]: [Target[]]): Promise<ActionReturnValue> {
1318
const target = ensureSingleTarget(targets);
1419

15-
await this.graph.editStyles.displayPendingEditDecorations(
16-
targets,
17-
this.graph.editStyles.referenced,
18-
);
20+
await flashTargets(ide(), targets, FlashStyle.referenced);
1921

2022
const openedLink = await ide()
2123
.getEditableTextEditor(target.editor)

src/actions/GenerateSnippet/GenerateSnippet.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import { Range } from "@cursorless/common";
2+
import { FlashStyle } from "../../libs/common/ide/types/FlashDescriptor";
23
import ide from "../../libs/cursorless-engine/singletons/ide.singleton";
34
import { Offsets } from "../../processTargets/modifiers/surroundingPair/types";
45
import isTesting from "../../testUtil/isTesting";
56
import { Target } from "../../typings/target.types";
67
import { Graph } from "../../typings/Types";
7-
import { ensureSingleTarget } from "../../util/targetUtils";
8+
import { ensureSingleTarget, flashTargets } from "../../util/targetUtils";
89
import { Action, ActionReturnValue } from "../actions.types";
910
import { constructSnippetBody } from "./constructSnippetBody";
1011
import { editText } from "./editText";
@@ -62,10 +63,7 @@ export default class GenerateSnippet implements Action {
6263
// immediately starts saying the name of the snippet (eg command chain
6364
// "snippet make funk camel my function"), we're more likely to
6465
// win the race and have the input box ready for them
65-
this.graph.editStyles.displayPendingEditDecorations(
66-
targets,
67-
this.graph.editStyles.referenced,
68-
);
66+
flashTargets(ide(), targets, FlashStyle.referenced);
6967

7068
if (snippetName == null) {
7169
snippetName = await ide().showInputBox({

src/actions/GetText.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
import { FlashStyle } from "../libs/common/ide/types/FlashDescriptor";
2+
import ide from "../libs/cursorless-engine/singletons/ide.singleton";
13
import { Target } from "../typings/target.types";
24
import { Graph } from "../typings/Types";
3-
import { ensureSingleTarget } from "../util/targetUtils";
5+
import { ensureSingleTarget, flashTargets } from "../util/targetUtils";
46
import { Action, ActionReturnValue } from "./actions.types";
57

68
export default class GetText implements Action {
@@ -16,10 +18,7 @@ export default class GetText implements Action {
1618
} = {},
1719
): Promise<ActionReturnValue> {
1820
if (showDecorations) {
19-
await this.graph.editStyles.displayPendingEditDecorations(
20-
targets,
21-
this.graph.editStyles.referenced,
22-
);
21+
await flashTargets(ide(), targets, FlashStyle.referenced);
2322
}
2423

2524
if (doEnsureSingleTarget) {

src/actions/Highlight.ts

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
1-
import { EditStyleName } from "../core/editStyles";
1+
import { HighlightId } from "@cursorless/common";
2+
import ide from "../libs/cursorless-engine/singletons/ide.singleton";
23
import { Target } from "../typings/target.types";
34
import { Graph } from "../typings/Types";
5+
import {
6+
runOnTargetsForEachEditor,
7+
toGeneralizedRange,
8+
} from "../util/targetUtils";
49
import { Action, ActionReturnValue } from "./actions.types";
510

611
export default class Highlight implements Action {
@@ -10,12 +15,33 @@ export default class Highlight implements Action {
1015

1116
async run(
1217
[targets]: [Target[]],
13-
styleName: EditStyleName = "highlight0",
18+
highlightId?: HighlightId,
1419
): Promise<ActionReturnValue> {
15-
const style = this.graph.editStyles[styleName];
20+
if (ide().capabilities.commands["highlight"] == null) {
21+
throw Error(`The highlight action is not supported by your ide`);
22+
}
1623

17-
this.graph.editStyles.clearDecorations(style);
18-
await this.graph.editStyles.setDecorations(targets, style);
24+
if (targets.length === 0) {
25+
// Special case to clear highlights for the active editor when user says
26+
// "highlight nothing"
27+
const { activeTextEditor } = ide();
28+
29+
if (activeTextEditor == null) {
30+
throw Error(
31+
"The `highlight nothing` command requires an active text editor",
32+
);
33+
}
34+
35+
await ide().setHighlightRanges(highlightId, activeTextEditor, []);
36+
} else {
37+
await runOnTargetsForEachEditor(targets, (editor, targets) =>
38+
ide().setHighlightRanges(
39+
highlightId,
40+
editor,
41+
targets.map(toGeneralizedRange),
42+
),
43+
);
44+
}
1945

2046
return {
2147
thatTargets: targets,

src/actions/InsertCopy.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@ import {
22
RangeExpansionBehavior,
33
Selection,
44
TextEditor,
5+
toCharacterRange,
56
} from "@cursorless/common";
67
import { flatten, zip } from "lodash";
78
import { performEditsAndUpdateSelectionsWithBehavior } from "../core/updateSelections/updateSelections";
9+
import { FlashStyle } from "../libs/common/ide/types/FlashDescriptor";
810
import ide from "../libs/cursorless-engine/singletons/ide.singleton";
911
import { containingLineIfUntypedStage } from "../processTargets/modifiers/commonContainingScopeIfUntypedStages";
1012
import { Target } from "../typings/target.types";
@@ -26,15 +28,14 @@ class InsertCopy implements Action {
2628
await runOnTargetsForEachEditor(targets, this.runForEditor),
2729
);
2830

29-
await this.graph.editStyles.displayPendingEditDecorationsForRanges(
31+
await ide().flashRanges(
3032
results.flatMap((result) =>
3133
result.thatMark.map((that) => ({
3234
editor: that.editor,
33-
range: that.selection,
35+
range: toCharacterRange(that.selection),
36+
style: FlashStyle.justAdded,
3437
})),
3538
),
36-
this.graph.editStyles.justAdded,
37-
true,
3839
);
3940

4041
return {

src/actions/InsertEmptyLines.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import { Range, Selection } from "@cursorless/common";
22
import { flatten } from "lodash";
33
import { performEditsAndUpdateSelections } from "../core/updateSelections/updateSelections";
4+
import { FlashStyle } from "../libs/common/ide/types/FlashDescriptor";
5+
import { toLineRange } from "../libs/common/types/GeneralizedRange";
46
import ide from "../libs/cursorless-engine/singletons/ide.singleton";
57
import { Target } from "../typings/target.types";
68
import { Graph } from "../typings/Types";
@@ -85,10 +87,14 @@ class InsertEmptyLines implements Action {
8587
}),
8688
);
8789

88-
await this.graph.editStyles.displayPendingEditDecorationsForRanges(
89-
results.flatMap((result) => result.lineSelections),
90-
this.graph.editStyles.justAdded,
91-
false,
90+
await ide().flashRanges(
91+
results.flatMap((result) =>
92+
result.lineSelections.map(({ editor, range }) => ({
93+
editor,
94+
range: toLineRange(range),
95+
style: FlashStyle.justAdded,
96+
})),
97+
),
9298
);
9399

94100
const thatMark = results.flatMap((result) => result.thatMark);

0 commit comments

Comments
 (0)