mode-ruby.js 18 KB

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