Implement backspace on Buffer

This commit is contained in:
Corey Johnson & Nathan Sobo 2012-01-25 14:33:47 -08:00
parent 045c3c39bf
commit 3a423e2a16
3 changed files with 34 additions and 2 deletions

View File

@ -69,6 +69,31 @@ describe 'Buffer', ->
expect(buffer.getLine(3)).toBe originalLine.substring(27)
expect(buffer.getLine(4)).toBe lineBelowOriginalLine
describe ".backspace(position)", ->
it "can remove a character from middle of line", ->
originalLineLength = buffer.getLine(1).length
expect(buffer.getLine(1).charAt(6)).toBe 's'
buffer.backspace({row: 1, col: 7})
expect(buffer.getLine(1).charAt(6)).toBe 'o'
expect(buffer.getLine(1).length).toBe originalLineLength - 1
it "can remove a character from the end of the line", ->
originalLineLength = buffer.getLine(1).length
expect(buffer.getLine(1).charAt(originalLineLength - 1)).toBe '{'
buffer.backspace({row: 1, col: originalLineLength})
expect(buffer.getLine(1).length).toBe originalLineLength - 1
expect(buffer.getLine(1).charAt(originalLineLength - 2)).toBe '{'
it "can remove a character from the begining of the line", ->
originalLineCount = buffer.getLines().length
originalLineLengthFromAbove = buffer.getLine(11).length
originalLineLength = buffer.getLine(12).length
expect(buffer.getLine(12).charAt(0)).toBe '}'
buffer.backspace({row: 12, col: 0})
expect(buffer.getLines().length).toBe originalLineCount - 1
expect(buffer.getLine(11).charAt(originalLineLengthFromAbove)).toBe '}'
expect(buffer.getLine(11).length).toBe originalLineLengthFromAbove + originalLineLength
describe ".save()", ->
describe "when the buffer has a path", ->
filePath = null

View File

@ -218,7 +218,7 @@ describe "Editor", ->
expect(editor).not.toMatchSelector ':focus'
expect(editor.hiddenInput).toMatchSelector ':focus'
fdescribe "when text input events are triggered on the hidden input element", ->
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(row: 1, col: 6)
@ -230,7 +230,7 @@ describe "Editor", ->
expect(editor.getPosition()).toEqual(row: 1, col: 7)
expect(editor.lines.find('pre:eq(1)')).toHaveText editor.getCurrentLine()
fdescribe "when return is pressed", ->
describe "when return is pressed", ->
describe "when the cursor is at the beginning of a line", ->
it "inserts an empty line before it", ->
editor.setPosition(row: 1, col: 0)

View File

@ -41,6 +41,13 @@ class Buffer
start: {row, col}
end: {row, col}
backspace: ({row, col}) ->
line = @lines[row]
if col == 0
@lines[row-1..row] = @lines[row - 1] + @lines[row]
else
@lines[row] = line[row..col] + line[col + 1..]
numLines: ->
@getLines().length