X and Y were inverted

This commit is contained in:
Corey Johnson & Nathan Sobo 2012-01-25 12:20:58 -08:00
parent 0528a7b8cb
commit c11a618a9f
5 changed files with 107 additions and 107 deletions

View File

@ -38,20 +38,20 @@ describe 'Buffer', ->
describe "insert(position, string)", ->
it "inserts the given string at the given position", ->
expect(buffer.getLine(1).charAt(6)).not.toBe 'q'
buffer.insert({x: 1, y: 6}, 'q')
buffer.insert({row: 1, col: 6}, 'q')
expect(buffer.getLine(1).charAt(6)).toBe 'q'
it "emits an event with the range of the change and the new text", ->
insertHandler = jasmine.createSpy 'insertHandler'
buffer.on 'insert', insertHandler
buffer.insert({x: 1, y: 6}, 'q')
buffer.insert({row: 1, col: 6}, 'q')
expect(insertHandler).toHaveBeenCalled()
[event] = insertHandler.argsForCall[0]
expect(event.range.start).toEqual(x: 1, y: 6)
expect(event.range.end).toEqual(x: 1, y: 6)
expect(event.range.start).toEqual(row: 1, col: 6)
expect(event.range.end).toEqual(row: 1, col: 6)
expect(event.string).toBe 'q'
describe ".save()", ->

View File

