File modules/editor/codemirror/mode/cypher/cypher.js
Last commit: Sun Dec 17 01:14:09 2017 +0100 Jan Dankert Integration eines weiteren Code-Editors: Codemirror. Demnächst müssen wir hier mal aufräumen und andere Editoren rauswerfen.
1 // CodeMirror, copyright (c) by Marijn Haverbeke and others 2 // Distributed under an MIT license: http://codemirror.net/LICENSE 3 4 // By the Neo4j Team and contributors. 5 // https://github.com/neo4j-contrib/CodeMirror 6 7 (function(mod) { 8 if (typeof exports == "object" && typeof module == "object") // CommonJS 9 mod(require("../../lib/codemirror")); 10 else if (typeof define == "function" && define.amd) // AMD 11 define(["../../lib/codemirror"], mod); 12 else // Plain browser env 13 mod(CodeMirror); 14 })(function(CodeMirror) { 15 "use strict"; 16 var wordRegexp = function(words) { 17 return new RegExp("^(?:" + words.join("|") + ")$", "i"); 18 }; 19 20 CodeMirror.defineMode("cypher", function(config) { 21 var tokenBase = function(stream/*, state*/) { 22 var ch = stream.next(); 23 if (ch ==='"') { 24 stream.match(/.*?"/); 25 return "string"; 26 } 27 if (ch === "'") { 28 stream.match(/.*?'/); 29 return "string"; 30 } 31 if (/[{}\(\),\.;\[\]]/.test(ch)) { 32 curPunc = ch; 33 return "node"; 34 } else if (ch === "/" && stream.eat("/")) { 35 stream.skipToEnd(); 36 return "comment"; 37 } else if (operatorChars.test(ch)) { 38 stream.eatWhile(operatorChars); 39 return null; 40 } else { 41 stream.eatWhile(/[_\w\d]/); 42 if (stream.eat(":")) { 43 stream.eatWhile(/[\w\d_\-]/); 44 return "atom"; 45 } 46 var word = stream.current(); 47 if (funcs.test(word)) return "builtin"; 48 if (preds.test(word)) return "def"; 49 if (keywords.test(word)) return "keyword"; 50 return "variable"; 51 } 52 }; 53 var pushContext = function(state, type, col) { 54 return state.context = { 55 prev: state.context, 56 indent: state.indent, 57 col: col, 58 type: type 59 }; 60 }; 61 var popContext = function(state) { 62 state.indent = state.context.indent; 63 return state.context = state.context.prev; 64 }; 65 var indentUnit = config.indentUnit; 66 var curPunc; 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 operatorChars = /[*+\-<>=&|~%^]/; 71 72 return { 73 startState: function(/*base*/) { 74 return { 75 tokenize: tokenBase, 76 context: null, 77 indent: 0, 78 col: 0 79 }; 80 }, 81 token: function(stream, state) { 82 if (stream.sol()) { 83 if (state.context && (state.context.align == null)) { 84 state.context.align = false; 85 } 86 state.indent = stream.indentation(); 87 } 88 if (stream.eatSpace()) { 89 return null; 90 } 91 var style = state.tokenize(stream, state); 92 if (style !== "comment" && state.context && (state.context.align == null) && state.context.type !== "pattern") { 93 state.context.align = true; 94 } 95 if (curPunc === "(") { 96 pushContext(state, ")", stream.column()); 97 } else if (curPunc === "[") { 98 pushContext(state, "]", stream.column()); 99 } else if (curPunc === "{") { 100 pushContext(state, "}", stream.column()); 101 } else if (/[\]\}\)]/.test(curPunc)) { 102 while (state.context && state.context.type === "pattern") { 103 popContext(state); 104 } 105 if (state.context && curPunc === state.context.type) { 106 popContext(state); 107 } 108 } else if (curPunc === "." && state.context && state.context.type === "pattern") { 109 popContext(state); 110 } else if (/atom|string|variable/.test(style) && state.context) { 111 if (/[\}\]]/.test(state.context.type)) { 112 pushContext(state, "pattern", stream.column()); 113 } else if (state.context.type === "pattern" && !state.context.align) { 114 state.context.align = true; 115 state.context.col = stream.column(); 116 } 117 } 118 return style; 119 }, 120 indent: function(state, textAfter) { 121 var firstChar = textAfter && textAfter.charAt(0); 122 var context = state.context; 123 if (/[\]\}]/.test(firstChar)) { 124 while (context && context.type === "pattern") { 125 context = context.prev; 126 } 127 } 128 var closing = context && firstChar === context.type; 129 if (!context) return 0; 130 if (context.type === "keywords") return CodeMirror.commands.newlineAndIndent; 131 if (context.align) return context.col + (closing ? 0 : 1); 132 return context.indent + (closing ? 0 : indentUnit); 133 } 134 }; 135 }); 136 137 CodeMirror.modeExtensions["cypher"] = { 138 autoFormatLineBreaks: function(text) { 139 var i, lines, reProcessedPortion; 140 var lines = text.split("\n"); 141 var reProcessedPortion = /\s+\b(return|where|order by|match|with|skip|limit|create|delete|set)\b\s/g; 142 for (var i = 0; i < lines.length; i++) 143 lines[i] = lines[i].replace(reProcessedPortion, " \n$1 ").trim(); 144 return lines.join("\n"); 145 } 146 }; 147 148 CodeMirror.defineMIME("application/x-cypher-query", "cypher"); 149 150 });
Downloadmodules/editor/codemirror/mode/cypher/cypher.js
History Sun, 17 Dec 2017 01:14:09 +0100 Jan Dankert Integration eines weiteren Code-Editors: Codemirror. Demnächst müssen wir hier mal aufräumen und andere Editoren rauswerfen.