Restore previous selections after tailing newlines are added

Closes #496
This commit is contained in:
Corey Johnson & Nathan Sobo
2013-04-16 14:06:34 -07:00
committed by Corey Johnson
parent f21571eab1
commit ca49d0714c
2 changed files with 12 additions and 2 deletions

View File

@@ -1,11 +1,12 @@
module.exports =
activate: ->
rootView.eachBuffer (buffer) => @whitespaceBeforeSave(buffer)
rootView.eachEditSession (editSession) => @whitespaceBeforeSave(editSession)
configDefaults:
ensureSingleTrailingNewline: true
whitespaceBeforeSave: (buffer) ->
whitespaceBeforeSave: (editSession) ->
buffer = editSession.buffer
buffer.on 'will-be-saved', ->
buffer.transact ->
buffer.scan /[ \t]+$/g, ({replace}) -> replace('')
@@ -16,4 +17,6 @@ module.exports =
while row and buffer.lineForRow(row) is ''
buffer.deleteRow(row--)
else
selectedBufferRanges = editSession.getSelectedBufferRanges()
buffer.append('\n')
editSession.setSelectedBufferRanges(selectedBufferRanges)

View File

@@ -79,3 +79,10 @@ describe "Whitespace", ->
editor.insertText "no trailing newline"
editor.getBuffer().save()
expect(editor.getText()).toBe "no trailing newline"
it "does not move the cursor when the new line is added", ->
editor.insertText "foo"
expect(editor.getCursorBufferPosition()).toEqual([0,3])
editor.getBuffer().save()
expect(editor.getText()).toBe "foo\n"
expect(editor.getCursorBufferPosition()).toEqual([0,3])