LaravelTest
233 строки · 7.8 Кб
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.defineMode("groovy", function(config) {15function words(str) {16var obj = {}, words = str.split(" ");17for (var i = 0; i < words.length; ++i) obj[words[i]] = true;18return obj;19}20var keywords = words(21"abstract as assert boolean break byte case catch char class const continue def default " +22"do double else enum extends final finally float for goto if implements import in " +23"instanceof int interface long native new package private protected public return " +24"short static strictfp super switch synchronized threadsafe throw throws trait transient " +25"try void volatile while");26var blockKeywords = words("catch class def do else enum finally for if interface switch trait try while");27var standaloneKeywords = words("return break continue");28var atoms = words("null true false this");29
30var curPunc;31function tokenBase(stream, state) {32var ch = stream.next();33if (ch == '"' || ch == "'") {34return startString(ch, stream, state);35}36if (/[\[\]{}\(\),;\:\.]/.test(ch)) {37curPunc = ch;38return null;39}40if (/\d/.test(ch)) {41stream.eatWhile(/[\w\.]/);42if (stream.eat(/eE/)) { stream.eat(/\+\-/); stream.eatWhile(/\d/); }43return "number";44}45if (ch == "/") {46if (stream.eat("*")) {47state.tokenize.push(tokenComment);48return tokenComment(stream, state);49}50if (stream.eat("/")) {51stream.skipToEnd();52return "comment";53}54if (expectExpression(state.lastToken, false)) {55return startString(ch, stream, state);56}57}58if (ch == "-" && stream.eat(">")) {59curPunc = "->";60return null;61}62if (/[+\-*&%=<>!?|\/~]/.test(ch)) {63stream.eatWhile(/[+\-*&%=<>|~]/);64return "operator";65}66stream.eatWhile(/[\w\$_]/);67if (ch == "@") { stream.eatWhile(/[\w\$_\.]/); return "meta"; }68if (state.lastToken == ".") return "property";69if (stream.eat(":")) { curPunc = "proplabel"; return "property"; }70var cur = stream.current();71if (atoms.propertyIsEnumerable(cur)) { return "atom"; }72if (keywords.propertyIsEnumerable(cur)) {73if (blockKeywords.propertyIsEnumerable(cur)) curPunc = "newstatement";74else if (standaloneKeywords.propertyIsEnumerable(cur)) curPunc = "standalone";75return "keyword";76}77return "variable";78}79tokenBase.isBase = true;80
81function startString(quote, stream, state) {82var tripleQuoted = false;83if (quote != "/" && stream.eat(quote)) {84if (stream.eat(quote)) tripleQuoted = true;85else return "string";86}87function t(stream, state) {88var escaped = false, next, end = !tripleQuoted;89while ((next = stream.next()) != null) {90if (next == quote && !escaped) {91if (!tripleQuoted) { break; }92if (stream.match(quote + quote)) { end = true; break; }93}94if (quote == '"' && next == "$" && !escaped && stream.eat("{")) {95state.tokenize.push(tokenBaseUntilBrace());96return "string";97}98escaped = !escaped && next == "\\";99}100if (end) state.tokenize.pop();101return "string";102}103state.tokenize.push(t);104return t(stream, state);105}106
107function tokenBaseUntilBrace() {108var depth = 1;109function t(stream, state) {110if (stream.peek() == "}") {111depth--;112if (depth == 0) {113state.tokenize.pop();114return state.tokenize[state.tokenize.length-1](stream, state);115}116} else if (stream.peek() == "{") {117depth++;118}119return tokenBase(stream, state);120}121t.isBase = true;122return t;123}124
125function tokenComment(stream, state) {126var maybeEnd = false, ch;127while (ch = stream.next()) {128if (ch == "/" && maybeEnd) {129state.tokenize.pop();130break;131}132maybeEnd = (ch == "*");133}134return "comment";135}136
137function expectExpression(last, newline) {138return !last || last == "operator" || last == "->" || /[\.\[\{\(,;:]/.test(last) ||139last == "newstatement" || last == "keyword" || last == "proplabel" ||140(last == "standalone" && !newline);141}142
143function Context(indented, column, type, align, prev) {144this.indented = indented;145this.column = column;146this.type = type;147this.align = align;148this.prev = prev;149}150function pushContext(state, col, type) {151return state.context = new Context(state.indented, col, type, null, state.context);152}153function popContext(state) {154var t = state.context.type;155if (t == ")" || t == "]" || t == "}")156state.indented = state.context.indented;157return state.context = state.context.prev;158}159
160// Interface161
162return {163startState: function(basecolumn) {164return {165tokenize: [tokenBase],166context: new Context((basecolumn || 0) - config.indentUnit, 0, "top", false),167indented: 0,168startOfLine: true,169lastToken: null170};171},172
173token: function(stream, state) {174var ctx = state.context;175if (stream.sol()) {176if (ctx.align == null) ctx.align = false;177state.indented = stream.indentation();178state.startOfLine = true;179// Automatic semicolon insertion180if (ctx.type == "statement" && !expectExpression(state.lastToken, true)) {181popContext(state); ctx = state.context;182}183}184if (stream.eatSpace()) return null;185curPunc = null;186var style = state.tokenize[state.tokenize.length-1](stream, state);187if (style == "comment") return style;188if (ctx.align == null) ctx.align = true;189
190if ((curPunc == ";" || curPunc == ":") && ctx.type == "statement") popContext(state);191// Handle indentation for {x -> \n ... }192else if (curPunc == "->" && ctx.type == "statement" && ctx.prev.type == "}") {193popContext(state);194state.context.align = false;195}196else if (curPunc == "{") pushContext(state, stream.column(), "}");197else if (curPunc == "[") pushContext(state, stream.column(), "]");198else if (curPunc == "(") pushContext(state, stream.column(), ")");199else if (curPunc == "}") {200while (ctx.type == "statement") ctx = popContext(state);201if (ctx.type == "}") ctx = popContext(state);202while (ctx.type == "statement") ctx = popContext(state);203}204else if (curPunc == ctx.type) popContext(state);205else if (ctx.type == "}" || ctx.type == "top" || (ctx.type == "statement" && curPunc == "newstatement"))206pushContext(state, stream.column(), "statement");207state.startOfLine = false;208state.lastToken = curPunc || style;209return style;210},211
212indent: function(state, textAfter) {213if (!state.tokenize[state.tokenize.length-1].isBase) return CodeMirror.Pass;214var firstChar = textAfter && textAfter.charAt(0), ctx = state.context;215if (ctx.type == "statement" && !expectExpression(state.lastToken, true)) ctx = ctx.prev;216var closing = firstChar == ctx.type;217if (ctx.type == "statement") return ctx.indented + (firstChar == "{" ? 0 : config.indentUnit);218else if (ctx.align) return ctx.column + (closing ? 0 : 1);219else return ctx.indented + (closing ? 0 : config.indentUnit);220},221
222electricChars: "{}",223closeBrackets: {triples: "'\""},224fold: "brace",225blockCommentStart: "/*",226blockCommentEnd: "*/",227lineComment: "//"228};229});230
231CodeMirror.defineMIME("text/x-groovy", "groovy");232
233});234