Skip to content
This repository was archived by the owner on Dec 15, 2022. It is now read-only.

Create methods for getting and setting preferred line endings #91

Merged
merged 2 commits into from
Aug 13, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions spec/text-buffer-spec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -2245,6 +2245,30 @@ describe "TextBuffer", ->
buffer.append("hello\n1\r\n2\n")
expect(buffer.getText()).toBe "\ninitialtexthello\n1\n2\n"

describe "::setPreferredLineEnding(lineEnding)", ->
it "uses the given line ending when normalizing, rather than inferring one from the surrounding text", ->
buffer = new TextBuffer(text: "a \r\n")

expect(buffer.getPreferredLineEnding()).toBe null
buffer.append(" b \n")
expect(buffer.getText()).toBe "a \r\n b \r\n"

buffer.setPreferredLineEnding("\n")
expect(buffer.getPreferredLineEnding()).toBe "\n"
buffer.append(" c \n")
expect(buffer.getText()).toBe "a \r\n b \r\n c \n"

buffer.setPreferredLineEnding(null)
buffer.append(" d \r\n")
expect(buffer.getText()).toBe "a \r\n b \r\n c \n d \n"

it "persists across serialization and deserialization", ->
bufferA = new TextBuffer
bufferA.setPreferredLineEnding("\r\n")

bufferB = TextBuffer.deserialize(bufferA.serialize())
expect(bufferB.getPreferredLineEnding()).toBe "\r\n"

describe "character set encoding support", ->
it "allows the encoding to be set on creation", ->
filePath = join(__dirname, 'fixtures', 'win1251.txt')
Expand Down
12 changes: 10 additions & 2 deletions src/text-buffer.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ class TextBuffer
@history = params?.history ? new History(this)
@markerStore = params?.markerStore ? new MarkerStore(this)
@setEncoding(params?.encoding)
@setPreferredLineEnding(params?.preferredLineEnding)

@loaded = false
@transactCallDepth = 0
Expand All @@ -125,6 +126,7 @@ class TextBuffer
filePath: @getPath()
modifiedWhenLastPersisted: @isModified()
digestWhenLastPersisted: @file?.getDigestSync()
preferredLineEnding: @preferredLineEnding

###
Section: Event Subscription
Expand Down Expand Up @@ -392,6 +394,12 @@ class TextBuffer
# Public: Returns the {String} encoding of this buffer.
getEncoding: -> @encoding ? @file?.getEncoding()

setPreferredLineEnding: (preferredLineEnding=null) ->
@preferredLineEnding = preferredLineEnding

getPreferredLineEnding: ->
@preferredLineEnding

# Public: Get the path of the associated file.
#
# Returns a {String}.
Expand Down Expand Up @@ -652,8 +660,8 @@ class TextBuffer

# Determine how to normalize the line endings of inserted text if enabled
if normalizeLineEndings
normalizedEnding = @lineEndingForRow(startRow)
if normalizedEnding is ''
normalizedEnding = @preferredLineEnding ? @lineEndingForRow(startRow)
unless normalizedEnding
if startRow > 0
normalizedEnding = @lineEndingForRow(startRow - 1)
else
Expand Down