WIP: Use a goal column when moving down

Still needs to be cleared when moving horizontally.
This commit is contained in:
Corey Johnson & Nathan Sobo
2012-01-23 13:40:37 -08:00
parent 7d4c236418
commit 1dcebbae45
3 changed files with 38 additions and 11 deletions

View File

@@ -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", ->

View File

@@ -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)

View File

@@ -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 }