Repository navigation
Expand file tree
/
Copy pathregion.js
More file actions
179 lines (140 loc) · 3.7 KB
/
Copy pathregion.js
File metadata and controls
179 lines (140 loc) · 3.7 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
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();
}
reMarkOn(markable) {
this.marker = markable.markRange(this.getRange(), {invalidate: 'never', exclusive: false});
}
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);
}
isChange() {
return true;
}
}
export class Addition extends Region {
static origin = '+';
isAddition() {
return true;
}
invertIn(nextBuffer) {
return new Deletion(nextBuffer.markRange(this.getRange()));
}
}
export class Deletion extends Region {
static origin = '-';
isDeletion() {
return true;
}
invertIn(nextBuffer) {
return new Addition(nextBuffer.markRange(this.getRange()));
}
}
export class Unchanged extends Region {
static origin = ' ';
isUnchanged() {
return true;
}
isChange() {
return false;
}
invertIn(nextBuffer) {
return new Unchanged(nextBuffer.markRange(this.getRange()));
}
}
export class NoNewline extends Region {
static origin = '\\';
isNoNewline() {
return true;
}
isChange() {
return false;
}
invertIn(nextBuffer) {
return new NoNewline(nextBuffer.markRange(this.getRange()));
}
}