1 |
|
2 |
|
3 |
|
4 | (function(mod) {
|
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);
|
9 | else
|
10 | mod(CodeMirror);
|
11 | })(function(CodeMirror) {
|
12 | "use strict";
|
13 |
|
14 | CodeMirror.defineMode("ruby", function(config) {
|
15 | function wordObj(words) {
|
16 | var o = {};
|
17 | for (var i = 0, e = words.length; i < e; ++i) o[words[i]] = true;
|
18 | return o;
|
19 | }
|
20 | var keywords = wordObj([
|
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 | ]);
|
28 | var indentWords = wordObj(["def", "class", "case", "for", "while", "until", "module", "then",
|
29 | "catch", "loop", "proc", "begin"]);
|
30 | var dedentWords = wordObj(["end", "until"]);
|
31 | var opening = {"[": "]", "{": "}", "(": ")"};
|
32 | var closing = {"]": "[", "}": "{", ")": "("};
|
33 | var curPunc;
|
34 |
|
35 | function chain(newtok, stream, state) {
|
36 | state.tokenize.push(newtok);
|
37 | return newtok(stream, state);
|
38 | }
|
39 |
|
40 | function tokenBase(stream, state) {
|
41 | if (stream.sol() && stream.match("=begin") && stream.eol()) {
|
42 | state.tokenize.push(readBlockComment);
|
43 | return "comment";
|
44 | }
|
45 | if (stream.eatSpace()) return null;
|
46 | var ch = stream.next(), m;
|
47 | if (ch == "`" || ch == "'" || ch == '"') {
|
48 | return chain(readQuoted(ch, "string", ch == '"' || ch == "`"), stream, state);
|
49 | } else if (ch == "/") {
|
50 | if (regexpAhead(stream))
|
51 | return chain(readQuoted(ch, "string-2", true), stream, state);
|
52 | else
|
53 | return "operator";
|
54 | } else if (ch == "%") {
|
55 | var style = "string", embed = true;
|
56 | if (stream.eat("s")) style = "atom";
|
57 | else if (stream.eat(/[WQ]/)) style = "string";
|
58 | else if (stream.eat(/[r]/)) style = "string-2";
|
59 | else if (stream.eat(/[wxq]/)) { style = "string"; embed = false; }
|
60 | var delim = stream.eat(/[^\w\s=]/);
|
61 | if (!delim) return "operator";
|
62 | if (opening.propertyIsEnumerable(delim)) delim = opening[delim];
|
63 | return chain(readQuoted(delim, style, embed, true), stream, state);
|
64 | } else if (ch == "#") {
|
65 | stream.skipToEnd();
|
66 | return "comment";
|
67 | } else if (ch == "<" && (m = stream.match(/^<([-~])[\`\"\']?([a-zA-Z_?]\w*)[\`\"\']?(?:;|$)/))) {
|
68 | return chain(readHereDoc(m[2], m[1]), stream, state);
|
69 | } else if (ch == "0") {
|
70 | if (stream.eat("x")) stream.eatWhile(/[\da-fA-F]/);
|
71 | else if (stream.eat("b")) stream.eatWhile(/[01]/);
|
72 | else stream.eatWhile(/[0-7]/);
|
73 | return "number";
|
74 | } else if (/\d/.test(ch)) {
|
75 | stream.match(/^[\d_]*(?:\.[\d_]+)?(?:[eE][+\-]?[\d_]+)?/);
|
76 | return "number";
|
77 | } else if (ch == "?") {
|
78 | while (stream.match(/^\\[CM]-/)) {}
|
79 | if (stream.eat("\\")) stream.eatWhile(/\w/);
|
80 | else stream.next();
|
81 | return "string";
|
82 | } else if (ch == ":") {
|
83 | if (stream.eat("'")) return chain(readQuoted("'", "atom", false), stream, state);
|
84 | if (stream.eat('"')) return chain(readQuoted('"', "atom", true), stream, state);
|
85 |
|
86 |
|
87 | if (stream.eat(/[\<\>]/)) {
|
88 | stream.eat(/[\<\>]/);
|
89 | return "atom";
|
90 | }
|
91 |
|
92 |
|
93 | if (stream.eat(/[\+\-\*\/\&\|\:\!]/)) {
|
94 | return "atom";
|
95 | }
|
96 |
|
97 |
|
98 | if (stream.eat(/[a-zA-Z$@_\xa1-\uffff]/)) {
|
99 | stream.eatWhile(/[\w$\xa1-\uffff]/);
|
100 |
|
101 | stream.eat(/[\?\!\=]/);
|
102 | return "atom";
|
103 | }
|
104 | return "operator";
|
105 | } else if (ch == "@" && stream.match(/^@?[a-zA-Z_\xa1-\uffff]/)) {
|
106 | stream.eat("@");
|
107 | stream.eatWhile(/[\w\xa1-\uffff]/);
|
108 | return "variable-2";
|
109 | } else if (ch == "$") {
|
110 | if (stream.eat(/[a-zA-Z_]/)) {
|
111 | stream.eatWhile(/[\w]/);
|
112 | } else if (stream.eat(/\d/)) {
|
113 | stream.eat(/\d/);
|
114 | } else {
|
115 | stream.next();
|
116 | }
|
117 | return "variable-3";
|
118 | } else if (/[a-zA-Z_\xa1-\uffff]/.test(ch)) {
|
119 | stream.eatWhile(/[\w\xa1-\uffff]/);
|
120 | stream.eat(/[\?\!]/);
|
121 | if (stream.eat(":")) return "atom";
|
122 | return "ident";
|
123 | } else if (ch == "|" && (state.varList || state.lastTok == "{" || state.lastTok == "do")) {
|
124 | curPunc = "|";
|
125 | return null;
|
126 | } else if (/[\(\)\[\]{}\\;]/.test(ch)) {
|
127 | curPunc = ch;
|
128 | return null;
|
129 | } else if (ch == "-" && stream.eat(">")) {
|
130 | return "arrow";
|
131 | } else if (/[=+\-\/*:\.^%<>~|]/.test(ch)) {
|
132 | var more = stream.eatWhile(/[=+\-\/*:\.^%<>~|]/);
|
133 | if (ch == "." && !more) curPunc = ".";
|
134 | return "operator";
|
135 | } else {
|
136 | return null;
|
137 | }
|
138 | }
|
139 |
|
140 | function regexpAhead(stream) {
|
141 | var start = stream.pos, depth = 0, next, found = false, escaped = false
|
142 | while ((next = stream.next()) != null) {
|
143 | if (!escaped) {
|
144 | if ("[{(".indexOf(next) > -1) {
|
145 | depth++
|
146 | } else if ("]})".indexOf(next) > -1) {
|
147 | depth--
|
148 | if (depth < 0) break
|
149 | } else if (next == "/" && depth == 0) {
|
150 | found = true
|
151 | break
|
152 | }
|
153 | escaped = next == "\\"
|
154 | } else {
|
155 | escaped = false
|
156 | }
|
157 | }
|
158 | stream.backUp(stream.pos - start)
|
159 | return found
|
160 | }
|
161 |
|
162 | function tokenBaseUntilBrace(depth) {
|
163 | if (!depth) depth = 1;
|
164 | return function(stream, state) {
|
165 | if (stream.peek() == "}") {
|
166 | if (depth == 1) {
|
167 | state.tokenize.pop();
|
168 | return state.tokenize[state.tokenize.length-1](stream, state);
|
169 | } else {
|
170 | state.tokenize[state.tokenize.length - 1] = tokenBaseUntilBrace(depth - 1);
|
171 | }
|
172 | } else if (stream.peek() == "{") {
|
173 | state.tokenize[state.tokenize.length - 1] = tokenBaseUntilBrace(depth + 1);
|
174 | }
|
175 | return tokenBase(stream, state);
|
176 | };
|
177 | }
|
178 | function tokenBaseOnce() {
|
179 | var alreadyCalled = false;
|
180 | return function(stream, state) {
|
181 | if (alreadyCalled) {
|
182 | state.tokenize.pop();
|
183 | return state.tokenize[state.tokenize.length-1](stream, state);
|
184 | }
|
185 | alreadyCalled = true;
|
186 | return tokenBase(stream, state);
|
187 | };
|
188 | }
|
189 | function readQuoted(quote, style, embed, unescaped) {
|
190 | return function(stream, state) {
|
191 | var escaped = false, ch;
|
192 |
|
193 | if (state.context.type === 'read-quoted-paused') {
|
194 | state.context = state.context.prev;
|
195 | stream.eat("}");
|
196 | }
|
197 |
|
198 | while ((ch = stream.next()) != null) {
|
199 | if (ch == quote && (unescaped || !escaped)) {
|
200 | state.tokenize.pop();
|
201 | break;
|
202 | }
|
203 | if (embed && ch == "#" && !escaped) {
|
204 | if (stream.eat("{")) {
|
205 | if (quote == "}") {
|
206 | state.context = {prev: state.context, type: 'read-quoted-paused'};
|
207 | }
|
208 | state.tokenize.push(tokenBaseUntilBrace());
|
209 | break;
|
210 | } else if (/[@\$]/.test(stream.peek())) {
|
211 | state.tokenize.push(tokenBaseOnce());
|
212 | break;
|
213 | }
|
214 | }
|
215 | escaped = !escaped && ch == "\\";
|
216 | }
|
217 | return style;
|
218 | };
|
219 | }
|
220 | function readHereDoc(phrase, mayIndent) {
|
221 | return function(stream, state) {
|
222 | if (mayIndent) stream.eatSpace()
|
223 | if (stream.match(phrase)) state.tokenize.pop();
|
224 | else stream.skipToEnd();
|
225 | return "string";
|
226 | };
|
227 | }
|
228 | function readBlockComment(stream, state) {
|
229 | if (stream.sol() && stream.match("=end") && stream.eol())
|
230 | state.tokenize.pop();
|
231 | stream.skipToEnd();
|
232 | return "comment";
|
233 | }
|
234 |
|
235 | return {
|
236 | startState: function() {
|
237 | return {tokenize: [tokenBase],
|
238 | indented: 0,
|
239 | context: {type: "top", indented: -config.indentUnit},
|
240 | continuedLine: false,
|
241 | lastTok: null,
|
242 | varList: false};
|
243 | },
|
244 |
|
245 | token: function(stream, state) {
|
246 | curPunc = null;
|
247 | if (stream.sol()) state.indented = stream.indentation();
|
248 | var style = state.tokenize[state.tokenize.length-1](stream, state), kwtype;
|
249 | var thisTok = curPunc;
|
250 | if (style == "ident") {
|
251 | var word = stream.current();
|
252 | style = state.lastTok == "." ? "property"
|
253 | : keywords.propertyIsEnumerable(stream.current()) ? "keyword"
|
254 | : /^[A-Z]/.test(word) ? "tag"
|
255 | : (state.lastTok == "def" || state.lastTok == "class" || state.varList) ? "def"
|
256 | : "variable";
|
257 | if (style == "keyword") {
|
258 | thisTok = word;
|
259 | if (indentWords.propertyIsEnumerable(word)) kwtype = "indent";
|
260 | else if (dedentWords.propertyIsEnumerable(word)) kwtype = "dedent";
|
261 | else if ((word == "if" || word == "unless") && stream.column() == stream.indentation())
|
262 | kwtype = "indent";
|
263 | else if (word == "do" && state.context.indented < state.indented)
|
264 | kwtype = "indent";
|
265 | }
|
266 | }
|
267 | if (curPunc || (style && style != "comment")) state.lastTok = thisTok;
|
268 | if (curPunc == "|") state.varList = !state.varList;
|
269 |
|
270 | if (kwtype == "indent" || /[\(\[\{]/.test(curPunc))
|
271 | state.context = {prev: state.context, type: curPunc || style, indented: state.indented};
|
272 | else if ((kwtype == "dedent" || /[\)\]\}]/.test(curPunc)) && state.context.prev)
|
273 | state.context = state.context.prev;
|
274 |
|
275 | if (stream.eol())
|
276 | state.continuedLine = (curPunc == "\\" || style == "operator");
|
277 | return style;
|
278 | },
|
279 |
|
280 | indent: function(state, textAfter) {
|
281 | if (state.tokenize[state.tokenize.length-1] != tokenBase) return CodeMirror.Pass;
|
282 | var firstChar = textAfter && textAfter.charAt(0);
|
283 | var ct = state.context;
|
284 | var closed = ct.type == closing[firstChar] ||
|
285 | ct.type == "keyword" && /^(?:end|until|else|elsif|when|rescue)\b/.test(textAfter);
|
286 | return ct.indented + (closed ? 0 : config.indentUnit) +
|
287 | (state.continuedLine ? config.indentUnit : 0);
|
288 | },
|
289 |
|
290 | electricInput: /^\s*(?:end|rescue|elsif|else|\})$/,
|
291 | lineComment: "#",
|
292 | fold: "indent"
|
293 | };
|
294 | });
|
295 |
|
296 | CodeMirror.defineMIME("text/x-ruby", "ruby");
|
297 |
|
298 | });
|