Don't spill out of document when moving right and left

This commit is contained in:
Danny Greg & Nathan Sobo 2012-01-19 15:40:26 -08:00
parent a606fe55b3
commit 030e241c75
2 changed files with 31 additions and 10 deletions

View File

@ -53,16 +53,37 @@ fdescribe "Editor", ->
expect(editor.getPosition()).toEqual(row: lastLineIndex, col: lastLine.length)
describe "when left is pressed on the first column", ->
it "wraps to the end of the previous line", ->
editor.setPosition(row: 1, col: 0)
editor.moveLeft()
expect(editor.getPosition()).toEqual(row: 0, col: buffer.getLine(0).length)
describe "when there is a previous line", ->
it "wraps to the end of the previous line", ->
editor.setPosition(row: 1, col: 0)
editor.moveLeft()
expect(editor.getPosition()).toEqual(row: 0, col: buffer.getLine(0).length)
describe "when the cursor is on the first line", ->
it "remains in the same position (0,0)", ->
editor.setPosition(row: 0, col: 0)
editor.moveLeft()
expect(editor.getPosition()).toEqual(row: 0, col: 0)
describe "when right is pressed on the last column", ->
it "wraps to the beginning of the next line", ->
editor.setPosition(row: 0, col: buffer.getLine(0).length)
editor.moveRight()
expect(editor.getPosition()).toEqual(row: 1, col: 0)
describe "when there is a subsequent line", ->
it "wraps to the beginning of the next line", ->
editor.setPosition(row: 0, col: buffer.getLine(0).length)
editor.moveRight()
expect(editor.getPosition()).toEqual(row: 1, col: 0)
fdescribe "when the cursor is on the last line", ->
it "remains in the same position", ->
lastLineIndex = buffer.getLines().length - 1
lastLine = buffer.getLine(lastLineIndex)
expect(lastLine.length).toBeGreaterThan(0)
lastPosition = { row: lastLineIndex, col: lastLine.length }
editor.setPosition(lastPosition)
editor.moveRight()
expect(editor.getPosition()).toEqual(lastPosition)
describe ".setPosition({row, col})", ->
it "moves the cursor to cover the character at the given row and column", ->

View File

@ -37,7 +37,7 @@ class Editor extends Template
{ row, col } = @getPosition()
if col < @buffer.getLine(row).length
col++
else
else if row < @buffer.numLines() - 1
row++
col = 0
@setPosition({row, col})
@ -54,7 +54,7 @@ class Editor extends Template
{ row, col } = @getPosition()
if col > 0
col--
else
else if row > 0
row--
col = @buffer.getLine(row).length