You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

mode-json.js 18KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  1. /* ***** BEGIN LICENSE BLOCK *****
  2. * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  3. *
  4. * The contents of this file are subject to the Mozilla Public License Version
  5. * 1.1 (the "License"); you may not use this file except in compliance with
  6. * the License. You may obtain a copy of the License at
  7. * http://www.mozilla.org/MPL/
  8. *
  9. * Software distributed under the License is distributed on an "AS IS" basis,
  10. * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  11. * for the specific language governing rights and limitations under the
  12. * License.
  13. *
  14. * The Original Code is Ajax.org Code Editor (ACE).
  15. *
  16. * The Initial Developer of the Original Code is
  17. * Ajax.org B.V.
  18. * Portions created by the Initial Developer are Copyright (C) 2010
  19. * the Initial Developer. All Rights Reserved.
  20. *
  21. * Contributor(s):
  22. * Fabian Jakobs <fabian AT ajax DOT org>
  23. *
  24. * Alternatively, the contents of this file may be used under the terms of
  25. * either the GNU General Public License Version 2 or later (the "GPL"), or
  26. * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  27. * in which case the provisions of the GPL or the LGPL are applicable instead
  28. * of those above. If you wish to allow use of your version of this file only
  29. * under the terms of either the GPL or the LGPL, and not to allow others to
  30. * use your version of this file under the terms of the MPL, indicate your
  31. * decision by deleting the provisions above and replace them with the notice
  32. * and other provisions required by the GPL or the LGPL. If you do not delete
  33. * the provisions above, a recipient may use your version of this file under
  34. * the terms of any one of the MPL, the GPL or the LGPL.
  35. *
  36. * ***** END LICENSE BLOCK ***** */
  37. ace.define('ace/mode/json', ['require', 'exports', 'module' , 'ace/lib/oop', 'ace/mode/text', 'ace/tokenizer', 'ace/mode/json_highlight_rules', 'ace/mode/matching_brace_outdent', 'ace/mode/behaviour/cstyle', 'ace/mode/folding/cstyle', 'ace/worker/worker_client'], function(require, exports, module) {
  38. var oop = require("../lib/oop");
  39. var TextMode = require("./text").Mode;
  40. var Tokenizer = require("../tokenizer").Tokenizer;
  41. var HighlightRules = require("./json_highlight_rules").JsonHighlightRules;
  42. var MatchingBraceOutdent = require("./matching_brace_outdent").MatchingBraceOutdent;
  43. var CstyleBehaviour = require("./behaviour/cstyle").CstyleBehaviour;
  44. var CStyleFoldMode = require("./folding/cstyle").FoldMode;
  45. var WorkerClient = require("../worker/worker_client").WorkerClient;
  46. var Mode = function() {
  47. this.$tokenizer = new Tokenizer(new HighlightRules().getRules());
  48. this.$outdent = new MatchingBraceOutdent();
  49. this.$behaviour = new CstyleBehaviour();
  50. this.foldingRules = new CStyleFoldMode();
  51. };
  52. oop.inherits(Mode, TextMode);
  53. (function() {
  54. this.getNextLineIndent = function(state, line, tab) {
  55. var indent = this.$getIndent(line);
  56. if (state == "start") {
  57. var match = line.match(/^.*[\{\(\[]\s*$/);
  58. if (match) {
  59. indent += tab;
  60. }
  61. }
  62. return indent;
  63. };
  64. this.checkOutdent = function(state, line, input) {
  65. return this.$outdent.checkOutdent(line, input);
  66. };
  67. this.autoOutdent = function(state, doc, row) {
  68. this.$outdent.autoOutdent(doc, row);
  69. };
  70. this.createWorker = function(session) {
  71. var worker = new WorkerClient(["ace"], "worker-json.js", "ace/mode/json_worker", "JsonWorker");
  72. worker.attachToDocument(session.getDocument());
  73. worker.on("error", function(e) {
  74. session.setAnnotations([e.data]);
  75. });
  76. worker.on("ok", function() {
  77. session.clearAnnotations();
  78. });
  79. return worker;
  80. };
  81. }).call(Mode.prototype);
  82. exports.Mode = Mode;
  83. });
  84. ace.define('ace/mode/json_highlight_rules', ['require', 'exports', 'module' , 'ace/lib/oop', 'ace/mode/text_highlight_rules'], function(require, exports, module) {
  85. var oop = require("../lib/oop");
  86. var TextHighlightRules = require("./text_highlight_rules").TextHighlightRules;
  87. var JsonHighlightRules = function() {
  88. // regexp must not have capturing parentheses. Use (?:) instead.
  89. // regexps are ordered -> the first match is used
  90. this.$rules = {
  91. "start" : [
  92. {
  93. token : "variable", // single line
  94. regex : '["](?:(?:\\\\.)|(?:[^"\\\\]))*?["]\\s*(?=:)'
  95. }, {
  96. token : "string", // single line
  97. regex : '"',
  98. next : "string"
  99. }, {
  100. token : "constant.numeric", // hex
  101. regex : "0[xX][0-9a-fA-F]+\\b"
  102. }, {
  103. token : "constant.numeric", // float
  104. regex : "[+-]?\\d+(?:(?:\\.\\d*)?(?:[eE][+-]?\\d+)?)?\\b"
  105. }, {
  106. token : "constant.language.boolean",
  107. regex : "(?:true|false)\\b"
  108. }, {
  109. token : "invalid.illegal", // single quoted strings are not allowed
  110. regex : "['](?:(?:\\\\.)|(?:[^'\\\\]))*?[']"
  111. }, {
  112. token : "invalid.illegal", // comments are not allowed
  113. regex : "\\/\\/.*$"
  114. }, {
  115. token : "paren.lparen",
  116. regex : "[[({]"
  117. }, {
  118. token : "paren.rparen",
  119. regex : "[\\])}]"
  120. }, {
  121. token : "text",
  122. regex : "\\s+"
  123. }
  124. ],
  125. "string" : [
  126. {
  127. token : "constant.language.escape",
  128. regex : /\\(?:x[0-9a-fA-F]{2}|u[0-9a-fA-F]{4})/
  129. }, {
  130. token : "string",
  131. regex : '[^"\\\\]+',
  132. merge : true
  133. }, {
  134. token : "string",
  135. regex : '"',
  136. next : "start",
  137. merge : true
  138. }, {
  139. token : "string",
  140. regex : "",
  141. next : "start",
  142. merge : true
  143. }
  144. ]
  145. };
  146. };
  147. oop.inherits(JsonHighlightRules, TextHighlightRules);
  148. exports.JsonHighlightRules = JsonHighlightRules;
  149. });
  150. ace.define('ace/mode/matching_brace_outdent', ['require', 'exports', 'module' , 'ace/range'], function(require, exports, module) {
  151. var Range = require("../range").Range;
  152. var MatchingBraceOutdent = function() {};
  153. (function() {
  154. this.checkOutdent = function(line, input) {
  155. if (! /^\s+$/.test(line))
  156. return false;
  157. return /^\s*\}/.test(input);
  158. };
  159. this.autoOutdent = function(doc, row) {
  160. var line = doc.getLine(row);
  161. var match = line.match(/^(\s*\})/);
  162. if (!match) return 0;
  163. var column = match[1].length;
  164. var openBracePos = doc.findMatchingBracket({row: row, column: column});
  165. if (!openBracePos || openBracePos.row == row) return 0;
  166. var indent = this.$getIndent(doc.getLine(openBracePos.row));
  167. doc.replace(new Range(row, 0, row, column-1), indent);
  168. };
  169. this.$getIndent = function(line) {
  170. var match = line.match(/^(\s+)/);
  171. if (match) {
  172. return match[1];
  173. }
  174. return "";
  175. };
  176. }).call(MatchingBraceOutdent.prototype);
  177. exports.MatchingBraceOutdent = MatchingBraceOutdent;
  178. });
  179. ace.define('ace/mode/behaviour/cstyle', ['require', 'exports', 'module' , 'ace/lib/oop', 'ace/mode/behaviour'], function(require, exports, module) {
  180. var oop = require("../../lib/oop");
  181. var Behaviour = require("../behaviour").Behaviour;
  182. var CstyleBehaviour = function () {
  183. this.add("braces", "insertion", function (state, action, editor, session, text) {
  184. if (text == '{') {
  185. var selection = editor.getSelectionRange();
  186. var selected = session.doc.getTextRange(selection);
  187. if (selected !== "") {
  188. return {
  189. text: '{' + selected + '}',
  190. selection: false
  191. };
  192. } else {
  193. return {
  194. text: '{}',
  195. selection: [1, 1]
  196. };
  197. }
  198. } else if (text == '}') {
  199. var cursor = editor.getCursorPosition();
  200. var line = session.doc.getLine(cursor.row);
  201. var rightChar = line.substring(cursor.column, cursor.column + 1);
  202. if (rightChar == '}') {
  203. var matching = session.$findOpeningBracket('}', {column: cursor.column + 1, row: cursor.row});
  204. if (matching !== null) {
  205. return {
  206. text: '',
  207. selection: [1, 1]
  208. };
  209. }
  210. }
  211. } else if (text == "\n") {
  212. var cursor = editor.getCursorPosition();
  213. var line = session.doc.getLine(cursor.row);
  214. var rightChar = line.substring(cursor.column, cursor.column + 1);
  215. if (rightChar == '}') {
  216. var openBracePos = session.findMatchingBracket({row: cursor.row, column: cursor.column + 1});
  217. if (!openBracePos)
  218. return null;
  219. var indent = this.getNextLineIndent(state, line.substring(0, line.length - 1), session.getTabString());
  220. var next_indent = this.$getIndent(session.doc.getLine(openBracePos.row));
  221. return {
  222. text: '\n' + indent + '\n' + next_indent,
  223. selection: [1, indent.length, 1, indent.length]
  224. };
  225. }
  226. }
  227. });
  228. this.add("braces", "deletion", function (state, action, editor, session, range) {
  229. var selected = session.doc.getTextRange(range);
  230. if (!range.isMultiLine() && selected == '{') {
  231. var line = session.doc.getLine(range.start.row);
  232. var rightChar = line.substring(range.end.column, range.end.column + 1);
  233. if (rightChar == '}') {
  234. range.end.column++;
  235. return range;
  236. }
  237. }
  238. });
  239. this.add("parens", "insertion", function (state, action, editor, session, text) {
  240. if (text == '(') {
  241. var selection = editor.getSelectionRange();
  242. var selected = session.doc.getTextRange(selection);
  243. if (selected !== "") {
  244. return {
  245. text: '(' + selected + ')',
  246. selection: false
  247. };
  248. } else {
  249. return {
  250. text: '()',
  251. selection: [1, 1]
  252. };
  253. }
  254. } else if (text == ')') {
  255. var cursor = editor.getCursorPosition();
  256. var line = session.doc.getLine(cursor.row);
  257. var rightChar = line.substring(cursor.column, cursor.column + 1);
  258. if (rightChar == ')') {
  259. var matching = session.$findOpeningBracket(')', {column: cursor.column + 1, row: cursor.row});
  260. if (matching !== null) {
  261. return {
  262. text: '',
  263. selection: [1, 1]
  264. };
  265. }
  266. }
  267. }
  268. });
  269. this.add("parens", "deletion", function (state, action, editor, session, range) {
  270. var selected = session.doc.getTextRange(range);
  271. if (!range.isMultiLine() && selected == '(') {
  272. var line = session.doc.getLine(range.start.row);
  273. var rightChar = line.substring(range.start.column + 1, range.start.column + 2);
  274. if (rightChar == ')') {
  275. range.end.column++;
  276. return range;
  277. }
  278. }
  279. });
  280. this.add("string_dquotes", "insertion", function (state, action, editor, session, text) {
  281. if (text == '"' || text == "'") {
  282. var quote = text;
  283. var selection = editor.getSelectionRange();
  284. var selected = session.doc.getTextRange(selection);
  285. if (selected !== "") {
  286. return {
  287. text: quote + selected + quote,
  288. selection: false
  289. };
  290. } else {
  291. var cursor = editor.getCursorPosition();
  292. var line = session.doc.getLine(cursor.row);
  293. var leftChar = line.substring(cursor.column-1, cursor.column);
  294. // We're escaped.
  295. if (leftChar == '\\') {
  296. return null;
  297. }
  298. // Find what token we're inside.
  299. var tokens = session.getTokens(selection.start.row);
  300. var col = 0, token;
  301. var quotepos = -1; // Track whether we're inside an open quote.
  302. for (var x = 0; x < tokens.length; x++) {
  303. token = tokens[x];
  304. if (token.type == "string") {
  305. quotepos = -1;
  306. } else if (quotepos < 0) {
  307. quotepos = token.value.indexOf(quote);
  308. }
  309. if ((token.value.length + col) > selection.start.column) {
  310. break;
  311. }
  312. col += tokens[x].value.length;
  313. }
  314. // Try and be smart about when we auto insert.
  315. if (!token || (quotepos < 0 && token.type !== "comment" && (token.type !== "string" || ((selection.start.column !== token.value.length+col-1) && token.value.lastIndexOf(quote) === token.value.length-1)))) {
  316. return {
  317. text: quote + quote,
  318. selection: [1,1]
  319. };
  320. } else if (token && token.type === "string") {
  321. // Ignore input and move right one if we're typing over the closing quote.
  322. var rightChar = line.substring(cursor.column, cursor.column + 1);
  323. if (rightChar == quote) {
  324. return {
  325. text: '',
  326. selection: [1, 1]
  327. };
  328. }
  329. }
  330. }
  331. }
  332. });
  333. this.add("string_dquotes", "deletion", function (state, action, editor, session, range) {
  334. var selected = session.doc.getTextRange(range);
  335. if (!range.isMultiLine() && (selected == '"' || selected == "'")) {
  336. var line = session.doc.getLine(range.start.row);
  337. var rightChar = line.substring(range.start.column + 1, range.start.column + 2);
  338. if (rightChar == '"') {
  339. range.end.column++;
  340. return range;
  341. }
  342. }
  343. });
  344. };
  345. oop.inherits(CstyleBehaviour, Behaviour);
  346. exports.CstyleBehaviour = CstyleBehaviour;
  347. });
  348. ace.define('ace/mode/folding/cstyle', ['require', 'exports', 'module' , 'ace/lib/oop', 'ace/range', 'ace/mode/folding/fold_mode'], function(require, exports, module) {
  349. var oop = require("../../lib/oop");
  350. var Range = require("../../range").Range;
  351. var BaseFoldMode = require("./fold_mode").FoldMode;
  352. var FoldMode = exports.FoldMode = function() {};
  353. oop.inherits(FoldMode, BaseFoldMode);
  354. (function() {
  355. this.foldingStartMarker = /(\{|\[)[^\}\]]*$|^\s*(\/\*)/;
  356. this.foldingStopMarker = /^[^\[\{]*(\}|\])|^[\s\*]*(\*\/)/;
  357. this.getFoldWidgetRange = function(session, foldStyle, row) {
  358. var line = session.getLine(row);
  359. var match = line.match(this.foldingStartMarker);
  360. if (match) {
  361. var i = match.index;
  362. if (match[1])
  363. return this.openingBracketBlock(session, match[1], row, i);
  364. var range = session.getCommentFoldRange(row, i + match[0].length);
  365. range.end.column -= 2;
  366. return range;
  367. }
  368. if (foldStyle !== "markbeginend")
  369. return;
  370. var match = line.match(this.foldingStopMarker);
  371. if (match) {
  372. var i = match.index + match[0].length;
  373. if (match[2]) {
  374. var range = session.getCommentFoldRange(row, i);
  375. range.end.column -= 2;
  376. return range;
  377. }
  378. var end = {row: row, column: i};
  379. var start = session.$findOpeningBracket(match[1], end);
  380. if (!start)
  381. return;
  382. start.column++;
  383. end.column--;
  384. return Range.fromPoints(start, end);
  385. }
  386. };
  387. }).call(FoldMode.prototype);
  388. });
  389. ace.define('ace/mode/folding/fold_mode', ['require', 'exports', 'module' , 'ace/range'], function(require, exports, module) {
  390. var Range = require("../../range").Range;
  391. var FoldMode = exports.FoldMode = function() {};
  392. (function() {
  393. this.foldingStartMarker = null;
  394. this.foldingStopMarker = null;
  395. // must return "" if there's no fold, to enable caching
  396. this.getFoldWidget = function(session, foldStyle, row) {
  397. var line = session.getLine(row);
  398. if (this.foldingStartMarker.test(line))
  399. return "start";
  400. if (foldStyle == "markbeginend"
  401. && this.foldingStopMarker
  402. && this.foldingStopMarker.test(line))
  403. return "end";
  404. return "";
  405. };
  406. this.getFoldWidgetRange = function(session, foldStyle, row) {
  407. return null;
  408. };
  409. this.indentationBlock = function(session, row, column) {
  410. var re = /^\s*/;
  411. var startRow = row;
  412. var endRow = row;
  413. var line = session.getLine(row);
  414. var startColumn = column || line.length;
  415. var startLevel = line.match(re)[0].length;
  416. var maxRow = session.getLength()
  417. while (++row < maxRow) {
  418. line = session.getLine(row);
  419. var level = line.match(re)[0].length;
  420. if (level == line.length)
  421. continue;
  422. if (level <= startLevel)
  423. break;
  424. endRow = row;
  425. }
  426. if (endRow > startRow) {
  427. var endColumn = session.getLine(endRow).length;
  428. return new Range(startRow, startColumn, endRow, endColumn);
  429. }
  430. };
  431. this.openingBracketBlock = function(session, bracket, row, column, typeRe, allowBlankLine) {
  432. var start = {row: row, column: column + 1};
  433. var end = session.$findClosingBracket(bracket, start, typeRe, allowBlankLine);
  434. if (!end)
  435. return;
  436. var fw = session.foldWidgets[end.row];
  437. if (fw == null)
  438. fw = this.getFoldWidget(session, end.row);
  439. if (fw == "start") {
  440. end.row --;
  441. end.column = session.getLine(end.row).length;
  442. }
  443. return Range.fromPoints(start, end);
  444. };
  445. }).call(FoldMode.prototype);
  446. });