Scan buffer to remove trailing whitespace

This commit is contained in:
Kevin Sawicki
2014-10-16 10:50:21 -07:00
parent ffa528001c
commit f94983d4d9
2 changed files with 23 additions and 11 deletions

View File

@@ -3414,7 +3414,7 @@ describe "TextEditor", ->
it "joins the line below with the current line separated by a space and moves the cursor to the start of line that was moved up", ->
editor.joinLines()
expect(editor.lineTextForBufferRow(0)).toBe 'var quicksort = function () { var sort = function(items) {'
expect(editor.getCursorBufferPosition()).toEqual [0, 30]
expect(editor.getCursorBufferPosition()).toEqual [0, 29]
describe "when the line below is empty", ->
it "deletes the line below and moves the cursor to the end of the line", ->

View File

@@ -476,25 +476,37 @@ class Selection extends Model
for row in [0...rowCount]
@cursor.setBufferPosition([selectedRange.start.row])
@cursor.moveToEndOfLine()
# Remove traing whitespace from line
scanRange = @cursor.getCurrentLineBufferRange()
trailingWhitespaceRange = null
@editor.scanInBufferRange /[ \t]+$/, scanRange, ({range}) ->
trailingWhitespaceRange = range
if trailingWhitespaceRange?
@setBufferRange(trailingWhitespaceRange)
@deleteSelectedText()
nextRow = selectedRange.start.row + 1
if nextRow <= @editor.buffer.getLastRow() and @editor.buffer.lineLengthForRow(nextRow) > 0
insertSpace = nextRow <= @editor.buffer.getLastRow() and
@editor.buffer.lineLengthForRow(nextRow) > 0 and
@editor.buffer.lineLengthForRow(selectedRange.start.row) > 0
if insertSpace
@insertText(' ')
@cursor.moveToEndOfLine()
@selectToPreviousWordBoundary()
@deleteSelectedText()
@modifySelection =>
@cursor.moveRight()
@cursor.moveToFirstCharacterOfLine()
@deleteSelectedText()
@insertText(' ')
@modifySelection =>
@cursor.moveRight()
@cursor.moveToFirstCharacterOfLine()
@deleteSelectedText()
@cursor.moveLeft() if insertSpace
if joinMarker?
newSelectedRange = joinMarker.getBufferRange()
@setBufferRange(newSelectedRange)
joinMarker.destroy()
@cursor.moveLeft()
# Public: Removes one level of indent from the currently selected rows.
outdentSelectedRows: ->
[start, end] = @getBufferRowRange()