Skip to content

Instantly share code, notes, and snippets.

@nathansobo
Created August 5, 2014 18:51
Show Gist options
  • Select an option

  • Save nathansobo/c443d7ff0ae880a0b547 to your computer and use it in GitHub Desktop.

Select an option

Save nathansobo/c443d7ff0ae880a0b547 to your computer and use it in GitHub Desktop.
Splice-Based Line Updates
updateTileLines: (lines, tileStartRow, change) ->
tileSize = @getTileSize()
tileEndRow = tileStartRow + tileSize
changeStart = change.start
# Make change end-row exclusive to simplify logic below
changeOldEnd = change.end + 1
changeNewEnd = change.end + change.screenDelta + 1
# If the change starts past this tile, stop now
return lines if changeStart >= tileEndRow
if changeOldEnd < tileStartRow or changeNewEnd < tileStartRow
changeTranslation = tileStartRow - Math.min(changeOldEnd, changeNewEnd)
changeOldEnd += changeTranslation
changeNewEnd += changeTranslation
changeStart = Math.max(tileStartRow, changeStart)
changeOldEnd = Math.min(tileEndRow, changeOldEnd)
changeNewEnd = Math.min(tileEndRow, changeNewEnd)
console.log "splice"
if changeNewEnd > changeStart
newLines = @editor.linesForScreenRows(changeStart, changeNewEnd - 1)
lines = lines.splice(changeStart - tileStartRow, changeOldEnd - changeStart, newLines...)
else
lines = lines.splice(changeStart - tileStartRow, changeOldEnd - changeStart)
if lines.length > tileSize
console.log "splice"
lines = lines.splice(tileSize, lines.length - tileSize)
if lines.length < tileSize
console.log "splice"
lines = lines.splice(lines.length, 0, @editor.linesForScreenRows(tileStartRow + lines.length, tileEndRow - 1)...)
lines
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment