forked from atom/atom
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtokenized-line.coffee
68 lines (53 loc) · 1.56 KB
/
tokenized-line.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
Token = require './token'
CommentScopeRegex = /(\b|\.)comment/
idCounter = 1
module.exports =
class TokenizedLine
constructor: (properties) ->
@id = idCounter++
return unless properties?
{@openScopes, @text, @tags, @ruleStack, @tokenIterator} = properties
getTokenIterator: -> @tokenIterator.reset(this, arguments...)
Object.defineProperty @prototype, 'tokens', get: ->
iterator = @getTokenIterator()
tokens = []
while iterator.next()
tokens.push(new Token({
value: iterator.getText()
scopes: iterator.getScopes().slice()
}))
tokens
tokenAtBufferColumn: (bufferColumn) ->
@tokens[@tokenIndexAtBufferColumn(bufferColumn)]
tokenIndexAtBufferColumn: (bufferColumn) ->
column = 0
for token, index in @tokens
column += token.value.length
return index if column > bufferColumn
index - 1
tokenStartColumnForBufferColumn: (bufferColumn) ->
delta = 0
for token in @tokens
nextDelta = delta + token.bufferDelta
break if nextDelta > bufferColumn
delta = nextDelta
delta
isComment: ->
return @isCommentLine if @isCommentLine?
@isCommentLine = false
iterator = @getTokenIterator()
while iterator.next()
scopes = iterator.getScopes()
continue if scopes.length is 1
for scope in scopes
if CommentScopeRegex.test(scope)
@isCommentLine = true
break
break
@isCommentLine
tokenAtIndex: (index) ->
@tokens[index]
getTokenCount: ->
count = 0
count++ for tag in @tags when tag >= 0
count