Express LineMap insertion in terms of spliceByDelta

This commit is contained in:
Nathan Sobo
2012-02-29 13:57:57 -07:00
parent 63ea0baeee
commit 78c2ff7801
2 changed files with 56 additions and 55 deletions

View File

@@ -26,6 +26,15 @@ describe "LineMap", ->
expect(map.lineForScreenRow(3)).toEqual line3
expect(map.lineForScreenRow(4)).toEqual line4
it "allows for partial line fragments on the row following the insertion", ->
[line0a, line0b] = line0.splitAt(10)
map.insertAtBufferRow(0, [line0a, line0b])
map.insertAtBufferRow(0, [line1])
expect(map.lineForScreenRow(0)).toEqual line1
expect(map.lineForScreenRow(1)).toEqual line0a.concat(line0b)
describe ".spliceAtBufferRow(bufferRow, rowCount, screenLines)", ->
describe "when called with a row count of 0", ->
it "inserts the given line fragments before the specified buffer row", ->

View File

@@ -8,16 +8,7 @@ class LineMap
@lineFragments = []
insertAtBufferRow: (bufferRow, lineFragments) ->
delta = new Point
insertIndex = 0
for lineFragment in @lineFragments
nextDelta = delta.add(lineFragment.bufferDelta)
break if nextDelta.row > bufferRow
delta = nextDelta
insertIndex++
@lineFragments[insertIndex...insertIndex] = lineFragments
@spliceAtBufferRow(bufferRow, 0, lineFragments)
spliceAtBufferRow: (startRow, rowCount, lineFragments) ->
@spliceByDelta('bufferDelta', startRow, rowCount, lineFragments)
@@ -25,21 +16,6 @@ class LineMap
spliceAtScreenRow: (startRow, rowCount, lineFragments) ->
@spliceByDelta('screenDelta', startRow, rowCount, lineFragments)
spliceByDelta: (deltaType, startRow, rowCount, lineFragments) ->
stopRow = startRow + rowCount
startIndex = undefined
stopIndex = 0
delta = new Point
for lineFragment, i in @lineFragments
startIndex ?= i if delta.row == startRow
nextDelta = delta.add(lineFragment[deltaType])
break if nextDelta.row > stopRow
delta = nextDelta
stopIndex++
@lineFragments[startIndex...stopIndex] = lineFragments
replaceBufferRows: (start, end, lineFragments) ->
@spliceAtBufferRow(start, end - start + 1, lineFragments)
@@ -52,6 +28,52 @@ class LineMap
lineForScreenRow: (row) ->
@linesForScreenRows(row, row)[0]
bufferLineCount: ->
@bufferPositionForScreenPosition([Infinity, 0]).row
screenLineCount: ->
@screenPositionForBufferPosition([Infinity, 0]).row
lastScreenRow: ->
@screenLineCount() - 1
screenPositionForBufferPosition: (bufferPosition) ->
@translatePosition('bufferDelta', 'screenDelta', bufferPosition)
bufferPositionForScreenPosition: (screenPosition) ->
@translatePosition('screenDelta', 'bufferDelta', screenPosition)
screenRangeForBufferRange: (bufferRange) ->
start = @screenPositionForBufferPosition(bufferRange.start)
end = @screenPositionForBufferPosition(bufferRange.end)
new Range(start, end)
bufferRangeForScreenRange: (screenRange) ->
start = @bufferPositionForScreenPosition(screenRange.start)
end = @bufferPositionForScreenPosition(screenRange.end)
new Range(start, end)
logLines: (start=0, end=@screenLineCount() - 1)->
for row in [start..end]
line = @lineForScreenRow(row).text
console.log row, line, line.length
spliceByDelta: (deltaType, startRow, rowCount, lineFragments) ->
stopRow = startRow + rowCount
startIndex = undefined
stopIndex = 0
delta = new Point
for lineFragment, i in @lineFragments
startIndex ?= i if delta.row == startRow
break if rowCount == 0 and delta.row == stopRow
delta = delta.add(lineFragment[deltaType])
break if delta.row > stopRow
stopIndex++
startIndex ?= i
@lineFragments[startIndex...stopIndex] = lineFragments
linesForScreenRows: (startRow, endRow) ->
lines = []
delta = new Point
@@ -84,31 +106,6 @@ class LineMap
delta = delta.add(lineFragment.bufferDelta)
line
bufferLineCount: ->
@bufferPositionForScreenPosition([Infinity, 0]).row
screenLineCount: ->
@screenPositionForBufferPosition([Infinity, 0]).row
lastScreenRow: ->
@screenLineCount() - 1
screenPositionForBufferPosition: (bufferPosition) ->
@translatePosition('bufferDelta', 'screenDelta', bufferPosition)
bufferPositionForScreenPosition: (screenPosition) ->
@translatePosition('screenDelta', 'bufferDelta', screenPosition)
screenRangeForBufferRange: (bufferRange) ->
start = @screenPositionForBufferPosition(bufferRange.start)
end = @screenPositionForBufferPosition(bufferRange.end)
new Range(start, end)
bufferRangeForScreenRange: (screenRange) ->
start = @bufferPositionForScreenPosition(screenRange.start)
end = @bufferPositionForScreenPosition(screenRange.end)
new Range(start, end)
translatePosition: (sourceDeltaType, targetDeltaType, sourcePosition) ->
sourcePosition = Point.fromObject(sourcePosition)
sourceDelta = new Point
@@ -125,7 +122,6 @@ class LineMap
targetDelta
clipScreenPosition: (screenPosition, options) ->
wrapBeyondNewlines = options.wrapBeyondNewlines ? false
wrapAtSoftNewlines = options.wrapAtSoftNewlines ? false
@@ -166,7 +162,3 @@ class LineMap
new Point(screenDelta.row, Math.min(maxColumn, screenPosition.column))
logLines: (start=0, end=@screenLineCount() - 1)->
for row in [start..end]
line = @lineForScreenRow(row).text
console.log row, line, line.length