This repository was archived by the owner on Dec 15, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 324
/
Copy patheditor-binding.test.js
504 lines (431 loc) · 17.3 KB
/
editor-binding.test.js
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
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
const assert = require('assert')
const fs = require('fs')
const path = require('path')
const SAMPLE_TEXT = fs.readFileSync(path.join(__dirname, 'fixtures', 'sample.js'), 'utf8')
const {TextEditor, TextBuffer, Range} = require('atom')
const {Point} = TextBuffer
const EditorBinding = require('../lib/editor-binding')
const {buildAtomEnvironment, destroyAtomEnvironments} = require('./helpers/atom-environments')
const {loadPackageStyleSheets} = require('./helpers/ui-helpers')
const {
setEditorHeightInLines,
setEditorWidthInChars,
setEditorScrollTopInLines,
setEditorScrollLeftInChars
} = require('./helpers/editor-helpers')
const {FollowState} = require('@atom/teletype-client')
const FakeBufferProxy = require('./helpers/fake-buffer-proxy')
const BufferBinding = require('../lib/buffer-binding')
suite('EditorBinding', function () {
if (process.env.CI) this.timeout(process.env.TEST_TIMEOUT_IN_MS)
let attachedElements = []
setup(() => {
// Load the editor default styles by instantiating a new AtomEnvironment.
const environment = buildAtomEnvironment()
loadPackageStyleSheets(environment)
// Position editor absolutely to prevent its size from being affected by the
// window size of the test runner. We also give it an initial width and
// height so that the editor component can perform initial measurements.
environment.styles.addStyleSheet(`
atom-text-editor {
position: absolute;
top: 0;
left: 0;
width: 50px;
height: 50px;
}
`)
})
teardown(async () => {
if (!global.debugContent) {
let element
while (element = attachedElements.pop()) { // eslint-disable-line no-cond-assign
element.remove()
}
await destroyAtomEnvironments()
}
})
test('relays local selections and creates cursor decorations on the local editor based on the remote ones', () => {
const editor = new TextEditor()
editor.setText(SAMPLE_TEXT)
editor.setCursorBufferPosition([0, 0])
const binding = new EditorBinding({editor, portal: new FakePortal()})
const editorProxy = new FakeEditorProxy(binding)
binding.setEditorProxy(editorProxy)
assert.deepEqual(
editorProxy.selections,
{
1: {
range: Range.fromObject([[0, 0], [0, 0]]),
exclusive: true,
reversed: false
}
}
)
editor.setSelectedBufferRanges([
[[10, 0], [11, 4]],
[[20, 0], [20, 5]]
])
editor.getLastSelection().setBufferRange([[20, 0], [20, 5]], {reversed: true})
assert.deepEqual(
editorProxy.selections,
{
1: {
range: Range.fromObject([[10, 0], [11, 4]]),
exclusive: false,
reversed: false
},
2: {
range: Range.fromObject([[20, 0], [20, 5]]),
exclusive: false,
reversed: true
}
}
)
binding.updateSelectionsForSiteId(2, {
1: {range: {start: {row: 3, column: 0}, end: {row: 4, column: 2}}},
2: {range: {start: {row: 5, column: 0}, end: {row: 6, column: 0}}, reversed: true}
})
const cursorDecoratedRanges = getCursorDecoratedRanges(editor)
assert.deepEqual(
cursorDecoratedRanges,
[
{tail: Point(10, 0), head: Point(11, 4)},
{tail: Point(20, 5), head: Point(20, 0)},
{tail: Point(3, 0), head: Point(4, 2)},
{tail: Point(6, 0), head: Point(5, 0)}
]
)
editor.setSelectedBufferRanges([
[[0, 0], [0, 4]]
])
assert.deepEqual(
editorProxy.selections,
{
1: {
range: Range.fromObject([[0, 0], [0, 4]]),
exclusive: false,
reversed: false
}
}
)
})
test('clears remote selections for disconnected remote site', () => {
const editor = new TextEditor()
editor.setText(SAMPLE_TEXT)
editor.setCursorBufferPosition([0, 0])
const binding = new EditorBinding({editor, portal: new FakePortal()})
const editorProxy = new FakeEditorProxy(binding)
binding.setEditorProxy(editorProxy)
editor.setSelectedBufferRanges([
[[10, 0], [11, 4]],
[[20, 0], [20, 5]]
])
binding.updateSelectionsForSiteId(2, {
1: {range: {start: {row: 3, column: 0}, end: {row: 4, column: 2}}}
})
const cursorDecoratedRanges = getCursorDecoratedRanges(editor)
assert.deepEqual(
cursorDecoratedRanges,
[
{tail: Point(10, 0), head: Point(11, 4)},
{tail: Point(20, 0), head: Point(20, 5)},
{tail: Point(3, 0), head: Point(4, 2)}
]
)
binding.clearSelectionsForSiteId(2)
assert.deepEqual(
getCursorDecoratedRanges(editor),
[
{tail: {row: 10, column: 0}, head: {row: 11, column: 4}},
{tail: {row: 20, column: 0}, head: {row: 20, column: 5}}
]
)
})
test('clears the tail of remote selection markers when they become empty after an update', () => {
const editor = new TextEditor()
editor.setText(SAMPLE_TEXT)
editor.setCursorBufferPosition([0, 0])
const binding = new EditorBinding({editor, portal: new FakePortal()})
const editorProxy = new FakeEditorProxy(binding)
binding.setEditorProxy(editorProxy)
const originalRemoteSelection = {start: {row: 1, column: 0}, end: {row: 1, column: 5}}
binding.updateSelectionsForSiteId(2, {1: {range: originalRemoteSelection}})
assert.deepEqual(
getCursorDecoratedRanges(editor),
[
{tail: {row: 0, column: 0}, head: {row: 0, column: 0}}, // local selection
{tail: {row: 1, column: 0}, head: {row: 1, column: 5}} // remote selection
]
)
editor.getBuffer().setTextInRange(originalRemoteSelection, '', {undo: 'skip'})
const remoteSelectionAfterDelete = {start: {row: 1, column: 0}, end: {row: 1, column: 0}}
binding.updateSelectionsForSiteId(2, {1: {range: remoteSelectionAfterDelete}})
assert.deepEqual(
getCursorDecoratedRanges(editor),
[
{tail: {row: 0, column: 0}, head: {row: 0, column: 0}}, // local selection
{tail: {row: 1, column: 0}, head: {row: 1, column: 0}} // remote selection
]
)
editor.getBuffer().setTextInRange([remoteSelectionAfterDelete.start, remoteSelectionAfterDelete.start], 'a', {undo: 'skip'})
assert.deepEqual(
getCursorDecoratedRanges(editor),
[
{tail: {row: 0, column: 0}, head: {row: 0, column: 0}}, // local selection
{tail: {row: 1, column: 1}, head: {row: 1, column: 1}} // remote selection
]
)
})
test('clears the tail of local and remote selection markers when they become empty after a text change', () => {
const editor = new TextEditor()
editor.setText(SAMPLE_TEXT)
editor.setSelectedBufferRange([[0, 0], [0, 3]])
const binding = new EditorBinding({editor, portal: new FakePortal()})
const editorProxy = new FakeEditorProxy(binding)
binding.setEditorProxy(editorProxy)
binding.updateSelectionsForSiteId(2, {
1: {
range: {start: {row: 0, column: 0}, end: {row: 0, column: 1}}
}
})
// Ensure tail of remote selections is cleared after they become empty as a
// result of a local change.
editor.backspace()
editor.insertText('ABCDEFGH')
assert.deepEqual(
getCursorDecoratedRanges(editor),
[
{tail: {row: 0, column: 8}, head: {row: 0, column: 8}},
{tail: {row: 0, column: 8}, head: {row: 0, column: 8}}
]
)
// Ensure tail of local selections is cleared after they become empty as a
// result of a remote change.
editor.setSelectedBufferRange([[0, 0], [0, 5]])
editor.getBuffer().setTextInRange([[0, 0], [0, 5]], '', {undo: 'skip'})
editor.getBuffer().setTextInRange([[0, 0], [0, 0]], '123', {undo: 'skip'})
const cursorDecoratedRanges = getCursorDecoratedRanges(editor)
assert.deepEqual(
cursorDecoratedRanges,
[
{tail: Point(0, 3), head: Point(0, 3)},
{tail: Point(0, 6), head: Point(0, 6)}
]
)
})
test('relays exclusivity but does not apply it to the markers', () => {
const editor = new TextEditor()
editor.setText(SAMPLE_TEXT)
editor.setCursorBufferPosition([0, 0])
const binding = new EditorBinding({editor, portal: new FakePortal()})
const editorProxy = new FakeEditorProxy(binding)
binding.setEditorProxy(editorProxy)
// Ensure exclusivity is being relayed. This enables teletype-crdt to resolve
// logical ranges correctly when the local site performs an insertion right
// at a remote site cursor position, but before such cursor has been relayed
// to the local site.
assert.deepEqual(editorProxy.selections, {
1: {
range: editor.getSelectedBufferRange(),
reversed: false,
exclusive: true
}
})
// Ensure exclusivity is not overridden on the markers. This is because
// exclusivity is computed automatically based on whether the marker is
// empty or not. Overriding it would cause markers to not change their
// exclusivity if a later text update makes them become empty or non-empty.
binding.updateSelectionsForSiteId(2, {
1: {
range: {start: {row: 0, column: 2}, end: {row: 0, column: 3}},
reversed: false,
exclusive: false
}
})
editor.getBuffer().setTextInRange([[0, 1], [0, 4]], '')
editor.getBuffer().insert([0, 1], 'ABC')
assert.deepEqual(
getCursorDecoratedRanges(editor),
[
{tail: {row: 0, column: 0}, head: {row: 0, column: 0}},
{tail: {row: 0, column: 4}, head: {row: 0, column: 4}}
]
)
})
test('does not relay local selection changes if the associated marker moves because of a textual change', () => {
const editor = new TextEditor()
editor.setText(SAMPLE_TEXT)
const binding = new EditorBinding({editor, portal: new FakePortal()})
const editorProxy = new FakeEditorProxy(binding)
binding.setEditorProxy(editorProxy)
editor.setCursorBufferPosition([0, 0])
editorProxy.selections = {}
editor.insertText('X')
assert.deepEqual(editorProxy.selections, {})
// After deleting text in the selected range, the editor will set the cursor
// buffer position to the start of the selection.
editor.setSelectedBufferRange([[0, 0], [0, 3]])
editor.delete()
assert.deepEqual(editorProxy.selections, {
1: {
range: {start: {row: 0, column: 0}, end: {row: 0, column: 0}},
exclusive: true,
reversed: false
}
})
})
test('does not relay selections that are already destroyed when their creation event is emitted', () => {
const editor = new TextEditor({buffer: new TextBuffer(SAMPLE_TEXT)})
const binding = new EditorBinding({editor, portal: new FakePortal()})
const editorProxy = new FakeEditorProxy(binding)
binding.setEditorProxy(editorProxy)
editor.setSelectedBufferRange([[0, 2], [0, 6]])
editor.addSelectionForBufferRange([[0, 3], [0, 9]])
assert.deepEqual(editorProxy.selections, {
1: {
range: {start: {row: 0, column: 2}, end: {row: 0, column: 9}},
exclusive: false,
reversed: false
}
})
})
suite('guest editor binding', () => {
test('overrides the editor methods when setting the proxy', () => {
const buffer = new TextBuffer({text: SAMPLE_TEXT})
const editor = new TextEditor({buffer})
const editorBinding = new EditorBinding({editor, portal: new FakePortal(), isHost: false})
const editorProxy = new FakeEditorProxy(editorBinding)
const bufferBinding = new BufferBinding({buffer})
bufferBinding.setBufferProxy(editorProxy.bufferProxy)
editorBinding.setEditorProxy(editorProxy)
assert.equal(editor.getTitle(), '@site-1: fake-buffer-proxy-uri')
assert.equal(editor.copy(), null)
assert.equal(editor.serialize(), null)
assert.equal(buffer.getPath(), '@site-1:fake-buffer-proxy-uri')
assert(editor.element.classList.contains('teletype-RemotePaneItem'))
assert(!editor.getBuffer().isModified())
editor.save()
editor.save()
assert.equal(editorProxy.bufferProxy.saveRequestCount, 2)
})
})
test('decorates each cursor with a site-specific class name', () => {
const editor = new TextEditor()
editor.setText(SAMPLE_TEXT)
const binding = new EditorBinding({editor, portal: new FakePortal()})
const editorProxy = new FakeEditorProxy(binding, {siteId: 2})
binding.setEditorProxy(editorProxy)
assert.deepEqual(getCursorClasses(editor), ['ParticipantCursor--site-2'])
binding.updateSelectionsForSiteId(1, {1: {range: Range([0, 0], [0, 0])}})
assert.deepEqual(getCursorClasses(editor), ['ParticipantCursor--site-2', 'ParticipantCursor--site-1 non-blinking'])
binding.updateSelectionsForSiteId(3, {1: {range: Range([0, 0], [0, 0])}})
assert.deepEqual(getCursorClasses(editor), ['ParticipantCursor--site-2', 'ParticipantCursor--site-1 non-blinking', 'ParticipantCursor--site-3 non-blinking'])
binding.clearSelectionsForSiteId(1)
assert.deepEqual(getCursorClasses(editor), ['ParticipantCursor--site-2', 'ParticipantCursor--site-3 non-blinking'])
binding.dispose()
assert.deepEqual(getCursorClasses(editor), [])
})
test('isScrollNeededToViewPosition(position)', async () => {
const editor = new TextEditor({autoHeight: false})
const binding = new EditorBinding({editor, portal: new FakePortal()})
const editorProxy = new FakeEditorProxy(binding)
binding.setEditorProxy(editorProxy)
// If the editor is not yet attached to the DOM, scrolling isn't gonna help.
assert(!binding.isScrollNeededToViewPosition({row: 1, column: 0}))
assert(!binding.isScrollNeededToViewPosition({row: 0, column: 9}))
attachToDOM(editor.element)
await setEditorHeightInLines(editor, 4)
await setEditorWidthInChars(editor, 7)
editor.setText('a pretty long line\n'.repeat(100))
assert(!binding.isScrollNeededToViewPosition({row: 1, column: 0}))
assert(binding.isScrollNeededToViewPosition({row: 0, column: 9}))
assert(binding.isScrollNeededToViewPosition({row: 6, column: 0}))
// Ensure text is rendered, so that we can scroll down/right.
await editor.component.getNextUpdatePromise()
setEditorScrollTopInLines(editor, 5)
setEditorScrollLeftInChars(editor, 5)
assert(!binding.isScrollNeededToViewPosition({row: 6, column: 7}))
assert(binding.isScrollNeededToViewPosition({row: 6, column: 0}))
assert(binding.isScrollNeededToViewPosition({row: 3, column: 7}))
})
test('destroys folds intersecting the position of the leader', async () => {
const buffer = new TextBuffer({text: SAMPLE_TEXT})
const editor = new TextEditor({buffer})
const binding = new EditorBinding({editor, portal: new FakePortal()})
const editorProxy = new FakeEditorProxy(binding)
binding.setEditorProxy(editorProxy)
editor.foldBufferRange([[5, 4], [6, 12]])
editor.foldBufferRange([[9, 0], [10, 3]])
assert(editor.isFoldedAtBufferRow(5))
assert(editor.isFoldedAtBufferRow(10))
binding.updateTether(FollowState.RETRACTED, {row: 6, column: 0})
assert(!editor.isFoldedAtBufferRow(5))
assert(editor.isFoldedAtBufferRow(10))
binding.updateTether(FollowState.EXTENDED, {row: 9, column: 1})
assert(!editor.isFoldedAtBufferRow(5))
assert(editor.isFoldedAtBufferRow(10))
binding.updateTether(FollowState.RETRACTED, {row: 9, column: 1})
assert(!editor.isFoldedAtBufferRow(5))
assert(!editor.isFoldedAtBufferRow(10))
})
function getCursorDecoratedRanges (editor) {
const {decorationManager} = editor
const decorationsByMarker = decorationManager.decorationPropertiesByMarkerForScreenRowRange(0, Infinity)
const markers = []
for (const [marker, decorations] of decorationsByMarker) {
const hasCursorDecoration = decorations.some((d) => d.type === 'cursor')
if (hasCursorDecoration) markers.push(marker)
}
return markers.sort((a, b) => a.compare(b)).map(m => {
return {head: m.getHeadBufferPosition(), tail: m.getTailBufferPosition()}
})
}
function getCursorClasses (editor) {
const {decorationManager} = editor
const decorationsByMarker = decorationManager.decorationPropertiesByMarkerForScreenRowRange(0, Infinity)
const cursorDecorations = []
for (const [marker, decorations] of decorationsByMarker) { // eslint-disable-line no-unused-vars
let className = ''
for (const decoration of decorations) {
if (decoration.type === 'cursor' && decoration.class) {
className += ' ' + decoration.class
}
}
if (className) cursorDecorations.push(className.slice(1))
}
return cursorDecorations
}
function attachToDOM (element) {
attachedElements.push(element)
document.body.insertBefore(element, document.body.firstChild)
}
})
class FakeEditorProxy {
constructor (delegate, {siteId} = {}) {
this.delegate = delegate
this.bufferProxy = new FakeBufferProxy({uri: 'fake-buffer-proxy-uri'})
this.selections = {}
this.siteId = (siteId == null) ? 1 : siteId
this.disposed = false
}
dispose () {
this.disposed = true
}
didScroll () {}
updateSelections (selectionUpdates) {
for (const id in selectionUpdates) {
const selectionUpdate = selectionUpdates[id]
if (selectionUpdate) {
this.selections[id] = selectionUpdate
} else {
delete this.selections[id]
}
}
}
}
class FakePortal {
getSiteIdentity (siteId) {
return {login: 'site-' + siteId}
}
}