diff --git a/.pairs b/.pairs index eadca60a3..82176863a 100644 --- a/.pairs +++ b/.pairs @@ -1,6 +1,7 @@ pairs: ns: Nathan Sobo; nathan cj: Corey Johnson; cj + dg: Danny Greg; danny email: domain: github.com #global: true diff --git a/spec/atom/editor-spec.coffee b/spec/atom/editor-spec.coffee index 2c7537fad..7c5a0e522 100644 --- a/spec/atom/editor-spec.coffee +++ b/spec/atom/editor-spec.coffee @@ -3,28 +3,55 @@ Editor = require 'editor' $ = require 'jquery' fs = require 'fs' -describe "Editor", -> +fdescribe "Editor", -> buffer = null editor = null beforeEach -> buffer = new Buffer(require.resolve('fixtures/sample.js')) editor = Editor.build() + editor.enableKeymap() + editor.setBuffer(buffer) describe ".setBuffer", -> + beforeEach -> + it "creates a pre element for each line in the buffer", -> - editor.setBuffer(buffer) expect(editor.lines.find('pre').length).toEqual(buffer.numLines()) it "sets the cursor to the beginning of the file", -> expect(editor.getPosition()).toEqual(row: 0, col: 0) + describe "cursor movement", -> + it "moves the cursor when arrow keys are pressed", -> + editor.trigger keydownEvent('right') + expect(editor.getPosition()).toEqual(row: 0, col: 1) + + editor.trigger keydownEvent('down') + expect(editor.getPosition()).toEqual(row: 1, col: 1) + + editor.trigger keydownEvent('left') + expect(editor.getPosition()).toEqual(row: 1, col: 0) + + editor.trigger keydownEvent('up') + expect(editor.getPosition()).toEqual(row: 0, col: 0) + + describe ".setPosition({row, col})", -> it "moves the cursor to cover the character at the given row and column", -> editor.attachToDom() - editor.setBuffer(buffer) editor.setPosition(row: 2, col: 2) expect(editor.cursor.position().top).toBe(2 * editor.lineHeight()) expect(editor.cursor.position().left).toBe(2 * editor.charWidth()) + + describe "when the editor is attached to the dom", -> + it "updates the pixel position of the cursor", -> + editor.setPosition(row: 2, col: 2) + + editor.attachToDom() + + expect(editor.cursor.position().top).toBe(2 * editor.lineHeight()) + expect(editor.cursor.position().left).toBe(2 * editor.charWidth()) + diff --git a/spec/spec-helper.coffee b/spec/spec-helper.coffee index 7dbbdf122..f0f6f1ffe 100644 --- a/spec/spec-helper.coffee +++ b/spec/spec-helper.coffee @@ -34,6 +34,5 @@ $.fn.enableKeymap = -> @on 'keydown', (e) => atom.globalKeymap.handleKeyEvent(e) $.fn.attachToDom = -> - console.log this $('#jasmine-content').append(this) diff --git a/src/atom/cursor.coffee b/src/atom/cursor.coffee index 11e36f916..347a09757 100644 --- a/src/atom/cursor.coffee +++ b/src/atom/cursor.coffee @@ -7,8 +7,12 @@ class Cursor extends Template viewProperties: setPosition: (@_position) -> - @css(@parentView.toPixelPosition(@_position)) + @updateAbsolutePosition() getPosition: -> @_position + updateAbsolutePosition: -> + position = @parentView.toPixelPosition(@_position) + @css(position) + diff --git a/src/atom/editor.coffee b/src/atom/editor.coffee index 814379c46..0c17c6841 100644 --- a/src/atom/editor.coffee +++ b/src/atom/editor.coffee @@ -7,8 +7,8 @@ _ = require 'underscore' module.exports = class Editor extends Template content: -> - @div class: 'editor', => - @style @div outlet: 'lines' + @div class: 'editor', tabindex: -1, => + @div outlet: 'lines' @subview 'cursor', Cursor.build() viewProperties: @@ -17,13 +17,43 @@ class Editor extends Template initialize: () -> requireStylesheet 'editor.css' @setBuffer(new Buffer) - @one 'attach', => @calculateDimensions() + + atom.bindKeys '*', + right: 'move-right' + left: 'move-left' + down: 'move-down' + up: 'move-up' + + @on 'move-right', => @moveRight() + @on 'move-left', => @moveLeft() + @on 'move-down', => @moveDown() + @on 'move-up', => @moveUp() + + @one 'attach', => + @calculateDimensions() + + + moveRight: -> + { row, col } = @getPosition() + @setPosition({row, col: col + 1}) + + moveDown: -> + { row, col } = @getPosition() + @setPosition({row: row + 1, col}) + + moveLeft: -> + { row, col } = @getPosition() + @setPosition({row, col: col - 1}) + + moveUp: -> + { row, col } = @getPosition() + @setPosition({row: row - 1, col}) setBuffer: (@buffer) -> @lines.empty() for line in @buffer.getLines() @lines.append "
#{line}
" - _.defer => @setPosition(row: 3, col: 4) + @setPosition(row: 0, col: 0) setPosition: (position) -> @cursor.setPosition(position) @@ -46,4 +76,5 @@ class Editor extends Template @cachedCharWidth = fragment.width() @cachedLineHeight = fragment.outerHeight() fragment.remove() + @cursor.updateAbsolutePosition()