Skip to content
This repository has been archived by the owner on Sep 6, 2021. It is now read-only.

Commit

Permalink
editor -> cm, Remove unused CodeMirror require
Browse files Browse the repository at this point in the history
  • Loading branch information
Marcel Gerber committed Dec 10, 2014
1 parent 6776bc0 commit 2d920f5
Showing 1 changed file with 16 additions and 17 deletions.
33 changes: 16 additions & 17 deletions src/utils/TokenUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,28 +33,27 @@
define(function (require, exports, module) {
"use strict";

var _ = require("thirdparty/lodash"),
CodeMirror = require("thirdparty/CodeMirror2/lib/codemirror");
var _ = require("thirdparty/lodash");

var cache,
CACHE_MAX_AGE = 1000; // cache for 1 second


/*
* Caches the tokens for the given editor/line if needed
* @param {!CodeMirror} editor
* @param {!CodeMirror} cm
* @param {!number} line
* @return {Array.<Object>} (Cached) array of tokens
*/
function _manageCache(editor, line) {
if (!cache || !cache.tokens || cache.line !== line || cache.editor !== editor ||
function _manageCache(cm, line) {
if (!cache || !cache.tokens || cache.line !== line || cache.cm !== cm ||
cache.timeStamp < Date.now() - CACHE_MAX_AGE) {
// Cache is outdated/no longer matching -> Update
var tokens = editor.getLineTokens(line, false);
var tokens = cm.getLineTokens(line, false);
// Add empty beginning-of-line token for backwards compatibility
tokens.unshift(editor.getTokenAt({line: line, ch: 0}, false));
tokens.unshift(cm.getTokenAt({line: line, ch: 0}, false));
cache = {
editor: editor,
cm: cm,
line: line,
timeStamp: Date.now(),
tokens: tokens
Expand All @@ -65,34 +64,34 @@ define(function (require, exports, module) {

/*
* Like cm.getTokenAt, but with caching
* @param {!CodeMirror} editor
* @param {!CodeMirror} cm
* @param {!{ch:number, line:number}} pos
* @param {boolean} precise If given, results in more current results. Suppresses caching.
* @return {Object} Token for position
*/
function _getToken(editor, pos, precise) {
function _getToken(cm, pos, precise) {
if (precise) {
cache = null; // reset cache
return editor.getTokenAt(pos, precise);
return cm.getTokenAt(pos, precise);
}
var cachedTokens = _manageCache(editor, pos.line),
var cachedTokens = _manageCache(cm, pos.line),
tokenIndex = _.sortedIndex(cachedTokens, {end: pos.ch}, "end"), // binary search is faster for long arrays
token = cachedTokens[tokenIndex];
return token || editor.getTokenAt(pos, precise); // fall back to CMs getTokenAt, for example in an empty line
return token || cm.getTokenAt(pos, precise); // fall back to CMs getTokenAt, for example in an empty line
}

/**
* Creates a context object for the given editor and position, suitable for passing to the
* move functions.
* @param {!CodeMirror} editor
* @param {!CodeMirror} cm
* @param {!{ch:number, line:number}} pos
* @return {!{editor:!CodeMirror, pos:!{ch:number, line:number}, token:Object}}
*/
function getInitialContext(editor, pos) {
function getInitialContext(cm, pos) {
return {
"editor": editor,
"editor": cm,
"pos": pos,
"token": editor.getTokenAt(pos, true)
"token": cm.getTokenAt(pos, true)
};
}

Expand Down

0 comments on commit 2d920f5

Please sign in to comment.