From 29f15d0f2079d624445a92d8a0dc83d03d60c8bd Mon Sep 17 00:00:00 2001 From: Ben Ogle Date: Wed, 3 Sep 2014 15:27:42 -0700 Subject: [PATCH] Make moveLeft() with huge values span multiple rows --- spec/editor-spec.coffee | 14 ++++++++++++-- src/cursor.coffee | 11 ++++++++--- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/spec/editor-spec.coffee b/spec/editor-spec.coffee index 053a81573..f243b67c7 100644 --- a/spec/editor-spec.coffee +++ b/spec/editor-spec.coffee @@ -326,6 +326,16 @@ 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", -> @@ -333,7 +343,7 @@ describe "Editor", -> 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] @@ -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] diff --git a/src/cursor.coffee b/src/cursor.coffee index b2c00ce71..70f0938d8 100644 --- a/src/cursor.coffee +++ b/src/cursor.coffee @@ -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})