LaravelTest
190 строк · 6.6 Кб
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
14CodeMirror.registerHelper("wordChars", "r", /[\w.]/);15
16CodeMirror.defineMode("r", function(config) {17function wordObj(words) {18var res = {};19for (var i = 0; i < words.length; ++i) res[words[i]] = true;20return res;21}22var commonAtoms = ["NULL", "NA", "Inf", "NaN", "NA_integer_", "NA_real_", "NA_complex_", "NA_character_", "TRUE", "FALSE"];23var commonBuiltins = ["list", "quote", "bquote", "eval", "return", "call", "parse", "deparse"];24var commonKeywords = ["if", "else", "repeat", "while", "function", "for", "in", "next", "break"];25var commonBlockKeywords = ["if", "else", "repeat", "while", "function", "for"];26
27CodeMirror.registerHelper("hintWords", "r", commonAtoms.concat(commonBuiltins, commonKeywords));28
29var atoms = wordObj(commonAtoms);30var builtins = wordObj(commonBuiltins);31var keywords = wordObj(commonKeywords);32var blockkeywords = wordObj(commonBlockKeywords);33var opChars = /[+\-*\/^<>=!&|~$:]/;34var curPunc;35
36function tokenBase(stream, state) {37curPunc = null;38var ch = stream.next();39if (ch == "#") {40stream.skipToEnd();41return "comment";42} else if (ch == "0" && stream.eat("x")) {43stream.eatWhile(/[\da-f]/i);44return "number";45} else if (ch == "." && stream.eat(/\d/)) {46stream.match(/\d*(?:e[+\-]?\d+)?/);47return "number";48} else if (/\d/.test(ch)) {49stream.match(/\d*(?:\.\d+)?(?:e[+\-]\d+)?L?/);50return "number";51} else if (ch == "'" || ch == '"') {52state.tokenize = tokenString(ch);53return "string";54} else if (ch == "`") {55stream.match(/[^`]+`/);56return "variable-3";57} else if (ch == "." && stream.match(/.(?:[.]|\d+)/)) {58return "keyword";59} else if (/[a-zA-Z\.]/.test(ch)) {60stream.eatWhile(/[\w\.]/);61var word = stream.current();62if (atoms.propertyIsEnumerable(word)) return "atom";63if (keywords.propertyIsEnumerable(word)) {64// Block keywords start new blocks, except 'else if', which only starts65// one new block for the 'if', no block for the 'else'.66if (blockkeywords.propertyIsEnumerable(word) &&67!stream.match(/\s*if(\s+|$)/, false))68curPunc = "block";69return "keyword";70}71if (builtins.propertyIsEnumerable(word)) return "builtin";72return "variable";73} else if (ch == "%") {74if (stream.skipTo("%")) stream.next();75return "operator variable-2";76} else if (77(ch == "<" && stream.eat("-")) ||78(ch == "<" && stream.match("<-")) ||79(ch == "-" && stream.match(/>>?/))80) {81return "operator arrow";82} else if (ch == "=" && state.ctx.argList) {83return "arg-is";84} else if (opChars.test(ch)) {85if (ch == "$") return "operator dollar";86stream.eatWhile(opChars);87return "operator";88} else if (/[\(\){}\[\];]/.test(ch)) {89curPunc = ch;90if (ch == ";") return "semi";91return null;92} else {93return null;94}95}96
97function tokenString(quote) {98return function(stream, state) {99if (stream.eat("\\")) {100var ch = stream.next();101if (ch == "x") stream.match(/^[a-f0-9]{2}/i);102else if ((ch == "u" || ch == "U") && stream.eat("{") && stream.skipTo("}")) stream.next();103else if (ch == "u") stream.match(/^[a-f0-9]{4}/i);104else if (ch == "U") stream.match(/^[a-f0-9]{8}/i);105else if (/[0-7]/.test(ch)) stream.match(/^[0-7]{1,2}/);106return "string-2";107} else {108var next;109while ((next = stream.next()) != null) {110if (next == quote) { state.tokenize = tokenBase; break; }111if (next == "\\") { stream.backUp(1); break; }112}113return "string";114}115};116}117
118var ALIGN_YES = 1, ALIGN_NO = 2, BRACELESS = 4119
120function push(state, type, stream) {121state.ctx = {type: type,122indent: state.indent,123flags: 0,124column: stream.column(),125prev: state.ctx};126}127function setFlag(state, flag) {128var ctx = state.ctx129state.ctx = {type: ctx.type,130indent: ctx.indent,131flags: ctx.flags | flag,132column: ctx.column,133prev: ctx.prev}134}135function pop(state) {136state.indent = state.ctx.indent;137state.ctx = state.ctx.prev;138}139
140return {141startState: function() {142return {tokenize: tokenBase,143ctx: {type: "top",144indent: -config.indentUnit,145flags: ALIGN_NO},146indent: 0,147afterIdent: false};148},149
150token: function(stream, state) {151if (stream.sol()) {152if ((state.ctx.flags & 3) == 0) state.ctx.flags |= ALIGN_NO153if (state.ctx.flags & BRACELESS) pop(state)154state.indent = stream.indentation();155}156if (stream.eatSpace()) return null;157var style = state.tokenize(stream, state);158if (style != "comment" && (state.ctx.flags & ALIGN_NO) == 0) setFlag(state, ALIGN_YES)159
160if ((curPunc == ";" || curPunc == "{" || curPunc == "}") && state.ctx.type == "block") pop(state);161if (curPunc == "{") push(state, "}", stream);162else if (curPunc == "(") {163push(state, ")", stream);164if (state.afterIdent) state.ctx.argList = true;165}166else if (curPunc == "[") push(state, "]", stream);167else if (curPunc == "block") push(state, "block", stream);168else if (curPunc == state.ctx.type) pop(state);169else if (state.ctx.type == "block" && style != "comment") setFlag(state, BRACELESS)170state.afterIdent = style == "variable" || style == "keyword";171return style;172},173
174indent: function(state, textAfter) {175if (state.tokenize != tokenBase) return 0;176var firstChar = textAfter && textAfter.charAt(0), ctx = state.ctx,177closing = firstChar == ctx.type;178if (ctx.flags & BRACELESS) ctx = ctx.prev179if (ctx.type == "block") return ctx.indent + (firstChar == "{" ? 0 : config.indentUnit);180else if (ctx.flags & ALIGN_YES) return ctx.column + (closing ? 0 : 1);181else return ctx.indent + (closing ? 0 : config.indentUnit);182},183
184lineComment: "#"185};186});187
188CodeMirror.defineMIME("text/x-rsrc", "r");189
190});191