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.

198 lines
7.4 KiB

3 years ago
  1. // CodeMirror, copyright (c) by Marijn Haverbeke and others
  2. // Distributed under an MIT license: http://codemirror.net/LICENSE
  3. (function(mod) {
  4. if (typeof exports == "object" && typeof module == "object") // CommonJS
  5. mod(require("../../lib/codemirror"), require("../htmlmixed/htmlmixed"));
  6. else if (typeof define == "function" && define.amd) // AMD
  7. define(["../../lib/codemirror", "../htmlmixed/htmlmixed"], mod);
  8. else // Plain browser env
  9. mod(CodeMirror);
  10. })(function(CodeMirror) {
  11. "use strict";
  12. var indentingTags = ["template", "literal", "msg", "fallbackmsg", "let", "if", "elseif",
  13. "else", "switch", "case", "default", "foreach", "ifempty", "for",
  14. "call", "param", "deltemplate", "delcall", "log"];
  15. CodeMirror.defineMode("soy", function(config) {
  16. var textMode = CodeMirror.getMode(config, "text/plain");
  17. var modes = {
  18. html: CodeMirror.getMode(config, {name: "text/html", multilineTagIndentFactor: 2, multilineTagIndentPastTag: false}),
  19. attributes: textMode,
  20. text: textMode,
  21. uri: textMode,
  22. css: CodeMirror.getMode(config, "text/css"),
  23. js: CodeMirror.getMode(config, {name: "text/javascript", statementIndent: 2 * config.indentUnit})
  24. };
  25. function last(array) {
  26. return array[array.length - 1];
  27. }
  28. function tokenUntil(stream, state, untilRegExp) {
  29. var oldString = stream.string;
  30. var match = untilRegExp.exec(oldString.substr(stream.pos));
  31. if (match) {
  32. // We don't use backUp because it backs up just the position, not the state.
  33. // This uses an undocumented API.
  34. stream.string = oldString.substr(0, stream.pos + match.index);
  35. }
  36. var result = stream.hideFirstChars(state.indent, function() {
  37. return state.localMode.token(stream, state.localState);
  38. });
  39. stream.string = oldString;
  40. return result;
  41. }
  42. return {
  43. startState: function() {
  44. return {
  45. kind: [],
  46. kindTag: [],
  47. soyState: [],
  48. indent: 0,
  49. localMode: modes.html,
  50. localState: CodeMirror.startState(modes.html)
  51. };
  52. },
  53. copyState: function(state) {
  54. return {
  55. tag: state.tag, // Last seen Soy tag.
  56. kind: state.kind.concat([]), // Values of kind="" attributes.
  57. kindTag: state.kindTag.concat([]), // Opened tags with kind="" attributes.
  58. soyState: state.soyState.concat([]),
  59. indent: state.indent, // Indentation of the following line.
  60. localMode: state.localMode,
  61. localState: CodeMirror.copyState(state.localMode, state.localState)
  62. };
  63. },
  64. token: function(stream, state) {
  65. var match;
  66. switch (last(state.soyState)) {
  67. case "comment":
  68. if (stream.match(/^.*?\*\//)) {
  69. state.soyState.pop();
  70. } else {
  71. stream.skipToEnd();
  72. }
  73. return "comment";
  74. case "variable":
  75. if (stream.match(/^}/)) {
  76. state.indent -= 2 * config.indentUnit;
  77. state.soyState.pop();
  78. return "variable-2";
  79. }
  80. stream.next();
  81. return null;
  82. case "tag":
  83. if (stream.match(/^\/?}/)) {
  84. if (state.tag == "/template" || state.tag == "/deltemplate") state.indent = 0;
  85. else state.indent -= (stream.current() == "/}" || indentingTags.indexOf(state.tag) == -1 ? 2 : 1) * config.indentUnit;
  86. state.soyState.pop();
  87. return "keyword";
  88. } else if (stream.match(/^(\w+)(?==)/)) {
  89. if (stream.current() == "kind" && (match = stream.match(/^="([^"]+)/, false))) {
  90. var kind = match[1];
  91. state.kind.push(kind);
  92. state.kindTag.push(state.tag);
  93. state.localMode = modes[kind] || modes.html;
  94. state.localState = CodeMirror.startState(state.localMode);
  95. }
  96. return "attribute";
  97. } else if (stream.match(/^"/)) {
  98. state.soyState.push("string");
  99. return "string";
  100. }
  101. stream.next();
  102. return null;
  103. case "literal":
  104. if (stream.match(/^(?=\{\/literal})/)) {
  105. state.indent -= config.indentUnit;
  106. state.soyState.pop();
  107. return this.token(stream, state);
  108. }
  109. return tokenUntil(stream, state, /\{\/literal}/);
  110. case "string":
  111. if (stream.match(/^.*?"/)) {
  112. state.soyState.pop();
  113. } else {
  114. stream.skipToEnd();
  115. }
  116. return "string";
  117. }
  118. if (stream.match(/^\/\*/)) {
  119. state.soyState.push("comment");
  120. return "comment";
  121. } else if (stream.match(stream.sol() ? /^\s*\/\/.*/ : /^\s+\/\/.*/)) {
  122. return "comment";
  123. } else if (stream.match(/^\{\$\w*/)) {
  124. state.indent += 2 * config.indentUnit;
  125. state.soyState.push("variable");
  126. return "variable-2";
  127. } else if (stream.match(/^\{literal}/)) {
  128. state.indent += config.indentUnit;
  129. state.soyState.push("literal");
  130. return "keyword";
  131. } else if (match = stream.match(/^\{([\/@\\]?\w*)/)) {
  132. if (match[1] != "/switch")
  133. state.indent += (/^(\/|(else|elseif|case|default)$)/.test(match[1]) && state.tag != "switch" ? 1 : 2) * config.indentUnit;
  134. state.tag = match[1];
  135. if (state.tag == "/" + last(state.kindTag)) {
  136. // We found the tag that opened the current kind="".
  137. state.kind.pop();
  138. state.kindTag.pop();
  139. state.localMode = modes[last(state.kind)] || modes.html;
  140. state.localState = CodeMirror.startState(state.localMode);
  141. }
  142. state.soyState.push("tag");
  143. return "keyword";
  144. }
  145. return tokenUntil(stream, state, /\{|\s+\/\/|\/\*/);
  146. },
  147. indent: function(state, textAfter) {
  148. var indent = state.indent, top = last(state.soyState);
  149. if (top == "comment") return CodeMirror.Pass;
  150. if (top == "literal") {
  151. if (/^\{\/literal}/.test(textAfter)) indent -= config.indentUnit;
  152. } else {
  153. if (/^\s*\{\/(template|deltemplate)\b/.test(textAfter)) return 0;
  154. if (/^\{(\/|(fallbackmsg|elseif|else|ifempty)\b)/.test(textAfter)) indent -= config.indentUnit;
  155. if (state.tag != "switch" && /^\{(case|default)\b/.test(textAfter)) indent -= config.indentUnit;
  156. if (/^\{\/switch\b/.test(textAfter)) indent -= config.indentUnit;
  157. }
  158. if (indent && state.localMode.indent)
  159. indent += state.localMode.indent(state.localState, textAfter);
  160. return indent;
  161. },
  162. innerMode: function(state) {
  163. if (state.soyState.length && last(state.soyState) != "literal") return null;
  164. else return {state: state.localState, mode: state.localMode};
  165. },
  166. electricInput: /^\s*\{(\/|\/template|\/deltemplate|\/switch|fallbackmsg|elseif|else|case|default|ifempty|\/literal\})$/,
  167. lineComment: "//",
  168. blockCommentStart: "/*",
  169. blockCommentEnd: "*/",
  170. blockCommentContinue: " * ",
  171. fold: "indent"
  172. };
  173. }, "htmlmixed");
  174. CodeMirror.registerHelper("hintWords", "soy", indentingTags.concat(
  175. ["delpackage", "namespace", "alias", "print", "css", "debugger"]));
  176. CodeMirror.defineMIME("text/x-soy", "soy");
  177. });