From 3a85148f690be9a0217ac0954496b2952e29897e Mon Sep 17 00:00:00 2001 From: Ben Ogle Date: Tue, 2 Sep 2014 17:28:32 -0700 Subject: [PATCH 1/9] Fix doc strings --- src/cursor.coffee | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/cursor.coffee b/src/cursor.coffee index f86721d6d..0b449eaa1 100644 --- a/src/cursor.coffee +++ b/src/cursor.coffee @@ -225,6 +225,11 @@ class Cursor extends Model @editor.lineTextForBufferRow(@getBufferRow()) # Public: Moves the cursor up one screen row. + # + # * `rowCount` (optional) {Number} number of rows to move (default: 1) + # * `options` (optional) {Object} with the following keys: + # * `moveToEndOfSelection` if true, move to the left of the selection if a + # selection exists. moveUp: (rowCount=1, {moveToEndOfSelection}={}) -> range = @marker.getScreenRange() if moveToEndOfSelection and not range.isEmpty() @@ -237,7 +242,12 @@ class Cursor extends Model @goalColumn = column # Public: Moves the cursor down one screen row. - moveDown: (rowCount = 1, {moveToEndOfSelection}={}) -> + # + # * `rowCount` (optional) {Number} number of rows to move (default: 1) + # * `options` (optional) {Object} with the following keys: + # * `moveToEndOfSelection` if true, move to the left of the selection if a + # selection exists. + moveDown: (rowCount=1, {moveToEndOfSelection}={}) -> range = @marker.getScreenRange() if moveToEndOfSelection and not range.isEmpty() { row, column } = range.end From 851034c8d3b1d831606efa4d1206faa51f7c4f6c Mon Sep 17 00:00:00 2001 From: Ben Ogle Date: Tue, 2 Sep 2014 17:29:05 -0700 Subject: [PATCH 2/9] Failing test --- spec/editor-spec.coffee | 5 +++++ src/cursor.coffee | 3 ++- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/spec/editor-spec.coffee b/spec/editor-spec.coffee index 36eb57bee..a97a5d2dd 100644 --- a/spec/editor-spec.coffee +++ b/spec/editor-spec.coffee @@ -321,6 +321,11 @@ describe "Editor", -> editor.moveLeft() expect(editor.getCursorScreenPosition()).toEqual [1, 7] + it "moves the cursor by n columns to the left", -> + editor.setCursorScreenPosition([1, 8]) + editor.moveLeft(4) + expect(editor.getCursorScreenPosition()).toEqual [1, 4] + 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", -> diff --git a/src/cursor.coffee b/src/cursor.coffee index 0b449eaa1..23343fc3d 100644 --- a/src/cursor.coffee +++ b/src/cursor.coffee @@ -260,10 +260,11 @@ class Cursor extends Model # Public: Moves the cursor left one screen column. # + # * `columnCount` (optional) {Number} number of rows to move (default: 1) # * `options` (optional) {Object} with the following keys: # * `moveToEndOfSelection` if true, move to the left of the selection if a # selection exists. - moveLeft: ({moveToEndOfSelection}={}) -> + moveLeft: (columnCount=1, {moveToEndOfSelection}={}) -> range = @marker.getScreenRange() if moveToEndOfSelection and not range.isEmpty() @setScreenPosition(range.start) From 99c07decf2d9444df659cc2f2f5dc7289ed3257c Mon Sep 17 00:00:00 2001 From: Ben Ogle Date: Tue, 2 Sep 2014 17:29:30 -0700 Subject: [PATCH 3/9] Add `columnCount` var to Editor::moveLeft and Cursor::moveLeft --- spec/editor-spec.coffee | 10 ++++++++++ src/cursor.coffee | 9 ++++++++- src/editor.coffee | 4 ++-- 3 files changed, 20 insertions(+), 3 deletions(-) diff --git a/spec/editor-spec.coffee b/spec/editor-spec.coffee index a97a5d2dd..053a81573 100644 --- a/spec/editor-spec.coffee +++ b/spec/editor-spec.coffee @@ -333,12 +333,22 @@ 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", -> + editor.setCursorScreenPosition([1, 0]) + editor.moveLeft(4) + expect(editor.getCursorScreenPosition()).toEqual [0, 26] + describe "when the cursor is on the first line", -> it "remains in the same position (0,0)", -> editor.setCursorScreenPosition(row: 0, column: 0) editor.moveLeft() expect(editor.getCursorScreenPosition()).toEqual(row: 0, column: 0) + it "moves the cursor by n columns to the left", -> + editor.setCursorScreenPosition([0, 0]) + editor.moveLeft(4) + expect(editor.getCursorScreenPosition()).toEqual [0, 0] + describe "when softTabs is enabled and the cursor is preceded by leading whitespace", -> it "skips tabLength worth of whitespace at a time", -> editor.setCursorBufferPosition([5, 6]) diff --git a/src/cursor.coffee b/src/cursor.coffee index 23343fc3d..b2c00ce71 100644 --- a/src/cursor.coffee +++ b/src/cursor.coffee @@ -270,7 +270,14 @@ class Cursor extends Model @setScreenPosition(range.start) else {row, column} = @getScreenPosition() - [row, column] = if column > 0 then [row, column - 1] else [row - 1, Infinity] + newColumn = column - columnCount + + if newColumn >= 0 + column = newColumn + else if row > 0 + row-- + column = @editor.lineTextForScreenRow(row).length + newColumn + 1 + @setScreenPosition({row, column}) # Public: Moves the cursor right one screen column. diff --git a/src/editor.coffee b/src/editor.coffee index 337b5da9d..0b3227507 100644 --- a/src/editor.coffee +++ b/src/editor.coffee @@ -1636,8 +1636,8 @@ class Editor extends Model @moveDown(lineCount) # Essential: Move every cursor left one column. - moveLeft: -> - @moveCursors (cursor) -> cursor.moveLeft(moveToEndOfSelection: true) + moveLeft: (columnCount) -> + @moveCursors (cursor) -> cursor.moveLeft(columnCount, moveToEndOfSelection: true) moveCursorLeft: -> deprecate("Use Editor::moveLeft() instead") @moveLeft() From 29f15d0f2079d624445a92d8a0dc83d03d60c8bd Mon Sep 17 00:00:00 2001 From: Ben Ogle Date: Wed, 3 Sep 2014 15:27:42 -0700 Subject: [PATCH 4/9] 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}) From f82c59d865ac0412f814ba6c1c58057f59a3bfb8 Mon Sep 17 00:00:00 2001 From: Ben Ogle Date: Wed, 3 Sep 2014 15:54:46 -0700 Subject: [PATCH 5/9] Add columnCount to moveRight --- spec/editor-spec.coffee | 22 +++++++++++++++++++++- src/cursor.coffee | 18 ++++++++++++++++-- src/editor.coffee | 4 ++-- 3 files changed, 39 insertions(+), 5 deletions(-) diff --git a/spec/editor-spec.coffee b/spec/editor-spec.coffee index f243b67c7..c2ca4c01a 100644 --- a/spec/editor-spec.coffee +++ b/spec/editor-spec.coffee @@ -343,7 +343,7 @@ describe "Editor", -> editor.moveLeft() expect(editor.getCursorScreenPosition()).toEqual(row: 0, column: buffer.lineForRow(0).length) - it "moves the cursor by one row up columns to the left", -> + it "moves the cursor by one row up and n columns to the left", -> editor.setCursorScreenPosition([1, 0]) editor.moveLeft(4) expect(editor.getCursorScreenPosition()).toEqual [0, 26] @@ -393,6 +393,21 @@ describe "Editor", -> editor.moveRight() expect(editor.getCursorScreenPosition()).toEqual [3, 4] + it "moves the cursor by n columns to the right", -> + editor.setCursorScreenPosition([3, 7]) + editor.moveRight(4) + expect(editor.getCursorScreenPosition()).toEqual [3, 11] + + it "moves the cursor by two rows down when the columnCount is longer than an entire line", -> + editor.setCursorScreenPosition([0, 28]) + editor.moveRight(32) + expect(editor.getCursorScreenPosition()).toEqual [2, 0] + + it "moves the cursor to the end of the buffer when columnCount is longer than the number of characters following the cursor position", -> + editor.setCursorScreenPosition([11, 5]) + editor.moveRight(100) + expect(editor.getCursorScreenPosition()).toEqual [12, 2] + describe "when the cursor is on the last column of a line", -> describe "when there is a subsequent line", -> it "wraps to the beginning of the next line", -> @@ -400,6 +415,11 @@ describe "Editor", -> editor.moveRight() expect(editor.getCursorScreenPosition()).toEqual [1, 0] + it "moves the cursor by one row down and n columns to the right", -> + editor.setCursorScreenPosition([0, buffer.lineForRow(0).length]) + editor.moveRight(4) + expect(editor.getCursorScreenPosition()).toEqual [1, 3] + describe "when the cursor is on the last line", -> it "remains in the same position", -> lastLineIndex = buffer.getLines().length - 1 diff --git a/src/cursor.coffee b/src/cursor.coffee index 70f0938d8..4a7c37692 100644 --- a/src/cursor.coffee +++ b/src/cursor.coffee @@ -290,13 +290,27 @@ class Cursor extends Model # * `options` (optional) {Object} with the following keys: # * `moveToEndOfSelection` if true, move to the right of the selection if a # selection exists. - moveRight: ({moveToEndOfSelection}={}) -> + moveRight: (columnCount=1, {moveToEndOfSelection}={}) -> range = @marker.getScreenRange() if moveToEndOfSelection and not range.isEmpty() @setScreenPosition(range.end) else { row, column } = @getScreenPosition() - @setScreenPosition([row, column + 1], skipAtomicTokens: true, wrapBeyondNewlines: true, wrapAtSoftNewlines: true) + newColumn = column + columnCount + rowLength = @editor.lineTextForScreenRow(row).length + + if newColumn <= rowLength + column = newColumn + else + columnDelta = newColumn - rowLength - 1 + maxLines = @editor.getScreenLineCount() + while columnDelta >= 0 and row < maxLines - 1 + row++ + rowLength = @editor.lineTextForScreenRow(row).length + column = columnDelta + columnDelta -= rowLength + + @setScreenPosition({row, column}, skipAtomicTokens: true, wrapBeyondNewlines: true, wrapAtSoftNewlines: true) # Public: Moves the cursor to the top of the buffer. moveToTop: -> diff --git a/src/editor.coffee b/src/editor.coffee index 0b3227507..7e4184466 100644 --- a/src/editor.coffee +++ b/src/editor.coffee @@ -1643,8 +1643,8 @@ class Editor extends Model @moveLeft() # Essential: Move every cursor right one column. - moveRight: -> - @moveCursors (cursor) -> cursor.moveRight(moveToEndOfSelection: true) + moveRight: (columnCount) -> + @moveCursors (cursor) -> cursor.moveRight(columnCount, moveToEndOfSelection: true) moveCursorRight: -> deprecate("Use Editor::moveRight() instead") @moveRight() From 06165b2167fb85ce392f5a9a8aaa12c7d0608b52 Mon Sep 17 00:00:00 2001 From: Ben Ogle Date: Wed, 3 Sep 2014 15:55:50 -0700 Subject: [PATCH 6/9] Remove the max call --- src/cursor.coffee | 1 - 1 file changed, 1 deletion(-) diff --git a/src/cursor.coffee b/src/cursor.coffee index 4a7c37692..e91b8cbf1 100644 --- a/src/cursor.coffee +++ b/src/cursor.coffee @@ -281,7 +281,6 @@ class Cursor extends Model rowLength = @editor.lineTextForScreenRow(row).length column = rowLength - columnDelta columnDelta -= rowLength - column = Math.max(column, 0) @setScreenPosition({row, column}) From 7a3893c7bbbd6307ced4ffd88e9d164317fcb06f Mon Sep 17 00:00:00 2001 From: Ben Ogle Date: Wed, 3 Sep 2014 15:57:53 -0700 Subject: [PATCH 7/9] Update doc strings --- src/cursor.coffee | 3 ++- src/editor.coffee | 8 ++++++-- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/src/cursor.coffee b/src/cursor.coffee index e91b8cbf1..ed34a1336 100644 --- a/src/cursor.coffee +++ b/src/cursor.coffee @@ -260,7 +260,7 @@ class Cursor extends Model # Public: Moves the cursor left one screen column. # - # * `columnCount` (optional) {Number} number of rows to move (default: 1) + # * `columnCount` (optional) {Number} number of columns to move (default: 1) # * `options` (optional) {Object} with the following keys: # * `moveToEndOfSelection` if true, move to the left of the selection if a # selection exists. @@ -286,6 +286,7 @@ class Cursor extends Model # Public: Moves the cursor right one screen column. # + # * `columnCount` (optional) {Number} number of columns to move (default: 1) # * `options` (optional) {Object} with the following keys: # * `moveToEndOfSelection` if true, move to the right of the selection if a # selection exists. diff --git a/src/editor.coffee b/src/editor.coffee index 7e4184466..9d88d976e 100644 --- a/src/editor.coffee +++ b/src/editor.coffee @@ -1619,7 +1619,7 @@ class Editor extends Model # Essential: Move every cursor up one row in screen coordinates. # - # * `lineCount` {Number} number of lines to move + # * `lineCount` (optional) {Number} number of lines to move moveUp: (lineCount) -> @moveCursors (cursor) -> cursor.moveUp(lineCount, moveToEndOfSelection: true) moveCursorUp: (lineCount) -> @@ -1628,7 +1628,7 @@ class Editor extends Model # Essential: Move every cursor down one row in screen coordinates. # - # * `lineCount` {Number} number of lines to move + # * `lineCount` (optional) {Number} number of lines to move moveDown: (lineCount) -> @moveCursors (cursor) -> cursor.moveDown(lineCount, moveToEndOfSelection: true) moveCursorDown: (lineCount) -> @@ -1636,6 +1636,8 @@ class Editor extends Model @moveDown(lineCount) # Essential: Move every cursor left one column. + # + # * `columnCount` (optional) {Number} number of columns to move (default: 1) moveLeft: (columnCount) -> @moveCursors (cursor) -> cursor.moveLeft(columnCount, moveToEndOfSelection: true) moveCursorLeft: -> @@ -1643,6 +1645,8 @@ class Editor extends Model @moveLeft() # Essential: Move every cursor right one column. + # + # * `columnCount` (optional) {Number} number of columns to move (default: 1) moveRight: (columnCount) -> @moveCursors (cursor) -> cursor.moveRight(columnCount, moveToEndOfSelection: true) moveCursorRight: -> From 48b693c1c17ad5a52b515bbf2941ea8251ef87d2 Mon Sep 17 00:00:00 2001 From: Ben Ogle Date: Wed, 3 Sep 2014 16:20:25 -0700 Subject: [PATCH 8/9] Add columnCounts to selection methods --- spec/editor-spec.coffee | 21 +++++++++++++++++++++ src/editor.coffee | 16 ++++++++++------ src/selection.coffee | 16 ++++++++++++---- 3 files changed, 43 insertions(+), 10 deletions(-) diff --git a/spec/editor-spec.coffee b/spec/editor-spec.coffee index c2ca4c01a..e8c092ded 100644 --- a/spec/editor-spec.coffee +++ b/spec/editor-spec.coffee @@ -969,6 +969,27 @@ describe "Editor", -> expect(selection1.getScreenRange()).toEqual([[0, 9], [1, 21]]) expect(selection1.isReversed()).toBeFalsy() + describe "when counts are passed into the selection functions", -> + it "expands each selection to its cursor's new location", -> + editor.setSelectedBufferRanges([[[0,9], [0,13]], [[3,16], [3,21]]]) + [selection1, selection2] = editor.getSelections() + + editor.selectRight(2) + expect(selection1.getBufferRange()).toEqual [[0,9], [0,15]] + expect(selection2.getBufferRange()).toEqual [[3,16], [3,23]] + + editor.selectLeft(3) + expect(selection1.getBufferRange()).toEqual [[0,9], [0,12]] + expect(selection2.getBufferRange()).toEqual [[3,16], [3,20]] + + editor.selectDown(3) + expect(selection1.getBufferRange()).toEqual [[0,9], [3,12]] + expect(selection2.getBufferRange()).toEqual [[3,16], [6,20]] + + editor.selectUp(2) + expect(selection1.getBufferRange()).toEqual [[0,9], [1,12]] + expect(selection2.getBufferRange()).toEqual [[3,16], [4,20]] + describe ".selectToBufferPosition(bufferPosition)", -> it "expands the last selection to the given position", -> editor.setSelectedBufferRange([[3, 0], [4, 5]]) diff --git a/src/editor.coffee b/src/editor.coffee index 9d88d976e..3ade2ecb9 100644 --- a/src/editor.coffee +++ b/src/editor.coffee @@ -1982,7 +1982,7 @@ class Editor extends Model # Essential: Move the cursor of each selection one character upward while # preserving the selection's tail position. # - # * `rowCount` {Number} of rows to select up + # * `rowCount` (optional) {Number} number of rows to select (default: 1) # # This method may merge selections that end up intesecting. selectUp: (rowCount) -> @@ -1991,7 +1991,7 @@ class Editor extends Model # Essential: Move the cursor of each selection one character downward while # preserving the selection's tail position. # - # * `rowCount` {Number} of rows to select down + # * `rowCount` (optional) {Number} number of rows to select (default: 1) # # This method may merge selections that end up intesecting. selectDown: (rowCount) -> @@ -2000,16 +2000,20 @@ class Editor extends Model # Essential: Move the cursor of each selection one character leftward while # preserving the selection's tail position. # + # * `columnCount` (optional) {Number} number of columns to select (default: 1) + # # This method may merge selections that end up intesecting. - selectLeft: -> - @expandSelectionsBackward (selection) -> selection.selectLeft() + selectLeft: (columnCount) -> + @expandSelectionsBackward (selection) -> selection.selectLeft(columnCount) # Essential: Move the cursor of each selection one character rightward while # preserving the selection's tail position. # + # * `columnCount` (optional) {Number} number of columns to select (default: 1) + # # This method may merge selections that end up intesecting. - selectRight: -> - @expandSelectionsForward (selection) -> selection.selectRight() + selectRight: (columnCount) -> + @expandSelectionsForward (selection) -> selection.selectRight(columnCount) # Essential: Select from the top of the buffer to the end of the last selection # in the buffer. diff --git a/src/selection.coffee b/src/selection.coffee index a0db277c1..38198fef4 100644 --- a/src/selection.coffee +++ b/src/selection.coffee @@ -198,18 +198,26 @@ class Selection extends Model @modifySelection => @cursor.setBufferPosition(position) # Public: Selects the text one position right of the cursor. - selectRight: -> - @modifySelection => @cursor.moveRight() + # + # * `columnCount` (optional) {Number} number of columns to select (default: 1) + selectRight: (columnCount) -> + @modifySelection => @cursor.moveRight(columnCount) # Public: Selects the text one position left of the cursor. - selectLeft: -> - @modifySelection => @cursor.moveLeft() + # + # * `columnCount` (optional) {Number} number of columns to select (default: 1) + selectLeft: (columnCount) -> + @modifySelection => @cursor.moveLeft(columnCount) # Public: Selects all the text one position above the cursor. + # + # * `rowCount` (optional) {Number} number of rows to select (default: 1) selectUp: (rowCount) -> @modifySelection => @cursor.moveUp(rowCount) # Public: Selects all the text one position below the cursor. + # + # * `rowCount` (optional) {Number} number of rows to select (default: 1) selectDown: (rowCount) -> @modifySelection => @cursor.moveDown(rowCount) From 691c7ee585c5ee80ad135cf26916fe5fac85a406 Mon Sep 17 00:00:00 2001 From: Ben Ogle Date: Wed, 3 Sep 2014 17:19:06 -0700 Subject: [PATCH 9/9] Considerably more elegant (and correct) moveLeft and moveRight --- spec/editor-spec.coffee | 20 ++++++++++++++++---- src/cursor.coffee | 36 +++++++++++++++--------------------- 2 files changed, 31 insertions(+), 25 deletions(-) diff --git a/spec/editor-spec.coffee b/spec/editor-spec.coffee index e8c092ded..5b0af428c 100644 --- a/spec/editor-spec.coffee +++ b/spec/editor-spec.coffee @@ -329,7 +329,7 @@ describe "Editor", -> 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] + expect(editor.getCursorScreenPosition()).toEqual [0, 29] it "moves the cursor to the beginning columnCount is longer than the position in the buffer", -> editor.setCursorScreenPosition([1, 0]) @@ -348,6 +348,12 @@ describe "Editor", -> editor.moveLeft(4) expect(editor.getCursorScreenPosition()).toEqual [0, 26] + describe "when the next line is empty", -> + it "wraps to the beginning of the previous line", -> + editor.setCursorScreenPosition([11, 0]) + editor.moveLeft() + expect(editor.getCursorScreenPosition()).toEqual [10, 0] + describe "when the cursor is on the first line", -> it "remains in the same position (0,0)", -> editor.setCursorScreenPosition(row: 0, column: 0) @@ -399,9 +405,9 @@ describe "Editor", -> expect(editor.getCursorScreenPosition()).toEqual [3, 11] it "moves the cursor by two rows down when the columnCount is longer than an entire line", -> - editor.setCursorScreenPosition([0, 28]) - editor.moveRight(32) - expect(editor.getCursorScreenPosition()).toEqual [2, 0] + editor.setCursorScreenPosition([0, 29]) + editor.moveRight(34) + expect(editor.getCursorScreenPosition()).toEqual [2, 2] it "moves the cursor to the end of the buffer when columnCount is longer than the number of characters following the cursor position", -> editor.setCursorScreenPosition([11, 5]) @@ -420,6 +426,12 @@ describe "Editor", -> editor.moveRight(4) expect(editor.getCursorScreenPosition()).toEqual [1, 3] + describe "when the next line is empty", -> + it "wraps to the beginning of the next line", -> + editor.setCursorScreenPosition([9, 4]) + editor.moveRight() + expect(editor.getCursorScreenPosition()).toEqual [10, 0] + describe "when the cursor is on the last line", -> it "remains in the same position", -> lastLineIndex = buffer.getLines().length - 1 diff --git a/src/cursor.coffee b/src/cursor.coffee index ed34a1336..68018a72c 100644 --- a/src/cursor.coffee +++ b/src/cursor.coffee @@ -270,18 +270,13 @@ class Cursor extends Model @setScreenPosition(range.start) else {row, column} = @getScreenPosition() - newColumn = column - columnCount - if newColumn >= 0 - column = newColumn - else - columnDelta = -(newColumn + 1) - while columnDelta >= 0 and row > 0 - row-- - rowLength = @editor.lineTextForScreenRow(row).length - column = rowLength - columnDelta - columnDelta -= rowLength + while columnCount > column and row > 0 + columnCount -= column + column = @editor.lineTextForScreenRow(--row).length + columnCount-- # subtract 1 for the row move + column = column - columnCount @setScreenPosition({row, column}) # Public: Moves the cursor right one screen column. @@ -296,20 +291,19 @@ class Cursor extends Model @setScreenPosition(range.end) else { row, column } = @getScreenPosition() - newColumn = column + columnCount + maxLines = @editor.getScreenLineCount() rowLength = @editor.lineTextForScreenRow(row).length + columnsRemainingInLine = rowLength - column - if newColumn <= rowLength - column = newColumn - else - columnDelta = newColumn - rowLength - 1 - maxLines = @editor.getScreenLineCount() - while columnDelta >= 0 and row < maxLines - 1 - row++ - rowLength = @editor.lineTextForScreenRow(row).length - column = columnDelta - columnDelta -= rowLength + while columnCount > columnsRemainingInLine and row < maxLines - 1 + columnCount -= columnsRemainingInLine + columnCount-- # subtract 1 for the row move + column = 0 + rowLength = @editor.lineTextForScreenRow(++row).length + columnsRemainingInLine = rowLength + + column = column + columnCount @setScreenPosition({row, column}, skipAtomicTokens: true, wrapBeyondNewlines: true, wrapAtSoftNewlines: true) # Public: Moves the cursor to the top of the buffer.