From 86cc45f4da05e26f3cc9e95050444e42effeadab Mon Sep 17 00:00:00 2001 From: Nathan Sobo Date: Mon, 12 Mar 2012 14:21:00 -0600 Subject: [PATCH] Add Editor.spliceLineELements --- spec/atom/editor-spec.coffee | 57 ++++++++++++++++++++++++++++++++++++ src/atom/editor.coffee | 18 ++++++++++++ 2 files changed, 75 insertions(+) diff --git a/spec/atom/editor-spec.coffee b/spec/atom/editor-spec.coffee index cb3e0d880..a16e8a2e1 100644 --- a/spec/atom/editor-spec.coffee +++ b/spec/atom/editor-spec.coffee @@ -2,6 +2,7 @@ Buffer = require 'buffer' Editor = require 'editor' Range = require 'range' $ = require 'jquery' +{$$} = require 'space-pen' _ = require 'underscore' fs = require 'fs' @@ -1000,3 +1001,59 @@ describe "Editor", -> selectedFilePath = null editor.save() expect(fs.exists(selectedFilePath)).toBeFalsy() + + describe ".spliceLineElements(startRow, rowCount, lineElements)", -> + elements = null + + beforeEach -> + elements = $$ -> + @div "A", class: 'line' + @div "B", class: 'line' + + describe "when the start row is 0", -> + describe "when the row count is 0", -> + it "inserts the given elements before the first row", -> + editor.spliceLineElements 0, 0, elements + + expect(editor.lines.find('.line:eq(0)').text()).toBe 'A' + expect(editor.lines.find('.line:eq(1)').text()).toBe 'B' + expect(editor.lines.find('.line:eq(2)').text()).toBe 'var quicksort = function () {' + + describe "when the row count is > 0", -> + it "replaces the initial rows with the given elements", -> + editor.spliceLineElements 0, 2, elements + + expect(editor.lines.find('.line:eq(0)').text()).toBe 'A' + expect(editor.lines.find('.line:eq(1)').text()).toBe 'B' + expect(editor.lines.find('.line:eq(2)').text()).toBe ' if (items.length <= 1) return items;' + + describe "when the start row is less than the last row", -> + describe "when the row count is 0", -> + it "inserts the elements at the specified location", -> + editor.spliceLineElements 2, 0, elements + + expect(editor.lines.find('.line:eq(2)').text()).toBe 'A' + expect(editor.lines.find('.line:eq(3)').text()).toBe 'B' + expect(editor.lines.find('.line:eq(4)').text()).toBe ' if (items.length <= 1) return items;' + + describe "when the row count is > 0", -> + it "replaces the elements at the specified location", -> + editor.spliceLineElements 2, 2, elements + + expect(editor.lines.find('.line:eq(2)').text()).toBe 'A' + expect(editor.lines.find('.line:eq(3)').text()).toBe 'B' + expect(editor.lines.find('.line:eq(4)').text()).toBe ' while(items.length > 0) {' + + describe "when the start row is the last row", -> + it "appends the elements to the end of the lines", -> + editor.spliceLineElements 13, 0, elements + + expect(editor.lines.find('.line:eq(12)').text()).toBe '};' + expect(editor.lines.find('.line:eq(13)').text()).toBe 'A' + expect(editor.lines.find('.line:eq(14)').text()).toBe 'B' + expect(editor.lines.find('.line:eq(15)')).not.toExist() + + + + + diff --git a/src/atom/editor.coffee b/src/atom/editor.coffee index fff16b288..971a45308 100644 --- a/src/atom/editor.coffee +++ b/src/atom/editor.coffee @@ -205,6 +205,24 @@ class Editor extends View else @updateLineElement(row, screenLines.shift()) + + spliceLineElements: (startRow, rowCount, lineElements) -> + endRow = startRow + rowCount + insertBeforeElement = @lineCache[startRow] + removeElements = $(_.map @lineCache[startRow...endRow], (elt) -> elt[0]) + @lineCache[startRow...endRow] = lineElements or [] + + if lineElements + if removeElements.length + removeElements.replaceWith(lineElements) + else if insertBeforeElement + insertBeforeElement.before(lineElements) + else + @lines.append(lineElements) + else + removeElements.remove() + + updateLineElement: (row, screenLine) -> if @lineCache.length == 0 @insertLineElement(row, screenLine)