cypher.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. // CodeMirror, copyright (c) by Marijn Haverbeke and others
  2. // Distributed under an MIT license: http://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(), curPunc = null;
  20. if (ch === "\"" || ch === "'") {
  21. stream.match(/.+?["']/);
  22. return "string";
  23. }
  24. if (/[{}\(\),\.;\[\]]/.test(ch)) {
  25. curPunc = ch;
  26. return "node";
  27. } else if (ch === "/" && stream.eat("/")) {
  28. stream.skipToEnd();
  29. return "comment";
  30. } else if (operatorChars.test(ch)) {
  31. stream.eatWhile(operatorChars);
  32. return null;
  33. } else {
  34. stream.eatWhile(/[_\w\d]/);
  35. if (stream.eat(":")) {
  36. stream.eatWhile(/[\w\d_\-]/);
  37. return "atom";
  38. }
  39. var word = stream.current();
  40. if (funcs.test(word)) return "builtin";
  41. if (preds.test(word)) return "def";
  42. if (keywords.test(word)) return "keyword";
  43. return "variable";
  44. }
  45. };
  46. var pushContext = function(state, type, col) {
  47. return state.context = {
  48. prev: state.context,
  49. indent: state.indent,
  50. col: col,
  51. type: type
  52. };
  53. };
  54. var popContext = function(state) {
  55. state.indent = state.context.indent;
  56. return state.context = state.context.prev;
  57. };
  58. var indentUnit = config.indentUnit;
  59. var curPunc;
  60. 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", "labels", "last", "left", "length", "log", "log10", "lower", "ltrim", "max", "min", "node", "nodes", "percentileCont", "percentileDisc", "pi", "radians", "rand", "range", "reduce", "rel", "relationship", "relationships", "replace", "right", "round", "rtrim", "shortestPath", "sign", "sin", "split", "sqrt", "startnode", "stdev", "stdevp", "str", "substring", "sum", "tail", "tan", "timestamp", "toFloat", "toInt", "trim", "type", "upper"]);
  61. var preds = wordRegexp(["all", "and", "any", "has", "in", "none", "not", "or", "single", "xor"]);
  62. var keywords = wordRegexp(["as", "asc", "ascending", "assert", "by", "case", "commit", "constraint", "create", "csv", "cypher", "delete", "desc", "descending", "distinct", "drop", "else", "end", "explain", "false", "fieldterminator", "foreach", "from", "headers", "in", "index", "is", "limit", "load", "match", "merge", "null", "on", "optional", "order", "periodic", "profile", "remove", "return", "scan", "set", "skip", "start", "then", "true", "union", "unique", "unwind", "using", "when", "where", "with"]);
  63. var operatorChars = /[*+\-<>=&|~%^]/;
  64. return {
  65. startState: function(/*base*/) {
  66. return {
  67. tokenize: tokenBase,
  68. context: null,
  69. indent: 0,
  70. col: 0
  71. };
  72. },
  73. token: function(stream, state) {
  74. if (stream.sol()) {
  75. if (state.context && (state.context.align == null)) {
  76. state.context.align = false;
  77. }
  78. state.indent = stream.indentation();
  79. }
  80. if (stream.eatSpace()) {
  81. return null;
  82. }
  83. var style = state.tokenize(stream, state);
  84. if (style !== "comment" && state.context && (state.context.align == null) && state.context.type !== "pattern") {
  85. state.context.align = true;
  86. }
  87. if (curPunc === "(") {
  88. pushContext(state, ")", stream.column());
  89. } else if (curPunc === "[") {
  90. pushContext(state, "]", stream.column());
  91. } else if (curPunc === "{") {
  92. pushContext(state, "}", stream.column());
  93. } else if (/[\]\}\)]/.test(curPunc)) {
  94. while (state.context && state.context.type === "pattern") {
  95. popContext(state);
  96. }
  97. if (state.context && curPunc === state.context.type) {
  98. popContext(state);
  99. }
  100. } else if (curPunc === "." && state.context && state.context.type === "pattern") {
  101. popContext(state);
  102. } else if (/atom|string|variable/.test(style) && state.context) {
  103. if (/[\}\]]/.test(state.context.type)) {
  104. pushContext(state, "pattern", stream.column());
  105. } else if (state.context.type === "pattern" && !state.context.align) {
  106. state.context.align = true;
  107. state.context.col = stream.column();
  108. }
  109. }
  110. return style;
  111. },
  112. indent: function(state, textAfter) {
  113. var firstChar = textAfter && textAfter.charAt(0);
  114. var context = state.context;
  115. if (/[\]\}]/.test(firstChar)) {
  116. while (context && context.type === "pattern") {
  117. context = context.prev;
  118. }
  119. }
  120. var closing = context && firstChar === context.type;
  121. if (!context) return 0;
  122. if (context.type === "keywords") return CodeMirror.commands.newlineAndIndent;
  123. if (context.align) return context.col + (closing ? 0 : 1);
  124. return context.indent + (closing ? 0 : indentUnit);
  125. }
  126. };
  127. });
  128. CodeMirror.modeExtensions["cypher"] = {
  129. autoFormatLineBreaks: function(text) {
  130. var i, lines, reProcessedPortion;
  131. var lines = text.split("\n");
  132. var reProcessedPortion = /\s+\b(return|where|order by|match|with|skip|limit|create|delete|set)\b\s/g;
  133. for (var i = 0; i < lines.length; i++)
  134. lines[i] = lines[i].replace(reProcessedPortion, " \n$1 ").trim();
  135. return lines.join("\n");
  136. }
  137. };
  138. CodeMirror.defineMIME("application/x-cypher-query", "cypher");
  139. });