5
if (typeof exports == "object" && typeof module == "object")
6
mod(require("../../lib/codemirror"));
7
else if (typeof define == "function" && define.amd)
8
define(["../../lib/codemirror"], mod);
11
})(function(CodeMirror) {
14
function wordObj(words) {
16
for (var i = 0, e = words.length; i < e; ++i) o[words[i]] = true;
21
"alias", "and", "BEGIN", "begin", "break", "case", "class", "def", "defined?", "do", "else",
22
"elsif", "END", "end", "ensure", "false", "for", "if", "in", "module", "next", "not", "or",
23
"redo", "rescue", "retry", "return", "self", "super", "then", "true", "undef", "unless",
24
"until", "when", "while", "yield", "nil", "raise", "throw", "catch", "fail", "loop", "callcc",
25
"caller", "lambda", "proc", "public", "protected", "private", "require", "load",
26
"require_relative", "extend", "autoload", "__END__", "__FILE__", "__LINE__", "__dir__"
27
], keywords = wordObj(keywordList);
29
var indentWords = wordObj(["def", "class", "case", "for", "while", "until", "module", "then",
30
"catch", "loop", "proc", "begin"]);
31
var dedentWords = wordObj(["end", "until"]);
32
var opening = {"[": "]", "{": "}", "(": ")"};
33
var closing = {"]": "[", "}": "{", ")": "("};
35
CodeMirror.defineMode("ruby", function(config) {
38
function chain(newtok, stream, state) {
39
state.tokenize.push(newtok);
40
return newtok(stream, state);
43
function tokenBase(stream, state) {
44
if (stream.sol() && stream.match("=begin") && stream.eol()) {
45
state.tokenize.push(readBlockComment);
48
if (stream.eatSpace()) return null;
49
var ch = stream.next(), m;
50
if (ch == "`" || ch == "'" || ch == '"') {
51
return chain(readQuoted(ch, "string", ch == '"' || ch == "`"), stream, state);
52
} else if (ch == "/") {
53
if (regexpAhead(stream))
54
return chain(readQuoted(ch, "string-2", true), stream, state);
57
} else if (ch == "%") {
58
var style = "string", embed = true;
59
if (stream.eat("s")) style = "atom";
60
else if (stream.eat(/[WQ]/)) style = "string";
61
else if (stream.eat(/[r]/)) style = "string-2";
62
else if (stream.eat(/[wxq]/)) { style = "string"; embed = false; }
63
var delim = stream.eat(/[^\w\s=]/);
64
if (!delim) return "operator";
65
if (opening.propertyIsEnumerable(delim)) delim = opening[delim];
66
return chain(readQuoted(delim, style, embed, true), stream, state);
67
} else if (ch == "#") {
70
} else if (ch == "<" && (m = stream.match(/^<([-~])[\`\"\']?([a-zA-Z_?]\w*)[\`\"\']?(?:;|$)/))) {
71
return chain(readHereDoc(m[2], m[1]), stream, state);
72
} else if (ch == "0") {
73
if (stream.eat("x")) stream.eatWhile(/[\da-fA-F]/);
74
else if (stream.eat("b")) stream.eatWhile(/[01]/);
75
else stream.eatWhile(/[0-7]/);
77
} else if (/\d/.test(ch)) {
78
stream.match(/^[\d_]*(?:\.[\d_]+)?(?:[eE][+\-]?[\d_]+)?/);
80
} else if (ch == "?") {
81
while (stream.match(/^\\[CM]-/)) {}
82
if (stream.eat("\\")) stream.eatWhile(/\w/);
85
} else if (ch == ":") {
86
if (stream.eat("'")) return chain(readQuoted("'", "atom", false), stream, state);
87
if (stream.eat('"')) return chain(readQuoted('"', "atom", true), stream, state);
90
if (stream.eat(/[\<\>]/)) {
96
if (stream.eat(/[\+\-\*\/\&\|\:\!]/)) {
101
if (stream.eat(/[a-zA-Z$@_\xa1-\uffff]/)) {
102
stream.eatWhile(/[\w$\xa1-\uffff]/);
104
stream.eat(/[\?\!\=]/);
108
} else if (ch == "@" && stream.match(/^@?[a-zA-Z_\xa1-\uffff]/)) {
110
stream.eatWhile(/[\w\xa1-\uffff]/);
112
} else if (ch == "$") {
113
if (stream.eat(/[a-zA-Z_]/)) {
114
stream.eatWhile(/[\w]/);
115
} else if (stream.eat(/\d/)) {
121
} else if (/[a-zA-Z_\xa1-\uffff]/.test(ch)) {
122
stream.eatWhile(/[\w\xa1-\uffff]/);
123
stream.eat(/[\?\!]/);
124
if (stream.eat(":")) return "atom";
126
} else if (ch == "|" && (state.varList || state.lastTok == "{" || state.lastTok == "do")) {
129
} else if (/[\(\)\[\]{}\\;]/.test(ch)) {
132
} else if (ch == "-" && stream.eat(">")) {
134
} else if (/[=+\-\/*:\.^%<>~|]/.test(ch)) {
135
var more = stream.eatWhile(/[=+\-\/*:\.^%<>~|]/);
136
if (ch == "." && !more) curPunc = ".";
143
function regexpAhead(stream) {
144
var start = stream.pos, depth = 0, next, found = false, escaped = false
145
while ((next = stream.next()) != null) {
147
if ("[{(".indexOf(next) > -1) {
149
} else if ("]})".indexOf(next) > -1) {
152
} else if (next == "/" && depth == 0) {
156
escaped = next == "\\"
161
stream.backUp(stream.pos - start)
165
function tokenBaseUntilBrace(depth) {
166
if (!depth) depth = 1;
167
return function(stream, state) {
168
if (stream.peek() == "}") {
170
state.tokenize.pop();
171
return state.tokenize[state.tokenize.length-1](stream, state);
173
state.tokenize[state.tokenize.length - 1] = tokenBaseUntilBrace(depth - 1);
175
} else if (stream.peek() == "{") {
176
state.tokenize[state.tokenize.length - 1] = tokenBaseUntilBrace(depth + 1);
178
return tokenBase(stream, state);
181
function tokenBaseOnce() {
182
var alreadyCalled = false;
183
return function(stream, state) {
185
state.tokenize.pop();
186
return state.tokenize[state.tokenize.length-1](stream, state);
188
alreadyCalled = true;
189
return tokenBase(stream, state);
192
function readQuoted(quote, style, embed, unescaped) {
193
return function(stream, state) {
194
var escaped = false, ch;
196
if (state.context.type === 'read-quoted-paused') {
197
state.context = state.context.prev;
201
while ((ch = stream.next()) != null) {
202
if (ch == quote && (unescaped || !escaped)) {
203
state.tokenize.pop();
206
if (embed && ch == "#" && !escaped) {
207
if (stream.eat("{")) {
209
state.context = {prev: state.context, type: 'read-quoted-paused'};
211
state.tokenize.push(tokenBaseUntilBrace());
213
} else if (/[@\$]/.test(stream.peek())) {
214
state.tokenize.push(tokenBaseOnce());
218
escaped = !escaped && ch == "\\";
223
function readHereDoc(phrase, mayIndent) {
224
return function(stream, state) {
225
if (mayIndent) stream.eatSpace()
226
if (stream.match(phrase)) state.tokenize.pop();
227
else stream.skipToEnd();
231
function readBlockComment(stream, state) {
232
if (stream.sol() && stream.match("=end") && stream.eol())
233
state.tokenize.pop();
239
startState: function() {
240
return {tokenize: [tokenBase],
242
context: {type: "top", indented: -config.indentUnit},
243
continuedLine: false,
248
token: function(stream, state) {
250
if (stream.sol()) state.indented = stream.indentation();
251
var style = state.tokenize[state.tokenize.length-1](stream, state), kwtype;
252
var thisTok = curPunc;
253
if (style == "ident") {
254
var word = stream.current();
255
style = state.lastTok == "." ? "property"
256
: keywords.propertyIsEnumerable(stream.current()) ? "keyword"
257
: /^[A-Z]/.test(word) ? "tag"
258
: (state.lastTok == "def" || state.lastTok == "class" || state.varList) ? "def"
260
if (style == "keyword") {
262
if (indentWords.propertyIsEnumerable(word)) kwtype = "indent";
263
else if (dedentWords.propertyIsEnumerable(word)) kwtype = "dedent";
264
else if ((word == "if" || word == "unless") && stream.column() == stream.indentation())
266
else if (word == "do" && state.context.indented < state.indented)
270
if (curPunc || (style && style != "comment")) state.lastTok = thisTok;
271
if (curPunc == "|") state.varList = !state.varList;
273
if (kwtype == "indent" || /[\(\[\{]/.test(curPunc))
274
state.context = {prev: state.context, type: curPunc || style, indented: state.indented};
275
else if ((kwtype == "dedent" || /[\)\]\}]/.test(curPunc)) && state.context.prev)
276
state.context = state.context.prev;
279
state.continuedLine = (curPunc == "\\" || style == "operator");
283
indent: function(state, textAfter) {
284
if (state.tokenize[state.tokenize.length-1] != tokenBase) return CodeMirror.Pass;
285
var firstChar = textAfter && textAfter.charAt(0);
286
var ct = state.context;
287
var closed = ct.type == closing[firstChar] ||
288
ct.type == "keyword" && /^(?:end|until|else|elsif|when|rescue)\b/.test(textAfter);
289
return ct.indented + (closed ? 0 : config.indentUnit) +
290
(state.continuedLine ? config.indentUnit : 0);
293
electricInput: /^\s*(?:end|rescue|elsif|else|\})$/,
299
CodeMirror.defineMIME("text/x-ruby", "ruby");
301
CodeMirror.registerHelper("hintWords", "ruby", keywordList);