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
6.9KB

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