Skip to content

Commit d419437

Browse files
Use generalized ranges in breakpoint action (#2766)
We had a specific type for breakpoint descriptions which is more or less the same format as our generalized ranges that we use in other places. ## 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: Phil Cohen <[email protected]>
1 parent facd2f5 commit d419437

File tree

6 files changed

+40
-67
lines changed

6 files changed

+40
-67
lines changed

packages/common/src/types/TextEditor.ts

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type {
22
Edit,
3+
GeneralizedRange,
34
Range,
45
RevealLineAt,
56
Selection,
@@ -154,9 +155,9 @@ export interface EditableTextEditor extends TextEditor {
154155
* remove all breakpoints overlapping with the given descriptor if it overlaps
155156
* with any existing breakpoint, otherwise add a new breakpoint at the given
156157
* location.
157-
* @param descriptors A list of breakpoint descriptors
158+
* @param ranges A list of breakpoint ranges
158159
*/
159-
toggleBreakpoint(descriptors?: BreakpointDescriptor[]): Promise<void>;
160+
toggleBreakpoint(ranges?: GeneralizedRange[]): Promise<void>;
160161

161162
/**
162163
* Toggle line comments
@@ -237,21 +238,3 @@ export interface EditableTextEditor extends TextEditor {
237238
*/
238239
extractVariable(range?: Range): Promise<void>;
239240
}
240-
241-
interface LineBreakpointDescriptor {
242-
type: "line";
243-
startLine: number;
244-
/**
245-
* Last line, inclusive
246-
*/
247-
endLine: number;
248-
}
249-
250-
interface InlineBreakpointDescriptor {
251-
type: "inline";
252-
range: Range;
253-
}
254-
255-
export type BreakpointDescriptor =
256-
| LineBreakpointDescriptor
257-
| InlineBreakpointDescriptor;

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

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
1-
import type { BreakpointDescriptor } from "@cursorless/common";
21
import { FlashStyle } from "@cursorless/common";
32
import type { ModifierStageFactory } from "../processTargets/ModifierStageFactory";
43
import { containingLineIfUntypedModifier } from "../processTargets/modifiers/commonContainingScopeIfUntypedModifiers";
54
import { ide } from "../singletons/ide.singleton";
65
import type { Target } from "../typings/target.types";
7-
import { flashTargets, runOnTargetsForEachEditor } from "../util/targetUtils";
8-
import type { SimpleAction, ActionReturnValue } from "./actions.types";
6+
import {
7+
flashTargets,
8+
runOnTargetsForEachEditor,
9+
toGeneralizedRange,
10+
} from "../util/targetUtils";
11+
import type { ActionReturnValue, SimpleAction } from "./actions.types";
912

1013
export default class ToggleBreakpoint implements SimpleAction {
1114
getFinalStages = () => [
@@ -22,25 +25,11 @@ export default class ToggleBreakpoint implements SimpleAction {
2225
await flashTargets(ide(), thatTargets, FlashStyle.referenced);
2326

2427
await runOnTargetsForEachEditor(targets, async (editor, targets) => {
25-
const breakpointDescriptors: BreakpointDescriptor[] = targets.map(
26-
(target) => {
27-
const range = target.contentRange;
28-
return target.isLine
29-
? {
30-
type: "line",
31-
startLine: range.start.line,
32-
endLine: range.end.line,
33-
}
34-
: {
35-
type: "inline",
36-
range,
37-
};
38-
},
39-
);
28+
const generalizedRanges = targets.map(toGeneralizedRange);
4029

4130
await ide()
4231
.getEditableTextEditor(editor)
43-
.toggleBreakpoint(breakpointDescriptors);
32+
.toggleBreakpoint(generalizedRanges);
4433
});
4534

4635
return {

packages/cursorless-everywhere-talon-core/src/ide/TalonJsEditor.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import {
22
selectionsEqual,
3-
type BreakpointDescriptor,
43
type Edit,
54
type EditableTextEditor,
5+
type GeneralizedRange,
66
type InMemoryTextDocument,
77
type OpenLinkOptions,
88
type Range,
@@ -100,9 +100,7 @@ export class TalonJsEditor implements EditableTextEditor {
100100
throw new Error("unfold not implemented.");
101101
}
102102

103-
toggleBreakpoint(
104-
_descriptors?: BreakpointDescriptor[] | undefined,
105-
): Promise<void> {
103+
toggleBreakpoint(_ranges?: GeneralizedRange[]): Promise<void> {
106104
throw new Error("toggleBreakpoint not implemented.");
107105
}
108106

packages/cursorless-vscode/src/ide/vscode/VscodeTextEditorImpl.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type {
2-
BreakpointDescriptor,
32
Edit,
43
EditableTextEditor,
4+
GeneralizedRange,
55
OpenLinkOptions,
66
Range,
77
RevealLineAt,
@@ -164,8 +164,8 @@ export class VscodeTextEditorImpl implements EditableTextEditor {
164164
return vscodeUnfold(this.ide, this, ranges);
165165
}
166166

167-
public toggleBreakpoint(descriptors?: BreakpointDescriptor[]): Promise<void> {
168-
return vscodeToggleBreakpoint(this, descriptors);
167+
public toggleBreakpoint(ranges?: GeneralizedRange[]): Promise<void> {
168+
return vscodeToggleBreakpoint(this, ranges);
169169
}
170170

171171
public async toggleLineComment(_ranges?: Range[]): Promise<void> {
Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
import type { BreakpointDescriptor } from "@cursorless/common";
2-
import { toVscodeRange } from "@cursorless/vscode-common";
1+
import type { GeneralizedRange, Position } from "@cursorless/common";
2+
import { toVscodePosition } from "@cursorless/vscode-common";
33
import * as vscode from "vscode";
44
import type { VscodeTextEditorImpl } from "./VscodeTextEditorImpl";
55

66
export async function vscodeToggleBreakpoint(
77
editor: VscodeTextEditorImpl,
8-
descriptors: BreakpointDescriptor[] | undefined,
8+
ranges?: GeneralizedRange[] | undefined,
99
): Promise<void> {
10-
if (descriptors == null) {
10+
if (ranges == null) {
1111
return await vscode.commands.executeCommand(
1212
"editor.debug.action.toggleBreakpoint",
1313
);
@@ -17,18 +17,19 @@ export async function vscodeToggleBreakpoint(
1717
const toAdd: vscode.Breakpoint[] = [];
1818
const toRemove: vscode.Breakpoint[] = [];
1919

20-
descriptors.forEach((descriptor) => {
21-
const existing = getBreakpoints(uri, descriptor);
20+
ranges.forEach((range) => {
21+
const existing = getBreakpoints(uri, range);
22+
2223
if (existing.length > 0) {
2324
toRemove.push(...existing);
2425
} else {
2526
toAdd.push(
2627
new vscode.SourceBreakpoint(
2728
new vscode.Location(
2829
uri,
29-
descriptor.type === "line"
30-
? new vscode.Range(descriptor.startLine, 0, descriptor.endLine, 0)
31-
: toVscodeRange(descriptor.range),
30+
range.type === "line"
31+
? new vscode.Range(range.start, 0, range.end, 0)
32+
: toVscodeRange(range.start, range.end),
3233
),
3334
),
3435
);
@@ -39,22 +40,26 @@ export async function vscodeToggleBreakpoint(
3940
vscode.debug.removeBreakpoints(toRemove);
4041
}
4142

42-
function getBreakpoints(uri: vscode.Uri, descriptor: BreakpointDescriptor) {
43-
let rangeInterceptsDescriptor: (range: vscode.Range) => boolean;
43+
function getBreakpoints(uri: vscode.Uri, range: GeneralizedRange) {
44+
let rangeInterceptPredicate: (range: vscode.Range) => boolean;
4445

45-
if (descriptor.type === "line") {
46-
rangeInterceptsDescriptor = ({ start, end }) =>
47-
descriptor.startLine <= end.line && descriptor.endLine >= start.line;
46+
if (range.type === "line") {
47+
rangeInterceptPredicate = ({ start, end }) =>
48+
range.start <= end.line && range.end >= start.line;
4849
} else {
49-
const descriptorRange = toVscodeRange(descriptor.range);
50-
rangeInterceptsDescriptor = (range) =>
50+
const descriptorRange = toVscodeRange(range.start, range.end);
51+
rangeInterceptPredicate = (range) =>
5152
range.intersection(descriptorRange) != null;
5253
}
5354

5455
return vscode.debug.breakpoints.filter(
5556
(breakpoint) =>
5657
breakpoint instanceof vscode.SourceBreakpoint &&
5758
breakpoint.location.uri.toString() === uri.toString() &&
58-
rangeInterceptsDescriptor(breakpoint.location.range),
59+
rangeInterceptPredicate(breakpoint.location.range),
5960
);
6061
}
62+
63+
function toVscodeRange(start: Position, end: Position): vscode.Range {
64+
return new vscode.Range(toVscodePosition(start), toVscodePosition(end));
65+
}

packages/neovim-common/src/ide/neovim/NeovimTextEditorImpl.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type {
2-
BreakpointDescriptor,
32
Edit,
43
EditableTextEditor,
4+
GeneralizedRange,
55
OpenLinkOptions,
66
Range,
77
RevealLineAt,
@@ -127,9 +127,7 @@ export class NeovimTextEditorImpl implements EditableTextEditor {
127127
throw Error("unfold Not implemented");
128128
}
129129

130-
public toggleBreakpoint(
131-
_descriptors?: BreakpointDescriptor[],
132-
): Promise<void> {
130+
public toggleBreakpoint(_ranges?: GeneralizedRange[]): Promise<void> {
133131
throw Error("toggleBreakpoint Not implemented");
134132
}
135133

0 commit comments

Comments
 (0)