8
if (typeof exports == "object" && typeof module == "object")
9
mod(require("../../lib/codemirror"));
10
else if (typeof define == "function" && define.amd)
11
define(["../../lib/codemirror"], mod);
14
})(function(CodeMirror) {
16
var wordRegexp = function(words) {
17
return new RegExp("^(?:" + words.join("|") + ")$", "i");
20
CodeMirror.defineMode("cypher", function(config) {
21
var tokenBase = function(stream) {
22
var ch = stream.next();
24
stream.match(/^[^"]*"/);
28
stream.match(/^[^']*'/);
31
if (/[{}\(\),\.;\[\]]/.test(ch)) {
34
} else if (ch === "/" && stream.eat("/")) {
37
} else if (operatorChars.test(ch)) {
38
stream.eatWhile(operatorChars);
41
stream.eatWhile(/[_\w\d]/);
42
if (stream.eat(":")) {
43
stream.eatWhile(/[\w\d_\-]/);
46
var word = stream.current();
47
if (funcs.test(word)) return "builtin";
48
if (preds.test(word)) return "def";
49
if (keywords.test(word) || systemKeywords.test(word)) return "keyword";
53
var pushContext = function(state, type, col) {
54
return state.context = {
61
var popContext = function(state) {
62
state.indent = state.context.indent;
63
return state.context = state.context.prev;
65
var indentUnit = config.indentUnit;
67
var 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"]);
68
var preds = wordRegexp(["all", "and", "any", "contains", "exists", "has", "in", "none", "not", "or", "single", "xor"]);
69
var 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"]);
70
var 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"]);
71
var operatorChars = /[*+\-<>=&|~%^]/;
74
startState: function() {
82
token: function(stream, state) {
84
if (state.context && (state.context.align == null)) {
85
state.context.align = false;
87
state.indent = stream.indentation();
89
if (stream.eatSpace()) {
92
var style = state.tokenize(stream, state);
93
if (style !== "comment" && state.context && (state.context.align == null) && state.context.type !== "pattern") {
94
state.context.align = true;
96
if (curPunc === "(") {
97
pushContext(state, ")", stream.column());
98
} else if (curPunc === "[") {
99
pushContext(state, "]", stream.column());
100
} else if (curPunc === "{") {
101
pushContext(state, "}", stream.column());
102
} else if (/[\]\}\)]/.test(curPunc)) {
103
while (state.context && state.context.type === "pattern") {
106
if (state.context && curPunc === state.context.type) {
109
} else if (curPunc === "." && state.context && state.context.type === "pattern") {
111
} else if (/atom|string|variable/.test(style) && state.context) {
112
if (/[\}\]]/.test(state.context.type)) {
113
pushContext(state, "pattern", stream.column());
114
} else if (state.context.type === "pattern" && !state.context.align) {
115
state.context.align = true;
116
state.context.col = stream.column();
121
indent: function(state, textAfter) {
122
var firstChar = textAfter && textAfter.charAt(0);
123
var context = state.context;
124
if (/[\]\}]/.test(firstChar)) {
125
while (context && context.type === "pattern") {
126
context = context.prev;
129
var closing = context && firstChar === context.type;
130
if (!context) return 0;
131
if (context.type === "keywords") return CodeMirror.commands.newlineAndIndent;
132
if (context.align) return context.col + (closing ? 0 : 1);
133
return context.indent + (closing ? 0 : indentUnit);
138
CodeMirror.modeExtensions["cypher"] = {
139
autoFormatLineBreaks: function(text) {
140
var i, lines, reProcessedPortion;
141
var lines = text.split("\n");
142
var reProcessedPortion = /\s+\b(return|where|order by|match|with|skip|limit|create|delete|set)\b\s/g;
143
for (var i = 0; i < lines.length; i++)
144
lines[i] = lines[i].replace(reProcessedPortion, " \n$1 ").trim();
145
return lines.join("\n");
149
CodeMirror.defineMIME("application/x-cypher-query", "cypher");