/* Copyright (C) 2012 Ariya Hidayat Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ /*jslint browser:true */ /*global esprima:true */ var parseTimer, syntax = null, markers = []; function parse() { 'use strict'; var code = window.editor.getValue(); syntax = null; try { syntax = esprima.parse(code, { loc: true, range: true, tolerant: true }); } catch (e) { } } function triggerParse(delay) { 'use strict'; if (parseTimer) { window.clearTimeout(parseTimer); } markers.forEach(function (marker) { marker.clear(); }); markers = []; parseTimer = window.setTimeout(parse, delay || 811); } function trackCursor(editor) { 'use strict'; var pos, code, node, id; markers.forEach(function (marker) { marker.clear(); }); markers = []; if (syntax === null) { parse(); if (syntax === null) { return; } } pos = editor.indexFromPos(editor.getCursor()); code = editor.getValue(); // Executes visitor on the object and its children (recursively). function traverse(object, visitor, master) { var key, child, parent, path; parent = (typeof master === 'undefined') ? [] : master; if (visitor.call(null, object, parent) === false) { return; } for (key in object) { if (object.hasOwnProperty(key)) { child = object[key]; path = [ object ]; path.push(parent); if (typeof child === 'object' && child !== null) { traverse(child, visitor, path); } } } } traverse(syntax, function (node, path) { var start, end; if (node.type !== esprima.Syntax.Identifier) { return; } if (pos >= node.range[0] && pos <= node.range[1]) { start = { line: node.loc.start.line - 1, ch: node.loc.start.column }; end = { line: node.loc.end.line - 1, ch: node.loc.end.column }; markers.push(editor.markText(start, end, 'identifier')); id = node; } }); if (typeof id === 'undefined') { return; } traverse(syntax, function (node, path) { var start, end; if (node.type !== esprima.Syntax.Identifier) { return; } if (node !== id && node.name === id.name) { start = { line: node.loc.start.line - 1, ch: node.loc.start.column }; end = { line: node.loc.end.line - 1, ch: node.loc.end.column }; markers.push(editor.markText(start, end, 'highlight')); } }); }