From 7462222387134aa2fba15b51aeb943fc79a97838 Mon Sep 17 00:00:00 2001 From: Corey Johnson & Nathan Sobo Date: Thu, 19 Apr 2012 11:19:24 -0600 Subject: [PATCH] Allow calls to startBatch/endBatch to be nested --- spec/app/undo-manager-spec.coffee | 50 ++++--------------------------- src/app/undo-manager.coffee | 6 ++++ 2 files changed, 12 insertions(+), 44 deletions(-) diff --git a/spec/app/undo-manager-spec.coffee b/spec/app/undo-manager-spec.coffee index f187335c1..c014c0032 100644 --- a/spec/app/undo-manager-spec.coffee +++ b/spec/app/undo-manager-spec.coffee @@ -64,11 +64,15 @@ describe "UndoManager", -> it "causes changes in batch to be undone simultaneously and returns an array of ranges to select from undo and redo", -> buffer.insert([0, 0], "foo") + ignoredRanges = [[[666, 666], [666, 666]], [[666, 666], [666, 666]]] beforeRanges = [[[1, 2], [1, 2]], [[1, 9], [1, 9]]] + afterRanges =[[[1, 5], [1, 5]], [[1, 12], [1, 12]]] + undoManager.startUndoBatch(beforeRanges) + undoManager.startUndoBatch(ignoredRanges) # calls can be nested buffer.insert([1, 2], "111") buffer.insert([1, 9], "222") - afterRanges =[[[1, 5], [1, 5]], [[1, 12], [1, 12]]] + undoManager.endUndoBatch(ignoredRanges) # calls can be nested undoManager.endUndoBatch(afterRanges) expect(buffer.lineForRow(1)).toBe ' 111var 222sort = function(items) {' @@ -95,49 +99,7 @@ describe "UndoManager", -> expect(ranges).toBe beforeRanges expect(buffer.lineForRow(1)).toBe ' var sort = function(items) {' - it "old: causes all changes before a call to .endUndoBatch to be undone at the same time", -> - buffer.insert([0, 0], "foo") - undoManager.startUndoBatch() - buffer.insert([1, 0], "bar") - buffer.insert([2, 0], "bar") - buffer.delete([[3, 4], [3, 8]]) - undoManager.endUndoBatch() - buffer.change([[4, 4], [4, 9]], "slongaz") - - expect(buffer.lineForRow(4)).not.toContain("while") - undoManager.undo() - expect(buffer.lineForRow(4)).toContain("while") - - expect(buffer.lineForRow(1)).toContain("bar") - expect(buffer.lineForRow(2)).toContain("bar") - expect(buffer.lineForRow(3)).not.toContain("var") - - undoManager.undo() - - expect(buffer.lineForRow(1)).not.toContain("bar") - expect(buffer.lineForRow(2)).not.toContain("bar") - expect(buffer.lineForRow(3)).toContain("var") - - undoManager.undo() - - expect(buffer.lineForRow(0)).not.toContain("foo") - - undoManager.redo() - - expect(buffer.lineForRow(0)).toContain("foo") - - undoManager.redo() - - expect(buffer.lineForRow(1)).toContain("bar") - expect(buffer.lineForRow(2)).toContain("bar") - expect(buffer.lineForRow(3)).not.toContain("var") - - undoManager.redo() - - expect(buffer.lineForRow(4)).not.toContain("while") - expect(buffer.lineForRow(4)).toContain("slongaz") - - it "does not store empty batches", -> + it "does not store empty batches", -> buffer.insert([0,0], "foo") undoManager.startUndoBatch() undoManager.endUndoBatch() diff --git a/src/app/undo-manager.coffee b/src/app/undo-manager.coffee index fdb2f7966..45a06fd17 100644 --- a/src/app/undo-manager.coffee +++ b/src/app/undo-manager.coffee @@ -4,8 +4,10 @@ class UndoManager redoHistory: null currentBatch: null preserveHistory: false + startBatchCallCount: null constructor: (@buffer) -> + @startBatchCallCount = 0 @undoHistory = [] @redoHistory = [] @buffer.on 'change', (op) => @@ -35,10 +37,14 @@ class UndoManager batch.newSelectionRanges startUndoBatch: (ranges) -> + @startBatchCallCount++ + return if @startBatchCallCount > 1 @currentBatch = [] @currentBatch.oldSelectionRanges = ranges endUndoBatch: (ranges) -> + @startBatchCallCount-- + return if @startBatchCallCount > 0 @currentBatch.newSelectionRanges = ranges @undoHistory.push(@currentBatch) if @currentBatch.length > 0 @currentBatch = null