@ -25,30 +25,30 @@ describe "Editor", ->
expect(editor.lines.find('pre:eq(10)').html()).toBe ' '
it "sets the cursor to the beginning of the file", ->
expect(editor.getPosition()).toEqual(x: 0, y: 0)
expect(editor.getPosition()).toEqual(row: 0, col: 0)
describe "cursor movement", ->
describe ".setPosition({x, y})", ->
it "moves the cursor to cover the character at the given x and y", ->
describe ".setPosition({row, col})", ->
it "moves the cursor to cover the character at the given row and column", ->
editor.attachToDom()
editor.setPosition(x: 2, y: 2)
editor.setPosition(row: 2, col: 2)
expect(editor.cursor.position().top).toBe(2 * editor.lineHeight)
expect(editor.cursor.position().left).toBe(2 * editor.charWidth)
describe "when the arrow keys are pressed", ->
it "moves the cursor by a single x/y", ->
it "moves the cursor by a single row/column", ->
editor.trigger keydownEvent('right')
expect(editor.getPosition()).toEqual(x: 0, y: 1)
expect(editor.getPosition()).toEqual(row: 0, col: 1)
editor.trigger keydownEvent('down')
expect(editor.getPosition()).toEqual(x: 1, y: 1)
expect(editor.getPosition()).toEqual(row: 1, col: 1)
editor.trigger keydownEvent('left')
expect(editor.getPosition()).toEqual(x: 1, y: 0)
expect(editor.getPosition()).toEqual(row: 1, col: 0)
editor.trigger keydownEvent('up')
expect(editor.getPosition()).toEqual(x: 0, y: 0)
expect(editor.getPosition()).toEqual(row: 0, col: 0)
describe "vertical movement", ->
describe "auto-scrolling", ->
@ -86,7 +86,7 @@ describe "Editor", ->
editor.moveUp()
expect(editor.scrollTop()).toBe(0)
describe "goal y retention", ->
describe "goal column retention", ->
lineLengths = null
beforeEach ->
@ -95,96 +95,96 @@ describe "Editor", ->
expect(lineLengths[5]).toBeGreaterThan(lineLengths[4])
expect(lineLengths[6]).toBeGreaterThan(lineLengths[3])
it "retains the goal y when moving up", ->
it "retains the goal column when moving up", ->
expect(lineLengths[6]).toBeGreaterThan(32)
editor.setPosition(x: 6, y: 32)
editor.setPosition(row: 6, col: 32)
editor.moveUp()
expect(editor.getPosition().y).toBe lineLengths[5]
expect(editor.getPosition().col).toBe lineLengths[5]
editor.moveUp()
expect(editor.getPosition().y).toBe lineLengths[4]
expect(editor.getPosition().col).toBe lineLengths[4]
editor.moveUp()
expect(editor.getPosition().y).toBe 32
expect(editor.getPosition().col).toBe 32
it "retains the goal y when moving down", ->
editor.setPosition(x: 3, y: lineLengths[3])
it "retains the goal column when moving down", ->
editor.setPosition(row: 3, col: lineLengths[3])
editor.moveDown()
expect(editor.getPosition().y).toBe lineLengths[4]
expect(editor.getPosition().col).toBe lineLengths[4]
editor.moveDown()
expect(editor.getPosition().y).toBe lineLengths[5]
expect(editor.getPosition().col).toBe lineLengths[5]
editor.moveDown()
expect(editor.getPosition().y).toBe lineLengths[3]
expect(editor.getPosition().col).toBe lineLengths[3]
it "clears the goal y when the cursor is set", ->
# set a goal y by moving down
editor.setPosition(x: 3, y: lineLengths[3])
it "clears the goal column when the cursor is set", ->
# set a goal column by moving down
editor.setPosition(row: 3, col: lineLengths[3])
editor.moveDown()
expect(editor.getPosition().y).not.toBe 6
expect(editor.getPosition().col).not.toBe 6
# clear the goal y by explicitly setting the cursor position
editor.setY(6)
expect(editor.getPosition().y).toBe 6
# clear the goal column by explicitly setting the cursor position
editor.setColumn(6)
expect(editor.getPosition().col).toBe 6
editor.moveDown()
expect(editor.getPosition().y).toBe 6
expect(editor.getPosition().col).toBe 6
describe "when up is pressed on the first line", ->
it "moves the cursor to the beginning of the line, but retains the goal y", ->
editor.setPosition(x: 0, y: 4)
it "moves the cursor to the beginning of the line, but retains the goal column", ->
editor.setPosition(row: 0, col: 4)
editor.moveUp()
expect(editor.getPosition()).toEqual(x: 0, y: 0)
expect(editor.getPosition()).toEqual(row: 0, col: 0)
editor.moveDown()
expect(editor.getPosition()).toEqual(x: 1, y: 4)
expect(editor.getPosition()).toEqual(row: 1, col: 4)
describe "when down is pressed on the last line", ->
it "moves the cursor to the end of line, but retains the goal y", ->
it "moves the cursor to the end of line, but retains the goal column", ->
lastLineIndex = buffer.getLines().length - 1
lastLine = buffer.getLine(lastLineIndex)
expect(lastLine.length).toBeGreaterThan(0)
editor.setPosition(x: lastLineIndex, y: 1)
editor.setPosition(row: lastLineIndex, col: 1)
editor.moveDown()
expect(editor.getPosition()).toEqual(x: lastLineIndex, y: lastLine.length)
expect(editor.getPosition()).toEqual(row: lastLineIndex, col: lastLine.length)
editor.moveUp()
expect(editor.getPosition().y).toBe 1
expect(editor.getPosition().col).toBe 1
it "retains a goal y of 0", ->
it "retains a goal column of 0", ->
lastLineIndex = buffer.getLines().length - 1
lastLine = buffer.getLine(lastLineIndex)
expect(lastLine.length).toBeGreaterThan(0)
editor.setPosition(x: lastLineIndex, y: 0)
editor.setPosition(row: lastLineIndex, col: 0)
editor.moveDown()
editor.moveUp()
expect(editor.getPosition().y).toBe 0
expect(editor.getPosition().col).toBe 0
describe "horizontal movement", ->
describe "when left is pressed on the first y", ->
describe "when left is pressed on the first column", ->
describe "when there is a previous line", ->
it "wraps to the end of the previous line", ->
editor.setPosition(x: 1, y: 0)
editor.setPosition(row: 1, col: 0)
editor.moveLeft()
expect(editor.getPosition()).toEqual(x: 0, y: buffer.getLine(0).length)
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(x: 0, y: 0)
editor.setPosition(row: 0, col: 0)
editor.moveLeft()
expect(editor.getPosition()).toEqual(x: 0, y: 0)
expect(editor.getPosition()).toEqual(row: 0, col: 0)
describe "when right is pressed on the last y", ->
describe "when right is pressed on the last column", ->
describe "when there is a subsequent line", ->
it "wraps to the beginning of the next line", ->
editor.setPosition(x: 0, y: buffer.getLine(0).length)
editor.setPosition(row: 0, col: buffer.getLine(0).length)
editor.moveRight()
expect(editor.getPosition()).toEqual(x: 1, y: 0)
expect(editor.getPosition()).toEqual(row: 1, col: 0)
describe "when the cursor is on the last line", ->
it "remains in the same position", ->
@ -192,7 +192,7 @@ describe "Editor", ->
lastLine = buffer.getLine(lastLineIndex)
expect(lastLine.length).toBeGreaterThan(0)
lastPosition = { x: lastLineIndex, y: lastLine.length }
lastPosition = { row: lastLineIndex, col: lastLine.length }
editor.setPosition(lastPosition)
editor.moveRight()
@ -200,7 +200,7 @@ describe "Editor", ->
describe "when the editor is attached to the dom", ->
it "updates the pixel position of the cursor", ->
editor.setPosition(x: 2, y: 2)
editor.setPosition(row: 2, col: 2)
editor.attachToDom()
@ -220,13 +220,13 @@ describe "Editor", ->
describe "when text input events are triggered on the hidden input element", ->
it "inserts the typed character at the cursor position, both in the buffer and the pre element", ->
editor.setPosition(x: 1, y: 6)
editor.setPosition(row: 1, col: 6)
expect(editor.getCurrentLine().charAt(6)).not.toBe 'q'
editor.hiddenInput.textInput 'q'
expect(editor.getCurrentLine().charAt(6)).toBe 'q'
expect(editor.getPosition()).toEqual(x: 1, y: 7)
expect(editor.getPosition()).toEqual(row: 1, col: 7)
expect(editor.lines.find('pre:eq(1)')).toHaveText editor.getCurrentLine()

