Skip to content

Commit

Permalink
Make moveLeft() with huge values span multiple rows
Browse files Browse the repository at this point in the history
  • Loading branch information
benogle committed Sep 3, 2014
1 parent 99c07de commit 29f15d0
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
14 changes: 12 additions & 2 deletions spec/editor-spec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -326,14 +326,24 @@ describe "Editor", ->
editor.moveLeft(4)
expect(editor.getCursorScreenPosition()).toEqual [1, 4]

it "moves the cursor by two rows up when the columnCount is longer than an entire line", ->
editor.setCursorScreenPosition([2, 2])
editor.moveLeft(34)
expect(editor.getCursorScreenPosition()).toEqual [0, 28]

it "moves the cursor to the beginning columnCount is longer than the position in the buffer", ->
editor.setCursorScreenPosition([1, 0])
editor.moveLeft(100)
expect(editor.getCursorScreenPosition()).toEqual [0, 0]

describe "when the cursor is in the first column", ->
describe "when there is a previous line", ->
it "wraps to the end of the previous line", ->
editor.setCursorScreenPosition(row: 1, column: 0)
editor.moveLeft()
expect(editor.getCursorScreenPosition()).toEqual(row: 0, column: buffer.lineForRow(0).length)

it "moves the cursor by n columns to the left", ->
it "moves the cursor by one row up columns to the left", ->
editor.setCursorScreenPosition([1, 0])
editor.moveLeft(4)
expect(editor.getCursorScreenPosition()).toEqual [0, 26]
Expand All @@ -344,7 +354,7 @@ describe "Editor", ->
editor.moveLeft()
expect(editor.getCursorScreenPosition()).toEqual(row: 0, column: 0)

it "moves the cursor by n columns to the left", ->
it "remains in the same position (0,0) when columnCount is specified", ->
editor.setCursorScreenPosition([0, 0])
editor.moveLeft(4)
expect(editor.getCursorScreenPosition()).toEqual [0, 0]
Expand Down
11 changes: 8 additions & 3 deletions src/cursor.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -274,9 +274,14 @@ class Cursor extends Model

if newColumn >= 0
column = newColumn
else if row > 0
row--
column = @editor.lineTextForScreenRow(row).length + newColumn + 1
else
columnDelta = -(newColumn + 1)
while columnDelta >= 0 and row > 0
row--
rowLength = @editor.lineTextForScreenRow(row).length
column = rowLength - columnDelta
columnDelta -= rowLength
column = Math.max(column, 0)

@setScreenPosition({row, column})

Expand Down

0 comments on commit 29f15d0

Please sign in to comment.