@@ -13,8 +13,8 @@ DefaultHistoryProvider = require './default-history-provider'
1313MarkerLayer = require ' ./marker-layer'
1414MatchIterator = require ' ./match-iterator'
1515DisplayLayer = require ' ./display-layer'
16- {spliceArray , newlineRegex , normalizePatchChanges , regexIsSingleLine , extentForText , debounce } = require ' ./helpers'
17- {traversal } = require ' ./point-helpers'
16+ {spliceArray , newlineRegex , patchFromChanges , normalizePatchChanges , regexIsSingleLine , extentForText , debounce } = require ' ./helpers'
17+ {traverse , traversal } = require ' ./point-helpers'
1818Grim = require ' grim'
1919
2020class TransactionAbortedError extends Error
@@ -828,6 +828,7 @@ class TextBuffer
828828 oldStart : oldRange .start ,
829829 newStart : oldRange .start ,
830830 oldEnd : oldRange .end ,
831+ newEnd : traverse (oldRange .start , extentForText (newText)),
831832 oldText,
832833 newText,
833834 normalizeLineEndings
@@ -860,11 +861,11 @@ class TextBuffer
860861
861862 # Applies a change to the buffer based on its old range and new text.
862863 applyChange : (change , pushToHistory = false ) ->
863- {newStart , oldStart , oldEnd , oldText , newText , normalizeLineEndings } = change
864+ {newStart , newEnd , oldStart , oldEnd , oldText , newText , normalizeLineEndings } = change
864865
865866 oldExtent = traversal (oldEnd, oldStart)
866- start = Point .fromObject (newStart)
867- oldRange = Range (start, start .traverse (oldExtent))
867+ newStart = Point .fromObject (newStart)
868+ oldRange = Range (newStart, newStart .traverse (oldExtent))
868869 oldRange .freeze ()
869870
870871 # Determine how to normalize the line endings of inserted text if enabled
@@ -876,9 +877,9 @@ class TextBuffer
876877 if normalizedEnding
877878 newText = newText .replace (newlineRegex, normalizedEnding)
878879
879- newExtent = extentForText (newText)
880- newRange = Range (start, start .traverse (newExtent))
880+ newRange = Range (newStart, newEnd)
881881 newRange .freeze ()
882+ newExtent = newRange .getExtent ()
882883
883884 if pushToHistory
884885 change .oldExtent ?= oldExtent
@@ -1752,10 +1753,6 @@ class TextBuffer
17521753 @ emitMarkerChangeEvents (markersSnapshot)
17531754 @ emitModifiedStatusChanged (@ isModified ())
17541755
1755- unless @loaded
1756- start = {row : 0 , column : 0 }
1757- end = {row : 0 , column : 0 }
1758-
17591756 @loaded = true
17601757 @emitter .emit (' did-reload' )
17611758 this
@@ -1861,17 +1858,10 @@ class TextBuffer
18611858
18621859 emitDidChangeTextEvent : ->
18631860 if @transactCallDepth is 0 and @changesSinceLastDidChangeTextEvent .length > 0
1864- patch = new Patch
1865- while change = @changesSinceLastDidChangeTextEvent .shift ()
1866- patch .splice (
1867- change .newStart ,
1868- extentForText (change .oldText ),
1869- extentForText (change .newText ),
1870- change .oldText ,
1871- change .newText
1872- )
1873-
1874- compactedChanges = Object .freeze (normalizePatchChanges (patch .getChanges ()))
1861+ compactedChanges = Object .freeze (normalizePatchChanges (
1862+ patchFromChanges (@changesSinceLastDidChangeTextEvent ).getChanges ()
1863+ ))
1864+ @changesSinceLastDidChangeTextEvent .length = 0
18751865 @emitter .emit ' did-change-text' , {changes : compactedChanges}
18761866 @ debouncedEmitDidStopChangingEvent ()
18771867
@@ -1886,24 +1876,11 @@ class TextBuffer
18861876 return if @destroyed
18871877
18881878 modifiedStatus = @ isModified ()
1889-
1890- patches = @changesSinceLastStoppedChangingEvent .map (change) ->
1891- patch = new Patch
1892- patch .splice (
1893- change .newStart ,
1894- extentForText (change .oldText ),
1895- extentForText (change .newText ),
1896- change .oldText ,
1897- change .newText
1898- )
1899- patch
1900-
1901- composedChanges = Patch .compose (patches).getChanges ()
1902- @emitter .emit (
1903- ' did-stop-changing' ,
1904- {changes : Object .freeze (normalizePatchChanges (composedChanges))}
1905- )
1906- @changesSinceLastStoppedChangingEvent = []
1879+ compactedChanges = Object .freeze (normalizePatchChanges (
1880+ patchFromChanges (@changesSinceLastStoppedChangingEvent ).getChanges ()
1881+ ))
1882+ @changesSinceLastStoppedChangingEvent .length = 0
1883+ @emitter .emit (' did-stop-changing' , {changes : compactedChanges})
19071884 @ emitModifiedStatusChanged (modifiedStatus)
19081885
19091886 emitModifiedStatusChanged : (modifiedStatus ) ->
0 commit comments