diff --git a/src/token-iterator.coffee b/src/token-iterator.coffee deleted file mode 100644 index f836d33d4..000000000 --- a/src/token-iterator.coffee +++ /dev/null @@ -1,56 +0,0 @@ -module.exports = -class TokenIterator - constructor: (@tokenizedBuffer) -> - - reset: (@line) -> - @index = null - @startColumn = 0 - @endColumn = 0 - @scopes = @line.openScopes.map (id) => @tokenizedBuffer.grammar.scopeForId(id) - @scopeStarts = @scopes.slice() - @scopeEnds = [] - this - - next: -> - {tags} = @line - - if @index? - @startColumn = @endColumn - @scopeEnds.length = 0 - @scopeStarts.length = 0 - @index++ - else - @index = 0 - - while @index < tags.length - tag = tags[@index] - if tag < 0 - scope = @tokenizedBuffer.grammar.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) - @index++ - else - @endColumn += tag - @text = @line.text.substring(@startColumn, @endColumn) - return true - - false - - getScopes: -> @scopes - - getScopeStarts: -> @scopeStarts - - getScopeEnds: -> @scopeEnds - - getText: -> @text - - getBufferStart: -> @startColumn - - getBufferEnd: -> @endColumn diff --git a/src/token-iterator.js b/src/token-iterator.js new file mode 100644 index 000000000..a698fc748 --- /dev/null +++ b/src/token-iterator.js @@ -0,0 +1,79 @@ +module.exports = +class TokenIterator { + constructor (tokenizedBuffer) { + this.tokenizedBuffer = tokenizedBuffer + } + + reset (line) { + this.line = line + this.index = null + this.startColumn = 0 + this.endColumn = 0 + this.scopes = this.line.openScopes.map(id => this.tokenizedBuffer.grammar.scopeForId(id)) + this.scopeStarts = this.scopes.slice() + this.scopeEnds = [] + return this + } + + next () { + const {tags} = this.line + + if (this.index != null) { + this.startColumn = this.endColumn + this.scopeEnds.length = 0 + this.scopeStarts.length = 0 + this.index++ + } else { + this.index = 0 + } + + while (this.index < tags.length) { + const tag = tags[this.index] + if (tag < 0) { + const scope = this.tokenizedBuffer.grammar.scopeForId(tag) + if ((tag % 2) === 0) { + if (this.scopeStarts[this.scopeStarts.length - 1] === scope) { + this.scopeStarts.pop() + } else { + this.scopeEnds.push(scope) + } + this.scopes.pop() + } else { + this.scopeStarts.push(scope) + this.scopes.push(scope) + } + this.index++ + } else { + this.endColumn += tag + this.text = this.line.text.substring(this.startColumn, this.endColumn) + return true + } + } + + return false + } + + getScopes () { + return this.scopes + } + + getScopeStarts () { + return this.scopeStarts + } + + getScopeEnds () { + return this.scopeEnds + } + + getText () { + return this.text + } + + getBufferStart () { + return this.startColumn + } + + getBufferEnd () { + return this.endColumn + } +}