Optimize highlightCursorLine by using cached element references

This commit is contained in:
Nathan Sobo
2012-11-01 13:51:22 -06:00
parent 5249e5e69c
commit 2efc91aad1
4 changed files with 22 additions and 15 deletions

View File

@@ -50,6 +50,9 @@ class Anchor
getScreenPosition: ->
@screenPosition
getScreenRow: ->
@screenPosition.row
setScreenPosition: (position, options={}) ->
previousScreenPosition = @screenPosition
@screenPosition = Point.fromObject(position)

View File

@@ -31,6 +31,9 @@ class Cursor
getScreenPosition: ->
@anchor.getScreenPosition()
getScreenRow: ->
@anchor.getScreenRow()
setBufferPosition: (bufferPosition, options) ->
@goalColumn = null
@clearSelection()

View File

@@ -430,6 +430,9 @@ class EditSession
getCursorScreenPosition: ->
@getLastCursor().getScreenPosition()
getCursorScreenRow: ->
@getLastCursor().getScreenRow()
setCursorBufferPosition: (position, options) ->
@moveCursors (cursor) -> cursor.setBufferPosition(position, options)

View File

@@ -178,6 +178,7 @@ class Editor extends View
moveCursorToEndOfLine: -> @activeEditSession.moveCursorToEndOfLine()
setCursorScreenPosition: (position) -> @activeEditSession.setCursorScreenPosition(position)
getCursorScreenPosition: -> @activeEditSession.getCursorScreenPosition()
getCursorScreenRow: -> @activeEditSession.getCursorScreenRow()
setCursorBufferPosition: (position, options) -> @activeEditSession.setCursorBufferPosition(position, options)
getCursorBufferPosition: -> @activeEditSession.getCursorBufferPosition()
@@ -344,7 +345,7 @@ class Editor extends View
else
@gutter.addClass('drop-shadow')
@on 'cursor-move', => @highlightCursorLine()
@on 'cursor-move', ({bufferChanged}) => @highlightCursorLine()
@on 'selection-change', => @highlightCursorLine()
selectOnMousemoveUntilMouseup: ->
@@ -638,6 +639,7 @@ class Editor extends View
@setScrollPositionFromActiveEditSession()
@renderLines()
@highlightCursorLine()
@activeEditSession.on 'screen-lines-change', (e) => @handleDisplayBufferChange(e)
getCursorView: (index) ->
@@ -817,6 +819,8 @@ class Editor extends View
@lastRenderedScreenRow = maxEndRow
@updatePaddingOfRenderedLines()
@highlightCursorLine()
buildLineElements: (startRow, endRow) ->
charWidth = @charWidth
charHeight = @charHeight
@@ -825,19 +829,15 @@ class Editor extends View
cursorScreenRow = @getCursorScreenPosition().row
mini = @mini
buildLineHtml = (line, lineClasses) => @buildLineHtml(line, lineClasses)
buildLineHtml = (line) => @buildLineHtml(line)
$$ ->
row = startRow
for line in lines
if mini or row isnt cursorScreenRow
lineClasses = null
else
lineClasses = ' cursor-line'
@raw(buildLineHtml(line, lineClasses))
@raw(buildLineHtml(line))
row++
buildLineHtml: (screenLine, lineClasses) ->
buildLineHtml: (screenLine) ->
scopeStack = []
line = []
@@ -869,8 +869,6 @@ class Editor extends View
else
lineAttributes = { class: 'line' }
lineAttributes.class += lineClasses if lineClasses
attributePairs = []
attributePairs.push "#{attributeName}=\"#{value}\"" for attributeName, value of lineAttributes
line.push("<pre #{attributePairs.join(' ')}>")
@@ -965,9 +963,9 @@ class Editor extends View
highlightCursorLine: ->
return if @mini
@cursorScreenRow = @getCursorScreenPosition().row
screenRow = @cursorScreenRow - @firstRenderedScreenRow
@find('pre.line.cursor-line').removeClass('cursor-line')
@highlightedLine?.removeClass('cursor-line')
if @getSelection().isSingleScreenLine()
@find("pre.line:eq(#{screenRow})").addClass('cursor-line')
@highlightedLine = @lineElementForScreenRow(@getCursorScreenRow())
@highlightedLine.addClass('cursor-line')
else
@highlightedLine = null