From 5ae40a3cb84ce33754d97c9ee42eb23e91307e71 Mon Sep 17 00:00:00 2001 From: Corey Johnson & Nathan Sobo Date: Wed, 9 May 2012 19:19:54 -0600 Subject: [PATCH] Editor only renders lines when it is attached to the DOM --- spec/app/editor-spec.coffee | 31 ++++++++++++++++++++++--------- src/app/editor.coffee | 10 ++++++---- 2 files changed, 28 insertions(+), 13 deletions(-) diff --git a/spec/app/editor-spec.coffee b/spec/app/editor-spec.coffee index 31db2d5b9..3e98726ba 100644 --- a/spec/app/editor-spec.coffee +++ b/spec/app/editor-spec.coffee @@ -24,6 +24,7 @@ describe "Editor", -> describe "construction", -> it "assigns an empty buffer and correctly handles text input (regression coverage)", -> editor = new Editor + editor.attachToDom() expect(editor.buffer.getPath()).toBeUndefined() expect(editor.lines.find('.line').length).toBe 1 editor.insertText('x') @@ -61,6 +62,9 @@ describe "Editor", -> expect(openHandler).not.toHaveBeenCalled() describe "text rendering", -> + beforeEach -> + editor.attachToDom() + it "creates a line element for each line in the buffer with the html-escaped text of the line", -> expect(editor.lines.find('.line').length).toEqual(buffer.numLines()) expect(buffer.lineForRow(2)).toContain('<') @@ -97,16 +101,8 @@ describe "Editor", -> describe "when soft-wrap is enabled", -> beforeEach -> - otherEditor = new Editor - otherEditor.setBuffer editor.buffer - otherEditor.attachToDom() - charWidth = otherEditor.charWidth - linesPositionLeft = otherEditor.lines.position().left - otherEditor.remove() - editor.width(charWidth * 50 + linesPositionLeft) + setEditorWidthInChars(editor, 50) editor.setSoftWrap(true) - editor.attachToDom() - expect(editor.renderer.maxLineLength).toBe 50 it "wraps lines that are too long to fit within the editor's width, adjusting cursor positioning accordingly", -> @@ -176,6 +172,16 @@ describe "Editor", -> editor.moveCursorRight() expect(editor.getCursorScreenPosition()).toEqual [11, 0] + it "calls .setMaxLineLength() when the editor is attached because now its dimensions are available to calculate it", -> + otherEditor = new Editor() + spyOn(otherEditor, 'setMaxLineLength') + + otherEditor.setSoftWrap(true) + expect(otherEditor.setMaxLineLength).not.toHaveBeenCalled() + + otherEditor.simulateDomAttachment() + expect(otherEditor.setMaxLineLength).toHaveBeenCalled() + describe "gutter rendering", -> it "creates a line number element for each line in the buffer", -> expect(editor.gutter.find('.line-number').length).toEqual(buffer.numLines()) @@ -1531,6 +1537,9 @@ describe "Editor", -> expect(selections[0].getBufferRange()).toEqual [[4,7], [5,27]] describe "buffer manipulation", -> + beforeEach -> + editor.attachToDom() + describe "when text input events are triggered on the hidden input element", -> describe "when there is no selection", -> it "inserts the typed character at the cursor position, both in the buffer and the pre element", -> @@ -1995,6 +2004,9 @@ describe "Editor", -> expect(buffer.lineForRow(1)).toBe " var first = function(items) {" describe "folding", -> + beforeEach -> + editor.attachToDom() + describe "when a fold-selection event is triggered", -> it "folds the selected text and moves the cursor to just after the placeholder, then treats the placeholder as a single character", -> editor.getSelection().setBufferRange(new Range([4, 29], [7, 4])) @@ -2087,6 +2099,7 @@ describe "Editor", -> elements = null beforeEach -> + editor.attachToDom() elements = $$ -> @div "A", class: 'line' @div "B", class: 'line' diff --git a/src/app/editor.coffee b/src/app/editor.coffee index 3da382b5f..aeb7f3ce8 100644 --- a/src/app/editor.coffee +++ b/src/app/editor.coffee @@ -214,6 +214,7 @@ class Editor extends View @attached = true @subscribeToFontSize() @calculateDimensions() + @renderLines() @hiddenInput.width(@charWidth) @setMaxLineLength() if @softWrap @focus() if @isFocused @@ -258,7 +259,7 @@ class Editor extends View @buffer.on "path-change.editor#{@id}", => @trigger 'editor-path-change' @renderer = new Renderer(@buffer, { maxLineLength: @calcMaxLineLength(), tabText: @tabText }) - @renderLines() + @renderLines() if @attached @gutter.renderLineNumbers() @loadEditSessionForBuffer(@buffer) @@ -316,8 +317,9 @@ class Editor extends View @compositeCursor.updateBufferPosition() unless e.bufferChanged - lineElements = @buildLineElements(newRange.start.row, newRange.end.row) - @replaceLineElements(oldRange.start.row, oldRange.end.row, lineElements) + if @attached + lineElements = @buildLineElements(newRange.start.row, newRange.end.row) + @replaceLineElements(oldRange.start.row, oldRange.end.row, lineElements) buildLineElements: (startRow, endRow) -> charWidth = @charWidth @@ -380,7 +382,7 @@ class Editor extends View @renderer.createFold(range) setSoftWrap: (@softWrap, maxLineLength=undefined) -> - @setMaxLineLength(maxLineLength) + @setMaxLineLength(maxLineLength) if @attached if @softWrap @addClass 'soft-wrap' @_setMaxLineLength = => @setMaxLineLength()