mirror of
https://github.com/atom/atom.git
synced 2026-02-07 13:14:55 -05:00
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:
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user