forked from ajaxorg/ace
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtoken_iterator.js
132 lines (112 loc) · 3.66 KB
/
token_iterator.js
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
"use strict";
/**
* @typedef {import("./edit_session").EditSession} EditSession
*/
var Range = require("./range").Range;
/**
* This class provides an essay way to treat the document as a stream of tokens, and provides methods to iterate over these tokens.
**/
class TokenIterator {
/**
* Creates a new token iterator object. The inital token index is set to the provided row and column coordinates.
* @param {EditSession} session The session to associate with
* @param {Number} initialRow The row to start the tokenizing at
* @param {Number} initialColumn The column to start the tokenizing at
**/
constructor(session, initialRow, initialColumn) {
this.$session = session;
this.$row = initialRow;
this.$rowTokens = session.getTokens(initialRow);
var token = session.getTokenAt(initialRow, initialColumn);
this.$tokenIndex = token ? token.index : -1;
}
/**
* Moves iterator position to the start of previous token.
* @returns {import("../ace-internal").Ace.Token|null}
**/
stepBackward() {
this.$tokenIndex -= 1;
while (this.$tokenIndex < 0) {
this.$row -= 1;
if (this.$row < 0) {
this.$row = 0;
return null;
}
this.$rowTokens = this.$session.getTokens(this.$row);
this.$tokenIndex = this.$rowTokens.length - 1;
}
return this.$rowTokens[this.$tokenIndex];
}
/**
* Moves iterator position to the start of next token.
* @returns {import("../ace-internal").Ace.Token|null}
**/
stepForward() {
this.$tokenIndex += 1;
var rowCount;
while (this.$tokenIndex >= this.$rowTokens.length) {
this.$row += 1;
if (!rowCount)
rowCount = this.$session.getLength();
if (this.$row >= rowCount) {
this.$row = rowCount - 1;
return null;
}
this.$rowTokens = this.$session.getTokens(this.$row);
this.$tokenIndex = 0;
}
return this.$rowTokens[this.$tokenIndex];
}
/**
*
* Returns current token.
* @returns {import("../ace-internal").Ace.Token}
**/
getCurrentToken() {
return this.$rowTokens[this.$tokenIndex];
}
/**
*
* Returns the current row.
* @returns {Number}
**/
getCurrentTokenRow() {
return this.$row;
}
/**
*
* Returns the current column.
* @returns {Number}
**/
getCurrentTokenColumn() {
var rowTokens = this.$rowTokens;
var tokenIndex = this.$tokenIndex;
// If a column was cached by EditSession.getTokenAt, then use it
var column = rowTokens[tokenIndex].start;
if (column !== undefined)
return column;
column = 0;
while (tokenIndex > 0) {
tokenIndex -= 1;
column += rowTokens[tokenIndex].value.length;
}
return column;
}
/**
* Return the current token position.
* @returns {import("../ace-internal").Ace.Point}
*/
getCurrentTokenPosition() {
return {row: this.$row, column: this.getCurrentTokenColumn()};
}
/**
* Return the current token range.
* @returns {Range}
*/
getCurrentTokenRange() {
var token = this.$rowTokens[this.$tokenIndex];
var column = this.getCurrentTokenColumn();
return new Range(this.$row, column, this.$row, column + token.value.length);
}
}
exports.TokenIterator = TokenIterator;