UNPKG

4.56 kBTypeScriptView Raw
1import { Point, PointCompatible, PointLike } from './text-buffer';
2
3/** Represents a region in a buffer in row/column coordinates. */
4export class Range {
5 // Properties
6 /** A Point representing the start of the Range. */
7 start: Point;
8
9 /** A Point representing the end of the Range. */
10 end: Point;
11
12 // Construction
13 /** Convert any range-compatible object to a Range. */
14 static fromObject(object: RangeCompatible, copy?: boolean): Range;
15
16 /** Construct a Range object. */
17 constructor(pointA?: PointCompatible, pointB?: PointCompatible);
18
19 /** Call this with the result of Range::serialize to construct a new Range. */
20 static deserialize(array: object): Range;
21
22 /** Returns a new range with the same start and end positions. */
23 copy(): Range;
24
25 /** Returns a new range with the start and end positions negated. */
26 negate(): Range;
27
28 // Serialization and Deserialization
29 /** Returns a plain javascript object representation of the Range. */
30 serialize(): number[][];
31
32 // Range Details
33 /** Is the start position of this range equal to the end position? */
34 isEmpty(): boolean;
35
36 /**
37 * Returns a boolean indicating whether this range starts and ends on the
38 * same row.
39 */
40 isSingleLine(): boolean;
41
42 /** Get the number of rows in this range. */
43 getRowCount(): number;
44
45 /** Returns an array of all rows in the range. */
46 getRows(): number[];
47
48 // Operations
49 /**
50 * Freezes the range and its start and end point so it becomes immutable
51 * and returns itself.
52 */
53 freeze(): Readonly<Range>;
54
55 // NOTE: this function doesn't actually take a range-compatible parameter.
56 /** Returns a new range that contains this range and the given range. */
57 union(other: RangeLike): Range;
58
59 /**
60 * Build and return a new range by translating this range's start and end
61 * points by the given delta(s).
62 */
63 translate(startDelta: PointCompatible, endDelta?: PointCompatible): Range;
64
65 /**
66 * Build and return a new range by traversing this range's start and end
67 * points by the given delta.
68 */
69 traverse(delta: PointCompatible): Range;
70
71 // Comparison
72 /**
73 * Compare two Ranges.
74 * Returns -1 if this range starts before the argument or contains it.
75 * Returns 0 if this range is equivalent to the argument.
76 * Returns 1 if this range starts after the argument or is contained by it.
77 */
78 compare(otherRange: RangeCompatible): number;
79
80 /**
81 * Returns a Boolean indicating whether this range has the same start and
82 * end points as the given Range.
83 */
84 isEqual(otherRange: RangeCompatible): boolean;
85
86 // NOTE: this function doesn't actually take a range-compatible parameter.
87 /**
88 * Returns a Boolean indicating whether this range starts and ends on the
89 * same row as the argument.
90 */
91 coversSameRows(otherRange: RangeLike): boolean;
92
93 // NOTE: this function doesn't actually take a range-compatible parameter.
94 /** Determines whether this range intersects with the argument. */
95 intersectsWith(otherRange: RangeLike, exclusive?: boolean): boolean;
96
97 /** Returns a boolean indicating whether this range contains the given range. */
98 containsRange(otherRange: RangeCompatible, exclusive?: boolean): boolean;
99
100 /** Returns a boolean indicating whether this range contains the given point. */
101 containsPoint(point: PointCompatible, exclusive?: boolean): boolean;
102
103 /**
104 * Returns a boolean indicating whether this range intersects the given
105 * row number.
106 */
107 intersectsRow(row: number): boolean;
108
109 /**
110 * Returns a boolean indicating whether this range intersects the row range
111 * indicated by the given startRow and endRow numbers.
112 */
113 intersectsRowRange(startRow: number, endRow: number): boolean;
114
115 // Conversion
116 /** Returns a string representation of the range. */
117 toString(): string;
118}
119
120/** The types usable when constructing a range via the Range::fromObject method. */
121export type RangeCompatible =
122 | RangeLike
123 | [PointLike, PointLike]
124 | [PointLike, [number, number]]
125 | [[number, number], PointLike]
126 | [[number, number], [number, number]];
127
128/** The interface that should be implemented for all "range-compatible" objects. */
129export interface RangeLike {
130 /** A Point representing the start of the Range. */
131 start: PointLike;
132
133 /** A Point representing the end of the Range. */
134 end: PointLike;
135}