You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

152 lines
7.0 KiB

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