From 1dcebbae45d17eee803d9bb8d11b21b2bd348d62 Mon Sep 17 00:00:00 2001 From: Corey Johnson & Nathan Sobo Date: Mon, 23 Jan 2012 13:40:37 -0800 Subject: [PATCH] WIP: Use a goal column when moving down Still needs to be cleared when moving horizontally. --- spec/atom/editor-spec.coffee | 36 ++++++++++++++++++++++++++++-------- src/atom/cursor.coffee | 9 ++++++--- src/atom/editor.coffee | 4 ++++ 3 files changed, 38 insertions(+), 11 deletions(-) diff --git a/spec/atom/editor-spec.coffee b/spec/atom/editor-spec.coffee index ecadc7ea4..8daabf987 100644 --- a/spec/atom/editor-spec.coffee +++ b/spec/atom/editor-spec.coffee @@ -48,15 +48,35 @@ fdescribe "Editor", -> editor.moveUp() expect(editor.getPosition()).toEqual(row: 0, col: 0) - describe "when down is pressed on the last line", -> - it "moves the cursor to the end of line", -> - lastLineIndex = buffer.getLines().length - 1 - lastLine = buffer.getLine(lastLineIndex) - expect(lastLine.length).toBeGreaterThan(0) + describe "down", -> + describe "when moving between lines of varying length", -> + fit "retains a goal column, keeping the cursor as close to the original x position as possible", -> + lineLengths = buffer.getLines().map (line) -> line.length + expect(lineLengths[3]).toBeGreaterThan(lineLengths[4]) + expect(lineLengths[5]).toBeGreaterThan(lineLengths[4]) + expect(lineLengths[6]).toBeGreaterThan(lineLengths[3]) - editor.setPosition(row: lastLineIndex, col: 0) - editor.moveDown() - expect(editor.getPosition()).toEqual(row: lastLineIndex, col: lastLine.length) + editor.setPosition(row: 3, col: lineLengths[3]) + + editor.moveDown() + expect(editor.getPosition().col).toBe lineLengths[4] + + editor.moveDown() + expect(editor.getPosition().col).toBe lineLengths[5] + + editor.moveDown() + expect(editor.getPosition().col).toBe lineLengths[3] + + + describe "when on the last line", -> + it "moves the cursor to the end of line", -> + lastLineIndex = buffer.getLines().length - 1 + lastLine = buffer.getLine(lastLineIndex) + expect(lastLine.length).toBeGreaterThan(0) + + editor.setPosition(row: lastLineIndex, col: 0) + editor.moveDown() + expect(editor.getPosition()).toEqual(row: lastLineIndex, col: lastLine.length) describe "when left is pressed on the first column", -> describe "when there is a previous line", -> diff --git a/src/atom/cursor.coffee b/src/atom/cursor.coffee index c1c001343..7892653e2 100644 --- a/src/atom/cursor.coffee +++ b/src/atom/cursor.coffee @@ -6,7 +6,8 @@ class Cursor extends Template @pre class: 'cursor', style: 'position: absolute;', => @raw ' ' viewProperties: - setPosition: (@point) -> + setPosition: (point) -> + @point = @parentView.clipPosition(point) @updateAbsolutePosition() getPosition: -> @point @@ -21,11 +22,14 @@ class Cursor extends Template moveDown: -> { row, col } = @getPosition() + if row < @parentView.buffer.numLines() - 1 row++ else col = @parentView.buffer.getLine(row).length - @setPosition({row, col}) + + @goalColumn ?= col + @setPosition({row, col: @goalColumn || col}) moveRight: -> { row, col } = @getPosition() @@ -46,7 +50,6 @@ class Cursor extends Template @setPosition({row, col}) - updateAbsolutePosition: -> position = @parentView.pixelPositionFromPoint(@point) @css(position) diff --git a/src/atom/editor.coffee b/src/atom/editor.coffee index 44f1b5bbb..f6719849d 100644 --- a/src/atom/editor.coffee +++ b/src/atom/editor.coffee @@ -42,6 +42,10 @@ class Editor extends Template @lines.append $$.pre(line) @setPosition(row: 0, col: 0) + clipPosition: ({row, col}) -> + line = @buffer.getLine(row) + { row: row, col: Math.min(line.length, col) } + pixelPositionFromPoint: ({row, col}) -> { top: row * @lineHeight, left: col * @charWidth }