Repository navigation
Expand file tree
/
Copy pathhunk.js
More file actions
145 lines (119 loc) · 3.12 KB
/
Copy pathhunk.js
File metadata and controls
145 lines (119 loc) · 3.12 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
export default class Hunk {
constructor({
oldStartRow,
newStartRow,
oldRowCount,
newRowCount,
sectionHeading,
marker,
regions,
}) {
this.oldStartRow = oldStartRow;
this.newStartRow = newStartRow;
this.oldRowCount = oldRowCount;
this.newRowCount = newRowCount;
this.sectionHeading = sectionHeading;
this.marker = marker;
this.regions = regions;
}
getOldStartRow() {
return this.oldStartRow;
}
getNewStartRow() {
return this.newStartRow;
}
getOldRowCount() {
return this.oldRowCount;
}
getNewRowCount() {
return this.newRowCount;
}
getHeader() {
return `@@ -${this.oldStartRow},${this.oldRowCount} +${this.newStartRow},${this.newRowCount} @@`;
}
getSectionHeading() {
return this.sectionHeading;
}
getRegions() {
return this.regions;
}
getChanges() {
return this.regions.filter(change => change.isChange());
}
getMarker() {
return this.marker;
}
getRange() {
return this.getMarker().getRange();
}
getBufferRows() {
return this.getRange().getRows();
}
bufferRowCount() {
return this.getRange().getRowCount();
}
includesBufferRow(row) {
return this.getRange().intersectsRow(row);
}
getOldRowAt(row) {
let current = this.oldStartRow;
for (const region of this.getRegions()) {
if (region.includesBufferRow(row)) {
const offset = row - region.getStartBufferRow();
return region.when({
unchanged: () => current + offset,
addition: () => null,
deletion: () => current + offset,
nonewline: () => null,
});
} else {
current += region.when({
unchanged: () => region.bufferRowCount(),
addition: () => 0,
deletion: () => region.bufferRowCount(),
nonewline: () => 0,
});
}
}
return null;
}
getNewRowAt(row) {
let current = this.newStartRow;
for (const region of this.getRegions()) {
if (region.includesBufferRow(row)) {
const offset = row - region.getStartBufferRow();
return region.when({
unchanged: () => current + offset,
addition: () => current + offset,
deletion: () => null,
nonewline: () => null,
});
} else {
current += region.when({
unchanged: () => region.bufferRowCount(),
addition: () => region.bufferRowCount(),
deletion: () => 0,
nonewline: () => 0,
});
}
}
return null;
}
getMaxLineNumberWidth() {
return Math.max(
(this.oldStartRow + this.oldRowCount).toString().length,
(this.newStartRow + this.newRowCount).toString().length,
);
}
changedLineCount() {
return this.regions
.filter(region => region.isChange())
.reduce((count, change) => count + change.bufferRowCount(), 0);
}
reMarkOn(markable) {
this.marker = markable.markRange(this.getRange(), {invalidate: 'never', exclusive: false});
}
toStringIn(buffer) {
return this.getRegions().reduce((str, region) => str + region.toStringIn(buffer), this.getHeader() + '\n');
}
}