Store line endings on a per-line basis in Buffer

The line ending for each line is recorded and reused
when lines are modified or inserted.

Closes #166
This commit is contained in:
Kevin Sawicki & Nathan Sobo
2013-01-27 15:45:53 -08:00
parent ecc50506c7
commit 98614592af
9 changed files with 108 additions and 33 deletions

View File

@@ -19,6 +19,7 @@ class Buffer
cachedMemoryContents: null
conflict: false
lines: null
lineEndings: null
file: null
anchors: null
anchorRanges: null
@@ -29,6 +30,7 @@ class Buffer
@anchors = []
@anchorRanges = []
@lines = ['']
@lineEndings = []
if path
throw "Path '#{path}' does not exist" unless fs.exists(path)
@@ -104,9 +106,10 @@ class Buffer
null
getText: ->
@cachedMemoryContents ?= @lines.join('\n')
@cachedMemoryContents ?= @getTextInRange(@getRange())
setText: (text) ->
@lineEndings = []
@change(@getRange(), text)
getRange: ->
@@ -118,12 +121,14 @@ class Buffer
return @lines[range.start.row][range.start.column...range.end.column]
multipleLines = []
multipleLines.push @lines[range.start.row][range.start.column..] # first line
multipleLines.push @lineForRow(range.start.row)[range.start.column..] # first line
multipleLines.push @lineEndingForRow(range.start.row)
for row in [range.start.row + 1...range.end.row]
multipleLines.push @lines[row] # middle lines
multipleLines.push @lines[range.end.row][0...range.end.column] # last line
multipleLines.push @lineForRow(row) # middle lines
multipleLines.push @lineEndingForRow(row)
multipleLines.push @lineForRow(range.end.row)[0...range.end.column] # last line
return multipleLines.join '\n'
return multipleLines.join ''
getLines: ->
@lines
@@ -131,6 +136,12 @@ class Buffer
lineForRow: (row) ->
@lines[row]
lineEndingForRow: (row) ->
@lineEndings[row] unless row is @getLastRow()
suggestedLineEndingForRow: (row) ->
@lineEndingForRow(row) ? @lineEndingForRow(row - 1)
lineLengthForRow: (row) ->
@lines[row].length
@@ -214,11 +225,6 @@ class Buffer
prefix: @lines[range.start.row][0...range.start.column]
suffix: @lines[range.end.row][range.end.column..]
replaceLines: (startRow, endRow, newLines) ->
@lines[startRow..endRow] = newLines
@cachedMemoryContents = null
@conflict = false if @conflict and !@isModified()
pushOperation: (operation, editSession) ->
if @undoManager
@undoManager.pushOperation(operation, editSession)