Editor only does not pre-build lines, it only builds them when they are visible on the screen

This commit is contained in:
Corey Johnson & Nathan Sobo
2012-05-10 10:40:16 -07:00
parent d76cc521bd
commit f6e9d99865
2 changed files with 19 additions and 29 deletions

View File

@@ -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

View File

@@ -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) =>