forked from atom/github
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheditor-conflict-controller.js
More file actions
233 lines (195 loc) · 7.91 KB
/
editor-conflict-controller.js
File metadata and controls
233 lines (195 loc) · 7.91 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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
import {CompositeDisposable} from 'event-kit';
import React from 'react';
import PropTypes from 'prop-types';
import compareSets from 'compare-sets';
import Commands, {Command} from '../atom/commands';
import Conflict from '../models/conflicts/conflict';
import ConflictController from './conflict-controller';
import {OURS, THEIRS, BASE} from '../models/conflicts/source';
import {autobind} from '../helpers';
/**
* Render a `ConflictController` for each conflict marker within an open TextEditor.
*/
export default class EditorConflictController extends React.Component {
static propTypes = {
editor: PropTypes.object.isRequired,
commands: PropTypes.object.isRequired,
resolutionProgress: PropTypes.object.isRequired,
isRebase: PropTypes.bool.isRequired,
refreshResolutionProgress: PropTypes.func.isRequired,
}
constructor(props, context) {
super(props, context);
autobind(this, 'resolveAsCurrent', 'revertConflictModifications', 'dismissCurrent');
// this.layer = props.editor.addMarkerLayer({
// maintainHistory: true,
// persistent: false,
// });
this.layer = props.editor.getDefaultMarkerLayer();
this.state = {
conflicts: new Set(Conflict.allFromEditor(props.editor, this.layer, props.isRebase)),
};
this.subscriptions = new CompositeDisposable();
this.updateMarkerCount();
}
componentDidMount() {
const buffer = this.props.editor.getBuffer();
this.subscriptions.add(
this.props.editor.onDidDestroy(() => this.props.refreshResolutionProgress(this.props.editor.getPath())),
buffer.onDidReload(() => this.reparseConflicts()),
);
}
render() {
this.updateMarkerCount();
return (
<div>
{this.state.conflicts.size > 0 && (
<Commands registry={this.props.commands} target="atom-text-editor">
<Command command="github:resolve-as-ours" callback={this.getResolverUsing([OURS])} />
<Command command="github:resolve-as-theirs" callback={this.getResolverUsing([THEIRS])} />
<Command command="github:resolve-as-base" callback={this.getResolverUsing([BASE])} />
<Command command="github:resolve-as-ours-then-theirs" callback={this.getResolverUsing([OURS, THEIRS])} />
<Command command="github:resolve-as-theirs-then-ours" callback={this.getResolverUsing([THEIRS, OURS])} />
<Command command="github:resolve-as-current" callback={this.resolveAsCurrent} />
<Command command="github:revert-conflict-modifications" callback={this.revertConflictModifications} />
<Command command="github:dismiss-conflict" callback={this.dismissCurrent} />
</Commands>
)}
{Array.from(this.state.conflicts, c => (
<ConflictController
key={c.getKey()}
editor={this.props.editor}
conflict={c}
resolveAsSequence={sources => this.resolveAsSequence(c, sources)}
dismiss={() => this.dismissConflicts([c])}
/>
))}
</div>
);
}
componentWillUnmount() {
// this.layer.destroy();
this.subscriptions.dispose();
}
/*
* Return an Array containing `Conflict` objects whose marked regions include any cursor position in the current
* `TextEditor` and the `Sides` that contain a cursor within each.
*
* This method is written to have linear complexity with respect to the number of cursors and the number of
* conflicts, to gracefully handle files with large numbers of both.
*/
getCurrentConflicts() {
const cursorPositions = this.props.editor.getCursorBufferPositions();
cursorPositions.sort((a, b) => a.compare(b));
const cursorIterator = cursorPositions[Symbol.iterator]();
const conflictIterator = this.state.conflicts.keys();
let currentCursor = cursorIterator.next();
let currentConflict = conflictIterator.next();
const activeConflicts = [];
while (!currentCursor.done && !currentConflict.done) {
// Advance currentCursor to the first cursor beyond the earliest conflict.
const earliestConflictPosition = currentConflict.value.getRange().start;
while (!currentCursor.done && currentCursor.value.isLessThan(earliestConflictPosition)) {
currentCursor = cursorIterator.next();
}
// Advance currentConflict until the first conflict that begins at a position after the current cursor.
// Compare each to the current cursor, and add it to activeConflicts if it contains it.
while (!currentConflict.done && !currentCursor.done &&
currentConflict.value.getRange().start.isLessThan(currentCursor.value)) {
if (currentConflict.value.includesPoint(currentCursor.value)) {
// Hit; determine which sides of this conflict contain cursors.
const conflict = currentConflict.value;
const endPosition = conflict.getRange().end;
const sides = new Set();
while (!currentCursor.done && currentCursor.value.isLessThan(endPosition)) {
const side = conflict.getSideContaining(currentCursor.value);
if (side) {
sides.add(side);
}
currentCursor = cursorIterator.next();
}
activeConflicts.push({conflict, sides});
}
currentConflict = conflictIterator.next();
}
}
return activeConflicts;
}
getResolverUsing(sequence) {
return () => {
this.getCurrentConflicts().forEach(match => this.resolveAsSequence(match.conflict, sequence));
};
}
resolveAsCurrent() {
this.getCurrentConflicts().forEach(match => {
if (match.sides.size === 1) {
const side = match.sides.keys().next().value;
this.resolveAs(match.conflict, side.getSource());
}
});
}
revertConflictModifications() {
this.getCurrentConflicts().forEach(match => {
match.sides.forEach(side => {
side.isModified() && side.revert();
side.isBannerModified() && side.revertBanner();
});
});
}
dismissCurrent() {
this.dismissConflicts(this.getCurrentConflicts().map(match => match.conflict));
}
dismissConflicts(conflicts) {
this.setState(prevState => {
const {added} = compareSets(new Set(conflicts), prevState.conflicts);
return {conflicts: added};
});
}
resolveAsSequence(conflict, sources) {
const [firstSide, ...restOfSides] = sources
.map(source => conflict.getSide(source))
.filter(side => side);
const textToAppend = restOfSides.map(side => side.getText()).join('');
this.props.editor.transact(() => {
// Append text from all but the first Side to the first Side. Adjust the following DisplayMarker so that only that
// Side's marker includes the appended text, not the next one.
const appendedRange = firstSide.appendText(textToAppend);
const nextMarker = conflict.markerAfter(firstSide.getPosition());
if (nextMarker) {
nextMarker.setTailBufferPosition(appendedRange.end);
}
this.innerResolveAs(conflict, sources[0]);
});
}
resolveAs(conflict, source) {
this.props.editor.transact(() => {
this.innerResolveAs(conflict, source);
});
}
innerResolveAs(conflict, source) {
conflict.resolveAs(source);
const chosenSide = conflict.getChosenSide();
if (!chosenSide.isBannerModified()) {
chosenSide.deleteBanner();
}
const separator = conflict.getSeparator();
if (!separator.isModified()) {
separator.delete();
}
conflict.getUnchosenSides().forEach(side => {
side.deleteBanner();
side.delete();
});
this.updateMarkerCount();
}
reparseConflicts() {
const newConflicts = new Set(Conflict.allFromEditor(this.props.editor, this.layer, this.props.isRebase));
this.setState({conflicts: newConflicts});
}
updateMarkerCount() {
this.props.resolutionProgress.reportMarkerCount(
this.props.editor.getPath(),
Array.from(this.state.conflicts, c => !c.isResolved()).filter(b => b).length,
);
}
}