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