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

Commit be7791d

Browse files
author
Nathan Sobo
authored
Merge pull request #255 from atom/custom-history-providers
Allow custom history providers to be assigned
2 parents 738c60e + ce1964f commit be7791d

File tree

6 files changed

+344
-101
lines changed

6 files changed

+344
-101
lines changed

spec/text-buffer-spec.coffee

Lines changed: 114 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ Random = require 'random-seed'
66
Point = require '../src/point'
77
Range = require '../src/range'
88
DisplayLayer = require '../src/display-layer'
9+
DefaultHistoryProvider = require '../src/default-history-provider'
910
TextBuffer = require '../src/text-buffer'
1011
SampleText = fs.readFileSync(join(__dirname, 'fixtures', 'sample.js'), 'utf8')
1112
{buildRandomLines, getRandomBufferRange} = require './helpers/random'
@@ -534,6 +535,7 @@ describe "TextBuffer", ->
534535
expect(buffer.getText()).toBe "hello\nworld\r\nhow are you doing?"
535536

536537
it "groups adjacent transactions within each other's grouping intervals", ->
538+
now += 1000
537539
buffer.transact 101, -> buffer.setTextInRange([[0, 2], [0, 5]], "y")
538540

539541
now += 100
@@ -813,6 +815,106 @@ describe "TextBuffer", ->
813815
buffer.undo()
814816
expect(buffer.getText()).toBe "a"
815817

818+
describe "::setHistoryProvider(provider)", ->
819+
it "replaces the currently active history provider with the passed one", ->
820+
buffer = new TextBuffer({text: ''})
821+
buffer.insert([0, 0], 'Lorem ')
822+
buffer.insert([0, 6], 'ipsum ')
823+
expect(buffer.getText()).toBe('Lorem ipsum ')
824+
825+
buffer.undo()
826+
expect(buffer.getText()).toBe('Lorem ')
827+
828+
buffer.setHistoryProvider(new DefaultHistoryProvider(buffer))
829+
buffer.undo()
830+
expect(buffer.getText()).toBe('Lorem ')
831+
832+
buffer.insert([0, 6], 'dolor ')
833+
expect(buffer.getText()).toBe('Lorem dolor ')
834+
835+
buffer.undo()
836+
expect(buffer.getText()).toBe('Lorem ')
837+
838+
describe "::getHistory(maxEntries) and restoreDefaultHistoryProvider(history)", ->
839+
it "returns a base text and the state of the last `maxEntries` entries in the undo and redo stacks", ->
840+
buffer = new TextBuffer({text: ''})
841+
markerLayer = buffer.addMarkerLayer({maintainHistory: true})
842+
843+
buffer.append('Lorem ')
844+
buffer.append('ipsum ')
845+
buffer.append('dolor ')
846+
markerLayer.markPosition([0, 2])
847+
markersSnapshotAtCheckpoint1 = buffer.createMarkerSnapshot()
848+
checkpoint1 = buffer.createCheckpoint()
849+
buffer.append('sit ')
850+
buffer.append('amet ')
851+
buffer.append('consecteur ')
852+
markerLayer.markPosition([0, 4])
853+
markersSnapshotAtCheckpoint2 = buffer.createMarkerSnapshot()
854+
checkpoint2 = buffer.createCheckpoint()
855+
buffer.append('adipiscit ')
856+
buffer.append('elit ')
857+
buffer.undo()
858+
buffer.undo()
859+
buffer.undo()
860+
861+
history = buffer.getHistory(3)
862+
expect(history.baseText).toBe('Lorem ipsum dolor ')
863+
expect(history.nextCheckpointId).toBe(buffer.createCheckpoint())
864+
expect(history.undoStack).toEqual([
865+
{
866+
type: 'checkpoint',
867+
id: checkpoint1,
868+
markers: markersSnapshotAtCheckpoint1
869+
},
870+
{
871+
type: 'transaction',
872+
changes: [{oldStart: Point(0, 18), oldEnd: Point(0, 18), newStart: Point(0, 18), newEnd: Point(0, 22), oldText: '', newText: 'sit '}],
873+
markersBefore: markersSnapshotAtCheckpoint1,
874+
markersAfter: markersSnapshotAtCheckpoint1
875+
},
876+
{
877+
type: 'transaction',
878+
changes: [{oldStart: Point(0, 22), oldEnd: Point(0, 22), newStart: Point(0, 22), newEnd: Point(0, 27), oldText: '', newText: 'amet '}],
879+
markersBefore: markersSnapshotAtCheckpoint1,
880+
markersAfter: markersSnapshotAtCheckpoint1
881+
}
882+
])
883+
expect(history.redoStack).toEqual([
884+
{
885+
type: 'transaction',
886+
changes: [{oldStart: Point(0, 38), oldEnd: Point(0, 38), newStart: Point(0, 38), newEnd: Point(0, 48), oldText: '', newText: 'adipiscit '}],
887+
markersBefore: markersSnapshotAtCheckpoint2,
888+
markersAfter: markersSnapshotAtCheckpoint2
889+
},
890+
{
891+
type: 'checkpoint',
892+
id: checkpoint2,
893+
markers: markersSnapshotAtCheckpoint2
894+
},
895+
{
896+
type: 'transaction',
897+
changes: [{oldStart: Point(0, 27), oldEnd: Point(0, 27), newStart: Point(0, 27), newEnd: Point(0, 38), oldText: '', newText: 'consecteur '}],
898+
markersBefore: markersSnapshotAtCheckpoint1,
899+
markersAfter: markersSnapshotAtCheckpoint1
900+
}
901+
])
902+
903+
buffer.createCheckpoint()
904+
buffer.append('x')
905+
buffer.undo()
906+
buffer.clearUndoStack()
907+
908+
expect(buffer.getHistory()).not.toEqual(history)
909+
buffer.restoreDefaultHistoryProvider(history)
910+
expect(buffer.getHistory()).toEqual(history)
911+
912+
it "throws an error when called within a transaction", ->
913+
buffer = new TextBuffer()
914+
expect(->
915+
buffer.transact(-> buffer.getHistory(3))
916+
).toThrowError()
917+
816918
describe "::getTextInRange(range)", ->
817919
it "returns the text in a given range", ->
818920
buffer = new TextBuffer(text: "hello\nworld\r\nhow are you doing?")
@@ -2004,6 +2106,7 @@ describe "TextBuffer", ->
20042106

