Normalize line endings in TextBuffer.change()

This commit is contained in:
Kevin Sawicki & Nathan Sobo 2013-07-01 20:06:21 -06:00 committed by Kevin Sawicki
parent 72f9af4d00
commit 05a3f35512
4 changed files with 20 additions and 6 deletions

View File

@ -1255,11 +1255,14 @@ describe 'TextBuffer', ->
describe "line ending support", ->
describe ".lineEndingForRow(line)", ->
it "return the line ending for each buffer line", ->
buffer.setText("a\r\nb\nc")
it "returns the line ending for each buffer line", ->
buffer.setText("a\r\nb\nc\r\nd")
expect(buffer.lineEndingForRow(0)).toBe '\r\n'
expect(buffer.lineEndingForRow(1)).toBe '\n'
expect(buffer.lineEndingForRow(2)).toBeUndefined()
expect(buffer.lineEndingForRow(2)).toBe '\r\n'
expect(buffer.lineEndingForRow(3)).toBeUndefined()
buffer.setText("abc\r")
expect(buffer.lineEndingForRow(0)).toBeUndefined()
describe ".lineForRow(line)", ->
it "returns the line text without the line ending for both lf and crlf lines", ->
@ -1267,6 +1270,8 @@ describe 'TextBuffer', ->
expect(buffer.lineForRow(0)).toBe 'a'
expect(buffer.lineForRow(1)).toBe 'b'
expect(buffer.lineForRow(2)).toBe 'c'
buffer.setText("abc\r")
expect(buffer.lineForRow(0)).toBe 'abc\r'
describe ".getText()", ->
it "returns the text with the corrent line endings for each row", ->

View File

@ -1245,6 +1245,7 @@ class Editor extends View
)
intactRanges = newIntactRanges
@pendingChanges = []
intactRanges
truncateIntactRanges: (intactRanges, renderFrom, renderTo) ->

View File

@ -191,7 +191,8 @@ class TextBuffer
#
# Returns a new {Range}, from `[0, 0]` to the end of the buffer.
getRange: ->
new Range([0, 0], [@getLastRow(), @getLastLine().length])
lastRow = @getLastRow()
new Range([0, 0], [lastRow, @lineLengthForRow(lastRow)])
# Given a range, returns the lines of text within it.
#
@ -653,12 +654,19 @@ class TextBuffer
abort: -> @undoManager.abort()
change: (oldRange, newText, options) ->
change: (oldRange, newText, options={}) ->
oldRange = Range.fromObject(oldRange)
newText = @normalizeLineEndings(oldRange.start.row, newText) if options.normalizeLineEndings ? true
operation = new BufferChangeOperation({buffer: this, oldRange, newText, options})
range = @pushOperation(operation)
range
normalizeLineEndings: (startRow, text) ->
if lineEnding = @suggestedLineEndingForRow(startRow)
text.replace(/\r?\n/g, lineEnding)
else
text
destroyMarker: (id) ->
if marker = @validMarkers[id] ? @invalidMarkers[id]
delete @validMarkers[id]

2
vendor/telepath vendored

@ -1 +1 @@
Subproject commit 6b7634edceb809a08c27aee11bf3bfd3b89a3c33
Subproject commit 9aa08223c4d0e2203e81a66a774eb271bf62ad3e