Considerably more elegant (and correct) moveLeft and moveRight

This commit is contained in:
Ben Ogle
2014-09-03 17:19:06 -07:00
parent 48b693c1c1
commit 691c7ee585
2 changed files with 31 additions and 25 deletions

View File

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

View File

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