20052107
buffer.insert([0, 0], "abc")
20062108
buffer.delete([[0, 0], [0, 1]])
2109+
20072110
assertChangesEqual(textChanges, [
20082111
{
20092112
oldRange: [[0, 0], [0, 0]],
@@ -2021,17 +2124,18 @@ describe "TextBuffer", ->
20212124

20222125
textChanges = []
20232126
buffer.transact ->
2024-
buffer.insert([1, 0], "x")
2025-
buffer.insert([1, 1], "y")
2127+
buffer.insert([1, 0], "v")
2128+
buffer.insert([1, 1], "x")
2129+
buffer.setTextInRange([[1, 2], [1, 2]], "y", {undo: 'skip'})
20262130
buffer.insert([2, 3], "zw")
20272131
buffer.delete([[2, 3], [2, 4]])
20282132

20292133
assertChangesEqual(textChanges, [
20302134
{
20312135
oldRange: [[1, 0], [1, 0]],
2032-
newRange: [[1, 0], [1, 2]],
2136+
newRange: [[1, 0], [1, 3]],
20332137
oldText: "",
2034-
newText: "xy",
2138+
newText: "vxy",
20352139
},
20362140
{
20372141
oldRange: [[2, 3], [2, 3]],
@@ -2047,7 +2151,7 @@ describe "TextBuffer", ->
20472151
{
20482152
oldRange: [[1, 0], [1, 2]],
20492153
newRange: [[1, 0], [1, 0]],
2050-
oldText: "xy",
2154+
oldText: "vx",
20512155
newText: "",
20522156
},
20532157
{
@@ -2065,7 +2169,7 @@ describe "TextBuffer", ->
20652169
oldRange: [[1, 0], [1, 0]],
20662170
newRange: [[1, 0], [1, 2]],
20672171
oldText: "",
2068-
newText: "xy",
2172+
newText: "vx",
20692173
},
20702174
{
20712175
oldRange: [[2, 3], [2, 3]],
@@ -2122,6 +2226,7 @@ describe "TextBuffer", ->
21222226
buffer.transact ->
21232227
buffer.insert([0, 0], 'b')
21242228
buffer.insert([1, 0], 'c')
2229+
buffer.setTextInRange([[1, 1], [1, 1]], 'd', {undo: 'skip'})
21252230
expect(didStopChangingCallback).not.toHaveBeenCalled()
21262231

21272232
wait delay / 2, ->
@@ -2138,10 +2243,10 @@ describe "TextBuffer", ->
21382243
},
21392244
{
21402245
oldRange: [[1, 0], [1, 0]],
2141-
newRange: [[1, 0], [1, 1]],
2246+
newRange: [[1, 0], [1, 2]],
21422247
oldText: "",
2143-
newText: "c",
2144-
},
2248+
newText: "cd",
2249+
}
21452250
])
21462251

21472252
didStopChangingCallback.calls.reset()

0 commit comments

Comments
 (0)