mode-jsx.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. ace.define("ace/mode/doc_comment_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",[], 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. defaultToken : "comment"
  141. }
  142. ]
  143. };
  144. this.embedRules(DocCommentHighlightRules, "doc-",
  145. [ DocCommentHighlightRules.getEndRule("start") ]);
  146. };
  147. oop.inherits(JsxHighlightRules, TextHighlightRules);
  148. exports.JsxHighlightRules = JsxHighlightRules;
  149. });
  150. ace.define("ace/mode/matching_brace_outdent",[], function(require, exports, module) {
  151. "use strict";
  152. var Range = require("../range").Range;
  153. var MatchingBraceOutdent = function() {};
  154. (function() {
  155. this.checkOutdent = function(line, input) {
  156. if (! /^\s+$/.test(line))
  157. return false;
  158. return /^\s*\}/.test(input);
  159. };
  160. this.autoOutdent = function(doc, row) {
  161. var line = doc.getLine(row);
  162. var match = line.match(/^(\s*\})/);
  163. if (!match) return 0;
  164. var column = match[1].length;
  165. var openBracePos = doc.findMatchingBracket({row: row, column: column});
  166. if (!openBracePos || openBracePos.row == row) return 0;
  167. var indent = this.$getIndent(doc.getLine(openBracePos.row));
  168. doc.replace(new Range(row, 0, row, column-1), indent);
  169. };
  170. this.$getIndent = function(line) {
  171. return line.match(/^\s*/)[0];
  172. };
  173. }).call(MatchingBraceOutdent.prototype);
  174. exports.MatchingBraceOutdent = MatchingBraceOutdent;
  175. });
  176. ace.define("ace/mode/folding/cstyle",[], function(require, exports, module) {
  177. "use strict";
  178. var oop = require("../../lib/oop");
  179. var Range = require("../../range").Range;
  180. var BaseFoldMode = require("./fold_mode").FoldMode;
  181. var FoldMode = exports.FoldMode = function(commentRegex) {
  182. if (commentRegex) {
  183. this.foldingStartMarker = new RegExp(
  184. this.foldingStartMarker.source.replace(/\|[^|]*?$/, "|" + commentRegex.start)
  185. );
  186. this.foldingStopMarker = new RegExp(
  187. this.foldingStopMarker.source.replace(/\|[^|]*?$/, "|" + commentRegex.end)
  188. );
  189. }
  190. };
  191. oop.inherits(FoldMode, BaseFoldMode);
  192. (function() {
  193. this.foldingStartMarker = /([\{\[\(])[^\}\]\)]*$|^\s*(\/\*)/;
  194. this.foldingStopMarker = /^[^\[\{\(]*([\}\]\)])|^[\s\*]*(\*\/)/;
  195. this.singleLineBlockCommentRe= /^\s*(\/\*).*\*\/\s*$/;
  196. this.tripleStarBlockCommentRe = /^\s*(\/\*\*\*).*\*\/\s*$/;
  197. this.startRegionRe = /^\s*(\/\*|\/\/)#?region\b/;
  198. this._getFoldWidgetBase = this.getFoldWidget;
  199. this.getFoldWidget = function(session, foldStyle, row) {
  200. var line = session.getLine(row);
  201. if (this.singleLineBlockCommentRe.test(line)) {
  202. if (!this.startRegionRe.test(line) && !this.tripleStarBlockCommentRe.test(line))
  203. return "";
  204. }
  205. var fw = this._getFoldWidgetBase(session, foldStyle, row);
  206. if (!fw && this.startRegionRe.test(line))
  207. return "start"; // lineCommentRegionStart
  208. return fw;
  209. };
  210. this.getFoldWidgetRange = function(session, foldStyle, row, forceMultiline) {
  211. var line = session.getLine(row);
  212. if (this.startRegionRe.test(line))
  213. return this.getCommentRegionBlock(session, line, row);
  214. var match = line.match(this.foldingStartMarker);
  215. if (match) {
  216. var i = match.index;
  217. if (match[1])
  218. return this.openingBracketBlock(session, match[1], row, i);
  219. var range = session.getCommentFoldRange(row, i + match[0].length, 1);
  220. if (range && !range.isMultiLine()) {
  221. if (forceMultiline) {
  222. range = this.getSectionRange(session, row);
  223. } else if (foldStyle != "all")
  224. range = null;
  225. }
  226. return range;
  227. }
  228. if (foldStyle === "markbegin")
  229. return;
  230. var match = line.match(this.foldingStopMarker);
  231. if (match) {
  232. var i = match.index + match[0].length;
  233. if (match[1])
  234. return this.closingBracketBlock(session, match[1], row, i);
  235. return session.getCommentFoldRange(row, i, -1);
  236. }
  237. };
  238. this.getSectionRange = function(session, row) {
  239. var line = session.getLine(row);
  240. var startIndent = line.search(/\S/);
  241. var startRow = row;
  242. var startColumn = line.length;
  243. row = row + 1;
  244. var endRow = row;
  245. var maxRow = session.getLength();
  246. while (++row < maxRow) {
  247. line = session.getLine(row);
  248. var indent = line.search(/\S/);
  249. if (indent === -1)
  250. continue;
  251. if (startIndent > indent)
  252. break;
  253. var subRange = this.getFoldWidgetRange(session, "all", row);
  254. if (subRange) {
  255. if (subRange.start.row <= startRow) {
  256. break;
  257. } else if (subRange.isMultiLine()) {
  258. row = subRange.end.row;
  259. } else if (startIndent == indent) {
  260. break;
  261. }
  262. }
  263. endRow = row;
  264. }
  265. return new Range(startRow, startColumn, endRow, session.getLine(endRow).length);
  266. };
  267. this.getCommentRegionBlock = function(session, line, row) {
  268. var startColumn = line.search(/\s*$/);
  269. var maxRow = session.getLength();
  270. var startRow = row;
  271. var re = /^\s*(?:\/\*|\/\/|--)#?(end)?region\b/;
  272. var depth = 1;
  273. while (++row < maxRow) {
  274. line = session.getLine(row);
  275. var m = re.exec(line);
  276. if (!m) continue;
  277. if (m[1]) depth--;
  278. else depth++;
  279. if (!depth) break;
  280. }
  281. var endRow = row;
  282. if (endRow > startRow) {
  283. return new Range(startRow, startColumn, endRow, line.length);
  284. }
  285. };
  286. }).call(FoldMode.prototype);
  287. });
  288. ace.define("ace/mode/jsx",[], function(require, exports, module) {
  289. "use strict";
  290. var oop = require("../lib/oop");
  291. var TextMode = require("./text").Mode;
  292. var JsxHighlightRules = require("./jsx_highlight_rules").JsxHighlightRules;
  293. var MatchingBraceOutdent = require("./matching_brace_outdent").MatchingBraceOutdent;
  294. var CstyleBehaviour = require("./behaviour/cstyle").CstyleBehaviour;
  295. var CStyleFoldMode = require("./folding/cstyle").FoldMode;
  296. function Mode() {
  297. this.HighlightRules = JsxHighlightRules;
  298. this.$outdent = new MatchingBraceOutdent();
  299. this.$behaviour = new CstyleBehaviour();
  300. this.foldingRules = new CStyleFoldMode();
  301. }
  302. oop.inherits(Mode, TextMode);
  303. (function() {
  304. this.lineCommentStart = "//";
  305. this.blockComment = {start: "/*", end: "*/"};
  306. this.getNextLineIndent = function(state, line, tab) {
  307. var indent = this.$getIndent(line);
  308. var tokenizedLine = this.getTokenizer().getLineTokens(line, state);
  309. var tokens = tokenizedLine.tokens;
  310. if (tokens.length && tokens[tokens.length-1].type == "comment") {
  311. return indent;
  312. }
  313. if (state == "start") {
  314. var match = line.match(/^.*[\{\(\[]\s*$/);
  315. if (match) {
  316. indent += tab;
  317. }
  318. }
  319. return indent;
  320. };
  321. this.checkOutdent = function(state, line, input) {
  322. return this.$outdent.checkOutdent(line, input);
  323. };
  324. this.autoOutdent = function(state, doc, row) {
  325. this.$outdent.autoOutdent(doc, row);
  326. };
  327. this.$id = "ace/mode/jsx";
  328. }).call(Mode.prototype);
  329. exports.Mode = Mode;
  330. });
  331. (function() {
  332. ace.require(["ace/mode/jsx"], function(m) {
  333. if (typeof module == "object" && typeof exports == "object" && module) {
  334. module.exports = m;
  335. }
  336. });
  337. })();