From 3b64b78336ad476f77122c7dc154256961b5829c Mon Sep 17 00:00:00 2001 From: Corey Johnson & Nathan Sobo Date: Fri, 27 Jan 2012 13:47:46 -0800 Subject: [PATCH] Backspace with a selection only deletes the selected text --- spec/atom/editor-spec.coffee | 27 +++++++++++++++++++-------- src/atom/point.coffee | 6 +++++- src/atom/selection.coffee | 13 +++++++------ 3 files changed, 31 insertions(+), 15 deletions(-) diff --git a/spec/atom/editor-spec.coffee b/spec/atom/editor-spec.coffee index b6c36a875..11bb7d71c 100644 --- a/spec/atom/editor-spec.coffee +++ b/spec/atom/editor-spec.coffee @@ -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 () {' diff --git a/src/atom/point.coffee b/src/atom/point.coffee index 0c98d0724..8e1076f9a 100644 --- a/src/atom/point.coffee +++ b/src/atom/point.coffee @@ -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) -> diff --git a/src/atom/selection.coffee b/src/atom/selection.coffee index 10d9984c3..897c180e3 100644 --- a/src/atom/selection.coffee +++ b/src/atom/selection.coffee @@ -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, '')