Skip to content
This repository was archived by the owner on Dec 15, 2022. It is now read-only.

Commit 14addc7

Browse files
Nathan SoboAntonio Scandurra
authored andcommitted
Add restoreDefaultHistoryProvider
1 parent f03eb56 commit 14addc7

File tree

5 files changed

+53
-9
lines changed

5 files changed

+53
-9
lines changed

spec/text-buffer-spec.coffee

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -836,7 +836,7 @@ describe "TextBuffer", ->
836836
buffer.undo()
837837
expect(buffer.getText()).toBe('Lorem ')
838838

839-
describe "::getHistory(maxEntries)", ->
839+
describe "::getHistory(maxEntries) and restoreDefaultHistoryProvider(history)", ->
840840
it "returns a base text and the state of the last `maxEntries` entries in the undo and redo stacks", ->
841841
buffer = new TextBuffer({text: ''})
842842
markerLayer = buffer.addMarkerLayer({maintainHistory: true})
@@ -859,14 +859,13 @@ describe "TextBuffer", ->
859859
buffer.undo()
860860
buffer.undo()
861861

862-
{baseText, nextCheckpointId, undoStack, redoStack} = buffer.getHistory(3)
863-
expect(baseText).toBe('Lorem ipsum dolor ')
864-
expect(nextCheckpointId).toBe(buffer.createCheckpoint())
865-
expect(undoStack).toEqual([
862+
history = buffer.getHistory(3)
863+
expect(history.baseText).toBe('Lorem ipsum dolor ')
864+
expect(history.nextCheckpointId).toBe(buffer.createCheckpoint())
865+
expect(history.undoStack).toEqual([
866866
{
867867
type: 'checkpoint',
868868
id: checkpoint1,
869-
isBarrier: false,
870869
markers: markersSnapshotAtCheckpoint1
871870
},
872871
{
@@ -882,7 +881,7 @@ describe "TextBuffer", ->
882881
markersAfter: markersSnapshotAtCheckpoint1
883882
}
884883
])
885-
expect(redoStack).toEqual([
884+
expect(history.redoStack).toEqual([
886885
{
887886
type: 'transaction',
888887
changes: [{oldStart: Point(0, 38), oldEnd: Point(0, 38), newStart: Point(0, 38), newEnd: Point(0, 48), oldText: '', newText: 'adipiscit '}],
@@ -892,7 +891,6 @@ describe "TextBuffer", ->
892891
{
893892
type: 'checkpoint',
894893
id: checkpoint2,
895-
isBarrier: false,
896894
markers: markersSnapshotAtCheckpoint2
897895
},
898896
{
@@ -903,6 +901,15 @@ describe "TextBuffer", ->
903901
}
904902
])
905903

904+
buffer.createCheckpoint()
905+
buffer.append('x')
906+
buffer.undo()
907+
buffer.clearUndoStack()
908+
909+
expect(buffer.getHistory()).not.toEqual(history)
910+
buffer.restoreDefaultHistoryProvider(history)
911+
expect(buffer.getHistory()).toEqual(history)
912+
906913
it "throws an error when called within a transaction", ->
907914
buffer = new TextBuffer()
908915
expect(->

src/default-history-provider.coffee

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{Patch} = require 'superstring'
22
MarkerLayer = require './marker-layer'
3+
{traversal} = require './point-helpers'
34

45
SerializationVersion = 6
56

@@ -286,6 +287,21 @@ class DefaultHistoryProvider
286287
redoStack
287288
}
288289

290+
restoreFromSnapshot: ({@nextCheckpointId, undoStack, redoStack}) ->
291+
@undoStack = undoStack.map (entry) ->
292+
switch entry.type
293+
when 'transaction'
294+
transactionFromSnapshot(entry)
295+
when 'checkpoint'
296+
checkpointFromSnapshot(entry)
297+
298+
@redoStack = redoStack.map (entry) ->
299+
switch entry.type
300+
when 'transaction'
301+
transactionFromSnapshot(entry)
302+
when 'checkpoint'
303+
checkpointFromSnapshot(entry)
304+
289305
###
290306
Section: Private
291307
###
@@ -353,10 +369,12 @@ getCheckpointSnapshot = (checkpoint) ->
353369
{
354370
type: 'checkpoint',
355371
id: checkpoint.id,
356-
isBarrier: checkpoint.isBarrier,
357372
markers: checkpoint.snapshot
358373
}
359374

375+
checkpointFromSnapshot = ({id, markers}) ->
376+
new Checkpoint(id, markers, false)
377+
360378
getTransactionSnapshot = (transaction) ->
361379
{
362380
type: 'transaction',
@@ -365,6 +383,16 @@ getTransactionSnapshot = (transaction) ->
365383
markersAfter: transaction.markerSnapshotAfter
366384
}
367385

386+
transactionFromSnapshot = ({changes, markersBefore, markersAfter}) ->
387+
patch = new Patch
388+
for {oldStart, oldEnd, oldText, newStart, newEnd, newText} in changes by -1
389+
oldExtent = traversal(oldEnd, oldStart)
390+
newExtent = traversal(newEnd, newStart)
391+
patch.splice(oldStart, oldExtent, newExtent, oldText, newText)
392+
393+
# TODO: Return raw patch if there's no markersBefore && markersAfter
394+
new Transaction(markersBefore, patch, markersAfter)
395+
368396
getPatchSnapshot = (patch) ->
369397
changes = []
370398
for change in patch.getChanges() by 1

src/marker-layer.coffee

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,7 @@ class MarkerLayer
384384
###
385385

386386
addMarker: (id, range, params) ->
387+
range = Range.fromObject(range)
387388
Point.assertValid(range.start)
388389
Point.assertValid(range.end)
389390
@index.insert(id, range.start, range.end)

src/marker.coffee

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,9 @@ class Marker
343343
update: (oldRange, {range, reversed, tailed, valid, exclusive, properties}, textChanged=false, suppressMarkerLayerUpdateEvents=false) ->
344344
return if @isDestroyed()
345345

346+
oldRange = Range.fromObject(oldRange)
347+
range = Range.fromObject(range) if range?
348+
346349
wasExclusive = @isExclusive()
347350
updated = propertiesChanged = false
348351

src/text-buffer.coffee

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1122,6 +1122,11 @@ class TextBuffer
11221122
setHistoryProvider: (historyProvider) ->
11231123
@historyProvider = historyProvider
11241124

1125+
restoreDefaultHistoryProvider: (history) ->
1126+
provider = new DefaultHistoryProvider(this)
1127+
provider.restoreFromSnapshot(history)
1128+
@setHistoryProvider(provider)
1129+
11251130
getHistory: (maxEntries) ->
11261131
if @transactCallDepth > 0
11271132
throw new Error('Cannot build history snapshots within transactions')

0 commit comments

Comments
 (0)