From 43ac7edf0fe3a2d3bcd708c7eeedd312a855df5f Mon Sep 17 00:00:00 2001 From: Corey Johnson & Nathan Sobo Date: Thu, 10 May 2012 15:15:34 -0600 Subject: [PATCH] Scrolling to bottom renders correct lines When we scroll more than a single screen's worth of lines, the new first visible screen row ends up exceeding the previous last rendered screen row, so we need to use the maximum of the two when deciding which new rows to render. --- spec/app/editor-spec.coffee | 10 +++++++++- src/app/editor.coffee | 23 +++++++++++++---------- 2 files changed, 22 insertions(+), 11 deletions(-) diff --git a/spec/app/editor-spec.coffee b/spec/app/editor-spec.coffee index 686a6b1d8..1df99234f 100644 --- a/spec/app/editor-spec.coffee +++ b/spec/app/editor-spec.coffee @@ -401,7 +401,7 @@ describe "Editor", -> expectedMarginBottom = (buffer.numLines() - 6) * editor.lineHeight expect(editor.lines.find('.line:last').css('margin-bottom')).toBe "#{expectedMarginBottom}px" - fit "when the lines are scrolled down, removes lines that become invisible and build lines that are become visisble", -> + it "when the lines are scrolled down, removes lines that become invisible and builds lines that become visisble", -> editor.scroller.scrollTop(editor.lineHeight * 2.5) editor.scroller.trigger 'scroll' @@ -416,6 +416,14 @@ describe "Editor", -> expect(editor.lines.find('.line:first').text()).toBe buffer.lineForRow(3) expect(editor.lines.find('.line:last').text()).toBe buffer.lineForRow(8) + fit "when the scroller is scrolled to the end, removes lines that become invisible and builds lines that become visible", -> + editor.scroller.scrollBottom(editor.scroller[0].scrollHeight) + editor.scroller.trigger 'scroll' + + expect(editor.lines.find('.line').length).toBe 6 + expect(editor.lines.find('.line:first').text()).toBe buffer.lineForRow(7) + expect(editor.lines.find('.line:last').text()).toBe buffer.lineForRow(12) + it "adjusts margins to account for non-rendered lines", -> editor.scroller.scrollTop(editor.lineHeight * 2.5) editor.scroller.trigger 'scroll' diff --git a/src/app/editor.coffee b/src/app/editor.coffee index f90d2dfdf..aed981e1f 100644 --- a/src/app/editor.coffee +++ b/src/app/editor.coffee @@ -243,27 +243,30 @@ class Editor extends View @firstRenderedScreenRow = 0 @lastRenderedScreenRow = @getLastVisibleScreenRow() - lineElements = @buildLineElements(@firstRenderedScreenRow, @lastRenderedScreenRow) - lineElements.last().css('margin-bottom', (@getLastScreenRow() - @lastRenderedScreenRow) * @lineHeight) - @insertLineElements(0, lineElements) + @insertLineElements(0, @buildLineElements(@firstRenderedScreenRow, @lastRenderedScreenRow)) + @lines.css('padding-bottom', (@getLastScreenRow() - @lastRenderedScreenRow) * @lineHeight) updateLines: -> firstVisibleScreenRow = @getFirstVisibleScreenRow() lastVisibleScreenRow = @getLastVisibleScreenRow() + console.log "updateLines", firstVisibleScreenRow, lastVisibleScreenRow + # console.log "cursor screen position", @getCursorScreenPosition().inspect() + if firstVisibleScreenRow > @firstRenderedScreenRow - @lines.find('.line:first').css('margin-top', 'inherit') + console.log "removing from", @firstRenderedScreenRow, "to", firstVisibleScreenRow - 1 @removeLineElements(@firstRenderedScreenRow, firstVisibleScreenRow - 1) - @lines.find('.line:first').css('margin-top', firstVisibleScreenRow * @lineHeight) + @lines.css('padding-top', firstVisibleScreenRow * @lineHeight) + console.log @lines if lastVisibleScreenRow > @lastRenderedScreenRow - @lines.find('.line:last').css('margin-bottom', 'inherit') - lineElements = @buildLineElements(@lastRenderedScreenRow + 1, lastVisibleScreenRow) - lineElements.last().css('margin-bottom', (@getLastScreenRow() - lastVisibleScreenRow) * @lineHeight) - @insertLineElements(@lastRenderedScreenRow + 1, lineElements) - @lastRenderedScreenRow = lastVisibleScreenRow + startRow = Math.max(@lastRenderedScreenRow + 1, firstVisibleScreenRow) + lineElements = @buildLineElements(startRow, lastVisibleScreenRow) + @insertLineElements(startRow, lineElements) + @lines.css('padding-bottom', (@getLastScreenRow() - lastVisibleScreenRow) * @lineHeight) @firstRenderedScreenRow = firstVisibleScreenRow + @lastRenderedScreenRow = lastVisibleScreenRow getFirstVisibleScreenRow: -> Math.floor(@scroller.scrollTop() / @lineHeight)