jsoniq.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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 WorkerClient = require("../worker/worker_client").WorkerClient;
  33. var oop = require("../lib/oop");
  34. var TextMode = require("./text").Mode;
  35. var TextHighlightRules = require("./text_highlight_rules").TextHighlightRules;
  36. var JSONiqLexer = require("./xquery/jsoniq_lexer").JSONiqLexer;
  37. var Range = require("../range").Range;
  38. var XQueryBehaviour = require("./behaviour/xquery").XQueryBehaviour;
  39. var CStyleFoldMode = require("./folding/cstyle").FoldMode;
  40. var Anchor = require("../anchor").Anchor;
  41. var Mode = function() {
  42. this.$tokenizer = new JSONiqLexer();
  43. this.$behaviour = new XQueryBehaviour();
  44. this.foldingRules = new CStyleFoldMode();
  45. this.$highlightRules = new TextHighlightRules();
  46. };
  47. oop.inherits(Mode, TextMode);
  48. (function() {
  49. this.completer = {
  50. getCompletions: function(editor, session, pos, prefix, callback) {
  51. if (!session.$worker)
  52. return callback();
  53. session.$worker.emit("complete", { data: { pos: pos, prefix: prefix } });
  54. session.$worker.on("complete", function(e){
  55. callback(null, e.data);
  56. });
  57. }
  58. };
  59. this.getNextLineIndent = function(state, line, tab) {
  60. var indent = this.$getIndent(line);
  61. var match = line.match(/\s*(?:then|else|return|[{\(]|<\w+>)\s*$/);
  62. if (match)
  63. indent += tab;
  64. return indent;
  65. };
  66. this.checkOutdent = function(state, line, input) {
  67. if (! /^\s+$/.test(line))
  68. return false;
  69. return /^\s*[\}\)]/.test(input);
  70. };
  71. this.autoOutdent = function(state, doc, row) {
  72. var line = doc.getLine(row);
  73. var match = line.match(/^(\s*[\}\)])/);
  74. if (!match) return 0;
  75. var column = match[1].length;
  76. var openBracePos = doc.findMatchingBracket({row: row, column: column});
  77. if (!openBracePos || openBracePos.row == row) return 0;
  78. var indent = this.$getIndent(doc.getLine(openBracePos.row));
  79. doc.replace(new Range(row, 0, row, column-1), indent);
  80. };
  81. this.toggleCommentLines = function(state, doc, startRow, endRow) {
  82. var i, line;
  83. var outdent = true;
  84. var re = /^\s*\(:(.*):\)/;
  85. for (i=startRow; i<= endRow; i++) {
  86. if (!re.test(doc.getLine(i))) {
  87. outdent = false;
  88. break;
  89. }
  90. }
  91. var range = new Range(0, 0, 0, 0);
  92. for (i=startRow; i<= endRow; i++) {
  93. line = doc.getLine(i);
  94. range.start.row = i;
  95. range.end.row = i;
  96. range.end.column = line.length;
  97. doc.replace(range, outdent ? line.match(re)[1] : "(:" + line + ":)");
  98. }
  99. };
  100. this.createWorker = function(session) {
  101. var worker = new WorkerClient(["ace"], "ace/mode/xquery_worker", "XQueryWorker");
  102. var that = this;
  103. worker.attachToDocument(session.getDocument());
  104. worker.on("ok", function(e) {
  105. session.clearAnnotations();
  106. });
  107. worker.on("markers", function(e) {
  108. session.clearAnnotations();
  109. that.addMarkers(e.data, session);
  110. });
  111. return worker;
  112. };
  113. this.removeMarkers = function(session) {
  114. var markers = session.getMarkers(false);
  115. for (var id in markers) {
  116. // All language analysis' markers are prefixed with language_highlight
  117. if (markers[id].clazz.indexOf('language_highlight_') === 0) {
  118. session.removeMarker(id);
  119. }
  120. }
  121. for (var i = 0; i < session.markerAnchors.length; i++) {
  122. session.markerAnchors[i].detach();
  123. }
  124. session.markerAnchors = [];
  125. };
  126. this.addMarkers = function(annos, mySession) {
  127. var _self = this;
  128. if (!mySession.markerAnchors) mySession.markerAnchors = [];
  129. this.removeMarkers(mySession);
  130. mySession.languageAnnos = [];
  131. annos.forEach(function(anno) {
  132. // Certain annotations can temporarily be disabled
  133. //if (_self.disabledMarkerTypes[anno.type])
  134. // return;
  135. // Multi-line markers are not supported, and typically are a result from a bad error recover, ignore
  136. //if(anno.pos.el && anno.pos.sl !== anno.pos.el)
  137. // return;
  138. // Using anchors here, to automaticaly move markers as text around the marker is updated
  139. var anchor = new Anchor(mySession.getDocument(), anno.pos.sl, anno.pos.sc || 0);
  140. mySession.markerAnchors.push(anchor);
  141. var markerId;
  142. var colDiff = anno.pos.ec - anno.pos.sc;
  143. var rowDiff = anno.pos.el - anno.pos.sl;
  144. var gutterAnno = {
  145. guttertext: anno.message,
  146. type: anno.level || "warning",
  147. text: anno.message
  148. // row will be filled in updateFloat()
  149. };
  150. function updateFloat(single) {
  151. if (markerId)
  152. mySession.removeMarker(markerId);
  153. gutterAnno.row = anchor.row;
  154. if (anno.pos.sc !== undefined && anno.pos.ec !== undefined) {
  155. var range = new Range(anno.pos.sl, anno.pos.sc, anno.pos.el, anno.pos.ec);
  156. //var range = Range.fromPoints(anchor.getPosition(), {
  157. // row: anchor.row + rowDiff,
  158. // column: anchor.column + colDiff
  159. //});
  160. markerId = mySession.addMarker(range, "language_highlight_" + (anno.type ? anno.type : "default"));
  161. }
  162. if (single) mySession.setAnnotations(mySession.languageAnnos);
  163. }
  164. updateFloat();
  165. anchor.on("change", function() {
  166. updateFloat(true);
  167. });
  168. if (anno.message) mySession.languageAnnos.push(gutterAnno);
  169. });
  170. mySession.setAnnotations(mySession.languageAnnos);
  171. };
  172. this.$id = "ace/mode/jsoniq";
  173. }).call(Mode.prototype);
  174. exports.Mode = Mode;
  175. });