mode-ruby.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  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. qString,
  96. qqString,
  97. tString,
  98. {
  99. token : "text", // namespaces aren't symbols
  100. regex : "::"
  101. }, {
  102. token : "variable.instance", // instance variable
  103. regex : "@{1,2}[a-zA-Z_\\d]+"
  104. }, {
  105. token : "support.class", // class name
  106. regex : "[A-Z][a-zA-Z_\\d]+"
  107. },
  108. constantOtherSymbol,
  109. constantNumericHex,
  110. constantNumericFloat,
  111. {
  112. token : "constant.language.boolean",
  113. regex : "(?:true|false)\\b"
  114. }, {
  115. token : keywordMapper,
  116. regex : "[a-zA-Z_$][a-zA-Z0-9_$]*\\b"
  117. }, {
  118. token : "punctuation.separator.key-value",
  119. regex : "=>"
  120. }, {
  121. stateName: "heredoc",
  122. onMatch : function(value, currentState, stack) {
  123. var next = value[2] == '-' ? "indentedHeredoc" : "heredoc";
  124. var tokens = value.split(this.splitRegex);
  125. stack.push(next, tokens[3]);
  126. return [
  127. {type:"constant", value: tokens[1]},
  128. {type:"string", value: tokens[2]},
  129. {type:"support.class", value: tokens[3]},
  130. {type:"string", value: tokens[4]}
  131. ];
  132. },
  133. regex : "(<<-?)(['\"`]?)([\\w]+)(['\"`]?)",
  134. rules: {
  135. heredoc: [{
  136. onMatch: function(value, currentState, stack) {
  137. if (value === stack[1]) {
  138. stack.shift();
  139. stack.shift();
  140. this.next = stack[0] || "start";
  141. return "support.class";
  142. }
  143. this.next = "";
  144. return "string";
  145. },
  146. regex: ".*$",
  147. next: "start"
  148. }],
  149. indentedHeredoc: [{
  150. token: "string",
  151. regex: "^ +"
  152. }, {
  153. onMatch: function(value, currentState, stack) {
  154. if (value === stack[1]) {
  155. stack.shift();
  156. stack.shift();
  157. this.next = stack[0] || "start";
  158. return "support.class";
  159. }
  160. this.next = "";
  161. return "string";
  162. },
  163. regex: ".*$",
  164. next: "start"
  165. }]
  166. }
  167. }, {
  168. regex : "$",
  169. token : "empty",
  170. next : function(currentState, stack) {
  171. if (stack[0] === "heredoc" || stack[0] === "indentedHeredoc")
  172. return stack[0];
  173. return currentState;
  174. }
  175. }, {
  176. token : "keyword.operator",
  177. regex : "!|\\$|%|&|\\*|\\-\\-|\\-|\\+\\+|\\+|~|===|==|=|!=|!==|<=|>=|<<=|>>=|>>>=|<>|<|>|!|&&|\\|\\||\\?\\:|\\*=|%=|\\+=|\\-=|&=|\\^=|\\b(?:in|instanceof|new|delete|typeof|void)"
  178. }, {
  179. token : "paren.lparen",
  180. regex : "[[({]"
  181. }, {
  182. token : "paren.rparen",
  183. regex : "[\\])}]"
  184. }, {
  185. token : "text",
  186. regex : "\\s+"
  187. }
  188. ],
  189. "comment" : [
  190. {
  191. token : "comment", // closing comment
  192. regex : "^=end(?:$|\\s.*$)",
  193. next : "start"
  194. }, {
  195. token : "comment", // comment spanning whole line
  196. regex : ".+"
  197. }
  198. ]
  199. };
  200. this.normalizeRules();
  201. };
  202. oop.inherits(RubyHighlightRules, TextHighlightRules);
  203. exports.RubyHighlightRules = RubyHighlightRules;
  204. });
  205. ace.define("ace/mode/matching_brace_outdent",["require","exports","module","ace/range"], function(require, exports, module) {
  206. "use strict";
  207. var Range = require("../range").Range;
  208. var MatchingBraceOutdent = function() {};
  209. (function() {
  210. this.checkOutdent = function(line, input) {
  211. if (! /^\s+$/.test(line))
  212. return false;
  213. return /^\s*\}/.test(input);
  214. };
  215. this.autoOutdent = function(doc, row) {
  216. var line = doc.getLine(row);
  217. var match = line.match(/^(\s*\})/);
  218. if (!match) return 0;
  219. var column = match[1].length;
  220. var openBracePos = doc.findMatchingBracket({row: row, column: column});
  221. if (!openBracePos || openBracePos.row == row) return 0;
  222. var indent = this.$getIndent(doc.getLine(openBracePos.row));
  223. doc.replace(new Range(row, 0, row, column-1), indent);
  224. };
  225. this.$getIndent = function(line) {
  226. return line.match(/^\s*/)[0];
  227. };
  228. }).call(MatchingBraceOutdent.prototype);
  229. exports.MatchingBraceOutdent = MatchingBraceOutdent;
  230. });
  231. ace.define("ace/mode/folding/coffee",["require","exports","module","ace/lib/oop","ace/mode/folding/fold_mode","ace/range"], function(require, exports, module) {
  232. "use strict";
  233. var oop = require("../../lib/oop");
  234. var BaseFoldMode = require("./fold_mode").FoldMode;
  235. var Range = require("../../range").Range;
  236. var FoldMode = exports.FoldMode = function() {};
  237. oop.inherits(FoldMode, BaseFoldMode);
  238. (function() {
  239. this.getFoldWidgetRange = function(session, foldStyle, row) {
  240. var range = this.indentationBlock(session, row);
  241. if (range)
  242. return range;
  243. var re = /\S/;
  244. var line = session.getLine(row);
  245. var startLevel = line.search(re);
  246. if (startLevel == -1 || line[startLevel] != "#")
  247. return;
  248. var startColumn = line.length;
  249. var maxRow = session.getLength();
  250. var startRow = row;
  251. var endRow = row;
  252. while (++row < maxRow) {
  253. line = session.getLine(row);
  254. var level = line.search(re);
  255. if (level == -1)
  256. continue;
  257. if (line[level] != "#")
  258. break;
  259. endRow = row;
  260. }
  261. if (endRow > startRow) {
  262. var endColumn = session.getLine(endRow).length;
  263. return new Range(startRow, startColumn, endRow, endColumn);
  264. }
  265. };
  266. this.getFoldWidget = function(session, foldStyle, row) {
  267. var line = session.getLine(row);
  268. var indent = line.search(/\S/);
  269. var next = session.getLine(row + 1);
  270. var prev = session.getLine(row - 1);
  271. var prevIndent = prev.search(/\S/);
  272. var nextIndent = next.search(/\S/);
  273. if (indent == -1) {
  274. session.foldWidgets[row - 1] = prevIndent!= -1 && prevIndent < nextIndent ? "start" : "";
  275. return "";
  276. }
  277. if (prevIndent == -1) {
  278. if (indent == nextIndent && line[indent] == "#" && next[indent] == "#") {
  279. session.foldWidgets[row - 1] = "";
  280. session.foldWidgets[row + 1] = "";
  281. return "start";
  282. }
  283. } else if (prevIndent == indent && line[indent] == "#" && prev[indent] == "#") {
  284. if (session.getLine(row - 2).search(/\S/) == -1) {
  285. session.foldWidgets[row - 1] = "start";
  286. session.foldWidgets[row + 1] = "";
  287. return "";
  288. }
  289. }
  290. if (prevIndent!= -1 && prevIndent < indent)
  291. session.foldWidgets[row - 1] = "start";
  292. else
  293. session.foldWidgets[row - 1] = "";
  294. if (indent < nextIndent)
  295. return "start";
  296. else
  297. return "";
  298. };
  299. }).call(FoldMode.prototype);
  300. });
  301. 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/folding/coffee"], function(require, exports, module) {
  302. "use strict";
  303. var oop = require("../lib/oop");
  304. var TextMode = require("./text").Mode;
  305. var RubyHighlightRules = require("./ruby_highlight_rules").RubyHighlightRules;
  306. var MatchingBraceOutdent = require("./matching_brace_outdent").MatchingBraceOutdent;
  307. var Range = require("../range").Range;
  308. var FoldMode = require("./folding/coffee").FoldMode;
  309. var Mode = function() {
  310. this.HighlightRules = RubyHighlightRules;
  311. this.$outdent = new MatchingBraceOutdent();
  312. this.foldingRules = new FoldMode();
  313. };
  314. oop.inherits(Mode, TextMode);
  315. (function() {
  316. this.lineCommentStart = "#";
  317. this.getNextLineIndent = function(state, line, tab) {
  318. var indent = this.$getIndent(line);
  319. var tokenizedLine = this.getTokenizer().getLineTokens(line, state);
  320. var tokens = tokenizedLine.tokens;
  321. if (tokens.length && tokens[tokens.length-1].type == "comment") {
  322. return indent;
  323. }
  324. if (state == "start") {
  325. var match = line.match(/^.*[\{\(\[]\s*$/);
  326. var startingClassOrMethod = line.match(/^\s*(class|def|module)\s.*$/);
  327. var startingDoBlock = line.match(/.*do(\s*|\s+\|.*\|\s*)$/);
  328. var startingConditional = line.match(/^\s*(if|else)\s*/)
  329. if (match || startingClassOrMethod || startingDoBlock || startingConditional) {
  330. indent += tab;
  331. }
  332. }
  333. return indent;
  334. };
  335. this.checkOutdent = function(state, line, input) {
  336. return /^\s+(end|else)$/.test(line + input) || this.$outdent.checkOutdent(line, input);
  337. };
  338. this.autoOutdent = function(state, session, row) {
  339. var line = session.getLine(row);
  340. if (/}/.test(line))
  341. return this.$outdent.autoOutdent(session, row);
  342. var indent = this.$getIndent(line);
  343. var prevLine = session.getLine(row - 1);
  344. var prevIndent = this.$getIndent(prevLine);
  345. var tab = session.getTabString();
  346. if (prevIndent.length <= indent.length) {
  347. if (indent.slice(-tab.length) == tab)
  348. session.remove(new Range(row, indent.length-tab.length, row, indent.length));
  349. }
  350. };
  351. this.$id = "ace/mode/ruby";
  352. }).call(Mode.prototype);
  353. exports.Mode = Mode;
  354. });