mirror of
https://github.com/atom/atom.git
synced 2026-02-05 04:05:05 -05:00
Inserting a mapping within an existing mapping preserves its shape
Inserting a mapping should never change the position of any existing mappings on screen or in the buffer. It's simply a statement about a range of rows in the buffer mapping to a range of existing rows on screen, but shouldn't add or remove any rows. Adding and removing rows on screen or in the buffer is the job of the applyBufferDelta and applyScreenDelta methods.
This commit is contained in:
@@ -11,6 +11,13 @@ class RowMap
|
||||
screenRow += targetBufferRow - bufferRow
|
||||
[screenRow, screenRow + 1]
|
||||
|
||||
bufferRowRangeForBufferRow: (targetBufferRow) ->
|
||||
{ mapping, screenRow, bufferRow } = @traverseToBufferRow(targetBufferRow)
|
||||
if mapping and mapping.bufferRows != mapping.screenRows # 1:n mapping
|
||||
[bufferRow, bufferRow + mapping.bufferRows]
|
||||
else # 1:1 mapping
|
||||
[targetBufferRow, targetBufferRow + 1]
|
||||
|
||||
bufferRowRangeForScreenRow: (targetScreenRow) ->
|
||||
{ mapping, screenRow, bufferRow } = @traverseToScreenRow(targetScreenRow)
|
||||
if mapping and mapping.bufferRows != mapping.screenRows # 1:n mapping
|
||||
@@ -20,24 +27,33 @@ class RowMap
|
||||
[bufferRow, bufferRow + 1]
|
||||
|
||||
mapBufferRowRange: (startBufferRow, endBufferRow, screenRows) ->
|
||||
{ mapping, index, bufferRow } = @traverseToBufferRow(startBufferRow)
|
||||
|
||||
if mapping and mapping.bufferRows != mapping.screenRows
|
||||
if bufferRow == startBufferRow and bufferRow + mapping.bufferRows == endBufferRow
|
||||
mapping.screenRows = screenRows
|
||||
return
|
||||
else
|
||||
throw new Error("Invalid mapping insertion")
|
||||
|
||||
padBefore = startBufferRow - bufferRow
|
||||
padAfter = (bufferRow + mapping?.bufferRows) - endBufferRow
|
||||
{ mapping, index, bufferRow, screenRow } = @traverseToBufferRow(startBufferRow)
|
||||
|
||||
newMappings = []
|
||||
newMappings.push(bufferRows: padBefore, screenRows: padBefore) if padBefore > 0
|
||||
newMappings.push(bufferRows: endBufferRow - startBufferRow, screenRows: screenRows)
|
||||
newMappings.push(bufferRows: padAfter, screenRows: padAfter) if padAfter > 0
|
||||
|
||||
preRows = startBufferRow - bufferRow
|
||||
if preRows > 0
|
||||
newMappings.push(bufferRows: preRows, screenRows: preRows)
|
||||
|
||||
bufferRows = endBufferRow - startBufferRow
|
||||
newMappings.push({bufferRows, screenRows})
|
||||
|
||||
if mapping
|
||||
postBufferRows = mapping.bufferRows - preRows - bufferRows
|
||||
postScreenRows = mapping.screenRows - preRows - screenRows
|
||||
if postBufferRows > 0 or postScreenRows > 0
|
||||
newMappings.push(bufferRows: postBufferRows, screenRows: postScreenRows)
|
||||
|
||||
@mappings[index..index] = newMappings
|
||||
|
||||
applyBufferDelta: (startBufferRow, delta) ->
|
||||
{ mapping } = @traverseToBufferRow(startBufferRow)
|
||||
mapping?.bufferRows += delta
|
||||
|
||||
applyScreenDelta: (startBufferRow, delta) ->
|
||||
{ mapping } = @traverseToScreenRow(startBufferRow)
|
||||
mapping?.screenRows += delta
|
||||
|
||||
traverseToBufferRow: (targetBufferRow) ->
|
||||
bufferRow = 0
|
||||
screenRow = 0
|
||||
|
||||
Reference in New Issue
Block a user