From 3d45a5201df0493f56d6ec99bbdf6919935ab26f Mon Sep 17 00:00:00 2001 From: Corey Johnson & Nathan Sobo Date: Mon, 23 Jan 2012 12:50:07 -0800 Subject: [PATCH] Move movement logic to cursor subview. --- spec/atom/editor-spec.coffee | 8 ++--- src/atom/cursor.coffee | 36 ++++++++++++++++++++++ src/atom/editor.coffee | 60 ++++++------------------------------ 3 files changed, 49 insertions(+), 55 deletions(-) diff --git a/spec/atom/editor-spec.coffee b/spec/atom/editor-spec.coffee index dd6bc79c1..ecadc7ea4 100644 --- a/spec/atom/editor-spec.coffee +++ b/spec/atom/editor-spec.coffee @@ -96,8 +96,8 @@ fdescribe "Editor", -> editor.attachToDom() editor.setPosition(row: 2, col: 2) - expect(editor.cursor.position().top).toBe(2 * editor.lineHeight()) - expect(editor.cursor.position().left).toBe(2 * editor.charWidth()) + 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", -> @@ -106,6 +106,6 @@ fdescribe "Editor", -> editor.attachToDom() - expect(editor.cursor.position().top).toBe(2 * editor.lineHeight()) - expect(editor.cursor.position().left).toBe(2 * editor.charWidth()) + expect(editor.cursor.position().top).toBe(2 * editor.lineHeight) + expect(editor.cursor.position().left).toBe(2 * editor.charWidth) diff --git a/src/atom/cursor.coffee b/src/atom/cursor.coffee index e8835caaa..67a75523c 100644 --- a/src/atom/cursor.coffee +++ b/src/atom/cursor.coffee @@ -12,6 +12,42 @@ class Cursor extends Template getPosition: -> @_position + moveUp: -> + { row, col } = @getPosition() + if row is 0 + col = 0 + else + row-- + @setPosition({row, col}) + + moveDown: -> + { row, col } = @getPosition() + if row < @parentView.buffer.numLines() - 1 + row++ + else + col = @parentView.buffer.getLine(row).length + @setPosition({row, col}) + + moveRight: -> + { row, col } = @getPosition() + if col < @parentView.buffer.getLine(row).length + col++ + else if row < @parentView.buffer.numLines() - 1 + row++ + col = 0 + @setPosition({row, col}) + + moveLeft: -> + { row, col } = @getPosition() + if col > 0 + col-- + else if row > 0 + row-- + col = @parentView.buffer.getLine(row).length + + @setPosition({row, col}) + + updateAbsolutePosition: -> position = @parentView.toPixelPosition(@_position) @css(position) diff --git a/src/atom/editor.coffee b/src/atom/editor.coffee index f71fdec73..03540dd7f 100644 --- a/src/atom/editor.coffee +++ b/src/atom/editor.coffee @@ -33,42 +33,6 @@ class Editor extends Template @one 'attach', => @calculateDimensions() - - moveRight: -> - { row, col } = @getPosition() - if col < @buffer.getLine(row).length - col++ - else if row < @buffer.numLines() - 1 - row++ - col = 0 - @setPosition({row, col}) - - moveDown: -> - { row, col } = @getPosition() - if row < @buffer.numLines() - 1 - row++ - else - col = @buffer.getLine(row).length - @setPosition({row, col}) - - moveLeft: -> - { row, col } = @getPosition() - if col > 0 - col-- - else if row > 0 - row-- - col = @buffer.getLine(row).length - - @setPosition({row, col}) - - moveUp: -> - { row, col } = @getPosition() - if row is 0 - col = 0 - else - row-- - @setPosition({row, col}) - setBuffer: (@buffer) -> @lines.empty() for line in @buffer.getLines() @@ -78,26 +42,20 @@ class Editor extends Template @lines.append $$.pre(line) @setPosition(row: 0, col: 0) - setPosition: (position) -> - @cursor.setPosition(position) - - getPosition: -> - @cursor.getPosition() - toPixelPosition: ({row, col}) -> - { top: row * @lineHeight(), left: col * @charWidth() } - - lineHeight: -> - @cachedLineHeight - - charWidth: -> - @cachedCharWidth + { top: row * @lineHeight, left: col * @charWidth } calculateDimensions: -> fragment = $('
x
') @lines.append(fragment) - @cachedCharWidth = fragment.width() - @cachedLineHeight = fragment.outerHeight() + @charWidth = fragment.width() + @lineHeight = fragment.outerHeight() fragment.remove() @cursor.updateAbsolutePosition() + moveUp: -> @cursor.moveUp() + moveDown: -> @cursor.moveDown() + moveRight: -> @cursor.moveRight() + moveLeft: -> @cursor.moveLeft() + setPosition: (position) -> @cursor.setPosition(position) + getPosition: -> @cursor.getPosition()