Skip to content

Commit

Permalink
🐎 Fetch scopes only if required
Browse files Browse the repository at this point in the history
  • Loading branch information
as-cii committed Oct 16, 2015
1 parent 3f53a72 commit e843c2f
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 22 deletions.
2 changes: 1 addition & 1 deletion src/display-buffer.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ class DisplayBuffer extends Model

getSoftWrapColumnForTokenizedLine: (tokenizedLine) ->
lineMaxWidth = @getSoftWrapColumn() * @getDefaultCharWidth()
iterator = tokenizedLine.getTokenIterator()
iterator = tokenizedLine.getTokenIterator(false)
column = 0
currentWidth = 0
while iterator.next()
Expand Down
4 changes: 2 additions & 2 deletions src/lines-yardstick.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class LinesYardstick
previousColumn = 0
previousLeft = 0

@tokenIterator.reset(line)
@tokenIterator.reset(line, false)
while @tokenIterator.next()
text = @tokenIterator.getText()
textIndex = 0
Expand Down Expand Up @@ -112,7 +112,7 @@ class LinesYardstick
indexWithinTextNode = null
charIndex = 0

@tokenIterator.reset(line)
@tokenIterator.reset(line, false)
while @tokenIterator.next()
break if foundIndexWithinTextNode?

Expand Down
45 changes: 27 additions & 18 deletions src/token-iterator.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -3,45 +3,33 @@

module.exports =
class TokenIterator
constructor: ({@grammarRegistry}, line) ->
@reset(line) if line?
constructor: ({@grammarRegistry}, line, enableScopes) ->
@reset(line, enableScopes) if line?

reset: (@line) ->
reset: (@line, @enableScopes=true) ->
@index = null
@bufferStart = @line.startBufferColumn
@bufferEnd = @bufferStart
@screenStart = 0
@screenEnd = 0
@scopes = @line.openScopes.map (id) => @grammarRegistry.scopeForId(id)
@scopeStarts = @scopes.slice()
@scopeEnds = []
@resetScopes() if @enableScopes
this

next: ->
{tags} = @line

if @index?
@index++
@scopeEnds.length = 0
@scopeStarts.length = 0
@bufferStart = @bufferEnd
@screenStart = @screenEnd
@clearScopeStartsAndEnds() if @enableScopes
else
@index = 0

while @index < tags.length
tag = tags[@index]
if tag < 0
scope = @grammarRegistry.scopeForId(tag)
if tag % 2 is 0
if @scopeStarts[@scopeStarts.length - 1] is scope
@scopeStarts.pop()
else
@scopeEnds.push(scope)
@scopes.pop()
else
@scopeStarts.push(scope)
@scopes.push(scope)
@handleScopeForTag(tag) if @enableScopes
@index++
else
if @isHardTab()
Expand All @@ -59,6 +47,27 @@ class TokenIterator

false

resetScopes: ->
@scopes = @line.openScopes.map (id) => @grammarRegistry.scopeForId(id)
@scopeStarts = @scopes.slice()
@scopeEnds = []

clearScopeStartsAndEnds: ->
@scopeEnds.length = 0
@scopeStarts.length = 0

handleScopeForTag: (tag) ->
scope = @grammarRegistry.scopeForId(tag)
if tag % 2 is 0
if @scopeStarts[@scopeStarts.length - 1] is scope
@scopeStarts.pop()
else
@scopeEnds.push(scope)
@scopes.pop()
else
@scopeStarts.push(scope)
@scopes.push(scope)

getBufferStart: -> @bufferStart
getBufferEnd: -> @bufferEnd

Expand Down
2 changes: 1 addition & 1 deletion src/tokenized-line.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ class TokenizedLine
@lineIsWhitespaceOnly = true
@firstTrailingWhitespaceIndex = 0

getTokenIterator: -> @tokenIterator.reset(this)
getTokenIterator: -> @tokenIterator.reset(this, arguments...)

Object.defineProperty @prototype, 'tokens', get: ->
iterator = @getTokenIterator()
Expand Down

0 comments on commit e843c2f

Please sign in to comment.