Repository navigation
Expand file tree
/
Copy pathregion.js
More file actions
216 lines (169 loc) · 4.42 KB
/
Copy pathregion.js
File metadata and controls
216 lines (169 loc) · 4.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
import {Range} from 'atom';
class Region {
constructor(marker) {
this.marker = marker;
}
getMarker() {
return this.marker;
}
getRange() {
return this.marker.getRange();
}
getStartBufferRow() {
return this.getRange().start.row;
}
getEndBufferRow() {
return this.getRange().end.row;
}
includesBufferRow(row) {
return this.getRange().intersectsRow(row);
}
/*
* intersectRows breaks a Region into runs of rows that are included in
* rowSet and rows that are not. For example:
* @this Region row 10-20
* @param rowSet row 11, 12, 13, 17, 19
* @param includeGaps true (whether the result will include gaps or not)
* @return an array of regions like this:
* (10, gap = true) (11, 12, 13, gap = false) (14, 15, 16, gap = true)
* (17, gap = false) (18, gap = true) (19, gap = false) (20, gap = true)
*/
intersectRows(rowSet, includeGaps) {
const intersections = [];
let withinIntersection = false;
let currentRow = this.getRange().start.row;
let nextStartRow = currentRow;
const finishRowRange = isGap => {
if (isGap && !includeGaps) {
nextStartRow = currentRow;
return;
}
if (currentRow <= this.getRange().start.row) {
return;
}
intersections.push({
intersection: Range.fromObject([[nextStartRow, 0], [currentRow - 1, Infinity]]),
gap: isGap,
});
nextStartRow = currentRow;
};
while (currentRow <= this.getRange().end.row) {
if (rowSet.has(currentRow) && !withinIntersection) {
// One row past the end of a gap. Start of intersecting row range.
finishRowRange(true);
withinIntersection = true;
} else if (!rowSet.has(currentRow) && withinIntersection) {
// One row past the end of intersecting row range. Start of the next gap.
finishRowRange(false);
withinIntersection = false;
}
currentRow++;
}
finishRowRange(!withinIntersection);
return intersections;
}
isAddition() {
return false;
}
isDeletion() {
return false;
}
isUnchanged() {
return false;
}
isNoNewline() {
return false;
}
getBufferRows() {
return this.getRange().getRows();
}
bufferRowCount() {
return this.getRange().getRowCount();
}
when(callbacks) {
const callback = callbacks[this.constructor.name.toLowerCase()] || callbacks.default || (() => undefined);
return callback();
}
updateMarkers(map) {
this.marker = map.get(this.marker) || this.marker;
}
destroyMarkers() {
this.marker.destroy();
}
toStringIn(buffer) {
const raw = buffer.getTextInRange(this.getRange());
return this.constructor.origin + raw.replace(/\r?\n/g, '$&' + this.constructor.origin) +
buffer.lineEndingForRow(this.getRange().end.row);
}
/*
* Construct a String containing internal diagnostic information.
*/
/* istanbul ignore next */
inspect(opts = {}) {
const options = {
indent: 0,
...opts,
};
let indentation = '';
for (let i = 0; i < options.indent; i++) {
indentation += ' ';
}
let inspectString = `${indentation}(${this.constructor.name} marker=${this.marker.id})`;
if (this.marker.isDestroyed()) {
inspectString += ' [destroyed]';
}
if (!this.marker.isValid()) {
inspectString += ' [invalid]';
}
return inspectString + '\n';
}
isChange() {
return true;
}
}
export class Addition extends Region {
static origin = '+';
static layerName = 'addition';
isAddition() {
return true;
}
invertIn(nextBuffer) {
return new Deletion(nextBuffer.markRange(this.getRange()));
}
}
export class Deletion extends Region {
static origin = '-';
static layerName = 'deletion';
isDeletion() {
return true;
}
invertIn(nextBuffer) {
return new Addition(nextBuffer.markRange(this.getRange()));
}
}
export class Unchanged extends Region {
static origin = ' ';
static layerName = 'unchanged';
isUnchanged() {
return true;
}
isChange() {
return false;
}
invertIn(nextBuffer) {
return new Unchanged(nextBuffer.markRange(this.getRange()));
}
}
export class NoNewline extends Region {
static origin = '\\';
static layerName = 'nonewline';
isNoNewline() {
return true;
}
isChange() {
return false;
}
invertIn(nextBuffer) {
return new NoNewline(nextBuffer.markRange(this.getRange()));
}
}