Merge adjacent isomorphic regions after adding new regions

This commit is contained in:
Nathan Sobo 2013-05-09 15:13:09 -06:00
parent d9c258f27e
commit b4c95d4fc9
2 changed files with 21 additions and 0 deletions

View File

@ -187,6 +187,13 @@ describe "RowMap", ->
expect(map.regions[2]).toEqual(bufferRows: 3, screenRows: 8)
expect(map.regions[3]).toEqual(bufferRows: 2, screenRows: 1)
it "merges adjacent isomorphic mappings", ->
map.mapBufferRowRange(2, 4, 1)
map.mapBufferRowRange(4, 5, 2)
map.mapBufferRowRange(1, 4, 3)
expect(map.regions).toEqual [{bufferRows: 5, screenRows: 5}]
describe ".applyBufferDelta(startBufferRow, delta)", ->
describe "when applying a positive delta", ->
it "expands the region containing the given start row by the given delta", ->

View File

@ -94,6 +94,20 @@ class RowMap
newRegions.push(bufferRows: overlapEndBufferRow - endBufferRow, screenRows: overlapEndScreenRow - endScreenRow)
@regions[overlapStartIndex..overlapEndIndex] = newRegions
@mergeIsomorphicRegions(Math.max(0, overlapStartIndex - 1), Math.min(@regions.length - 1, overlapEndIndex + 1))
mergeIsomorphicRegions: (startIndex, endIndex) ->
return if startIndex == endIndex
region = @regions[startIndex]
nextRegion = @regions[startIndex + 1]
if region.bufferRows == region.screenRows and nextRegion.bufferRows == nextRegion.screenRows
@regions[startIndex..startIndex + 1] =
bufferRows: region.bufferRows + nextRegion.bufferRows
screenRows: region.screenRows + nextRegion.screenRows
@mergeIsomorphicRegions(startIndex, endIndex - 1)
else
@mergeIsomorphicRegions(startIndex + 1, endIndex)
# This method records insertion or removal of rows in the buffer, adjusting the
# buffer dimension of regions following the start row accordingly.