mode-ruby.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  1. ace.define("ace/mode/ruby_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 constantOtherSymbol = exports.constantOtherSymbol = {
  6. token : "constant.other.symbol.ruby", // symbol
  7. regex : "[:](?:[A-Za-z_]|[@$](?=[a-zA-Z0-9_]))[a-zA-Z0-9_]*[!=?]?"
  8. };
  9. var qString = exports.qString = {
  10. token : "string", // single line
  11. regex : "['](?:(?:\\\\.)|(?:[^'\\\\]))*?[']"
  12. };
  13. var qqString = exports.qqString = {
  14. token : "string", // single line
  15. regex : '["](?:(?:\\\\.)|(?:[^"\\\\]))*?["]'
  16. };
  17. var tString = exports.tString = {
  18. token : "string", // backtick string
  19. regex : "[`](?:(?:\\\\.)|(?:[^'\\\\]))*?[`]"
  20. };
  21. var constantNumericHex = exports.constantNumericHex = {
  22. token : "constant.numeric", // hex
  23. regex : "0[xX][0-9a-fA-F](?:[0-9a-fA-F]|_(?=[0-9a-fA-F]))*\\b"
  24. };
  25. var constantNumericFloat = exports.constantNumericFloat = {
  26. token : "constant.numeric", // float
  27. regex : "[+-]?\\d(?:\\d|_(?=\\d))*(?:(?:\\.\\d(?:\\d|_(?=\\d))*)?(?:[eE][+-]?\\d+)?)?\\b"
  28. };
  29. var instanceVariable = exports.instanceVariable = {
  30. token : "variable.instance", // instance variable
  31. regex : "@{1,2}[a-zA-Z_\\d]+"
  32. };
  33. var RubyHighlightRules = function() {
  34. var builtinFunctions = (
  35. "abort|Array|assert|assert_equal|assert_not_equal|assert_same|assert_not_same|" +
  36. "assert_nil|assert_not_nil|assert_match|assert_no_match|assert_in_delta|assert_throws|" +
  37. "assert_raise|assert_nothing_raised|assert_instance_of|assert_kind_of|assert_respond_to|" +
  38. "assert_operator|assert_send|assert_difference|assert_no_difference|assert_recognizes|" +
  39. "assert_generates|assert_response|assert_redirected_to|assert_template|assert_select|" +
  40. "assert_select_email|assert_select_rjs|assert_select_encoded|css_select|at_exit|" +
  41. "attr|attr_writer|attr_reader|attr_accessor|attr_accessible|autoload|binding|block_given?|callcc|" +
  42. "caller|catch|chomp|chomp!|chop|chop!|defined?|delete_via_redirect|eval|exec|exit|" +
  43. "exit!|fail|Float|flunk|follow_redirect!|fork|form_for|form_tag|format|gets|global_variables|gsub|" +
  44. "gsub!|get_via_redirect|host!|https?|https!|include|Integer|lambda|link_to|" +
  45. "link_to_unless_current|link_to_function|link_to_remote|load|local_variables|loop|open|open_session|" +
  46. "p|print|printf|proc|putc|puts|post_via_redirect|put_via_redirect|raise|rand|" +
  47. "raw|readline|readlines|redirect?|request_via_redirect|require|scan|select|" +
  48. "set_trace_func|sleep|split|sprintf|srand|String|stylesheet_link_tag|syscall|system|sub|sub!|test|" +
  49. "throw|trace_var|trap|untrace_var|atan2|cos|exp|frexp|ldexp|log|log10|sin|sqrt|tan|" +
  50. "render|javascript_include_tag|csrf_meta_tag|label_tag|text_field_tag|submit_tag|check_box_tag|" +
  51. "content_tag|radio_button_tag|text_area_tag|password_field_tag|hidden_field_tag|" +
  52. "fields_for|select_tag|options_for_select|options_from_collection_for_select|collection_select|" +
  53. "time_zone_select|select_date|select_time|select_datetime|date_select|time_select|datetime_select|" +
  54. "select_year|select_month|select_day|select_hour|select_minute|select_second|file_field_tag|" +
  55. "file_field|respond_to|skip_before_filter|around_filter|after_filter|verify|" +
  56. "protect_from_forgery|rescue_from|helper_method|redirect_to|before_filter|" +
  57. "send_data|send_file|validates_presence_of|validates_uniqueness_of|validates_length_of|" +
  58. "validates_format_of|validates_acceptance_of|validates_associated|validates_exclusion_of|" +
  59. "validates_inclusion_of|validates_numericality_of|validates_with|validates_each|" +
  60. "authenticate_or_request_with_http_basic|authenticate_or_request_with_http_digest|" +
  61. "filter_parameter_logging|match|get|post|resources|redirect|scope|assert_routing|" +
  62. "translate|localize|extract_locale_from_tld|caches_page|expire_page|caches_action|expire_action|" +
  63. "cache|expire_fragment|expire_cache_for|observe|cache_sweeper|" +
  64. "has_many|has_one|belongs_to|has_and_belongs_to_many"
  65. );
  66. var keywords = (
  67. "alias|and|BEGIN|begin|break|case|class|def|defined|do|else|elsif|END|end|ensure|" +
  68. "__FILE__|finally|for|gem|if|in|__LINE__|module|next|not|or|private|protected|public|" +
  69. "redo|rescue|retry|return|super|then|undef|unless|until|when|while|yield"
  70. );
  71. var buildinConstants = (
  72. "true|TRUE|false|FALSE|nil|NIL|ARGF|ARGV|DATA|ENV|RUBY_PLATFORM|RUBY_RELEASE_DATE|" +
  73. "RUBY_VERSION|STDERR|STDIN|STDOUT|TOPLEVEL_BINDING"
  74. );
  75. var builtinVariables = (
  76. "$DEBUG|$defout|$FILENAME|$LOAD_PATH|$SAFE|$stdin|$stdout|$stderr|$VERBOSE|" +
  77. "$!|root_url|flash|session|cookies|params|request|response|logger|self"
  78. );
  79. var keywordMapper = this.$keywords = this.createKeywordMapper({
  80. "keyword": keywords,
  81. "constant.language": buildinConstants,
  82. "variable.language": builtinVariables,
  83. "support.function": builtinFunctions,
  84. "invalid.deprecated": "debugger" // TODO is this a remnant from js mode?
  85. }, "identifier");
  86. this.$rules = {
  87. "start" : [
  88. {
  89. token : "comment",
  90. regex : "#.*$"
  91. }, {
  92. token : "comment", // multi line comment
  93. regex : "^=begin(?:$|\\s.*$)",
  94. next : "comment"
  95. }, {
  96. token : "string.regexp",
  97. regex : "[/](?:(?:\\[(?:\\\\]|[^\\]])+\\])|(?:\\\\/|[^\\]/]))*[/]\\w*\\s*(?=[).,;]|$)"
  98. },
  99. [{
  100. regex: "[{}]", onMatch: function(val, state, stack) {
  101. this.next = val == "{" ? this.nextState : "";
  102. if (val == "{" && stack.length) {
  103. stack.unshift("start", state);
  104. return "paren.lparen";
  105. }
  106. if (val == "}" && stack.length) {
  107. stack.shift();
  108. this.next = stack.shift();
  109. if (this.next.indexOf("string") != -1)
  110. return "paren.end";
  111. }
  112. return val == "{" ? "paren.lparen" : "paren.rparen";
  113. },
  114. nextState: "start"
  115. }, {
  116. token : "string.start",
  117. regex : /"/,
  118. push : [{
  119. token : "constant.language.escape",
  120. regex : /\\(?:[nsrtvfbae'"\\]|c.|C-.|M-.(?:\\C-.)?|[0-7]{3}|x[\da-fA-F]{2}|u[\da-fA-F]{4})/
  121. }, {
  122. token : "paren.start",
  123. regex : /#{/,
  124. push : "start"
  125. }, {
  126. token : "string.end",
  127. regex : /"/,
  128. next : "pop"
  129. }, {
  130. defaultToken: "string"
  131. }]
  132. }, {
  133. token : "string.start",
  134. regex : /`/,
  135. push : [{
  136. token : "constant.language.escape",
  137. regex : /\\(?:[nsrtvfbae'"\\]|c.|C-.|M-.(?:\\C-.)?|[0-7]{3}|x[\da-fA-F]{2}|u[\da-fA-F]{4})/
  138. }, {
  139. token : "paren.start",
  140. regex : /#{/,
  141. push : "start"
  142. }, {
  143. token : "string.end",
  144. regex : /`/,
  145. next : "pop"
  146. }, {
  147. defaultToken: "string"
  148. }]
  149. }, {
  150. token : "string.start",
  151. regex : /'/,
  152. push : [{
  153. token : "constant.language.escape",
  154. regex : /\\['\\]/
  155. }, {
  156. token : "string.end",
  157. regex : /'/,
  158. next : "pop"
  159. }, {
  160. defaultToken: "string"
  161. }]
  162. }],
  163. {
  164. token : "text", // namespaces aren't symbols
  165. regex : "::"
  166. }, {
  167. token : "variable.instance", // instance variable
  168. regex : "@{1,2}[a-zA-Z_\\d]+"
  169. }, {
  170. token : "support.class", // class name
  171. regex : "[A-Z][a-zA-Z_\\d]+"
  172. },
  173. constantOtherSymbol,
  174. constantNumericHex,
  175. constantNumericFloat,
  176. {
  177. token : "constant.language.boolean",
  178. regex : "(?:true|false)\\b"
  179. }, {
  180. token : keywordMapper,
  181. regex : "[a-zA-Z_$][a-zA-Z0-9_$]*\\b"
  182. }, {
  183. token : "punctuation.separator.key-value",
  184. regex : "=>"
  185. }, {
  186. stateName: "heredoc",
  187. onMatch : function(value, currentState, stack) {
  188. var next = value[2] == '-' ? "indentedHeredoc" : "heredoc";
  189. var tokens = value.split(this.splitRegex);
  190. stack.push(next, tokens[3]);
  191. return [
  192. {type:"constant", value: tokens[1]},
  193. {type:"string", value: tokens[2]},
  194. {type:"support.class", value: tokens[3]},
  195. {type:"string", value: tokens[4]}
  196. ];
  197. },
  198. regex : "(<<-?)(['\"`]?)([\\w]+)(['\"`]?)",
  199. rules: {
  200. heredoc: [{
  201. onMatch: function(value, currentState, stack) {
  202. if (value === stack[1]) {
  203. stack.shift();
  204. stack.shift();
  205. this.next = stack[0] || "start";
  206. return "support.class";
  207. }
  208. this.next = "";
  209. return "string";
  210. },
  211. regex: ".*$",
  212. next: "start"
  213. }],
  214. indentedHeredoc: [{
  215. token: "string",
  216. regex: "^ +"
  217. }, {
  218. onMatch: function(value, currentState, stack) {
  219. if (value === stack[1]) {
  220. stack.shift();
  221. stack.shift();
  222. this.next = stack[0] || "start";
  223. return "support.class";
  224. }
  225. this.next = "";
  226. return "string";
  227. },
  228. regex: ".*$",
  229. next: "start"
  230. }]
  231. }
  232. }, {
  233. regex : "$",
  234. token : "empty",
  235. next : function(currentState, stack) {
  236. if (stack[0] === "heredoc" || stack[0] === "indentedHeredoc")
  237. return stack[0];
  238. return currentState;
  239. }
  240. }, {
  241. token : "string.character",
  242. regex : "\\B\\?."
  243. }, {
  244. token : "keyword.operator",
  245. regex : "!|\\$|%|&|\\*|\\-\\-|\\-|\\+\\+|\\+|~|===|==|=|!=|!==|<=|>=|<<=|>>=|>>>=|<>|<|>|!|&&|\\|\\||\\?\\:|\\*=|%=|\\+=|\\-=|&=|\\^=|\\b(?:in|instanceof|new|delete|typeof|void)"
  246. }, {
  247. token : "paren.lparen",
  248. regex : "[[({]"
  249. }, {
  250. token : "paren.rparen",
  251. regex : "[\\])}]"
  252. }, {
  253. token : "text",
  254. regex : "\\s+"
  255. }
  256. ],
  257. "comment" : [
  258. {
  259. token : "comment", // closing comment
  260. regex : "^=end(?:$|\\s.*$)",
  261. next : "start"
  262. }, {
  263. token : "comment", // comment spanning whole line
  264. regex : ".+"
  265. }
  266. ]
  267. };
  268. this.normalizeRules();
  269. };
  270. oop.inherits(RubyHighlightRules, TextHighlightRules);
  271. exports.RubyHighlightRules = RubyHighlightRules;
  272. });
  273. ace.define("ace/mode/matching_brace_outdent",[], function(require, exports, module) {
  274. "use strict";
  275. var Range = require("../range").Range;
  276. var MatchingBraceOutdent = function() {};
  277. (function() {
  278. this.checkOutdent = function(line, input) {
  279. if (! /^\s+$/.test(line))
  280. return false;
  281. return /^\s*\}/.test(input);
  282. };
  283. this.autoOutdent = function(doc, row) {
  284. var line = doc.getLine(row);
  285. var match = line.match(/^(\s*\})/);
  286. if (!match) return 0;
  287. var column = match[1].length;
  288. var openBracePos = doc.findMatchingBracket({row: row, column: column});
  289. if (!openBracePos || openBracePos.row == row) return 0;
  290. var indent = this.$getIndent(doc.getLine(openBracePos.row));
  291. doc.replace(new Range(row, 0, row, column-1), indent);
  292. };
  293. this.$getIndent = function(line) {
  294. return line.match(/^\s*/)[0];
  295. };
  296. }).call(MatchingBraceOutdent.prototype);
  297. exports.MatchingBraceOutdent = MatchingBraceOutdent;
  298. });
  299. ace.define("ace/mode/folding/coffee",[], function(require, exports, module) {
  300. "use strict";
  301. var oop = require("../../lib/oop");
  302. var BaseFoldMode = require("./fold_mode").FoldMode;
  303. var Range = require("../../range").Range;
  304. var FoldMode = exports.FoldMode = function() {};
  305. oop.inherits(FoldMode, BaseFoldMode);
  306. (function() {
  307. this.getFoldWidgetRange = function(session, foldStyle, row) {
  308. var range = this.indentationBlock(session, row);
  309. if (range)
  310. return range;
  311. var re = /\S/;
  312. var line = session.getLine(row);
  313. var startLevel = line.search(re);
  314. if (startLevel == -1 || line[startLevel] != "#")
  315. return;
  316. var startColumn = line.length;
  317. var maxRow = session.getLength();
  318. var startRow = row;
  319. var endRow = row;
  320. while (++row < maxRow) {
  321. line = session.getLine(row);
  322. var level = line.search(re);
  323. if (level == -1)
  324. continue;
  325. if (line[level] != "#")
  326. break;
  327. endRow = row;
  328. }
  329. if (endRow > startRow) {
  330. var endColumn = session.getLine(endRow).length;
  331. return new Range(startRow, startColumn, endRow, endColumn);
  332. }
  333. };
  334. this.getFoldWidget = function(session, foldStyle, row) {
  335. var line = session.getLine(row);
  336. var indent = line.search(/\S/);
  337. var next = session.getLine(row + 1);
  338. var prev = session.getLine(row - 1);
  339. var prevIndent = prev.search(/\S/);
  340. var nextIndent = next.search(/\S/);
  341. if (indent == -1) {
  342. session.foldWidgets[row - 1] = prevIndent!= -1 && prevIndent < nextIndent ? "start" : "";
  343. return "";
  344. }
  345. if (prevIndent == -1) {
  346. if (indent == nextIndent && line[indent] == "#" && next[indent] == "#") {
  347. session.foldWidgets[row - 1] = "";
  348. session.foldWidgets[row + 1] = "";
  349. return "start";
  350. }
  351. } else if (prevIndent == indent && line[indent] == "#" && prev[indent] == "#") {
  352. if (session.getLine(row - 2).search(/\S/) == -1) {
  353. session.foldWidgets[row - 1] = "start";
  354. session.foldWidgets[row + 1] = "";
  355. return "";
  356. }
  357. }
  358. if (prevIndent!= -1 && prevIndent < indent)
  359. session.foldWidgets[row - 1] = "start";
  360. else
  361. session.foldWidgets[row - 1] = "";
  362. if (indent < nextIndent)
  363. return "start";
  364. else
  365. return "";
  366. };
  367. }).call(FoldMode.prototype);
  368. });
  369. ace.define("ace/mode/ruby",[], function(require, exports, module) {
  370. "use strict";
  371. var oop = require("../lib/oop");
  372. var TextMode = require("./text").Mode;
  373. var RubyHighlightRules = require("./ruby_highlight_rules").RubyHighlightRules;
  374. var MatchingBraceOutdent = require("./matching_brace_outdent").MatchingBraceOutdent;
  375. var Range = require("../range").Range;
  376. var CstyleBehaviour = require("./behaviour/cstyle").CstyleBehaviour;
  377. var FoldMode = require("./folding/coffee").FoldMode;
  378. var Mode = function() {
  379. this.HighlightRules = RubyHighlightRules;
  380. this.$outdent = new MatchingBraceOutdent();
  381. this.$behaviour = new CstyleBehaviour();
  382. this.foldingRules = new FoldMode();
  383. };
  384. oop.inherits(Mode, TextMode);
  385. (function() {
  386. this.lineCommentStart = "#";
  387. this.getNextLineIndent = function(state, line, tab) {
  388. var indent = this.$getIndent(line);
  389. var tokenizedLine = this.getTokenizer().getLineTokens(line, state);
  390. var tokens = tokenizedLine.tokens;
  391. if (tokens.length && tokens[tokens.length-1].type == "comment") {
  392. return indent;
  393. }
  394. if (state == "start") {
  395. var match = line.match(/^.*[\{\(\[]\s*$/);
  396. var startingClassOrMethod = line.match(/^\s*(class|def|module)\s.*$/);
  397. var startingDoBlock = line.match(/.*do(\s*|\s+\|.*\|\s*)$/);
  398. var startingConditional = line.match(/^\s*(if|else|when)\s*/);
  399. if (match || startingClassOrMethod || startingDoBlock || startingConditional) {
  400. indent += tab;
  401. }
  402. }
  403. return indent;
  404. };
  405. this.checkOutdent = function(state, line, input) {
  406. return /^\s+(end|else)$/.test(line + input) || this.$outdent.checkOutdent(line, input);
  407. };
  408. this.autoOutdent = function(state, session, row) {
  409. var line = session.getLine(row);
  410. if (/}/.test(line))
  411. return this.$outdent.autoOutdent(session, row);
  412. var indent = this.$getIndent(line);
  413. var prevLine = session.getLine(row - 1);
  414. var prevIndent = this.$getIndent(prevLine);
  415. var tab = session.getTabString();
  416. if (prevIndent.length <= indent.length) {
  417. if (indent.slice(-tab.length) == tab)
  418. session.remove(new Range(row, indent.length-tab.length, row, indent.length));
  419. }
  420. };
  421. this.$id = "ace/mode/ruby";
  422. }).call(Mode.prototype);
  423. exports.Mode = Mode;
  424. });
  425. (function() {
  426. ace.require(["ace/mode/ruby"], function(m) {
  427. if (typeof module == "object" && typeof exports == "object" && module) {
  428. module.exports = m;
  429. }
  430. });
  431. })();