Files
atom/spec/app/edit-session-spec.coffee

408 lines
17 KiB
CoffeeScript

Buffer = require 'buffer'
EditSession = require 'edit-session'
describe "EditSession", ->
[buffer, editSession, lineLengths] = []
beforeEach ->
fakeEditor =
calcSoftWrapColumn: ->
tabText: ' '
buffer = new Buffer(require.resolve('fixtures/sample.js'))
editSession = new EditSession(fakeEditor, buffer)
lineLengths = buffer.getLines().map (line) -> line.length
describe "cursor movement", ->
describe ".setCursorScreenPosition(screenPosition)", ->
it "clears a goal column established by vertical movement", ->
# set a goal column by moving down
editSession.setCursorScreenPosition(row: 3, column: lineLengths[3])
editSession.moveCursorDown()
expect(editSession.getCursorScreenPosition().column).not.toBe 6
# clear the goal column by explicitly setting the cursor position
editSession.setCursorScreenPosition([4,6])
expect(editSession.getCursorScreenPosition().column).toBe 6
editSession.moveCursorDown()
expect(editSession.getCursorScreenPosition().column).toBe 6
describe ".moveCursorUp()", ->
it "moves the cursor up", ->
editSession.setCursorScreenPosition([2, 2])
editSession.moveCursorUp()
expect(editSession.getCursorScreenPosition()).toEqual [1, 2]
it "retains the goal column across lines of differing length", ->
expect(lineLengths[6]).toBeGreaterThan(32)
editSession.setCursorScreenPosition(row: 6, column: 32)
editSession.moveCursorUp()
expect(editSession.getCursorScreenPosition().column).toBe lineLengths[5]
editSession.moveCursorUp()
expect(editSession.getCursorScreenPosition().column).toBe lineLengths[4]
editSession.moveCursorUp()
expect(editSession.getCursorScreenPosition().column).toBe 32
describe "when the cursor is on the first line", ->
it "moves the cursor to the beginning of the line, but retains the goal column", ->
editSession.setCursorScreenPosition(row: 0, column: 4)
editSession.moveCursorUp()
expect(editSession.getCursorScreenPosition()).toEqual(row: 0, column: 0)
editSession.moveCursorDown()
expect(editSession.getCursorScreenPosition()).toEqual(row: 1, column: 4)
describe ".moveCursorDown()", ->
it "moves the cursor down", ->
editSession.setCursorScreenPosition([2, 2])
editSession.moveCursorDown()
expect(editSession.getCursorScreenPosition()).toEqual [3, 2]
it "retains the goal column across lines of differing length", ->
editSession.setCursorScreenPosition(row: 3, column: lineLengths[3])
editSession.moveCursorDown()
expect(editSession.getCursorScreenPosition().column).toBe lineLengths[4]
editSession.moveCursorDown()
expect(editSession.getCursorScreenPosition().column).toBe lineLengths[5]
editSession.moveCursorDown()
expect(editSession.getCursorScreenPosition().column).toBe lineLengths[3]
describe "when the cursor is on the last line", ->
it "moves the cursor to the end of line, but retains the goal column when moving back up", ->
lastLineIndex = buffer.getLines().length - 1
lastLine = buffer.lineForRow(lastLineIndex)
expect(lastLine.length).toBeGreaterThan(0)
editSession.setCursorScreenPosition(row: lastLineIndex, column: 1)
editSession.moveCursorDown()
expect(editSession.getCursorScreenPosition()).toEqual(row: lastLineIndex, column: lastLine.length)
editSession.moveCursorUp()
expect(editSession.getCursorScreenPosition().column).toBe 1
it "retains a goal column of 0 when moving back up", ->
lastLineIndex = buffer.getLines().length - 1
lastLine = buffer.lineForRow(lastLineIndex)
expect(lastLine.length).toBeGreaterThan(0)
editSession.setCursorScreenPosition(row: lastLineIndex, column: 0)
editSession.moveCursorDown()
editSession.moveCursorUp()
expect(editSession.getCursorScreenPosition().column).toBe 0
describe ".moveCursorLeft()", ->
it "moves the cursor by one column to the left", ->
editSession.setCursorScreenPosition([3, 3])
editSession.moveCursorLeft()
expect(editSession.getCursorScreenPosition()).toEqual [3, 2]
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", ->
editSession.setCursorScreenPosition(row: 1, column: 0)
editSession.moveCursorLeft()
expect(editSession.getCursorScreenPosition()).toEqual(row: 0, column: buffer.lineForRow(0).length)
describe "when the cursor is on the first line", ->
it "remains in the same position (0,0)", ->
editSession.setCursorScreenPosition(row: 0, column: 0)
editSession.moveCursorLeft()
expect(editSession.getCursorScreenPosition()).toEqual(row: 0, column: 0)
describe ".moveCursorRight()", ->
it "moves the cursor by one column to the right", ->
editSession.setCursorScreenPosition([3, 3])
editSession.moveCursorRight()
expect(editSession.getCursorScreenPosition()).toEqual [3, 4]
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", ->
editSession.setCursorScreenPosition(row: 0, column: buffer.lineForRow(0).length)
editSession.moveCursorRight()
expect(editSession.getCursorScreenPosition()).toEqual(row: 1, column: 0)
describe "when the cursor is on the last line", ->
it "remains in the same position", ->
lastLineIndex = buffer.getLines().length - 1
lastLine = buffer.lineForRow(lastLineIndex)
expect(lastLine.length).toBeGreaterThan(0)
lastPosition = { row: lastLineIndex, column: lastLine.length }
editSession.setCursorScreenPosition(lastPosition)
editSession.moveCursorRight()
expect(editSession.getCursorScreenPosition()).toEqual(lastPosition)
describe ".moveCursorToTop()", ->
it "moves the cursor to the top of the buffer", ->
editSession.setCursorScreenPosition [11,1]
editSession.addCursorAtScreenPosition [12,0]
editSession.moveCursorToTop()
expect(editSession.getCursors().length).toBe 1
expect(editSession.getCursorBufferPosition()).toEqual [0,0]
describe ".moveCursorToBottom()", ->
it "moves the cusor to the bottom of the buffer", ->
editSession.setCursorScreenPosition [0,0]
editSession.addCursorAtScreenPosition [1,0]
editSession.moveCursorToBottom()
expect(editSession.getCursors().length).toBe 1
expect(editSession.getCursorBufferPosition()).toEqual [12,2]
describe ".moveCursorToBeginningOfLine()", ->
it "moves cursor to the beginning of line", ->
editSession.setCursorScreenPosition [0,5]
editSession.addCursorAtScreenPosition [1,7]
editSession.moveCursorToBeginningOfLine()
expect(editSession.getCursors().length).toBe 2
[cursor1, cursor2] = editSession.getCursors()
expect(cursor1.getBufferPosition()).toEqual [0,0]
expect(cursor2.getBufferPosition()).toEqual [1,0]
describe ".moveCursorToEndOfLine()", ->
it "moves cursor to the end of line", ->
editSession.setCursorScreenPosition [0,0]
editSession.addCursorAtScreenPosition [1,0]
editSession.moveCursorToEndOfLine()
expect(editSession.getCursors().length).toBe 2
[cursor1, cursor2] = editSession.getCursors()
expect(cursor1.getBufferPosition()).toEqual [0,29]
expect(cursor2.getBufferPosition()).toEqual [1,30]
describe ".moveCursorToFirstCharacterOfLine()", ->
it "moves to the first character of the current line or the beginning of the line if it's already on the first character", ->
editSession.setCursorScreenPosition [0,5]
editSession.addCursorAtScreenPosition [1,7]
editSession.moveCursorToFirstCharacterOfLine()
[cursor1, cursor2] = editSession.getCursors()
expect(cursor1.getBufferPosition()).toEqual [0,0]
expect(cursor2.getBufferPosition()).toEqual [1,2]
editSession.moveCursorToFirstCharacterOfLine()
expect(cursor1.getBufferPosition()).toEqual [0,0]
expect(cursor2.getBufferPosition()).toEqual [1,0]
it "does not throw an exception on an empty line", ->
editSession.setCursorBufferPosition([10, 0])
editSession.moveCursorToFirstCharacterOfLine()
describe ".moveCursorToNextWord()", ->
it "moves the cursor to the next word or the end of file if there is no next word", ->
editSession.setCursorBufferPosition [2, 5]
editSession.addCursorAtBufferPosition [3, 60]
[cursor1, cursor2] = editSession.getCursors()
editSession.moveCursorToNextWord()
expect(cursor1.getBufferPosition()).toEqual [2, 7]
expect(cursor2.getBufferPosition()).toEqual [4, 4]
buffer.insert([12, 2], ' ')
cursor1.setBufferPosition([12, 1])
editSession.moveCursorToNextWord()
expect(cursor1.getBufferPosition()).toEqual [12, 5]
describe ".moveCursorToBeginningOfWord()", ->
it "moves the cursor to the beginning of the word", ->
editSession.setCursorBufferPosition [0, 8]
editSession.addCursorAtBufferPosition [1, 12]
editSession.addCursorAtBufferPosition [3, 0]
[cursor1, cursor2, cursor3] = editSession.getCursors()
editSession.moveCursorToBeginningOfWord()
expect(cursor1.getBufferPosition()).toEqual [0, 4]
expect(cursor2.getBufferPosition()).toEqual [1, 11]
expect(cursor3.getBufferPosition()).toEqual [2, 39]
it "does not fail at position [0, 0]", ->
editSession.setCursorBufferPosition([0, 0])
editSession.moveCursorToBeginningOfWord()
describe ".moveCursorToEndOfWord()", ->
it "moves the cursor to the end of the word", ->
editSession.setCursorBufferPosition [0, 6]
editSession.addCursorAtBufferPosition [1, 10]
editSession.addCursorAtBufferPosition [2, 40]
[cursor1, cursor2, cursor3] = editSession.getCursors()
editSession.moveCursorToEndOfWord()
expect(cursor1.getBufferPosition()).toEqual [0, 13]
expect(cursor2.getBufferPosition()).toEqual [1, 12]
expect(cursor3.getBufferPosition()).toEqual [3, 7]
describe "selection", ->
selection = null
beforeEach ->
selection = editSession.getSelection()
describe ".selectUp/Down/Left/Right()", ->
it "expands the selection to the cursor's new location", ->
editSession.setCursorScreenPosition(row: 1, column: 6)
expect(selection.isEmpty()).toBeTruthy()
editSession.selectRight()
expect(selection.isEmpty()).toBeFalsy()
range = selection.getScreenRange()
expect(range.start).toEqual(row: 1, column: 6)
expect(range.end).toEqual(row: 1, column: 7)
editSession.selectRight()
range = selection.getScreenRange()
expect(range.start).toEqual(row: 1, column: 6)
expect(range.end).toEqual(row: 1, column: 8)
editSession.selectDown()
range = selection.getScreenRange()
expect(range.start).toEqual(row: 1, column: 6)
expect(range.end).toEqual(row: 2, column: 8)
editSession.selectLeft()
range = selection.getScreenRange()
expect(range.start).toEqual(row: 1, column: 6)
expect(range.end).toEqual(row: 2, column: 7)
editSession.selectUp()
range = selection.getScreenRange()
expect(range.start).toEqual(row: 1, column: 6)
expect(range.end).toEqual(row: 1, column: 7)
describe ".selectToTop()", ->
it "selects text from cusor position to the top of the buffer", ->
editSession.setCursorScreenPosition [11,2]
editSession.addCursorAtScreenPosition [10,0]
editSession.selectToTop()
expect(editSession.getCursors().length).toBe 1
expect(editSession.getCursorBufferPosition()).toEqual [0,0]
expect(editSession.getSelection().getBufferRange()).toEqual [[0,0], [11,2]]
expect(editSession.getSelection().isReversed()).toBeTruthy()
describe ".selectToBottom()", ->
it "selects text from cusor position to the bottom of the buffer", ->
editSession.setCursorScreenPosition [10,0]
editSession.addCursorAtScreenPosition [9,3]
editSession.selectToBottom()
expect(editSession.getCursors().length).toBe 1
expect(editSession.getCursorBufferPosition()).toEqual [12,2]
expect(editSession.getSelection().getBufferRange()).toEqual [[9,3], [12,2]]
expect(editSession.getSelection().isReversed()).toBeFalsy()
describe ".selectAll()", ->
it "selects the entire buffer", ->
editSession.selectAll()
expect(editSession.getSelection().getBufferRange()).toEqual buffer.getRange()
describe ".selectToBeginningOfLine()", ->
it "selects text from cusor position to beginning of line", ->
editSession.setCursorScreenPosition [12,2]
editSession.addCursorAtScreenPosition [11,3]
editSession.selectToBeginningOfLine()
expect(editSession.getCursors().length).toBe 2
[cursor1, cursor2] = editSession.getCursors()
expect(cursor1.getBufferPosition()).toEqual [12,0]
expect(cursor2.getBufferPosition()).toEqual [11,0]
expect(editSession.getSelections().length).toBe 2
[selection1, selection2] = editSession.getSelections()
expect(selection1.getBufferRange()).toEqual [[12,0], [12,2]]
expect(selection1.isReversed()).toBeTruthy()
expect(selection2.getBufferRange()).toEqual [[11,0], [11,3]]
expect(selection2.isReversed()).toBeTruthy()
describe ".selectToEndOfLine()", ->
it "selects text from cusor position to end of line", ->
editSession.setCursorScreenPosition [12,0]
editSession.addCursorAtScreenPosition [11,3]
editSession.selectToEndOfLine()
expect(editSession.getCursors().length).toBe 2
[cursor1, cursor2] = editSession.getCursors()
expect(cursor1.getBufferPosition()).toEqual [12,2]
expect(cursor2.getBufferPosition()).toEqual [11,44]
expect(editSession.getSelections().length).toBe 2
[selection1, selection2] = editSession.getSelections()
expect(selection1.getBufferRange()).toEqual [[12,0], [12,2]]
expect(selection1.isReversed()).toBeFalsy()
expect(selection2.getBufferRange()).toEqual [[11,3], [11,44]]
expect(selection2.isReversed()).toBeFalsy()
describe ".selectToBeginningOfWord()", ->
it "selects text from cusor position to beginning of word", ->
editSession.setCursorScreenPosition [0,13]
editSession.addCursorAtScreenPosition [3,49]
editSession.selectToBeginningOfWord()
expect(editSession.getCursors().length).toBe 2
[cursor1, cursor2] = editSession.getCursors()
expect(cursor1.getBufferPosition()).toEqual [0,4]
expect(cursor2.getBufferPosition()).toEqual [3,47]
expect(editSession.getSelections().length).toBe 2
[selection1, selection2] = editSession.getSelections()
expect(selection1.getBufferRange()).toEqual [[0,4], [0,13]]
expect(selection1.isReversed()).toBeTruthy()
expect(selection2.getBufferRange()).toEqual [[3,47], [3,49]]
expect(selection2.isReversed()).toBeTruthy()
describe ".selectToEndOfWord()", ->
it "selects text from cusor position to end of word", ->
editSession.setCursorScreenPosition [0,4]
editSession.addCursorAtScreenPosition [3,48]
editSession.selectToEndOfWord()
expect(editSession.getCursors().length).toBe 2
[cursor1, cursor2] = editSession.getCursors()
expect(cursor1.getBufferPosition()).toEqual [0,13]
expect(cursor2.getBufferPosition()).toEqual [3,50]
expect(editSession.getSelections().length).toBe 2
[selection1, selection2] = editSession.getSelections()
expect(selection1.getBufferRange()).toEqual [[0,4], [0,13]]
expect(selection1.isReversed()).toBeFalsy()
expect(selection2.getBufferRange()).toEqual [[3,48], [3,50]]
expect(selection2.isReversed()).toBeFalsy()
describe "when the cursor is moved while there is a selection", ->
makeSelection = -> selection.setBufferRange [[1, 2], [1, 5]]
it "clears the selection", ->
makeSelection()
editSession.moveCursorDown()
expect(selection.isEmpty()).toBeTruthy()
makeSelection()
editSession.moveCursorUp()
expect(selection.isEmpty()).toBeTruthy()
makeSelection()
editSession.moveCursorLeft()
expect(selection.isEmpty()).toBeTruthy()
makeSelection()
editSession.moveCursorRight()
expect(selection.isEmpty()).toBeTruthy()
makeSelection()
editSession.setCursorScreenPosition([3, 3])
expect(selection.isEmpty()).toBeTruthy()