Optimize isFoldable

Signed-off-by: Nathan Sobo <nathan@github.com>
This commit is contained in:
Antonio Scandurra
2017-05-10 18:48:18 +02:00
committed by Nathan Sobo
parent 58a9682a0d
commit 3c87b7499e
2 changed files with 31 additions and 23 deletions

View File

@@ -268,7 +268,7 @@ class TokenizedBuffer extends Model
buildTokenizedLineForRowWithText: (row, text, ruleStack = @stackForRow(row - 1), openScopes = @openScopesForRow(row)) -> buildTokenizedLineForRowWithText: (row, text, ruleStack = @stackForRow(row - 1), openScopes = @openScopesForRow(row)) ->
lineEnding = @buffer.lineEndingForRow(row) lineEnding = @buffer.lineEndingForRow(row)
{tags, ruleStack} = @grammar.tokenizeLine(text, ruleStack, row is 0, false) {tags, ruleStack} = @grammar.tokenizeLine(text, ruleStack, row is 0, false)
new TokenizedLine({openScopes, text, tags, ruleStack, lineEnding, @tokenIterator}) new TokenizedLine({openScopes, text, tags, ruleStack, lineEnding, @tokenIterator, @grammar})
tokenizedLineForRow: (bufferRow) -> tokenizedLineForRow: (bufferRow) ->
if 0 <= bufferRow <= @buffer.getLastRow() if 0 <= bufferRow <= @buffer.getLastRow()
@@ -278,7 +278,7 @@ class TokenizedBuffer extends Model
text = @buffer.lineForRow(bufferRow) text = @buffer.lineForRow(bufferRow)
lineEnding = @buffer.lineEndingForRow(bufferRow) lineEnding = @buffer.lineEndingForRow(bufferRow)
tags = [@grammar.startIdForScope(@grammar.scopeName), text.length, @grammar.endIdForScope(@grammar.scopeName)] tags = [@grammar.startIdForScope(@grammar.scopeName), text.length, @grammar.endIdForScope(@grammar.scopeName)]
@tokenizedLines[bufferRow] = new TokenizedLine({openScopes: [], text, tags, lineEnding, @tokenIterator}) @tokenizedLines[bufferRow] = new TokenizedLine({openScopes: [], text, tags, lineEnding, @tokenIterator, @grammar})
tokenizedLinesForRows: (startRow, endRow) -> tokenizedLinesForRows: (startRow, endRow) ->
for row in [startRow..endRow] by 1 for row in [startRow..endRow] by 1
@@ -344,17 +344,16 @@ class TokenizedBuffer extends Model
@indentLevelForLine(line) @indentLevelForLine(line)
indentLevelForLine: (line) -> indentLevelForLine: (line) ->
if match = line.match(/^[\t ]+/) indentLength = 0
indentLength = 0 for char in line
for character in match[0] if char is '\t'
if character is '\t' indentLength += @getTabLength() - (indentLength % @getTabLength())
indentLength += @getTabLength() - (indentLength % @getTabLength()) else if char is ' '
else indentLength++
indentLength++ else
break
indentLength / @getTabLength() indentLength / @getTabLength()
else
0
scopeDescriptorForPosition: (position) -> scopeDescriptorForPosition: (position) ->
{row, column} = @buffer.clipPosition(Point.fromObject(position)) {row, column} = @buffer.clipPosition(Point.fromObject(position))

View File

@@ -1,5 +1,5 @@
Token = require './token' Token = require './token'
CommentScopeRegex = /(\b|\.)comment/ CommentScopeRegex = /(\b|\.)comment/
idCounter = 1 idCounter = 1
@@ -10,7 +10,7 @@ class TokenizedLine
return unless properties? return unless properties?
{@openScopes, @text, @tags, @ruleStack, @tokenIterator} = properties {@openScopes, @text, @tags, @ruleStack, @tokenIterator, @grammar} = properties
getTokenIterator: -> @tokenIterator.reset(this) getTokenIterator: -> @tokenIterator.reset(this)
@@ -48,17 +48,26 @@ class TokenizedLine
return @isCommentLine if @isCommentLine? return @isCommentLine if @isCommentLine?
@isCommentLine = false @isCommentLine = false
iterator = @getTokenIterator()
while iterator.next() for tag in @openScopes
scopes = iterator.getScopes() if @isCommentOpenTag(tag)
continue if scopes.length is 1 @isCommentLine = true
for scope in scopes return @isCommentLine
if CommentScopeRegex.test(scope)
@isCommentLine = true for tag in @tags
break if @isCommentOpenTag(tag)
break @isCommentLine = true
return @isCommentLine
@isCommentLine @isCommentLine
isCommentOpenTag: (tag) ->
if tag < 0 and (tag & 1) is 1
scope = @grammar.scopeForId(tag)
if CommentScopeRegex.test(scope)
return true
false
tokenAtIndex: (index) -> tokenAtIndex: (index) ->
@tokens[index] @tokens[index]