From b4c95d4fc9c4e5fb914fc38df4922c985912cfa3 Mon Sep 17 00:00:00 2001 From: Nathan Sobo Date: Thu, 9 May 2013 15:13:09 -0600 Subject: [PATCH] Merge adjacent isomorphic regions after adding new regions --- spec/app/row-map-spec.coffee | 7 +++++++ src/app/row-map.coffee | 14 ++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/spec/app/row-map-spec.coffee b/spec/app/row-map-spec.coffee index 202d8c4ba..aca260928 100644 --- a/spec/app/row-map-spec.coffee +++ b/spec/app/row-map-spec.coffee @@ -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", -> diff --git a/src/app/row-map.coffee b/src/app/row-map.coffee index 3e4f533a4..5c8290014 100644 --- a/src/app/row-map.coffee +++ b/src/app/row-map.coffee @@ -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.