LaravelTest
151 строка · 6.9 Кб
1// CodeMirror, copyright (c) by Marijn Haverbeke and others
2// Distributed under an MIT license: https://codemirror.net/LICENSE
3
4// By the Neo4j Team and contributors.
5// https://github.com/neo4j-contrib/CodeMirror
6
7(function(mod) {8if (typeof exports == "object" && typeof module == "object") // CommonJS9mod(require("../../lib/codemirror"));10else if (typeof define == "function" && define.amd) // AMD11define(["../../lib/codemirror"], mod);12else // Plain browser env13mod(CodeMirror);14})(function(CodeMirror) {15"use strict";16var wordRegexp = function(words) {17return new RegExp("^(?:" + words.join("|") + ")$", "i");18};19
20CodeMirror.defineMode("cypher", function(config) {21var tokenBase = function(stream/*, state*/) {22var ch = stream.next();23if (ch ==='"') {24stream.match(/^[^"]*"/);25return "string";26}27if (ch === "'") {28stream.match(/^[^']*'/);29return "string";30}31if (/[{}\(\),\.;\[\]]/.test(ch)) {32curPunc = ch;33return "node";34} else if (ch === "/" && stream.eat("/")) {35stream.skipToEnd();36return "comment";37} else if (operatorChars.test(ch)) {38stream.eatWhile(operatorChars);39return null;40} else {41stream.eatWhile(/[_\w\d]/);42if (stream.eat(":")) {43stream.eatWhile(/[\w\d_\-]/);44return "atom";45}46var word = stream.current();47if (funcs.test(word)) return "builtin";48if (preds.test(word)) return "def";49if (keywords.test(word) || systemKeywords.test(word)) return "keyword";50return "variable";51}52};53var pushContext = function(state, type, col) {54return state.context = {55prev: state.context,56indent: state.indent,57col: col,58type: type59};60};61var popContext = function(state) {62state.indent = state.context.indent;63return state.context = state.context.prev;64};65var indentUnit = config.indentUnit;66var curPunc;67var funcs = wordRegexp(["abs", "acos", "allShortestPaths", "asin", "atan", "atan2", "avg", "ceil", "coalesce", "collect", "cos", "cot", "count", "degrees", "e", "endnode", "exp", "extract", "filter", "floor", "haversin", "head", "id", "keys", "labels", "last", "left", "length", "log", "log10", "lower", "ltrim", "max", "min", "node", "nodes", "percentileCont", "percentileDisc", "pi", "radians", "rand", "range", "reduce", "rel", "relationship", "relationships", "replace", "reverse", "right", "round", "rtrim", "shortestPath", "sign", "sin", "size", "split", "sqrt", "startnode", "stdev", "stdevp", "str", "substring", "sum", "tail", "tan", "timestamp", "toFloat", "toInt", "toString", "trim", "type", "upper"]);68var preds = wordRegexp(["all", "and", "any", "contains", "exists", "has", "in", "none", "not", "or", "single", "xor"]);69var keywords = wordRegexp(["as", "asc", "ascending", "assert", "by", "case", "commit", "constraint", "create", "csv", "cypher", "delete", "desc", "descending", "detach", "distinct", "drop", "else", "end", "ends", "explain", "false", "fieldterminator", "foreach", "from", "headers", "in", "index", "is", "join", "limit", "load", "match", "merge", "null", "on", "optional", "order", "periodic", "profile", "remove", "return", "scan", "set", "skip", "start", "starts", "then", "true", "union", "unique", "unwind", "using", "when", "where", "with", "call", "yield"]);70var systemKeywords = wordRegexp(["access", "active", "assign", "all", "alter", "as", "catalog", "change", "copy", "create", "constraint", "constraints", "current", "database", "databases", "dbms", "default", "deny", "drop", "element", "elements", "exists", "from", "grant", "graph", "graphs", "if", "index", "indexes", "label", "labels", "management", "match", "name", "names", "new", "node", "nodes", "not", "of", "on", "or", "password", "populated", "privileges", "property", "read", "relationship", "relationships", "remove", "replace", "required", "revoke", "role", "roles", "set", "show", "start", "status", "stop", "suspended", "to", "traverse", "type", "types", "user", "users", "with", "write"]);71var operatorChars = /[*+\-<>=&|~%^]/;72
73return {74startState: function(/*base*/) {75return {76tokenize: tokenBase,77context: null,78indent: 0,79col: 080};81},82token: function(stream, state) {83if (stream.sol()) {84if (state.context && (state.context.align == null)) {85state.context.align = false;86}87state.indent = stream.indentation();88}89if (stream.eatSpace()) {90return null;91}92var style = state.tokenize(stream, state);93if (style !== "comment" && state.context && (state.context.align == null) && state.context.type !== "pattern") {94state.context.align = true;95}96if (curPunc === "(") {97pushContext(state, ")", stream.column());98} else if (curPunc === "[") {99pushContext(state, "]", stream.column());100} else if (curPunc === "{") {101pushContext(state, "}", stream.column());102} else if (/[\]\}\)]/.test(curPunc)) {103while (state.context && state.context.type === "pattern") {104popContext(state);105}106if (state.context && curPunc === state.context.type) {107popContext(state);108}109} else if (curPunc === "." && state.context && state.context.type === "pattern") {110popContext(state);111} else if (/atom|string|variable/.test(style) && state.context) {112if (/[\}\]]/.test(state.context.type)) {113pushContext(state, "pattern", stream.column());114} else if (state.context.type === "pattern" && !state.context.align) {115state.context.align = true;116state.context.col = stream.column();117}118}119return style;120},121indent: function(state, textAfter) {122var firstChar = textAfter && textAfter.charAt(0);123var context = state.context;124if (/[\]\}]/.test(firstChar)) {125while (context && context.type === "pattern") {126context = context.prev;127}128}129var closing = context && firstChar === context.type;130if (!context) return 0;131if (context.type === "keywords") return CodeMirror.commands.newlineAndIndent;132if (context.align) return context.col + (closing ? 0 : 1);133return context.indent + (closing ? 0 : indentUnit);134}135};136});137
138CodeMirror.modeExtensions["cypher"] = {139autoFormatLineBreaks: function(text) {140var i, lines, reProcessedPortion;141var lines = text.split("\n");142var reProcessedPortion = /\s+\b(return|where|order by|match|with|skip|limit|create|delete|set)\b\s/g;143for (var i = 0; i < lines.length; i++)144lines[i] = lines[i].replace(reProcessedPortion, " \n$1 ").trim();145return lines.join("\n");146}147};148
149CodeMirror.defineMIME("application/x-cypher-query", "cypher");150
151});152