Skip to content

Commit 76085d1

Browse files
authored
improve Range function signatures and docs (#1896)
If the first arg isn't reliably used as start in a constructor, then don't call it that. (This bit me hard.) Call out swapping behavior in with. (So did this.) Make other docs more precise. ## 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
1 parent 1ba3331 commit 76085d1

File tree

1 file changed

+23
-27
lines changed

1 file changed

+23
-27
lines changed

packages/common/src/types/Range.ts

Lines changed: 23 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -12,32 +12,27 @@ export class Range {
1212
readonly end: Position;
1313

1414
/**
15-
* Create a new range from two positions. If `start` is not
16-
* before or equal to `end`, the values will be swapped.
15+
* Create a new range from two positions.
16+
* The earlier of `p1` and `p2` will be used as the start position.
1717
*
18-
* @param start A position.
19-
* @param end A position.
18+
* @param p1 A position.
19+
* @param p2 A position.
2020
*/
21-
constructor(start: Position, end: Position);
21+
constructor(p1: Position, p2: Position);
2222

2323
/**
24-
* Create a new range from number coordinates. It is a shorter equivalent of
25-
* using `new Range(new Position(startLine, startCharacter), new Position(endLine, endCharacter))`
24+
* Create a new range from number coordinates.
25+
* It is equivalent to `new Range(new Position(line1, char1), new Position(line2, char2))`
2626
*
27-
* @param startLine A zero-based line value.
28-
* @param startCharacter A zero-based character value.
29-
* @param endLine A zero-based line value.
30-
* @param endCharacter A zero-based character value.
27+
* @param line1 A zero-based line value.
28+
* @param char1 A zero-based character value.
29+
* @param line2 A zero-based line value.
30+
* @param char2 A zero-based character value.
3131
*/
32-
constructor(
33-
startLine: number,
34-
startCharacter: number,
35-
endLine: number,
36-
endCharacter: number,
37-
);
32+
constructor(line1: number, char1: number, line2: number, char2: number);
3833

3934
constructor(...args: any[]) {
40-
const [start, end]: [Position, Position] = (() => {
35+
const [p1, p2]: [Position, Position] = (() => {
4136
// Arguments are two positions
4237
if (args.length === 2) {
4338
return args as [Position, Position];
@@ -48,12 +43,12 @@ export class Range {
4843
})();
4944

5045
// Ranges are always non-reversed
51-
if (start.isBefore(end)) {
52-
this.start = start;
53-
this.end = end;
46+
if (p1.isBefore(p2)) {
47+
this.start = p1;
48+
this.end = p2;
5449
} else {
55-
this.start = end;
56-
this.end = start;
50+
this.start = p2;
51+
this.end = p1;
5752
}
5853
}
5954

@@ -98,8 +93,9 @@ export class Range {
9893
}
9994

10095
/**
101-
* Intersect `range` with this range and returns a new range or `undefined`
102-
* if the ranges have no overlap.
96+
* Intersect `range` with this range and returns a new range.
97+
* If the ranges are adjacent but non-overlapping, the resulting range is empty.
98+
* If the ranges have no overlap and are not adjacent, it returns `undefined`.
10399
*
104100
* @param other A range.
105101
* @return A range of the greater start and smaller end positions. Will
@@ -125,12 +121,12 @@ export class Range {
125121
}
126122

127123
/**
128-
* Derived a new range from this range.
124+
* Derive a new range from this range.
125+
* If the resulting range has end before start, they are swapped.
129126
*
130127
* @param start A position that should be used as start. The default value is the {@link Range.start current start}.
131128
* @param end A position that should be used as end. The default value is the {@link Range.end current end}.
132129
* @return A range derived from this range with the given start and end position.
133-
* If start and end are not different `this` range will be returned.
134130
*/
135131
public with(start?: Position, end?: Position): Range {
136132
return new Range(start ?? this.start, end ?? this.end);

0 commit comments

Comments
 (0)