background_tokenizer.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. /* ***** BEGIN LICENSE BLOCK *****
  2. * Distributed under the BSD license:
  3. *
  4. * Copyright (c) 2010, Ajax.org B.V.
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. * * Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * * Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * * Neither the name of Ajax.org B.V. nor the
  15. * names of its contributors may be used to endorse or promote products
  16. * derived from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL AJAX.ORG B.V. BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. *
  29. * ***** END LICENSE BLOCK ***** */
  30. define(function(require, exports, module) {
  31. "use strict";
  32. var oop = require("./lib/oop");
  33. var EventEmitter = require("./lib/event_emitter").EventEmitter;
  34. /**
  35. *
  36. *
  37. * Tokenizes the current [[Document `Document`]] in the background, and caches the tokenized rows for future use.
  38. *
  39. * If a certain row is changed, everything below that row is re-tokenized.
  40. *
  41. * @class BackgroundTokenizer
  42. **/
  43. /**
  44. * Creates a new `BackgroundTokenizer` object.
  45. * @param {Tokenizer} tokenizer The tokenizer to use
  46. * @param {Editor} editor The editor to associate with
  47. *
  48. *
  49. *
  50. * @constructor
  51. **/
  52. var BackgroundTokenizer = function(tokenizer, editor) {
  53. this.running = false;
  54. this.lines = [];
  55. this.states = [];
  56. this.currentLine = 0;
  57. this.tokenizer = tokenizer;
  58. var self = this;
  59. this.$worker = function() {
  60. if (!self.running) { return; }
  61. var workerStart = new Date();
  62. var currentLine = self.currentLine;
  63. var endLine = -1;
  64. var doc = self.doc;
  65. while (self.lines[currentLine])
  66. currentLine++;
  67. var startLine = currentLine;
  68. var len = doc.getLength();
  69. var processedLines = 0;
  70. self.running = false;
  71. while (currentLine < len) {
  72. self.$tokenizeRow(currentLine);
  73. endLine = currentLine;
  74. do {
  75. currentLine++;
  76. } while (self.lines[currentLine]);
  77. // only check every 5 lines
  78. processedLines ++;
  79. if ((processedLines % 5 == 0) && (new Date() - workerStart) > 20) {
  80. self.running = setTimeout(self.$worker, 20);
  81. self.currentLine = currentLine;
  82. return;
  83. }
  84. }
  85. self.currentLine = currentLine;
  86. if (startLine <= endLine)
  87. self.fireUpdateEvent(startLine, endLine);
  88. };
  89. };
  90. (function(){
  91. oop.implement(this, EventEmitter);
  92. /**
  93. * Sets a new tokenizer for this object.
  94. *
  95. * @param {Tokenizer} tokenizer The new tokenizer to use
  96. *
  97. **/
  98. this.setTokenizer = function(tokenizer) {
  99. this.tokenizer = tokenizer;
  100. this.lines = [];
  101. this.states = [];
  102. this.start(0);
  103. };
  104. /**
  105. * Sets a new document to associate with this object.
  106. * @param {Document} doc The new document to associate with
  107. **/
  108. this.setDocument = function(doc) {
  109. this.doc = doc;
  110. this.lines = [];
  111. this.states = [];
  112. this.stop();
  113. };
  114. /**
  115. * Fires whenever the background tokeniziers between a range of rows are going to be updated.
  116. *
  117. * @event update
  118. * @param {Object} e An object containing two properties, `first` and `last`, which indicate the rows of the region being updated.
  119. *
  120. **/
  121. /**
  122. * Emits the `'update'` event. `firstRow` and `lastRow` are used to define the boundaries of the region to be updated.
  123. * @param {Number} firstRow The starting row region
  124. * @param {Number} lastRow The final row region
  125. *
  126. **/
  127. this.fireUpdateEvent = function(firstRow, lastRow) {
  128. var data = {
  129. first: firstRow,
  130. last: lastRow
  131. };
  132. this._emit("update", {data: data});
  133. };
  134. /**
  135. * Starts tokenizing at the row indicated.
  136. *
  137. * @param {Number} startRow The row to start at
  138. *
  139. **/
  140. this.start = function(startRow) {
  141. this.currentLine = Math.min(startRow || 0, this.currentLine, this.doc.getLength());
  142. // remove all cached items below this line
  143. this.lines.splice(this.currentLine, this.lines.length);
  144. this.states.splice(this.currentLine, this.states.length);
  145. this.stop();
  146. // pretty long delay to prevent the tokenizer from interfering with the user
  147. this.running = setTimeout(this.$worker, 700);
  148. };
  149. this.scheduleStart = function() {
  150. if (!this.running)
  151. this.running = setTimeout(this.$worker, 700);
  152. }
  153. this.$updateOnChange = function(delta) {
  154. var range = delta.range;
  155. var startRow = range.start.row;
  156. var len = range.end.row - startRow;
  157. if (len === 0) {
  158. this.lines[startRow] = null;
  159. } else if (delta.action == "removeText" || delta.action == "removeLines") {
  160. this.lines.splice(startRow, len + 1, null);
  161. this.states.splice(startRow, len + 1, null);
  162. } else {
  163. var args = Array(len + 1);
  164. args.unshift(startRow, 1);
  165. this.lines.splice.apply(this.lines, args);
  166. this.states.splice.apply(this.states, args);
  167. }
  168. this.currentLine = Math.min(startRow, this.currentLine, this.doc.getLength());
  169. this.stop();
  170. };
  171. /**
  172. * Stops tokenizing.
  173. *
  174. **/
  175. this.stop = function() {
  176. if (this.running)
  177. clearTimeout(this.running);
  178. this.running = false;
  179. };
  180. /**
  181. * Gives list of tokens of the row. (tokens are cached)
  182. *
  183. * @param {Number} row The row to get tokens at
  184. *
  185. *
  186. *
  187. **/
  188. this.getTokens = function(row) {
  189. return this.lines[row] || this.$tokenizeRow(row);
  190. };
  191. /**
  192. * [Returns the state of tokenization at the end of a row.]{: #BackgroundTokenizer.getState}
  193. *
  194. * @param {Number} row The row to get state at
  195. **/
  196. this.getState = function(row) {
  197. if (this.currentLine == row)
  198. this.$tokenizeRow(row);
  199. return this.states[row] || "start";
  200. };
  201. this.$tokenizeRow = function(row) {
  202. var line = this.doc.getLine(row);
  203. var state = this.states[row - 1];
  204. var data = this.tokenizer.getLineTokens(line, state, row);
  205. if (this.states[row] + "" !== data.state + "") {
  206. this.states[row] = data.state;
  207. this.lines[row + 1] = null;
  208. if (this.currentLine > row + 1)
  209. this.currentLine = row + 1;
  210. } else if (this.currentLine == row) {
  211. this.currentLine = row + 1;
  212. }
  213. return this.lines[row] = data.tokens;
  214. };
  215. }).call(BackgroundTokenizer.prototype);
  216. exports.BackgroundTokenizer = BackgroundTokenizer;
  217. });