diff --git a/spec/text-editor-spec.coffee b/spec/text-editor-spec.coffee index 98f1bbef8..4d4e10940 100644 --- a/spec/text-editor-spec.coffee +++ b/spec/text-editor-spec.coffee @@ -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", -> diff --git a/src/selection.coffee b/src/selection.coffee index a1b9fe83f..0f65bf591 100644 --- a/src/selection.coffee +++ b/src/selection.coffee @@ -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()