Backspace with a selection only deletes the selected text

This commit is contained in:
Corey Johnson & Nathan Sobo 2012-01-27 13:47:46 -08:00
parent fbd4b0b78a
commit 3b64b78336
3 changed files with 31 additions and 15 deletions

View File

@ -280,18 +280,24 @@ describe "Editor", ->
editor.trigger keydownEvent('down')
expect(selection.isEmpty()).toBeTruthy()
describe "when text input events are triggered on the hidden input element", ->
it "inserts the typed character at the cursor position, both in the buffer and the pre element", ->
editor.setCursorPosition(row: 1, column: 6)
describe "when there is no selection", ->
it "inserts the typed character at the cursor position, both in the buffer and the pre element", ->
editor.setCursorPosition(row: 1, column: 6)
expect(editor.getCurrentLine().charAt(6)).not.toBe 'q'
expect(editor.getCurrentLine().charAt(6)).not.toBe 'q'
editor.hiddenInput.textInput 'q'
editor.hiddenInput.textInput 'q'
expect(editor.getCurrentLine().charAt(6)).toBe 'q'
expect(editor.getCursorPosition()).toEqual(row: 1, column: 7)
expect(editor.lines.find('pre:eq(1)')).toHaveText editor.getCurrentLine()
expect(editor.getCurrentLine().charAt(6)).toBe 'q'
expect(editor.getCursorPosition()).toEqual(row: 1, column: 7)
expect(editor.lines.find('pre:eq(1)')).toHaveText editor.getCurrentLine()
fdescribe "when there is a selection", ->
it "replaces the selected text with the typed text", ->
editor.selection.setRange(new Range([1, 6], [2, 4]))
editor.hiddenInput.textInput 'q'
expect(buffer.getLine(1)).toBe ' var qif (items.length <= 1) return items;'
describe "when return is pressed", ->
describe "when the cursor is at the beginning of a line", ->
@ -361,4 +367,9 @@ describe "Editor", ->
editor.setCursorPosition(row: 0, column: 0)
editor.trigger keydownEvent('backspace')
fdescribe "when there is a selection", ->
it "deletes the selection, but not the character before it", ->
editor.selection.setRange(new Range([0,5], [0,9]))
editor.trigger keydownEvent('backspace')
expect(editor.buffer.getLine(0)).toBe 'var qsort = function () {'

View File

@ -4,7 +4,11 @@ class Point
if object instanceof Point
object
else
{ row, column } = object
if object instanceof Array
[row, column] = object
else
{ row, column } = object
new Point(row, column)
constructor: (@row, @column) ->

View File

@ -87,12 +87,13 @@ class Selection extends Template
backspace: ->
range = @getRange()
if range.start.column == 0
return if range.start.row == 0
range.start.column = @editor.buffer.lines[range.start.row - 1].length
range.start.row--
else
range.start.column--
if range.isEmpty()
if range.start.column == 0
return if range.start.row == 0
range.start.column = @editor.buffer.lines[range.start.row - 1].length
range.start.row--
else
range.start.column--
@editor.buffer.change(range, '')