Delete removes character in front of cursor.

This commit is contained in:
Corey Johnson & Nathan Sobo
2012-01-27 14:16:17 -08:00
parent 3b64b78336
commit 900f745b65
3 changed files with 44 additions and 3 deletions

View File

@@ -293,7 +293,7 @@ describe "Editor", ->
expect(editor.getCursorPosition()).toEqual(row: 1, column: 7)
expect(editor.lines.find('pre:eq(1)')).toHaveText editor.getCurrentLine()
fdescribe "when there is a selection", ->
describe "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'
@@ -367,9 +367,34 @@ describe "Editor", ->
editor.setCursorPosition(row: 0, column: 0)
editor.trigger keydownEvent('backspace')
fdescribe "when there is a selection", ->
describe "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 () {'
describe "when delete is pressed", ->
describe "when the cursor is on the middle of a line", ->
it "deletes the character following the cursor", ->
editor.setCursorPosition([1, 6])
editor.trigger keydownEvent('delete')
expect(buffer.getLine(1)).toBe ' var ort = function(items) {'
describe "when the cursor is on the end of a line", ->
it "joins the line with the following line", ->
editor.setCursorPosition([1, buffer.getLine(1).length])
editor.trigger keydownEvent('delete')
expect(buffer.getLine(1)).toBe ' var sort = function(items) { if (items.length <= 1) return items;'
describe "when there is a selection", ->
it "deletes the selection, but not the character following it", ->
editor.selection.setRange(new Range([1,6], [1,8]))
editor.trigger keydownEvent 'delete'
expect(buffer.getLine(1)).toBe ' var rt = function(items) {'
describe "when the cursor is on the last column of the last line", ->
it "does nothing, but doesn't raise an error", ->
editor.setCursorPosition([12, buffer.getLine(12).length])
editor.trigger keydownEvent('delete')
expect(buffer.getLine(12)).toBe '};'

View File

@@ -39,6 +39,7 @@ class Editor extends Template
'shift-down': 'select-down'
enter: 'newline'
backspace: 'backspace'
delete: 'delete'
@on 'move-right', => @moveCursorRight()
@on 'move-left', => @moveCursorLeft()
@@ -50,6 +51,7 @@ class Editor extends Template
@on 'select-down', => @selectDown()
@on 'newline', => @insertNewline()
@on 'backspace', => @backspace()
@on 'delete', => @delete()
buildCursorAndSelection: ->
@@ -158,4 +160,5 @@ class Editor extends Template
insertText: (text) -> @selection.insertText(text)
insertNewline: -> @selection.insertNewline()
backspace: -> @selection.backspace()
delete: -> @selection.delete()

View File

@@ -90,13 +90,26 @@ class Selection extends Template
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.column = @editor.buffer.getLine(range.start.row - 1).length
range.start.row--
else
range.start.column--
@editor.buffer.change(range, '')
delete: ->
range = @getRange()
if range.isEmpty()
if range.end.column == @editor.buffer.getLine(range.end.row).length
return if range.end.row == @editor.buffer.numLines() - 1
range.end.column = 0
range.end.row++
else
range.end.column++
@editor.buffer.change(range, '')
isEmpty: ->
@getRange().isEmpty()