From f6e9d998651521311d3eeb6bfc4c39d798fc1502 Mon Sep 17 00:00:00 2001 From: Corey Johnson & Nathan Sobo Date: Thu, 10 May 2012 10:40:16 -0700 Subject: [PATCH] Editor only does not pre-build lines, it only builds them when they are visible on the screen --- spec/app/editor-spec.coffee | 16 ++++++++-------- src/app/editor.coffee | 32 +++++++++++--------------------- 2 files changed, 19 insertions(+), 29 deletions(-) diff --git a/spec/app/editor-spec.coffee b/spec/app/editor-spec.coffee index 5f299ffa7..39329c9d5 100644 --- a/spec/app/editor-spec.coffee +++ b/spec/app/editor-spec.coffee @@ -25,8 +25,8 @@ describe "Editor", -> editor = rootView.activeEditor() buffer = editor.buffer - editor.attachToDom = (options={}) -> - heightInLines = options.heightInLines ? this.buffer.numLines() + editor.attachToDom = ({ heightInLines } = {}) -> + heightInLines ?= this.buffer.numLines() this.height(getLineHeight() * heightInLines) $('#jasmine-content').append(this) @@ -60,7 +60,7 @@ describe "Editor", -> expect(newEditor.editSessions[0]).toEqual(editor.editSessions[0]) expect(newEditor.editSessions[0]).not.toBe(editor.editSessions[0]) - describe ".setBuffer(buffer)", -> + describe ".setBuffer(buffer)", -> it "sets the cursor to the beginning of the file", -> expect(editor.getCursorScreenPosition()).toEqual(row: 0, column: 0) @@ -392,7 +392,7 @@ describe "Editor", -> otherEditor.simulateDomAttachment() expect(otherEditor.setMaxLineLength).toHaveBeenCalled() - fdescribe "when some lines at the end of the buffer are not visible on screen", -> + describe "when some lines at the end of the buffer are not visible on screen", -> beforeEach -> editor.attachToDom(heightInLines: 5.5) @@ -405,13 +405,13 @@ describe "Editor", -> editor.scroller.scrollTop(editor.lineHeight * 2.5) editor.scroller.trigger 'scroll' - expect(editor.lines.find('.line').length).toBe 6 - expect(editor.lines.find('.line:first')).toHaveText buffer.lineForRow(2) - expect(editor.lines.find('.line:last')).toHaveText buffer.lineForRow(7) + expect(editor.lines.find('.line').length).toBe 8 + expect(editor.lines.find('.line:first').text()).toBe buffer.lineForRow(0) + expect(editor.lines.find('.line:last').text()).toBe buffer.lineForRow(7) for line, index in editor.lines.find('.line') marginBottom = $(line).css('margin-bottom') - if index == 5 + if index == 7 expectedMarginBottom = (buffer.numLines() - 8) * editor.lineHeight expect(marginBottom).toBe "#{expectedMarginBottom}px" else diff --git a/src/app/editor.coffee b/src/app/editor.coffee index a01ec0942..20f2c2c34 100644 --- a/src/app/editor.coffee +++ b/src/app/editor.coffee @@ -237,34 +237,26 @@ class Editor extends View @lineCache = [] @lines.find('.line').remove() - lastVisibleRow = @getLastVisibleRow() - @firstRenderedScreenRow = 0 - @lastRenderedScreenRow = lastVisibleRow + lastVisibleScreenRow = @getLastVisibleScreenRow() - lineElements = @buildLineElements(0, lastVisibleRow) - lineElements.last().css('margin-bottom', (@getLastScreenRow() - lastVisibleRow) * @lineHeight) + lineElements = @buildLineElements(0, lastVisibleScreenRow) + lineElements.last().css('margin-bottom', (@getLastScreenRow() - lastVisibleScreenRow) * @lineHeight) @insertLineElements(0, lineElements) updateLines: -> - firstVisibleRow = @getFirstVisibleRow() - lastVisibleRow = @getLastVisibleRow() + lastVisibleScreenRow = @getLastVisibleScreenRow() + highestRenderedScreenRow = @lines.find('.line').length - 1 - # if @firstRenderedScreenRow < firstVisibleRow - # @lines.find('.line:first').remove() for row in [@firstRenderedScreenRow...firstVisibleRow] - # @firstRenderedScreenRow = firstVisibleRow - - if @lastRenderedScreenRow < lastVisibleRow + if lastVisibleScreenRow > highestRenderedScreenRow @lines.find('.line:last').css('margin-bottom', 'inherit') - console.log "building line elements for", @lastRenderedScreenRow + 1, lastVisibleRow - lineElements = @buildLineElements(@lastRenderedScreenRow + 1, lastVisibleRow) - lineElements.last().css('margin-bottom', (@getLastScreenRow() - lastVisibleRow) * @lineHeight) - @insertLineElements(@lastRenderedScreenRow + 1, lineElements) - @lastRenderedScreenRow = lastVisibleRow + lineElements = @buildLineElements(highestRenderedScreenRow + 1, lastVisibleScreenRow) + lineElements.last().css('margin-bottom', (@getLastScreenRow() - lastVisibleScreenRow) * @lineHeight) + @insertLineElements(highestRenderedScreenRow + 1, lineElements) - getFirstVisibleRow: -> + getFirstVisibleScreenRow: -> Math.floor(@scroller.scrollTop() / @lineHeight) - getLastVisibleRow: -> + getLastVisibleScreenRow: -> Math.ceil((@scroller.scrollTop() + @scroller.height()) / @lineHeight) - 1 getScreenLines: -> @@ -375,7 +367,6 @@ class Editor extends View @spliceLineElements(startRow, endRow - startRow + 1, lineElements) spliceLineElements: (startRow, rowCount, lineElements) -> - console.log "splice", startRow, rowCount, lineElements.length endRow = startRow + rowCount elementToInsertBefore = @lineCache[startRow] elementsToReplace = @lineCache[startRow...endRow] @@ -388,7 +379,6 @@ class Editor extends View if elementToInsertBefore lines.insertBefore(fragment, elementToInsertBefore) else - console.log "appending child at start row", startRow, lineElements.text() lines.appendChild(fragment) elementsToReplace.forEach (element) =>