diff --git a/spec/text-editor-spec.coffee b/spec/text-editor-spec.coffee index 261d311c1..4de7168b7 100644 --- a/spec/text-editor-spec.coffee +++ b/spec/text-editor-spec.coffee @@ -4565,7 +4565,9 @@ describe "TextEditor", -> it "moves one active selection on one line one column to the left", -> editor.setSelectedBufferRange [[0, 4], [0, 13]] expect(editor.getSelectedText()).toBe 'quicksort' + editor.moveSelectionLeft() + expect(editor.getSelectedText()).toBe 'quicksort' expect(editor.getSelectedBufferRange()).toEqual [[0, 3], [0, 12]] @@ -4575,7 +4577,9 @@ describe "TextEditor", -> expect(selections[0].getText()).toBe 'quicksort' expect(selections[1].getText()).toBe 'function' + editor.moveSelectionLeft() + expect(selections[0].getText()).toBe 'quicksort' expect(selections[1].getText()).toBe 'function' expect(editor.getSelectedBufferRanges()).toEqual [[[0, 3], [0, 12]], [[0, 15], [0, 23]]] @@ -4586,7 +4590,9 @@ describe "TextEditor", -> expect(selections[0].getText()).toBe 'quicksort' expect(selections[1].getText()).toBe 'sort' + editor.moveSelectionLeft() + expect(selections[0].getText()).toBe 'quicksort' expect(selections[1].getText()).toBe 'sort' expect(editor.getSelectedBufferRanges()).toEqual [[[0, 3], [0, 12]], [[1, 5], [1, 9]]] @@ -4598,17 +4604,35 @@ describe "TextEditor", -> expect(selections[0].getText()).toBe 'var' expect(selections[1].getText()).toBe ' v' + editor.moveSelectionLeft() editor.moveSelectionLeft() + expect(selections[0].getText()).toBe 'var' expect(selections[1].getText()).toBe ' v' expect(editor.getSelectedBufferRanges()).toEqual [[[0, 0], [0, 3]], [[1, 0], [1, 3]]] + describe "when multiple selections are active on one line", -> + it "does not change the selection", -> + editor.setSelectedBufferRanges([[[0, 0], [0, 3]], [[0, 4], [0, 13]]]) + selections = editor.getSelections() + + expect(selections[0].getText()).toBe 'var' + expect(selections[1].getText()).toBe 'quicksort' + + editor.moveSelectionLeft() + + expect(selections[0].getText()).toBe 'var' + expect(selections[1].getText()).toBe 'quicksort' + expect(editor.getSelectedBufferRanges()).toEqual [[[0, 0], [0, 3]], [[0, 4], [0, 13]]] + describe ".moveSelectionRight()", -> it "moves one active selection on one line one column to the right", -> editor.setSelectedBufferRange [[0, 4], [0, 13]] expect(editor.getSelectedText()).toBe 'quicksort' + editor.moveSelectionRight() + expect(editor.getSelectedText()).toBe 'quicksort' expect(editor.getSelectedBufferRange()).toEqual [[0, 5], [0, 14]] @@ -4618,7 +4642,9 @@ describe "TextEditor", -> expect(selections[0].getText()).toBe 'quicksort' expect(selections[1].getText()).toBe 'function' + editor.moveSelectionRight() + expect(selections[0].getText()).toBe 'quicksort' expect(selections[1].getText()).toBe 'function' expect(editor.getSelectedBufferRanges()).toEqual [[[0, 5], [0, 14]], [[0, 17], [0, 25]]] @@ -4629,7 +4655,9 @@ describe "TextEditor", -> expect(selections[0].getText()).toBe 'quicksort' expect(selections[1].getText()).toBe 'sort' + editor.moveSelectionRight() + expect(selections[0].getText()).toBe 'quicksort' expect(selections[1].getText()).toBe 'sort' expect(editor.getSelectedBufferRanges()).toEqual [[[0, 5], [0, 14]], [[1, 7], [1, 11]]] @@ -4641,12 +4669,28 @@ describe "TextEditor", -> expect(selections[0].getText()).toBe 'items;' expect(selections[1].getText()).toBe 'shift();' + editor.moveSelectionRight() editor.moveSelectionRight() + expect(selections[0].getText()).toBe 'items;' expect(selections[1].getText()).toBe 'shift();' expect(editor.getSelectedBufferRanges()).toEqual [[[2, 34], [2, 40]], [[5, 22], [5, 30]]] + describe "when multiple selections are active on one line", -> + it "does not change the selection", -> + editor.setSelectedBufferRanges([[[2, 27], [2, 33]], [[2, 34], [2, 40]]]) + selections = editor.getSelections() + + expect(selections[0].getText()).toBe 'return' + expect(selections[1].getText()).toBe 'items;' + + editor.moveSelectionRight() + + expect(selections[0].getText()).toBe 'return' + expect(selections[1].getText()).toBe 'items;' + expect(editor.getSelectedBufferRanges()).toEqual [[[2, 27], [2, 33]], [[2, 34], [2, 40]]] + describe 'reading text', -> it '.lineTextForScreenRow(row)', -> editor.foldBufferRow(4) diff --git a/src/text-editor.coffee b/src/text-editor.coffee index 6f075b579..b6be33071 100644 --- a/src/text-editor.coffee +++ b/src/text-editor.coffee @@ -1077,46 +1077,46 @@ class TextEditor extends Model # Move any active selections one column to the left. moveSelectionLeft: -> selections = @getSelectedBufferRanges() + noSelectionAtStartOfLine = selections.every((selection) -> + selection.start.column isnt 0 + ) translationDelta = [0, -1] translatedRanges = [] @transact => - for selection in selections - charToLeftOfSelection = new Range(selection.start.translate(translationDelta), selection.start) - - if charToLeftOfSelection.start.column < 0 - translatedRanges.push(selection) - else + if noSelectionAtStartOfLine + for selection in selections + charToLeftOfSelection = new Range(selection.start.translate(translationDelta), selection.start) charTextToLeftOfSelection = @buffer.getTextInRange(charToLeftOfSelection) @buffer.insert(selection.end, charTextToLeftOfSelection) @buffer.delete(charToLeftOfSelection) translatedRanges.push(selection.translate(translationDelta)) - @setSelectedBufferRanges(translatedRanges) + @setSelectedBufferRanges(translatedRanges) # Move any active selections one column to the right. moveSelectionRight: -> selections = @getSelectedBufferRanges() + noSelectionAtEndOfLine = selections.every((selection) => + selection.end.column isnt @buffer.lineLengthForRow(selection.end.row) + ) translationDelta = [0, 1] translatedRanges = [] @transact => - for selection in selections - charToRightOfSelection = new Range(selection.end, selection.end.translate(translationDelta)) - - if charToRightOfSelection.end.column > @buffer.lineLengthForRow(charToRightOfSelection.end.row) - translatedRanges.push(selection) - else + if noSelectionAtEndOfLine + for selection in selections + charToRightOfSelection = new Range(selection.end, selection.end.translate(translationDelta)) charTextToRightOfSelection = @buffer.getTextInRange(charToRightOfSelection) @buffer.delete(charToRightOfSelection) @buffer.insert(selection.start, charTextToRightOfSelection) translatedRanges.push(selection.translate(translationDelta)) - @setSelectedBufferRanges(translatedRanges) + @setSelectedBufferRanges(translatedRanges) # Duplicate the most recent cursor's current line. duplicateLines: ->