mode-jsx.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. ace.define("ace/mode/doc_comment_highlight_rules",["require","exports","module","ace/lib/oop","ace/mode/text_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 DocCommentHighlightRules = function() {
  6. this.$rules = {
  7. "start" : [ {
  8. token : "comment.doc.tag",
  9. regex : "@[\\w\\d_]+" // TODO: fix email addresses
  10. },
  11. DocCommentHighlightRules.getTagRule(),
  12. {
  13. defaultToken : "comment.doc",
  14. caseInsensitive: true
  15. }]
  16. };
  17. };
  18. oop.inherits(DocCommentHighlightRules, TextHighlightRules);
  19. DocCommentHighlightRules.getTagRule = function(start) {
  20. return {
  21. token : "comment.doc.tag.storage.type",
  22. regex : "\\b(?:TODO|FIXME|XXX|HACK)\\b"
  23. };
  24. }
  25. DocCommentHighlightRules.getStartRule = function(start) {
  26. return {
  27. token : "comment.doc", // doc comment
  28. regex : "\\/\\*(?=\\*)",
  29. next : start
  30. };
  31. };
  32. DocCommentHighlightRules.getEndRule = function (start) {
  33. return {
  34. token : "comment.doc", // closing comment
  35. regex : "\\*\\/",
  36. next : start
  37. };
  38. };
  39. exports.DocCommentHighlightRules = DocCommentHighlightRules;
  40. });
  41. ace.define("ace/mode/jsx_highlight_rules",["require","exports","module","ace/lib/oop","ace/lib/lang","ace/mode/doc_comment_highlight_rules","ace/mode/text_highlight_rules"], function(require, exports, module) {
  42. var oop = require("../lib/oop");
  43. var lang = require("../lib/lang");
  44. var DocCommentHighlightRules = require("./doc_comment_highlight_rules").DocCommentHighlightRules;
  45. var TextHighlightRules = require("./text_highlight_rules").TextHighlightRules;
  46. var JsxHighlightRules = function() {
  47. var keywords = lang.arrayToMap(
  48. ("break|do|instanceof|typeof|case|else|new|var|catch|finally|return|void|continue|for|switch|default|while|function|this|" +
  49. "if|throw|" +
  50. "delete|in|try|" +
  51. "class|extends|super|import|from|into|implements|interface|static|mixin|override|abstract|final|" +
  52. "number|int|string|boolean|variant|" +
  53. "log|assert").split("|")
  54. );
  55. var buildinConstants = lang.arrayToMap(
  56. ("null|true|false|NaN|Infinity|__FILE__|__LINE__|undefined").split("|")
  57. );
  58. var reserved = lang.arrayToMap(
  59. ("debugger|with|" +
  60. "const|export|" +
  61. "let|private|public|yield|protected|" +
  62. "extern|native|as|operator|__fake__|__readonly__").split("|")
  63. );
  64. var identifierRe = "[a-zA-Z_][a-zA-Z0-9_]*\\b";
  65. this.$rules = {
  66. "start" : [
  67. {
  68. token : "comment",
  69. regex : "\\/\\/.*$"
  70. },
  71. DocCommentHighlightRules.getStartRule("doc-start"),
  72. {
  73. token : "comment", // multi line comment
  74. regex : "\\/\\*",
  75. next : "comment"
  76. }, {
  77. token : "string.regexp",
  78. regex : "[/](?:(?:\\[(?:\\\\]|[^\\]])+\\])|(?:\\\\/|[^\\]/]))*[/]\\w*\\s*(?=[).,;]|$)"
  79. }, {
  80. token : "string", // single line
  81. regex : '["](?:(?:\\\\.)|(?:[^"\\\\]))*?["]'
  82. }, {
  83. token : "string", // single line
  84. regex : "['](?:(?:\\\\.)|(?:[^'\\\\]))*?[']"
  85. }, {
  86. token : "constant.numeric", // hex
  87. regex : "0[xX][0-9a-fA-F]+\\b"
  88. }, {
  89. token : "constant.numeric", // float
  90. regex : "[+-]?\\d+(?:(?:\\.\\d*)?(?:[eE][+-]?\\d+)?)?\\b"
  91. }, {
  92. token : "constant.language.boolean",
  93. regex : "(?:true|false)\\b"
  94. }, {
  95. token : [
  96. "storage.type",
  97. "text",
  98. "entity.name.function"
  99. ],
  100. regex : "(function)(\\s+)(" + identifierRe + ")"
  101. }, {
  102. token : function(value) {
  103. if (value == "this")
  104. return "variable.language";
  105. else if (value == "function")
  106. return "storage.type";
  107. else if (keywords.hasOwnProperty(value) || reserved.hasOwnProperty(value))
  108. return "keyword";
  109. else if (buildinConstants.hasOwnProperty(value))
  110. return "constant.language";
  111. else if (/^_?[A-Z][a-zA-Z0-9_]*$/.test(value))
  112. return "language.support.class";
  113. else
  114. return "identifier";
  115. },
  116. regex : identifierRe
  117. }, {
  118. token : "keyword.operator",
  119. regex : "!|%|&|\\*|\\-\\-|\\-|\\+\\+|\\+|~|==|=|!=|<=|>=|<<=|>>=|>>>=|<>|<|>|!|&&|\\|\\||\\?\\:|\\*=|%=|\\+=|\\-=|&=|\\^=|\\b(?:in|instanceof|new|delete|typeof|void)"
  120. }, {
  121. token : "punctuation.operator",
  122. regex : "\\?|\\:|\\,|\\;|\\."
  123. }, {
  124. token : "paren.lparen",
  125. regex : "[[({<]"
  126. }, {
  127. token : "paren.rparen",
  128. regex : "[\\])}>]"
  129. }, {
  130. token : "text",
  131. regex : "\\s+"
  132. }
  133. ],
  134. "comment" : [
  135. {
  136. token : "comment", // closing comment
  137. regex : ".*?\\*\\/",
  138. next : "start"
  139. }, {
  140. token : "comment", // comment spanning whole line
  141. regex : ".+"
  142. }
  143. ]
  144. };
  145. this.embedRules(DocCommentHighlightRules, "doc-",
  146. [ DocCommentHighlightRules.getEndRule("start") ]);
  147. };
  148. oop.inherits(JsxHighlightRules, TextHighlightRules);
  149. exports.JsxHighlightRules = JsxHighlightRules;
  150. });
  151. ace.define("ace/mode/matching_brace_outdent",["require","exports","module","ace/range"], function(require, exports, module) {
  152. "use strict";
  153. var Range = require("../range").Range;
  154. var MatchingBraceOutdent = function() {};
  155. (function() {
  156. this.checkOutdent = function(line, input) {
  157. if (! /^\s+$/.test(line))
  158. return false;
  159. return /^\s*\}/.test(input);
  160. };
  161. this.autoOutdent = function(doc, row) {
  162. var line = doc.getLine(row);
  163. var match = line.match(/^(\s*\})/);
  164. if (!match) return 0;
  165. var column = match[1].length;
  166. var openBracePos = doc.findMatchingBracket({row: row, column: column});
  167. if (!openBracePos || openBracePos.row == row) return 0;
  168. var indent = this.$getIndent(doc.getLine(openBracePos.row));
  169. doc.replace(new Range(row, 0, row, column-1), indent);
  170. };
  171. this.$getIndent = function(line) {
  172. return line.match(/^\s*/)[0];
  173. };
  174. }).call(MatchingBraceOutdent.prototype);
  175. exports.MatchingBraceOutdent = MatchingBraceOutdent;
  176. });
  177. ace.define("ace/mode/folding/cstyle",["require","exports","module","ace/lib/oop","ace/range","ace/mode/folding/fold_mode"], function(require, exports, module) {
  178. "use strict";
  179. var oop = require("../../lib/oop");
  180. var Range = require("../../range").Range;
  181. var BaseFoldMode = require("./fold_mode").FoldMode;
  182. var FoldMode = exports.FoldMode = function(commentRegex) {
  183. if (commentRegex) {
  184. this.foldingStartMarker = new RegExp(
  185. this.foldingStartMarker.source.replace(/\|[^|]*?$/, "|" + commentRegex.start)
  186. );
  187. this.foldingStopMarker = new RegExp(
  188. this.foldingStopMarker.source.replace(/\|[^|]*?$/, "|" + commentRegex.end)
  189. );
  190. }
  191. };
  192. oop.inherits(FoldMode, BaseFoldMode);
  193. (function() {
  194. this.foldingStartMarker = /(\{|\[)[^\}\]]*$|^\s*(\/\*)/;
  195. this.foldingStopMarker = /^[^\[\{]*(\}|\])|^[\s\*]*(\*\/)/;
  196. this.singleLineBlockCommentRe= /^\s*(\/\*).*\*\/\s*$/;
  197. this.tripleStarBlockCommentRe = /^\s*(\/\*\*\*).*\*\/\s*$/;
  198. this.startRegionRe = /^\s*(\/\*|\/\/)#?region\b/;
  199. this._getFoldWidgetBase = this.getFoldWidget;
  200. this.getFoldWidget = function(session, foldStyle, row) {
  201. var line = session.getLine(row);
  202. if (this.singleLineBlockCommentRe.test(line)) {
  203. if (!this.startRegionRe.test(line) && !this.tripleStarBlockCommentRe.test(line))
  204. return "";
  205. }
  206. var fw = this._getFoldWidgetBase(session, foldStyle, row);
  207. if (!fw && this.startRegionRe.test(line))
  208. return "start"; // lineCommentRegionStart
  209. return fw;
  210. };
  211. this.getFoldWidgetRange = function(session, foldStyle, row, forceMultiline) {
  212. var line = session.getLine(row);
  213. if (this.startRegionRe.test(line))
  214. return this.getCommentRegionBlock(session, line, row);
  215. var match = line.match(this.foldingStartMarker);
  216. if (match) {
  217. var i = match.index;
  218. if (match[1])
  219. return this.openingBracketBlock(session, match[1], row, i);
  220. var range = session.getCommentFoldRange(row, i + match[0].length, 1);
  221. if (range && !range.isMultiLine()) {
  222. if (forceMultiline) {
  223. range = this.getSectionRange(session, row);
  224. } else if (foldStyle != "all")
  225. range = null;
  226. }
  227. return range;
  228. }
  229. if (foldStyle === "markbegin")
  230. return;
  231. var match = line.match(this.foldingStopMarker);
  232. if (match) {
  233. var i = match.index + match[0].length;
  234. if (match[1])
  235. return this.closingBracketBlock(session, match[1], row, i);
  236. return session.getCommentFoldRange(row, i, -1);
  237. }
  238. };
  239. this.getSectionRange = function(session, row) {
  240. var line = session.getLine(row);
  241. var startIndent = line.search(/\S/);
  242. var startRow = row;
  243. var startColumn = line.length;
  244. row = row + 1;
  245. var endRow = row;
  246. var maxRow = session.getLength();
  247. while (++row < maxRow) {
  248. line = session.getLine(row);
  249. var indent = line.search(/\S/);
  250. if (indent === -1)
  251. continue;
  252. if (startIndent > indent)
  253. break;
  254. var subRange = this.getFoldWidgetRange(session, "all", row);
  255. if (subRange) {
  256. if (subRange.start.row <= startRow) {
  257. break;
  258. } else if (subRange.isMultiLine()) {
  259. row = subRange.end.row;
  260. } else if (startIndent == indent) {
  261. break;
  262. }
  263. }
  264. endRow = row;
  265. }
  266. return new Range(startRow, startColumn, endRow, session.getLine(endRow).length);
  267. };
  268. this.getCommentRegionBlock = function(session, line, row) {
  269. var startColumn = line.search(/\s*$/);
  270. var maxRow = session.getLength();
  271. var startRow = row;
  272. var re = /^\s*(?:\/\*|\/\/|--)#?(end)?region\b/;
  273. var depth = 1;
  274. while (++row < maxRow) {
  275. line = session.getLine(row);
  276. var m = re.exec(line);
  277. if (!m) continue;
  278. if (m[1]) depth--;
  279. else depth++;
  280. if (!depth) break;
  281. }
  282. var endRow = row;
  283. if (endRow > startRow) {
  284. return new Range(startRow, startColumn, endRow, line.length);
  285. }
  286. };
  287. }).call(FoldMode.prototype);
  288. });
  289. ace.define("ace/mode/jsx",["require","exports","module","ace/lib/oop","ace/mode/text","ace/mode/jsx_highlight_rules","ace/mode/matching_brace_outdent","ace/mode/behaviour/cstyle","ace/mode/folding/cstyle"], function(require, exports, module) {
  290. "use strict";
  291. var oop = require("../lib/oop");
  292. var TextMode = require("./text").Mode;
  293. var JsxHighlightRules = require("./jsx_highlight_rules").JsxHighlightRules;
  294. var MatchingBraceOutdent = require("./matching_brace_outdent").MatchingBraceOutdent;
  295. var CstyleBehaviour = require("./behaviour/cstyle").CstyleBehaviour;
  296. var CStyleFoldMode = require("./folding/cstyle").FoldMode;
  297. function Mode() {
  298. this.HighlightRules = JsxHighlightRules;
  299. this.$outdent = new MatchingBraceOutdent();
  300. this.$behaviour = new CstyleBehaviour();
  301. this.foldingRules = new CStyleFoldMode();
  302. }
  303. oop.inherits(Mode, TextMode);
  304. (function() {
  305. this.lineCommentStart = "//";
  306. this.blockComment = {start: "/*", end: "*/"};
  307. this.getNextLineIndent = function(state, line, tab) {
  308. var indent = this.$getIndent(line);
  309. var tokenizedLine = this.getTokenizer().getLineTokens(line, state);
  310. var tokens = tokenizedLine.tokens;
  311. if (tokens.length && tokens[tokens.length-1].type == "comment") {
  312. return indent;
  313. }
  314. if (state == "start") {
  315. var match = line.match(/^.*[\{\(\[]\s*$/);
  316. if (match) {
  317. indent += tab;
  318. }
  319. }
  320. return indent;
  321. };
  322. this.checkOutdent = function(state, line, input) {
  323. return this.$outdent.checkOutdent(line, input);
  324. };
  325. this.autoOutdent = function(state, doc, row) {
  326. this.$outdent.autoOutdent(doc, row);
  327. };
  328. this.$id = "ace/mode/jsx";
  329. }).call(Mode.prototype);
  330. exports.Mode = Mode;
  331. });