This repository has been archived by the owner on Dec 15, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 73
/
Copy pathdisplay-marker-layer.coffee
402 lines (371 loc) · 19 KB
/
display-marker-layer.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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
{Emitter, CompositeDisposable} = require 'event-kit'
DisplayMarker = require './display-marker'
Range = require './range'
Point = require './point'
# Public: *Experimental:* A container for a related set of markers at the
# {DisplayLayer} level. Wraps an underlying {MarkerLayer} on the {TextBuffer}.
#
# This API is experimental and subject to change on any release.
module.exports =
class DisplayMarkerLayer
constructor: (@displayLayer, @bufferMarkerLayer, @ownsBufferMarkerLayer) ->
{@id} = @bufferMarkerLayer
@bufferMarkerLayer.displayMarkerLayers.add(this)
@markersById = {}
@destroyed = false
@emitter = new Emitter
@subscriptions = new CompositeDisposable
@markersWithDestroyListeners = new Set
@subscriptions.add(@bufferMarkerLayer.onDidUpdate(@emitDidUpdate.bind(this)))
###
Section: Lifecycle
###
# Essential: Destroy this layer.
destroy: ->
return if @destroyed
@destroyed = true
@clear()
@subscriptions.dispose()
@bufferMarkerLayer.displayMarkerLayers.delete(this)
@bufferMarkerLayer.destroy() if @ownsBufferMarkerLayer
@displayLayer.didDestroyMarkerLayer(@id)
@emitter.emit('did-destroy')
@emitter.clear()
# Public: Destroy all markers in this layer.
clear: ->
@bufferMarkerLayer.clear()
didClearBufferMarkerLayer: ->
@markersWithDestroyListeners.forEach (marker) -> marker.didDestroyBufferMarker()
@markersById = {}
# Essential: Determine whether this layer has been destroyed.
#
# Returns a {Boolean}.
isDestroyed: ->
@destroyed
###
Section: Event Subscription
###
# Public: Subscribe to be notified synchronously when this layer is destroyed.
#
# Returns a {Disposable}.
onDidDestroy: (callback) ->
@emitter.on('did-destroy', callback)
# Public: Subscribe to be notified asynchronously whenever markers are
# created, updated, or destroyed on this layer. *Prefer this method for
# optimal performance when interacting with layers that could contain large
# numbers of markers.*
#
# * `callback` A {Function} that will be called with no arguments when changes
# occur on this layer.
#
# Subscribers are notified once, asynchronously when any number of changes
# occur in a given tick of the event loop. You should re-query the layer
# to determine the state of markers in which you're interested in. It may
# be counter-intuitive, but this is much more efficient than subscribing to
# events on individual markers, which are expensive to deliver.
#
# Returns a {Disposable}.
onDidUpdate: (callback) ->
@emitter.on('did-update', callback)
# Public: Subscribe to be notified synchronously whenever markers are created
# on this layer. *Avoid this method for optimal performance when interacting
# with layers that could contain large numbers of markers.*
#
# * `callback` A {Function} that will be called with a {TextEditorMarker}
# whenever a new marker is created.
#
# You should prefer {onDidUpdate} when synchronous notifications aren't
# absolutely necessary.
#
# Returns a {Disposable}.
onDidCreateMarker: (callback) ->
@bufferMarkerLayer.onDidCreateMarker (bufferMarker) =>
callback(@getMarker(bufferMarker.id))
###
Section: Marker creation
###
# Public: Create a marker with the given screen range.
#
# * `range` A {Range} or range-compatible {Array}
# * `options` A hash of key-value pairs to associate with the marker. There
# are also reserved property names that have marker-specific meaning.
# * `reversed` (optional) {Boolean} Creates the marker in a reversed
# orientation. (default: false)
# * `invalidate` (optional) {String} Determines the rules by which changes
# to the buffer *invalidate* the marker. (default: 'overlap') It can be
# any of the following strategies, in order of fragility:
# * __never__: The marker is never marked as invalid. This is a good choice for
# markers representing selections in an editor.
# * __surround__: The marker is invalidated by changes that completely surround it.
# * __overlap__: The marker is invalidated by changes that surround the
# start or end of the marker. This is the default.
# * __inside__: The marker is invalidated by changes that extend into the
# inside of the marker. Changes that end at the marker's start or
# start at the marker's end do not invalidate the marker.
# * __touch__: The marker is invalidated by a change that touches the marked
# region in any way, including changes that end at the marker's
# start or start at the marker's end. This is the most fragile strategy.
# * `exclusive` {Boolean} indicating whether insertions at the start or end
# of the marked range should be interpreted as happening *outside* the
# marker. Defaults to `false`, except when using the `inside`
# invalidation strategy or when when the marker has no tail, in which
# case it defaults to true. Explicitly assigning this option overrides
# behavior in all circumstances.
# * `clipDirection` {String} If `'backward'`, returns the first valid
# position preceding an invalid position. If `'forward'`, returns the
# first valid position following an invalid position. If `'closest'`,
# returns the first valid position closest to an invalid position.
# Defaults to `'closest'`. Applies to the start and end of the given range.
#
# Returns a {DisplayMarker}.
markScreenRange: (screenRange, options) ->
screenRange = Range.fromObject(screenRange)
bufferRange = @displayLayer.translateScreenRange(screenRange, options)
@getMarker(@bufferMarkerLayer.markRange(bufferRange, options).id)
# Public: Create a marker on this layer with its head at the given screen
# position and no tail.
#
# * `screenPosition` A {Point} or point-compatible {Array}
# * `options` (optional) An {Object} with the following keys:
# * `invalidate` (optional) {String} Determines the rules by which changes
# to the buffer *invalidate* the marker. (default: 'overlap') It can be
# any of the following strategies, in order of fragility:
# * __never__: The marker is never marked as invalid. This is a good choice for
# markers representing selections in an editor.
# * __surround__: The marker is invalidated by changes that completely surround it.
# * __overlap__: The marker is invalidated by changes that surround the
# start or end of the marker. This is the default.
# * __inside__: The marker is invalidated by changes that extend into the
# inside of the marker. Changes that end at the marker's start or
# start at the marker's end do not invalidate the marker.
# * __touch__: The marker is invalidated by a change that touches the marked
# region in any way, including changes that end at the marker's
# start or start at the marker's end. This is the most fragile strategy.
# * `exclusive` {Boolean} indicating whether insertions at the start or end
# of the marked range should be interpreted as happening *outside* the
# marker. Defaults to `false`, except when using the `inside`
# invalidation strategy or when when the marker has no tail, in which
# case it defaults to true. Explicitly assigning this option overrides
# behavior in all circumstances.
# * `clipDirection` {String} If `'backward'`, returns the first valid
# position preceding an invalid position. If `'forward'`, returns the
# first valid position following an invalid position. If `'closest'`,
# returns the first valid position closest to an invalid position.
# Defaults to `'closest'`.
#
# Returns a {DisplayMarker}.
markScreenPosition: (screenPosition, options) ->
screenPosition = Point.fromObject(screenPosition)
bufferPosition = @displayLayer.translateScreenPosition(screenPosition, options)
@getMarker(@bufferMarkerLayer.markPosition(bufferPosition, options).id)
# Public: Create a marker with the given buffer range.
#
# * `range` A {Range} or range-compatible {Array}
# * `options` A hash of key-value pairs to associate with the marker. There
# are also reserved property names that have marker-specific meaning.
# * `reversed` (optional) {Boolean} Creates the marker in a reversed
# orientation. (default: false)
# * `invalidate` (optional) {String} Determines the rules by which changes
# to the buffer *invalidate* the marker. (default: 'overlap') It can be
# any of the following strategies, in order of fragility:
# * __never__: The marker is never marked as invalid. This is a good choice for
# markers representing selections in an editor.
# * __surround__: The marker is invalidated by changes that completely surround it.
# * __overlap__: The marker is invalidated by changes that surround the
# start or end of the marker. This is the default.
# * __inside__: The marker is invalidated by changes that extend into the
# inside of the marker. Changes that end at the marker's start or
# start at the marker's end do not invalidate the marker.
# * __touch__: The marker is invalidated by a change that touches the marked
# region in any way, including changes that end at the marker's
# start or start at the marker's end. This is the most fragile strategy.
# * `exclusive` {Boolean} indicating whether insertions at the start or end
# of the marked range should be interpreted as happening *outside* the
# marker. Defaults to `false`, except when using the `inside`
# invalidation strategy or when when the marker has no tail, in which
# case it defaults to true. Explicitly assigning this option overrides
# behavior in all circumstances.
#
# Returns a {DisplayMarker}.
markBufferRange: (bufferRange, options) ->
bufferRange = Range.fromObject(bufferRange)
@getMarker(@bufferMarkerLayer.markRange(bufferRange, options).id)
# Public: Create a marker on this layer with its head at the given buffer
# position and no tail.
#
# * `bufferPosition` A {Point} or point-compatible {Array}
# * `options` (optional) An {Object} with the following keys:
# * `invalidate` (optional) {String} Determines the rules by which changes
# to the buffer *invalidate* the marker. (default: 'overlap') It can be
# any of the following strategies, in order of fragility:
# * __never__: The marker is never marked as invalid. This is a good choice for
# markers representing selections in an editor.
# * __surround__: The marker is invalidated by changes that completely surround it.
# * __overlap__: The marker is invalidated by changes that surround the
# start or end of the marker. This is the default.
# * __inside__: The marker is invalidated by changes that extend into the
# inside of the marker. Changes that end at the marker's start or
# start at the marker's end do not invalidate the marker.
# * __touch__: The marker is invalidated by a change that touches the marked
# region in any way, including changes that end at the marker's
# start or start at the marker's end. This is the most fragile strategy.
# * `exclusive` {Boolean} indicating whether insertions at the start or end
# of the marked range should be interpreted as happening *outside* the
# marker. Defaults to `false`, except when using the `inside`
# invalidation strategy or when when the marker has no tail, in which
# case it defaults to true. Explicitly assigning this option overrides
# behavior in all circumstances.
#
# Returns a {DisplayMarker}.
markBufferPosition: (bufferPosition, options) ->
@getMarker(@bufferMarkerLayer.markPosition(Point.fromObject(bufferPosition), options).id)
###
Section: Querying
###
# Essential: Get an existing marker by its id.
#
# Returns a {DisplayMarker}.
getMarker: (id) ->
if displayMarker = @markersById[id]
displayMarker
else if bufferMarker = @bufferMarkerLayer.getMarker(id)
@markersById[id] = new DisplayMarker(this, bufferMarker)
# Essential: Get all markers in the layer.
#
# Returns an {Array} of {DisplayMarker}s.
getMarkers: ->
@bufferMarkerLayer.getMarkers().map ({id}) => @getMarker(id)
# Public: Get the number of markers in the marker layer.
#
# Returns a {Number}.
getMarkerCount: ->
@bufferMarkerLayer.getMarkerCount()
# Public: Find markers in the layer conforming to the given parameters.
#
# This method finds markers based on the given properties. Markers can be
# associated with custom properties that will be compared with basic equality.
# In addition, there are several special properties that will be compared
# with the range of the markers rather than their properties.
#
# * `properties` An {Object} containing properties that each returned marker
# must satisfy. Markers can be associated with custom properties, which are
# compared with basic equality. In addition, several reserved properties
# can be used to filter markers based on their current range:
# * `startBufferPosition` Only include markers starting at this {Point} in buffer coordinates.
# * `endBufferPosition` Only include markers ending at this {Point} in buffer coordinates.
# * `startScreenPosition` Only include markers starting at this {Point} in screen coordinates.
# * `endScreenPosition` Only include markers ending at this {Point} in screen coordinates.
# * `startsInBufferRange` Only include markers starting inside this {Range} in buffer coordinates.
# * `endsInBufferRange` Only include markers ending inside this {Range} in buffer coordinates.
# * `startsInScreenRange` Only include markers starting inside this {Range} in screen coordinates.
# * `endsInScreenRange` Only include markers ending inside this {Range} in screen coordinates.
# * `startBufferRow` Only include markers starting at this row in buffer coordinates.
# * `endBufferRow` Only include markers ending at this row in buffer coordinates.
# * `startScreenRow` Only include markers starting at this row in screen coordinates.
# * `endScreenRow` Only include markers ending at this row in screen coordinates.
# * `intersectsBufferRowRange` Only include markers intersecting this {Array}
# of `[startRow, endRow]` in buffer coordinates.
# * `intersectsScreenRowRange` Only include markers intersecting this {Array}
# of `[startRow, endRow]` in screen coordinates.
# * `containsBufferRange` Only include markers containing this {Range} in buffer coordinates.
# * `containsBufferPosition` Only include markers containing this {Point} in buffer coordinates.
# * `containedInBufferRange` Only include markers contained in this {Range} in buffer coordinates.
# * `containedInScreenRange` Only include markers contained in this {Range} in screen coordinates.
# * `intersectsBufferRange` Only include markers intersecting this {Range} in buffer coordinates.
# * `intersectsScreenRange` Only include markers intersecting this {Range} in screen coordinates.
#
# Returns an {Array} of {DisplayMarker}s
findMarkers: (params) ->
params = @translateToBufferMarkerLayerFindParams(params)
@bufferMarkerLayer.findMarkers(params).map (stringMarker) => @getMarker(stringMarker.id)
###
Section: Private
###
translateBufferPosition: (bufferPosition, options) ->
@displayLayer.translateBufferPosition(bufferPosition, options)
translateBufferRange: (bufferRange, options) ->
@displayLayer.translateBufferRange(bufferRange, options)
translateScreenPosition: (screenPosition, options) ->
@displayLayer.translateScreenPosition(screenPosition, options)
translateScreenRange: (screenRange, options) ->
@displayLayer.translateScreenRange(screenRange, options)
emitDidUpdate: ->
@emitter.emit('did-update')
notifyObserversIfMarkerScreenPositionsChanged: ->
for marker in @getMarkers()
marker.notifyObservers(false)
return
destroyMarker: (id) ->
if marker = @markersById[id]
marker.didDestroyBufferMarker()
didDestroyMarker: (marker) ->
@markersWithDestroyListeners.delete(marker)
delete @markersById[marker.id]
translateToBufferMarkerLayerFindParams: (params) ->
bufferMarkerLayerFindParams = {}
for key, value of params
switch key
when 'startBufferPosition'
key = 'startPosition'
when 'endBufferPosition'
key = 'endPosition'
when 'startScreenPosition'
key = 'startPosition'
value = @displayLayer.translateScreenPosition(value)
when 'endScreenPosition'
key = 'endPosition'
value = @displayLayer.translateScreenPosition(value)
when 'startsInBufferRange'
key = 'startsInRange'
when 'endsInBufferRange'
key = 'endsInRange'
when 'startsInScreenRange'
key = 'startsInRange'
value = @displayLayer.translateScreenRange(value)
when 'endsInScreenRange'
key = 'endsInRange'
value = @displayLayer.translateScreenRange(value)
when 'startBufferRow'
key = 'startRow'
when 'endBufferRow'
key = 'endRow'
when 'startScreenRow'
key = 'startsInRange'
startBufferPosition = @displayLayer.translateScreenPosition(Point(value, 0))
endBufferPosition = @displayLayer.translateScreenPosition(Point(value, Infinity))
value = Range(startBufferPosition, endBufferPosition)
when 'endScreenRow'
key = 'endsInRange'
startBufferPosition = @displayLayer.translateScreenPosition(Point(value, 0))
endBufferPosition = @displayLayer.translateScreenPosition(Point(value, Infinity))
value = Range(startBufferPosition, endBufferPosition)
when 'intersectsBufferRowRange'
key = 'intersectsRowRange'
when 'intersectsScreenRowRange'
key = 'intersectsRange'
[startScreenRow, endScreenRow] = value
startBufferPosition = @displayLayer.translateScreenPosition(Point(startScreenRow, 0))
endBufferPosition = @displayLayer.translateScreenPosition(Point(endScreenRow, Infinity))
value = Range(startBufferPosition, endBufferPosition)
when 'containsBufferRange'
key = 'containsRange'
when 'containsScreenRange'
key = 'containsRange'
value = @displayLayer.translateScreenRange(value)
when 'containsBufferPosition'
key = 'containsPosition'
when 'containsScreenPosition'
key = 'containsPosition'
value = @displayLayer.translateScreenPosition(value)
when 'containedInBufferRange'
key = 'containedInRange'
when 'containedInScreenRange'
key = 'containedInRange'
value = @displayLayer.translateScreenRange(value)
when 'intersectsBufferRange'
key = 'intersectsRange'
when 'intersectsScreenRange'
key = 'intersectsRange'
value = @displayLayer.translateScreenRange(value)
bufferMarkerLayerFindParams[key] = value
bufferMarkerLayerFindParams