View File

@ -24,17 +24,17 @@ class Buffer
getLine: (n) ->
@lines[n]
insert: ({x, y}, string) ->
line = @getLine(x)
before = line.substring(0, y)
after = line.substring(y)
@lines[x] = before + string + after
insert: ({row, col}, string) ->
line = @getLine(row)
before = line.substring(0, col)
after = line.substring(col)
@lines[row] = before + string + after
@trigger 'insert'
string: string
range:
start: {x, y}
end: {x, y}
start: {row, col}
end: {row, col}
numLines: ->
@getLines().length

View File

@ -13,68 +13,68 @@ class Cursor extends Template
setBuffer: (@buffer) ->
@buffer.on 'insert', (e) =>
@setY(@getY() + e.string.length)
@setColumn(@getColumn() + e.string.length)
setPosition: (point) ->
@point = @editor.clipPosition(point)
@goalY = null
@goalColumn = null
@updateAbsolutePosition()
getPosition: -> @point
setY: (y) ->
{ x } = @getPosition()
@setPosition {x, y}
setColumn: (col) ->
{ row } = @getPosition()
@setPosition {row, col}
getY: ->
@getPosition().y
getColumn: ->
@getPosition().col
moveUp: ->
{ x, y } = @getPosition()
y = @goalY if @goalY?
if x > 0
@setPosition({x: x - 1, y: y})
{ row, col } = @getPosition()
col = @goalColumn if @goalColumn?
if row > 0
@setPosition({row: row - 1, col: col})
else
@moveToLineStart()
@goalY = y
@goalColumn = col
moveDown: ->
{ x, y } = @getPosition()
y = @goalY if @goalY?
if x < @editor.buffer.numLines() - 1
@setPosition({x: x + 1, y: y})
{ row, col } = @getPosition()
col = @goalColumn if @goalColumn?
if row < @editor.buffer.numLines() - 1
@setPosition({row: row + 1, col: col})
else
@moveToLineEnd()
@goalY = y
@goalColumn = col
moveToLineEnd: ->
{ x } = @getPosition()
@setPosition({ x, y: @editor.buffer.getLine(x).length })
{ row } = @getPosition()
@setPosition({ row, col: @editor.buffer.getLine(row).length })
moveToLineStart: ->
{ x } = @getPosition()
@setPosition({ x, y: 0 })
{ row } = @getPosition()
@setPosition({ row, col: 0 })
moveRight: ->
{ x, y } = @getPosition()
if y < @editor.buffer.getLine(x).length
y++
else if x < @editor.buffer.numLines() - 1
x++
y = 0
@setPosition({x, y})
{ row, col } = @getPosition()
if col < @editor.buffer.getLine(row).length
col++
else if row < @editor.buffer.numLines() - 1
row++
col = 0
@setPosition({row, col})
moveLeft: ->
{ x, y } = @getPosition()
if y > 0
y--
else if x > 0
x--
y = @editor.buffer.getLine(x).length
{ row, col } = @getPosition()
if col > 0
col--
else if row > 0
row--
col = @editor.buffer.getLine(row).length
@setPosition({x, y})
@setPosition({row, col})
updateAbsolutePosition: ->
position = @editor.pixelPositionFromPoint(@point)

View File

@ -59,18 +59,18 @@ class Editor extends Template
@lines.append $$.pre -> @raw('&nbsp;')
else
@lines.append $$.pre(line)
@setPosition(x: 0, y: 0)
@setPosition(row: 0, col: 0)
@cursor.setBuffer(@buffer)
@buffer.on 'insert', (e) =>
{x} = e.range.start
@lines.find('pre').eq(x).replaceWith $$.pre(@buffer.getLine(x))
{row} = e.range.start
@lines.find('pre').eq(row).replaceWith $$.pre(@buffer.getLine(row))
clipPosition: ({x, y}) ->
line = @buffer.getLine(x)
{ x: x, y: Math.min(line.length, y) }
clipPosition: ({row, col}) ->
line = @buffer.getLine(row)
{ row: row, col: Math.min(line.length, col) }
pixelPositionFromPoint: ({x, y}) ->
{ top: x * @lineHeight, left: y * @charWidth }
pixelPositionFromPoint: ({row, col}) ->
{ top: row * @lineHeight, left: col * @charWidth }
calculateDimensions: ->
fragment = $('<pre style="position: absolute; visibility: hidden;">x</pre>')
@ -86,7 +86,7 @@ class Editor extends Template
else
@scrollTop() + @height()
getCurrentLine: -> @buffer.getLine(@getPosition().x)
getCurrentLine: -> @buffer.getLine(@getPosition().row)
moveUp: -> @cursor.moveUp()
moveDown: -> @cursor.moveDown()
@ -94,4 +94,4 @@ class Editor extends Template
moveLeft: -> @cursor.moveLeft()
setPosition: (point) -> @cursor.setPosition(point)
getPosition: -> @cursor.getPosition()
setY: (y)-> @cursor.setY y
setColumn: (column)-> @cursor.setColumn column