diff --git a/lib/ace/mode/xquery.js b/lib/ace/mode/xquery.js index e1a7b4f8..dfe81aff 100644 --- a/lib/ace/mode/xquery.js +++ b/lib/ace/mode/xquery.js @@ -125,7 +125,6 @@ oop.inherits(Mode, TextMode); worker.attachToDocument(session.getDocument()); worker.on("start", function(e) { - //console.log("start"); that.$deltas = []; }); @@ -138,51 +137,14 @@ oop.inherits(Mode, TextMode); }); worker.on("highlight", function(tokens) { + if(that.$deltas.length > 0) return; + var firstRow = 0; var lastRow = session.getLength() - 1; var lines = tokens.data.lines; var states = tokens.data.states; - - for(var i=0; i < that.$deltas.length; i++) - { - var delta = that.$deltas[i]; - - if (delta.action === "insertLines") - { - var newLineCount = delta.lines.length; - for (var i = 0; i < newLineCount; i++) { - lines.splice(delta.range.start.row + i, 0, undefined); - states.splice(delta.range.start.row + i, 0, undefined); - } - } - else if (delta.action === "insertText") - { - if (session.getDocument().isNewLine(delta.text)) - { - lines.splice(delta.range.end.row, 0, undefined); - states.splice(delta.range.end.row, 0, undefined); - } else { - lines[delta.range.start.row] = undefined; - states[delta.range.start.row] = undefined; - } - } else if (delta.action === "removeLines") { - var oldLineCount = delta.lines.length; - lines.splice(delta.range.start.row, oldLineCount); - states.splice(delta.range.start.row, oldLineCount); - } else if (delta.action === "removeText") { - if (session.getDocument().isNewLine(delta.text)) - { - lines[delta.range.start.row] = undefined; - lines.splice(delta.range.end.row, 1); - states[delta.range.start.row] = undefined; - states.splice(delta.range.end.row, 1); - } else { - lines[delta.range.start.row] = undefined; - states[delta.range.start.row] = undefined; - } - } - } + session.bgTokenizer.lines = lines; session.bgTokenizer.states = states; session.bgTokenizer.fireUpdateEvent(firstRow, lastRow); diff --git a/lib/ace/mode/xquery/visitors/SyntaxHighlighter.js b/lib/ace/mode/xquery/visitors/SyntaxHighlighter.js index 2815a91d..30aab67c 100644 --- a/lib/ace/mode/xquery/visitors/SyntaxHighlighter.js +++ b/lib/ace/mode/xquery/visitors/SyntaxHighlighter.js @@ -1,3 +1,33 @@ +/* ***** BEGIN LICENSE BLOCK ***** + * Distributed under the BSD license: + * + * Copyright (c) 2010, Ajax.org B.V. + * All rights reserved. + * + * 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. + * * Neither the name of Ajax.org B.V. nor the + * names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * 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 AJAX.ORG B.V. 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. + * + * ***** END LICENSE BLOCK ***** */ + define(function(require, exports, module){ var SyntaxHighlighter = exports.SyntaxHighlighter = function(source, tree) @@ -5,10 +35,19 @@ var SyntaxHighlighter = exports.SyntaxHighlighter = function(source, tree) var keywords = ['after', 'ancestor', 'ancestor-or-self', 'and', 'as', 'ascending', 'attribute', 'before', 'case', 'cast', 'castable', 'child', 'collation', 'comment', 'copy', 'count', 'declare', 'default', 'delete', 'descendant', 'descendant-or-self', 'descending', 'div', 'document', 'document-node', 'element', 'else', 'empty', 'empty-sequence', 'end', 'eq', 'every', 'except', 'first', 'following', 'following-sibling', 'for', 'function', 'ge', 'group', 'gt', 'idiv', 'if', 'then', 'import', 'insert', 'instance', 'intersect', 'into', 'is', 'item', 'last', 'le', 'let', 'lt', 'mod', 'modify', 'module', 'namespace', 'namespace-node', 'ne', 'node', 'only', 'or', 'order', 'ordered', 'parent', 'preceding', 'preceding-sibling', 'processing-instruction', 'rename', 'replace', 'return', 'satisfies', 'schema-attribute', 'schema-element', 'self', 'some', 'stable', 'start', 'switch', 'text', 'to', 'treat', 'try', 'typeswitch', 'union', 'unordered', 'validate', 'where', 'with', 'xquery', 'contains', 'paragraphs', 'sentences', 'times', 'words', 'by', 'collection', 'allowing', 'at', 'base-uri', 'boundary-space', 'break', 'catch', 'construction', 'context', 'continue', 'copy-namespaces', 'decimal-format', 'encoding', 'exit', 'external', 'ft-option', 'in', 'index', 'integrity', 'lax', 'nodes', 'option', 'ordering', 'revalidation', 'schema', 'score', 'sliding', 'strict', 'tumbling', 'type', 'updating', 'value', 'variable', 'version', 'while', 'constraint', 'loop', 'returning', 'append', 'array', 'json-item', 'object', 'structured-item', 'when', 'next', 'previous', 'window']; var states = ["cdata", "comment", "tag"]; var info = { lines: [ [] ], states: [] }; -// var inName = false; - this.getTokens = function() { + this.getTokens = function(recover) { this.visit(tree); + if(recover === true) { + var computed = ""; + for(var i in info.lines) { + for(var j in info.lines[i]) { + var token = info.lines[i][j]; + computed += token.value; + } + } + this.addTokens(source.substring(computed.length + 1), "text"); + } return info; }; diff --git a/lib/ace/mode/xquery_worker.js b/lib/ace/mode/xquery_worker.js index 863a06e3..c823bfee 100644 --- a/lib/ace/mode/xquery_worker.js +++ b/lib/ace/mode/xquery_worker.js @@ -51,14 +51,12 @@ oop.inherits(XQueryWorker, Mirror); var value = this.doc.getValue(); var h = new JSONParseTreeHandler(); var parser = new XQueryParser(value, h); + var recover = false; try { parser.parse_XQuery(); - var ast = h.getParseTree(); this.sender.emit("ok"); - var highlighter = new SyntaxHighlighter(value, ast); - var tokens = highlighter.getTokens(); - this.sender.emit("highlight", tokens); - } catch(e) { + } catch(e) { + var recover = true; var prefix = value.substring(0, e.getBegin()); var line = prefix.split("\n").length; var column = e.getBegin() - prefix.lastIndexOf("\n"); @@ -70,6 +68,10 @@ oop.inherits(XQueryWorker, Mirror); type: "error" }); } + var ast = h.getParseTree(); + var highlighter = new SyntaxHighlighter(value, ast); + var tokens = highlighter.getTokens(recover); + this.sender.emit("highlight", tokens); }; }).call(XQueryWorker.prototype);