Files
ace/lib/ace/token_iterator.js
Garen Torikian a0ed6dcbab More styles
2012-04-25 02:31:46 -07:00

163 lines
5.4 KiB
JavaScript

/* vim:ts=4:sts=4:sw=4:
* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is Ajax.org Code Editor (ACE).
*
* The Initial Developer of the Original Code is
* Ajax.org B.V.
* Portions created by the Initial Developer are Copyright (C) 2010
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
* Fabian Jakobs <fabian AT ajax DOT org>
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
define(function(require, exports, module) {
"use strict";
/**
* class TokenIterator
*
* This class provides an essay way to treat the document as a stream of tokens, and provides methods to iterate over these tokens.
*
**/
/**
* new TokenIterator(session, initialRow, initialColumn)
* - session (EditSession): The session to associate with
* - initialRow (Number): The row to start the tokenizing at
* - initialColumn (Number): The column to start the tokenizing at
*
* Creates a new token iterator object. The inital token index is set to the provided row and column coordinates.
*
**/
var TokenIterator = function(session, initialRow, initialColumn) {
this.$session = session;
this.$row = initialRow;
this.$rowTokens = session.getTokens(initialRow, initialRow)[0].tokens;
var token = session.getTokenAt(initialRow, initialColumn);
this.$tokenIndex = token ? token.index : -1;
};
(function() {
/**
* TokenIterator.stepBackward() -> [String]
* + (String): If the current point is not at the top of the file, this function returns `null`. Otherwise, it returns an array of the tokenized strings.
*
* Tokenizes all the items from the current point to the row prior in the document.
**/
this.stepBackward = function() {
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.$row)[0].tokens;
this.$tokenIndex = this.$rowTokens.length - 1;
}
return this.$rowTokens[this.$tokenIndex];
};
/**
* TokenIterator.stepForward() -> String
*
* Tokenizes all the items from the current point until the next row in the document. If the current point is at the end of the file, this function returns `null`. Otherwise, it returns the tokenized string.
**/
this.stepForward = function() {
var rowCount = this.$session.getLength();
this.$tokenIndex += 1;
while (this.$tokenIndex >= this.$rowTokens.length) {
this.$row += 1;
if (this.$row >= rowCount) {
this.$row = rowCount - 1;
return null;
}
this.$rowTokens = this.$session.getTokens(this.$row, this.$row)[0].tokens;
this.$tokenIndex = 0;
}
return this.$rowTokens[this.$tokenIndex];
};
/**
* TokenIterator.getCurrentToken() -> String
*
* Returns the current tokenized string.
*
**/
this.getCurrentToken = function () {
return this.$rowTokens[this.$tokenIndex];
};
/**
* TokenIterator.getCurrentTokenRow() -> Number
*
* Returns the current row.
*
**/
this.getCurrentTokenRow = function () {
return this.$row;
};
/**
* TokenIterator.getCurrentTokenColumn() -> Number
*
* Returns the current column.
*
**/
this.getCurrentTokenColumn = function() {
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;
};
}).call(TokenIterator.prototype);
exports.TokenIterator = TokenIterator;
});