From fc4b77a1d8da32f47dc347e1c2a7ef58b3891ad2 Mon Sep 17 00:00:00 2001 From: Antonio Scandurra Date: Mon, 8 May 2017 13:43:32 +0200 Subject: [PATCH] Don't notify observers of `onDidChangeText` after an empty transaction --- spec/text-buffer-spec.coffee | 7 ++++++- src/text-buffer.coffee | 8 +++++--- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/spec/text-buffer-spec.coffee b/spec/text-buffer-spec.coffee index f60de9cea2..6550200926 100644 --- a/spec/text-buffer-spec.coffee +++ b/spec/text-buffer-spec.coffee @@ -2650,7 +2650,6 @@ describe "TextBuffer", -> buffer.insert([2, 3], "zw") buffer.delete([[2, 3], [2, 4]]) - assertChangesEqual(textChanges, [ { oldRange: [[1, 0], [1, 0]], @@ -2715,6 +2714,12 @@ describe "TextBuffer", -> } ]) + it "doesn't notify observers after an empty transaction", -> + didChangeTextSpy = jasmine.createSpy() + buffer.onDidChangeText(didChangeTextSpy) + buffer.transact(->) + expect(didChangeTextSpy).not.toHaveBeenCalled() + it "doesn't throw an error when clearing the undo stack within a transaction", -> buffer.onDidChangeText(didChangeTextSpy = jasmine.createSpy()) expect(-> buffer.transact(-> buffer.clearUndoStack())).not.toThrowError() diff --git a/src/text-buffer.coffee b/src/text-buffer.coffee index 2fb6c5c6f4..d7043ef4ef 100644 --- a/src/text-buffer.coffee +++ b/src/text-buffer.coffee @@ -1654,9 +1654,11 @@ class TextBuffer emitDidChangeTextEvent: (patch) -> return if @transactCallDepth isnt 0 - @emitter.emit 'did-change-text', {changes: Object.freeze(normalizePatchChanges(patch.getHunks()))} - @patchesSinceLastStoppedChangingEvent.push(patch) - @scheduleDidStopChangingEvent() + hunks = patch.getHunks() + if hunks.length > 0 + @emitter.emit 'did-change-text', {changes: Object.freeze(normalizePatchChanges(hunks))} + @patchesSinceLastStoppedChangingEvent.push(patch) + @scheduleDidStopChangingEvent() # Identifies if the buffer belongs to multiple editors. #