mode-json.js 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. ace.define("ace/mode/json_highlight_rules",[], function(require, exports, module) {
  2. "use strict";
  3. var oop = require("../lib/oop");
  4. var TextHighlightRules = require("./text_highlight_rules").TextHighlightRules;
  5. var JsonHighlightRules = function() {
  6. this.$rules = {
  7. "start" : [
  8. {
  9. token : "variable", // single line
  10. regex : '["](?:(?:\\\\.)|(?:[^"\\\\]))*?["]\\s*(?=:)'
  11. }, {
  12. token : "string", // single line
  13. regex : '"',
  14. next : "string"
  15. }, {
  16. token : "constant.numeric", // hex
  17. regex : "0[xX][0-9a-fA-F]+\\b"
  18. }, {
  19. token : "constant.numeric", // float
  20. regex : "[+-]?\\d+(?:(?:\\.\\d*)?(?:[eE][+-]?\\d+)?)?\\b"
  21. }, {
  22. token : "constant.language.boolean",
  23. regex : "(?:true|false)\\b"
  24. }, {
  25. token : "text", // single quoted strings are not allowed
  26. regex : "['](?:(?:\\\\.)|(?:[^'\\\\]))*?[']"
  27. }, {
  28. token : "comment", // comments are not allowed, but who cares?
  29. regex : "\\/\\/.*$"
  30. }, {
  31. token : "comment.start", // comments are not allowed, but who cares?
  32. regex : "\\/\\*",
  33. next : "comment"
  34. }, {
  35. token : "paren.lparen",
  36. regex : "[[({]"
  37. }, {
  38. token : "paren.rparen",
  39. regex : "[\\])}]"
  40. }, {
  41. token : "text",
  42. regex : "\\s+"
  43. }
  44. ],
  45. "string" : [
  46. {
  47. token : "constant.language.escape",
  48. regex : /\\(?:x[0-9a-fA-F]{2}|u[0-9a-fA-F]{4}|["\\\/bfnrt])/
  49. }, {
  50. token : "string",
  51. regex : '"|$',
  52. next : "start"
  53. }, {
  54. defaultToken : "string"
  55. }
  56. ],
  57. "comment" : [
  58. {
  59. token : "comment.end", // comments are not allowed, but who cares?
  60. regex : "\\*\\/",
  61. next : "start"
  62. }, {
  63. defaultToken: "comment"
  64. }
  65. ]
  66. };
  67. };
  68. oop.inherits(JsonHighlightRules, TextHighlightRules);
  69. exports.JsonHighlightRules = JsonHighlightRules;
  70. });
  71. ace.define("ace/mode/matching_brace_outdent",[], function(require, exports, module) {
  72. "use strict";
  73. var Range = require("../range").Range;
  74. var MatchingBraceOutdent = function() {};
  75. (function() {
  76. this.checkOutdent = function(line, input) {
  77. if (! /^\s+$/.test(line))
  78. return false;
  79. return /^\s*\}/.test(input);
  80. };
  81. this.autoOutdent = function(doc, row) {
  82. var line = doc.getLine(row);
  83. var match = line.match(/^(\s*\})/);
  84. if (!match) return 0;
  85. var column = match[1].length;
  86. var openBracePos = doc.findMatchingBracket({row: row, column: column});
  87. if (!openBracePos || openBracePos.row == row) return 0;
  88. var indent = this.$getIndent(doc.getLine(openBracePos.row));
  89. doc.replace(new Range(row, 0, row, column-1), indent);
  90. };
  91. this.$getIndent = function(line) {
  92. return line.match(/^\s*/)[0];
  93. };
  94. }).call(MatchingBraceOutdent.prototype);
  95. exports.MatchingBraceOutdent = MatchingBraceOutdent;
  96. });
  97. ace.define("ace/mode/folding/cstyle",[], function(require, exports, module) {
  98. "use strict";
  99. var oop = require("../../lib/oop");
  100. var Range = require("../../range").Range;
  101. var BaseFoldMode = require("./fold_mode").FoldMode;
  102. var FoldMode = exports.FoldMode = function(commentRegex) {
  103. if (commentRegex) {
  104. this.foldingStartMarker = new RegExp(
  105. this.foldingStartMarker.source.replace(/\|[^|]*?$/, "|" + commentRegex.start)
  106. );
  107. this.foldingStopMarker = new RegExp(
  108. this.foldingStopMarker.source.replace(/\|[^|]*?$/, "|" + commentRegex.end)
  109. );
  110. }
  111. };
  112. oop.inherits(FoldMode, BaseFoldMode);
  113. (function() {
  114. this.foldingStartMarker = /([\{\[\(])[^\}\]\)]*$|^\s*(\/\*)/;
  115. this.foldingStopMarker = /^[^\[\{\(]*([\}\]\)])|^[\s\*]*(\*\/)/;
  116. this.singleLineBlockCommentRe= /^\s*(\/\*).*\*\/\s*$/;
  117. this.tripleStarBlockCommentRe = /^\s*(\/\*\*\*).*\*\/\s*$/;
  118. this.startRegionRe = /^\s*(\/\*|\/\/)#?region\b/;
  119. this._getFoldWidgetBase = this.getFoldWidget;
  120. this.getFoldWidget = function(session, foldStyle, row) {
  121. var line = session.getLine(row);
  122. if (this.singleLineBlockCommentRe.test(line)) {
  123. if (!this.startRegionRe.test(line) && !this.tripleStarBlockCommentRe.test(line))
  124. return "";
  125. }
  126. var fw = this._getFoldWidgetBase(session, foldStyle, row);
  127. if (!fw && this.startRegionRe.test(line))
  128. return "start"; // lineCommentRegionStart
  129. return fw;
  130. };
  131. this.getFoldWidgetRange = function(session, foldStyle, row, forceMultiline) {
  132. var line = session.getLine(row);
  133. if (this.startRegionRe.test(line))
  134. return this.getCommentRegionBlock(session, line, row);
  135. var match = line.match(this.foldingStartMarker);
  136. if (match) {
  137. var i = match.index;
  138. if (match[1])
  139. return this.openingBracketBlock(session, match[1], row, i);
  140. var range = session.getCommentFoldRange(row, i + match[0].length, 1);
  141. if (range && !range.isMultiLine()) {
  142. if (forceMultiline) {
  143. range = this.getSectionRange(session, row);
  144. } else if (foldStyle != "all")
  145. range = null;
  146. }
  147. return range;
  148. }
  149. if (foldStyle === "markbegin")
  150. return;
  151. var match = line.match(this.foldingStopMarker);
  152. if (match) {
  153. var i = match.index + match[0].length;
  154. if (match[1])
  155. return this.closingBracketBlock(session, match[1], row, i);
  156. return session.getCommentFoldRange(row, i, -1);
  157. }
  158. };
  159. this.getSectionRange = function(session, row) {
  160. var line = session.getLine(row);
  161. var startIndent = line.search(/\S/);
  162. var startRow = row;
  163. var startColumn = line.length;
  164. row = row + 1;
  165. var endRow = row;
  166. var maxRow = session.getLength();
  167. while (++row < maxRow) {
  168. line = session.getLine(row);
  169. var indent = line.search(/\S/);
  170. if (indent === -1)
  171. continue;
  172. if (startIndent > indent)
  173. break;
  174. var subRange = this.getFoldWidgetRange(session, "all", row);
  175. if (subRange) {
  176. if (subRange.start.row <= startRow) {
  177. break;
  178. } else if (subRange.isMultiLine()) {
  179. row = subRange.end.row;
  180. } else if (startIndent == indent) {
  181. break;
  182. }
  183. }
  184. endRow = row;
  185. }
  186. return new Range(startRow, startColumn, endRow, session.getLine(endRow).length);
  187. };
  188. this.getCommentRegionBlock = function(session, line, row) {
  189. var startColumn = line.search(/\s*$/);
  190. var maxRow = session.getLength();
  191. var startRow = row;
  192. var re = /^\s*(?:\/\*|\/\/|--)#?(end)?region\b/;
  193. var depth = 1;
  194. while (++row < maxRow) {
  195. line = session.getLine(row);
  196. var m = re.exec(line);
  197. if (!m) continue;
  198. if (m[1]) depth--;
  199. else depth++;
  200. if (!depth) break;
  201. }
  202. var endRow = row;
  203. if (endRow > startRow) {
  204. return new Range(startRow, startColumn, endRow, line.length);
  205. }
  206. };
  207. }).call(FoldMode.prototype);
  208. });
  209. ace.define("ace/mode/json",[], function(require, exports, module) {
  210. "use strict";
  211. var oop = require("../lib/oop");
  212. var TextMode = require("./text").Mode;
  213. var HighlightRules = require("./json_highlight_rules").JsonHighlightRules;
  214. var MatchingBraceOutdent = require("./matching_brace_outdent").MatchingBraceOutdent;
  215. var CstyleBehaviour = require("./behaviour/cstyle").CstyleBehaviour;
  216. var CStyleFoldMode = require("./folding/cstyle").FoldMode;
  217. var WorkerClient = require("../worker/worker_client").WorkerClient;
  218. var Mode = function() {
  219. this.HighlightRules = HighlightRules;
  220. this.$outdent = new MatchingBraceOutdent();
  221. this.$behaviour = new CstyleBehaviour();
  222. this.foldingRules = new CStyleFoldMode();
  223. };
  224. oop.inherits(Mode, TextMode);
  225. (function() {
  226. this.getNextLineIndent = function(state, line, tab) {
  227. var indent = this.$getIndent(line);
  228. if (state == "start") {
  229. var match = line.match(/^.*[\{\(\[]\s*$/);
  230. if (match) {
  231. indent += tab;
  232. }
  233. }
  234. return indent;
  235. };
  236. this.checkOutdent = function(state, line, input) {
  237. return this.$outdent.checkOutdent(line, input);
  238. };
  239. this.autoOutdent = function(state, doc, row) {
  240. this.$outdent.autoOutdent(doc, row);
  241. };
  242. this.createWorker = function(session) {
  243. var worker = new WorkerClient(["ace"], "ace/mode/json_worker", "JsonWorker");
  244. worker.attachToDocument(session.getDocument());
  245. worker.on("annotate", function(e) {
  246. session.setAnnotations(e.data);
  247. });
  248. worker.on("terminate", function() {
  249. session.clearAnnotations();
  250. });
  251. return worker;
  252. };
  253. this.$id = "ace/mode/json";
  254. }).call(Mode.prototype);
  255. exports.Mode = Mode;
  256. });
  257. (function() {
  258. ace.require(["ace/mode/json"], function(m) {
  259. if (typeof module == "object" && typeof exports == "object" && module) {
  260. module.exports = m;
  261. }
  262. });
  263. })();