From b7aa421e4e460f2af0859d2621d40d53a7801411 Mon Sep 17 00:00:00 2001 From: Max Brunsfeld Date: Wed, 5 Nov 2014 11:10:51 -0800 Subject: [PATCH] Add TextEditor::withGroupingInterval This method temporarily instructs the editor to apply undo grouping with a given interval. This way, undo grouping can be made optional without adding optional arguments to every buffer manipulation method. --- spec/text-editor-spec.coffee | 30 ++++++++++++++++++++++++++++++ src/text-editor.coffee | 18 ++++++++++++++++-- 2 files changed, 46 insertions(+), 2 deletions(-) diff --git a/spec/text-editor-spec.coffee b/spec/text-editor-spec.coffee index 56e86edc6..f74f248fd 100644 --- a/spec/text-editor-spec.coffee +++ b/spec/text-editor-spec.coffee @@ -2816,6 +2816,36 @@ describe "TextEditor", -> editor.redo() expect(editor.getSelectedBufferRanges()).toEqual [[[1, 6], [1, 6]], [[1, 18], [1, 18]]] + describe ".withGroupingInterval(interval)", -> + currentTime = null + + beforeEach -> + currentTime = 0 + spyOn(Date, 'now').andCallFake -> currentTime + + it "allows undo entries to be grouped", -> + buffer.setText("") + + editor.withGroupingInterval 200, -> + editor.insertText("1") + + currentTime += 199 + editor.insertText("2") + + currentTime += 199 + editor.insertText("3") + + currentTime += 200 + editor.insertText("4") + + expect(buffer.getText()).toBe "1234" + + editor.undo() + expect(buffer.getText()).toBe "123" + + editor.undo() + expect(buffer.getText()).toBe "" + xit "restores folds after undo and redo", -> editor.foldBufferRow(1) editor.setSelectedBufferRange([[1, 0], [10, Infinity]], preserveFolds: true) diff --git a/src/text-editor.coffee b/src/text-editor.coffee index 812524482..382ebf0a6 100644 --- a/src/text-editor.coffee +++ b/src/text-editor.coffee @@ -68,6 +68,7 @@ class TextEditor extends Model suppressSelectionMerging: false updateBatchDepth: 0 selectionFlashDuration: 500 + groupingInterval: 0 @delegatesMethods 'suggestedIndentForBufferRow', 'autoIndentBufferRow', 'autoIndentBufferRows', 'autoDecreaseIndentForBufferRow', 'toggleLineCommentForBufferRow', 'toggleLineCommentsForBufferRows', @@ -1097,7 +1098,7 @@ class TextEditor extends Model # execution and revert any changes performed up to the abortion. # # * `fn` A {Function} to call inside the transaction. - transact: (fn) -> @buffer.transact(fn) + transact: (fn) -> @buffer.transact(fn, @groupingInterval) # Extended: Start an open-ended transaction. # @@ -1105,7 +1106,7 @@ class TextEditor extends Model # transaction. If you nest calls to transactions, only the outermost # transaction is considered. You must match every begin with a matching # commit, but a single call to abort will cancel all nested transactions. - beginTransaction: -> @buffer.beginTransaction() + beginTransaction: -> @buffer.beginTransaction(@groupingInterval) # Extended: Commit an open-ended transaction started with {::beginTransaction} # and push it to the undo stack. @@ -1117,6 +1118,19 @@ class TextEditor extends Model # within the transaction. abortTransaction: -> @buffer.abortTransaction() + # Extended: Set the time interval over which undo/redo operations are grouped. + # + # * `interval` A {Number} of milliseconds within which operations should be + # grouped with respec to undo/redo + # * `fn` A {Function} to call with the given interval setting + withGroupingInterval: (interval, fn) -> + previousInterval = @groupingInterval + @groupingInterval = interval + try + fn() + finally + @groupingInterval = previousInterval + ### Section: TextEditor Coordinates ###