This repository has been archived by the owner on Nov 8, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 42
/
conflicted-editor.coffee
282 lines (241 loc) · 9.71 KB
/
conflicted-editor.coffee
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
{$} = require 'space-pen'
_ = require 'underscore-plus'
{Emitter, CompositeDisposable} = require 'atom'
{Conflict} = require './conflict'
{SideView} = require './view/side-view'
{NavigationView} = require './view/navigation-view'
{ResolverView} = require './view/resolver-view'
# Public: Mediate conflict-related decorations and events on behalf of a specific TextEditor.
#
class ConflictedEditor
# Public: Instantiate a new ConflictedEditor to manage the decorations and events of a specific
# TextEditor.
#
# state [MergeState] - Merge-wide conflict state.
# pkg [Emitter] - The package object containing event dispatch and subscription methods.
# editor [TextEditor] - An editor containing text that, presumably, includes conflict markers.
#
constructor: (@state, @pkg, @editor) ->
@subs = new CompositeDisposable
@coveringViews = []
@conflicts = []
# Public: Locate Conflicts within this specific TextEditor.
#
# Install a pair of SideViews and a NavigationView for each Conflict discovered within the
# editor's text. Subscribe to package events related to relevant Conflicts and broadcast
# per-editor progress events as they are resolved. Install Atom commands related to conflict
# navigation and resolution.
#
mark: ->
@conflicts = Conflict.all(@state, @editor)
@coveringViews = []
for c in @conflicts
@coveringViews.push new SideView(c.ours, @editor)
@coveringViews.push new SideView(c.base, @editor) if c.base?
@coveringViews.push new NavigationView(c.navigator, @editor)
@coveringViews.push new SideView(c.theirs, @editor)
@subs.add c.onDidResolveConflict =>
unresolved = (v for v in @coveringViews when not v.conflict().isResolved())
resolvedCount = @conflicts.length - Math.floor(unresolved.length / 3)
@pkg.didResolveConflict
file: @editor.getPath(),
total: @conflicts.length, resolved: resolvedCount,
source: this
if @conflicts.length > 0
atom.views.getView(@editor).classList.add 'conflicted'
cv.decorate() for cv in @coveringViews
@installEvents()
@focusConflict @conflicts[0]
else
@pkg.didResolveConflict
file: @editor.getPath(),
total: 1, resolved: 1,
source: this
@conflictsResolved()
# Private: Install Atom commands related to Conflict resolution and navigation on the TextEditor.
#
# Listen for package-global events that relate to the local Conflicts and dispatch them
# appropriately.
#
installEvents: ->
@subs.add @editor.onDidStopChanging => @detectDirty()
@subs.add @editor.onDidDestroy => @cleanup()
@subs.add atom.commands.add 'atom-text-editor',
'merge-conflicts:accept-current': => @acceptCurrent(),
'merge-conflicts:accept-ours': => @acceptOurs(),
'merge-conflicts:accept-theirs': => @acceptTheirs(),
'merge-conflicts:ours-then-theirs': => @acceptOursThenTheirs(),
'merge-conflicts:theirs-then-ours': => @acceptTheirsThenOurs(),
'merge-conflicts:next-unresolved': => @nextUnresolved(),
'merge-conflicts:previous-unresolved': => @previousUnresolved(),
'merge-conflicts:revert-current': => @revertCurrent()
@subs.add @pkg.onDidResolveConflict ({total, resolved, file}) =>
if file is @editor.getPath() and total is resolved
@conflictsResolved()
@subs.add @pkg.onDidCompleteConflictResolution => @cleanup()
@subs.add @pkg.onDidQuitConflictResolution => @cleanup()
# Private: Undo any changes done to the underlying TextEditor.
#
cleanup: ->
atom.views.getView(@editor).classList.remove 'conflicted' if @editor?
for c in @conflicts
m.destroy() for m in c.markers()
v.remove() for v in @coveringViews
@subs.dispose()
# Private: Event handler invoked when all conflicts in this file have been resolved.
#
conflictsResolved: ->
atom.workspace.addTopPanel item: new ResolverView(@editor, @state, @pkg)
detectDirty: ->
# Only detect dirty regions within CoveringViews that have a cursor within them.
potentials = []
for c in @editor.getCursors()
for v in @coveringViews
potentials.push(v) if v.includesCursor(c)
v.detectDirty() for v in _.uniq(potentials)
# Private: Command that accepts each side of a conflict that contains a cursor.
#
# Conflicts with cursors in both sides will be ignored.
#
acceptCurrent: ->
return unless @editor is atom.workspace.getActiveTextEditor()
sides = @active()
# Do nothing if you have cursors in *both* sides of a single conflict.
duplicates = []
seen = {}
for side in sides
if side.conflict of seen
duplicates.push side
duplicates.push seen[side.conflict]
seen[side.conflict] = side
sides = _.difference sides, duplicates
@editor.transact ->
side.resolve() for side in sides
# Private: Command that accepts the "ours" side of the active conflict.
#
acceptOurs: ->
return unless @editor is atom.workspace.getActiveTextEditor()
@editor.transact =>
side.conflict.ours.resolve() for side in @active()
# Private: Command that accepts the "theirs" side of the active conflict.
#
acceptTheirs: ->
return unless @editor is atom.workspace.getActiveTextEditor()
@editor.transact =>
side.conflict.theirs.resolve() for side in @active()
# Private: Command that uses a composite resolution of the "ours" side followed by the "theirs"
# side of the active conflict.
#
acceptOursThenTheirs: ->
return unless @editor is atom.workspace.getActiveTextEditor()
@editor.transact =>
for side in @active()
@combineSides side.conflict.ours, side.conflict.theirs
# Private: Command that uses a composite resolution of the "theirs" side followed by the "ours"
# side of the active conflict.
#
acceptTheirsThenOurs: ->
return unless @editor is atom.workspace.getActiveTextEditor()
@editor.transact =>
for side in @active()
@combineSides side.conflict.theirs, side.conflict.ours
# Private: Command that navigates to the next unresolved conflict in the editor.
#
# If the cursor is on or after the final unresolved conflict in the editor, nothing happens.
#
nextUnresolved: ->
return unless @editor is atom.workspace.getActiveTextEditor()
final = _.last @active()
if final?
n = final.conflict.navigator.nextUnresolved()
@focusConflict(n) if n?
else
orderedCursors = _.sortBy @editor.getCursors(), (c) ->
c.getBufferPosition().row
lastCursor = _.last orderedCursors
return unless lastCursor?
pos = lastCursor.getBufferPosition()
firstAfter = null
for c in @conflicts
p = c.ours.marker.getBufferRange().start
if p.isGreaterThanOrEqual(pos) and not firstAfter?
firstAfter = c
return unless firstAfter?
if firstAfter.isResolved()
target = firstAfter.navigator.nextUnresolved()
else
target = firstAfter
return unless target?
@focusConflict target
# Private: Command that navigates to the previous unresolved conflict in the editor.
#
# If the cursor is on or before the first unresolved conflict in the editor, nothing happens.
#
previousUnresolved: ->
return unless @editor is atom.workspace.getActiveTextEditor()
initial = _.first @active()
if initial?
p = initial.conflict.navigator.previousUnresolved()
@focusConflict(p) if p?
else
orderedCursors = _.sortBy @editor.getCursors(), (c) ->
c.getBufferPosition().row
firstCursor = _.first orderedCursors
return unless firstCursor?
pos = firstCursor.getBufferPosition()
lastBefore = null
for c in @conflicts
p = c.ours.marker.getBufferRange().start
if p.isLessThanOrEqual pos
lastBefore = c
return unless lastBefore?
if lastBefore.isResolved()
target = lastBefore.navigator.previousUnresolved()
else
target = lastBefore
return unless target?
@focusConflict target
# Private: Revert manual edits to the current side of the active conflict.
#
revertCurrent: ->
return unless @editor is atom.workspace.getActiveTextEditor()
for side in @active()
for view in @coveringViews when view.conflict() is side.conflict
view.revert() if view.isDirty()
# Private: Collect a list of each Side of any Conflict within the editor that contains a cursor.
#
# Returns [Array<Side>]
#
active: ->
positions = (c.getBufferPosition() for c in @editor.getCursors())
matching = []
for c in @conflicts
for p in positions
if c.ours.marker.getBufferRange().containsPoint p
matching.push c.ours
if c.theirs.marker.getBufferRange().containsPoint p
matching.push c.theirs
matching
# Private: Resolve a conflict by combining its two Sides in a specific order.
#
# first [Side] The Side that should occur first in the resolved text.
# second [Side] The Side belonging to the same Conflict that should occur second in the resolved
# text.
#
combineSides: (first, second) ->
text = @editor.getTextInBufferRange second.marker.getBufferRange()
e = first.marker.getBufferRange().end
insertPoint = @editor.setTextInBufferRange([e, e], text).end
first.marker.setHeadBufferPosition insertPoint
first.followingMarker.setTailBufferPosition insertPoint
first.resolve()
# Private: Scroll the editor and place the cursor at the beginning of a marked conflict.
#
# conflict [Conflict] Any conflict within the current editor.
#
focusConflict: (conflict) ->
st = conflict.ours.marker.getBufferRange().start
@editor.scrollToBufferPosition st, center: true
@editor.setCursorBufferPosition st, autoscroll: false
module.exports =
ConflictedEditor: ConflictedEditor