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

Commit ce1964f

Browse files
Antonio ScandurraNathan Sobo
authored andcommitted
Extract and use a patchFromChanges helper
Signed-off-by: Nathan Sobo <nathan@github.com>
1 parent 2e6f336 commit ce1964f

File tree

3 files changed

+32
-47
lines changed

3 files changed

+32
-47
lines changed

src/default-history-provider.coffee

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{Patch} = require 'superstring'
22
MarkerLayer = require './marker-layer'
33
{traversal} = require './point-helpers'
4+
{patchFromChanges} = require './helpers'
45

56
SerializationVersion = 6
67

@@ -395,11 +396,5 @@ snapshotFromTransaction = (transaction) ->
395396
}
396397

397398
transactionFromSnapshot = ({changes, markersBefore, markersAfter}) ->
398-
patch = new Patch
399-
for {oldStart, oldEnd, oldText, newStart, newEnd, newText} in changes by -1
400-
oldExtent = traversal(oldEnd, oldStart)
401-
newExtent = traversal(newEnd, newStart)
402-
patch.splice(oldStart, oldExtent, newExtent, oldText, newText)
403-
404399
# TODO: Return raw patch if there's no markersBefore && markersAfter
405-
new Transaction(markersBefore, patch, markersAfter)
400+
new Transaction(markersBefore, patchFromChanges(changes), markersAfter)

src/helpers.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
const {Patch} = require('superstring')
12
const Range = require('./range')
3+
const {traversal} = require('./point-helpers')
24

35
const NEWLINE_REGEX = /\n/g
46
const MULTI_LINE_REGEX_REGEX = /\\s|\\r|\\n|\r|\n|^\[\^|[^\\]\[\^/
@@ -52,6 +54,17 @@ exports.spliceArray = function (array, start, removedCount, insertedItems = [])
5254
}
5355
}
5456

57+
exports.patchFromChanges = function (changes) {
58+
const patch = new Patch()
59+
for (let i = 0; i < changes.length; i++) {
60+
const {oldStart, oldEnd, oldText, newStart, newEnd, newText} = changes[i]
61+
const oldExtent = traversal(oldEnd, oldStart)
62+
const newExtent = traversal(newEnd, newStart)
63+
patch.splice(newStart, oldExtent, newExtent, oldText, newText)
64+
}
65+
return patch
66+
}
67+
5568
exports.normalizePatchChanges = function (changes) {
5669
return changes.map((change) =>
5770
new TextChange(

src/text-buffer.coffee

Lines changed: 17 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ DefaultHistoryProvider = require './default-history-provider'
1313
MarkerLayer = require './marker-layer'
1414
MatchIterator = require './match-iterator'
1515
DisplayLayer = 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'
1818
Grim = require 'grim'
1919

2020
class 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

Comments
 (0)