diff --git a/spec/atom/editor-spec.coffee b/spec/atom/editor-spec.coffee index a61862a0f..40bd8dc0f 100644 --- a/spec/atom/editor-spec.coffee +++ b/spec/atom/editor-spec.coffee @@ -218,7 +218,7 @@ describe "Editor", -> expect(editor).not.toMatchSelector ':focus' expect(editor.hiddenInput).toMatchSelector ':focus' - describe "when text input events are triggered on the hidden input element", -> + fdescribe "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.setPosition(row: 1, col: 6) @@ -230,3 +230,35 @@ describe "Editor", -> expect(editor.getPosition()).toEqual(row: 1, col: 7) expect(editor.lines.find('pre:eq(1)')).toHaveText editor.getCurrentLine() + fdescribe "when return is pressed", -> + describe "when the cursor is at the beginning of a line", -> + it "inserts an empty line before it", -> + editor.setPosition(row: 1, col: 0) + + editor.trigger keydownEvent('enter') + + expect(editor.lines.find('pre:eq(1)')).toHaveHtml ' ' + expect(editor.getPosition()).toEqual(row: 2, col: 0) + + describe "when the cursor is in the middle of a line", -> + it "splits the current line to form a new line", -> + editor.setPosition(row: 1, col: 6) + + originalLine = editor.lines.find('pre:eq(1)').text() + lineBelowOriginalLine = editor.lines.find('pre:eq(2)').text() + editor.trigger keydownEvent('enter') + + expect(editor.lines.find('pre:eq(1)')).toHaveText originalLine[0...6] + expect(editor.lines.find('pre:eq(2)')).toHaveText originalLine[6..] + expect(editor.lines.find('pre:eq(3)')).toHaveText lineBelowOriginalLine + expect(editor.getPosition()).toEqual(row: 2, col: 0) + + describe "when the cursor is on the end of a line", -> + it "inserts an empty line after it", -> + editor.setPosition(row: 1, col: buffer.getLine(1).length) + + editor.trigger keydownEvent('enter') + + expect(editor.lines.find('pre:eq(2)')).toHaveHtml ' ' + expect(editor.getPosition()).toEqual(row: 2, col: 0) + diff --git a/src/atom/cursor.coffee b/src/atom/cursor.coffee index b180790b0..2c2ed0f85 100644 --- a/src/atom/cursor.coffee +++ b/src/atom/cursor.coffee @@ -13,7 +13,10 @@ class Cursor extends Template setBuffer: (@buffer) -> @buffer.on 'insert', (e) => - @setColumn(@getColumn() + e.string.length) + if e.string == "\n" + @setPosition {row: @getRow() + 1, col: 0} + else + @setColumn(@getColumn() + e.string.length) setPosition: (point) -> @point = @editor.clipPosition(point) @@ -29,6 +32,9 @@ class Cursor extends Template getColumn: -> @getPosition().col + getRow: -> + @getPosition().row + moveUp: -> { row, col } = @getPosition() col = @goalColumn if @goalColumn? diff --git a/src/atom/editor.coffee b/src/atom/editor.coffee index ebf3a9f07..673748f9c 100644 --- a/src/atom/editor.coffee +++ b/src/atom/editor.coffee @@ -34,11 +34,13 @@ class Editor extends Template left: 'move-left' down: 'move-down' up: 'move-up' + enter: 'newline' @on 'move-right', => @moveRight() @on 'move-left', => @moveLeft() @on 'move-down', => @moveDown() @on 'move-up', => @moveUp() + @on 'newline', => @buffer.insert @getPosition(), "\n" handleEvents: -> @on 'focus', => @@ -52,18 +54,27 @@ class Editor extends Template @calculateDimensions() @focus() + buildLineElement: (lineText) -> + if lineText is '' + $$.pre -> @raw(' ') + else + $$.pre(lineText) + setBuffer: (@buffer) -> @lines.empty() for line in @buffer.getLines() - if line is '' - @lines.append $$.pre -> @raw(' ') - else - @lines.append $$.pre(line) + @lines.append @buildLineElement(line) + @setPosition(row: 0, col: 0) @cursor.setBuffer(@buffer) + @buffer.on 'insert', (e) => {row} = e.range.start - @lines.find('pre').eq(row).replaceWith $$.pre(@buffer.getLine(row)) + + updatedLine = @buildLineElement(@buffer.getLine(row)) + @lines.find('pre').eq(row).replaceWith(updatedLine) + if e.string == '\n' + updatedLine.after @buildLineElement(@buffer.getLine(row + 1)) clipPosition: ({row, col}) -> line = @buffer.getLine(row)