Delete works with multiple cursors.

This commit is contained in:
Nathan Sobo 2012-03-26 19:39:09 -07:00
parent e6e24158ae
commit 29a2cc5886
5 changed files with 74 additions and 10 deletions

View File

@ -862,7 +862,6 @@ describe "Editor", ->
expect(editor.lineForBufferRow(1)).toBe " = functi"
expect(editor.lineForBufferRow(2)).toBe " () {"
describe "backspace", ->
describe "when cursors are on the same line", ->
it "removes the characters preceding each cursor", ->
@ -914,6 +913,64 @@ describe "Editor", ->
expect(cursor1.getBufferPosition()).toEqual [2,40]
expect(cursor2.getBufferPosition()).toEqual [4,30]
describe "when selections are on the same line", ->
it "removes all selected text", ->
editor.setSelectionBufferRange([[0,4], [0,13]])
editor.addSelectionForBufferRange([[0,22], [0,24]])
editor.backspace()
expect(editor.lineForBufferRow(0)).toBe 'var = functio () {'
describe "delete", ->
describe "when cursors are on the same line", ->
it "removes the characters following each cursor", ->
editor.setCursorScreenPosition([3, 13])
editor.addCursorAtScreenPosition([3, 38])
editor.delete()
expect(editor.lineForBufferRow(3)).toBe " var pivot= items.shift(), current left = [], right = [];"
[cursor1, cursor2] = editor.compositeCursor.getCursors()
expect(cursor1.getBufferPosition()).toEqual [3, 13]
expect(cursor2.getBufferPosition()).toEqual [3, 37]
[selection1, selection2] = editor.compositeSelection.getSelections()
expect(selection1.isEmpty()).toBeTruthy()
expect(selection2.isEmpty()).toBeTruthy()
describe "when cursors are on different lines", ->
it "removes the characters following each cursor", ->
editor.setCursorScreenPosition([3, 13])
editor.addCursorAtScreenPosition([4, 10])
editor.delete()
expect(editor.lineForBufferRow(3)).toBe " var pivot= items.shift(), current, left = [], right = [];"
expect(editor.lineForBufferRow(4)).toBe " while(tems.length > 0) {"
[cursor1, cursor2] = editor.compositeCursor.getCursors()
expect(cursor1.getBufferPosition()).toEqual [3, 13]
expect(cursor2.getBufferPosition()).toEqual [4, 10]
[selection1, selection2] = editor.compositeSelection.getSelections()
expect(selection1.isEmpty()).toBeTruthy()
expect(selection2.isEmpty()).toBeTruthy()
describe "when deleting over newlines", ->
it "removes the newlines following each cursor", ->
editor.setCursorScreenPosition([0, 29])
editor.addCursorAtScreenPosition([1, 30])
editor.delete()
expect(editor.lineForBufferRow(0)).toBe "var quicksort = function () { var sort = function(items) { if (items.length <= 1) return items;"
[cursor1, cursor2] = editor.compositeCursor.getCursors()
expect(cursor1.getBufferPosition()).toEqual [0,29]
expect(cursor2.getBufferPosition()).toEqual [0,59]
describe "keyboard movement", ->
it "moves all cursors", ->
editor.setCursorScreenPosition([3, 13])

View File

@ -20,28 +20,28 @@ describe "Selection", ->
expect(selection.anchorScreenPosition).toEqual range.start
expect(selection.cursor.getScreenPosition()).toEqual range.end
describe ".delete()", ->
describe ".deleteSelectedText()", ->
describe "when nothing is selected", ->
it "deletes nothing", ->
selection.setBufferRange new Range([0,3], [0,3])
selection.delete()
selection.deleteSelectedText()
expect(editor.buffer.lineForRow(0)).toBe "var quicksort = function () {"
describe "when one line is selected", ->
it "deletes selected text", ->
selection.setBufferRange new Range([0,4], [0,14])
selection.delete()
selection.deleteSelectedText()
expect(editor.buffer.lineForRow(0)).toBe "var = function () {"
endOfLine = editor.buffer.lineForRow(0).length
selection.setBufferRange new Range([0,0], [0, endOfLine])
selection.delete()
selection.deleteSelectedText()
expect(editor.buffer.lineForRow(0)).toBe ""
describe "when multiple lines are selected", ->
it "deletes selected text", ->
selection.setBufferRange new Range([0,1], [2,39])
selection.delete()
selection.deleteSelectedText()
expect(editor.buffer.lineForRow(0)).toBe "v;"
describe ".updateAppearence()", ->

View File

@ -38,6 +38,10 @@ class CompositeSeleciton
for selection in @getSelections()
selection.backspace()
delete: ->
for selection in @getSelections()
selection.delete()
selectToScreenPosition: (position) ->
@lastSelection().selectToScreenPosition(position)

View File

@ -386,8 +386,7 @@ class Editor extends View
@compositeSelection.backspace()
delete: ->
@selectRight() if @getSelection().isEmpty()
@getSelection().delete()
@compositeSelection.delete()
undo: ->
@buffer.undo()
@ -442,4 +441,4 @@ class Editor extends View
@renderer.lineForRow(row).state
getCurrentMode: ->
@buffer.getMode()
@buffer.getMode()

View File

@ -121,10 +121,14 @@ class Selection extends View
backspace: ->
@selectLeft() if @isEmpty()
@delete()
@deleteSelectedText()
@clearSelection()
delete: ->
@selectRight() if @isEmpty()
@deleteSelectedText()
deleteSelectedText: ->
range = @getBufferRange()
@editor.buffer.delete(range) unless range.isEmpty()