From c536933c0b247caed6cfb4ebfbd65ae04366e46c Mon Sep 17 00:00:00 2001 From: Corey Johnson & Nathan Sobo Date: Wed, 25 Jan 2012 12:58:36 -0800 Subject: [PATCH] Newline chars can be inserted into the buffer. --- spec/atom/buffer-spec.coffee | 41 ++++++++++++++++++++++++------------ src/atom/buffer.coffee | 13 ++++++++---- 2 files changed, 37 insertions(+), 17 deletions(-) diff --git a/spec/atom/buffer-spec.coffee b/spec/atom/buffer-spec.coffee index c74c03c0a..431ee85af 100644 --- a/spec/atom/buffer-spec.coffee +++ b/spec/atom/buffer-spec.coffee @@ -36,23 +36,38 @@ describe 'Buffer', -> expect(buffer.getLines().join('\n')).toBe fileContents describe "insert(position, string)", -> - it "inserts the given string at the given position", -> - expect(buffer.getLine(1).charAt(6)).not.toBe 'q' - buffer.insert({row: 1, col: 6}, 'q') - expect(buffer.getLine(1).charAt(6)).toBe 'q' + describe "when inserting a single character", -> + it "inserts the given string at the given position", -> + expect(buffer.getLine(1).charAt(6)).not.toBe '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 + it "emits an event with the range of the change and the new text", -> + insertHandler = jasmine.createSpy 'insertHandler' + buffer.on 'insert', insertHandler - buffer.insert({row: 1, col: 6}, 'q') + buffer.insert({row: 1, col: 6}, 'q') - expect(insertHandler).toHaveBeenCalled() - [event] = insertHandler.argsForCall[0] + expect(insertHandler).toHaveBeenCalled() + [event] = insertHandler.argsForCall[0] - expect(event.range.start).toEqual(row: 1, col: 6) - expect(event.range.end).toEqual(row: 1, col: 6) - expect(event.string).toBe 'q' + expect(event.range.start).toEqual(row: 1, col: 6) + expect(event.range.end).toEqual(row: 1, col: 6) + expect(event.string).toBe 'q' + + describe "when inserting a newline", -> + it "splits the portion of the line following the given position onto the next line", -> + initialLineCount = buffer.getLines().length + + originalLine = buffer.getLine(2) + lineBelowOriginalLine = buffer.getLine(3) + + buffer.insert({row: 2, col: 27}, '\n') + + expect(buffer.getLines().length).toBe(initialLineCount + 1) + expect(buffer.getLine(2)).toBe originalLine.substring(0, 27) + expect(buffer.getLine(3)).toBe originalLine.substring(27) + expect(buffer.getLine(4)).toBe lineBelowOriginalLine describe ".save()", -> describe "when the buffer has a path", -> diff --git a/src/atom/buffer.coffee b/src/atom/buffer.coffee index 60fda8e9e..cbb7eaf55 100644 --- a/src/atom/buffer.coffee +++ b/src/atom/buffer.coffee @@ -25,10 +25,15 @@ class Buffer @lines[n] insert: ({row, col}, string) -> - line = @getLine(row) - before = line.substring(0, col) - after = line.substring(col) - @lines[row] = before + string + after + originalLine = @getLine(row) + originalPrefix = originalLine[0...col] + originalSuffix = originalLine[col..] + + if string == '\n' + @lines[row] = originalPrefix + @lines[row + 1...row + 1] = originalSuffix + else + @lines[row] = originalPrefix + string + originalSuffix @trigger 'insert' string: string