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.

199 lines
7.5 KiB

  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. var match = stream.match(/^.*?("|\\[\s\S])/);
  112. if (!match) {
  113. stream.skipToEnd();
  114. } else if (match[1] == "\"") {
  115. state.soyState.pop();
  116. }
  117. return "string";
  118. }
  119. if (stream.match(/^\/\*/)) {
  120. state.soyState.push("comment");
  121. return "comment";
  122. } else if (stream.match(stream.sol() ? /^\s*\/\/.*/ : /^\s+\/\/.*/)) {
  123. return "comment";
  124. } else if (stream.match(/^\{\$[\w?]*/)) {
  125. state.indent += 2 * config.indentUnit;
  126. state.soyState.push("variable");
  127. return "variable-2";
  128. } else if (stream.match(/^\{literal}/)) {
  129. state.indent += config.indentUnit;
  130. state.soyState.push("literal");
  131. return "keyword";
  132. } else if (match = stream.match(/^\{([\/@\\]?[\w?]*)/)) {
  133. if (match[1] != "/switch")
  134. state.indent += (/^(\/|(else|elseif|case|default)$)/.test(match[1]) && state.tag != "switch" ? 1 : 2) * config.indentUnit;
  135. state.tag = match[1];
  136. if (state.tag == "/" + last(state.kindTag)) {
  137. // We found the tag that opened the current kind="".
  138. state.kind.pop();
  139. state.kindTag.pop();
  140. state.localMode = modes[last(state.kind)] || modes.html;
  141. state.localState = CodeMirror.startState(state.localMode);
  142. }
  143. state.soyState.push("tag");
  144. return "keyword";
  145. }
  146. return tokenUntil(stream, state, /\{|\s+\/\/|\/\*/);
  147. },
  148. indent: function(state, textAfter) {
  149. var indent = state.indent, top = last(state.soyState);
  150. if (top == "comment") return CodeMirror.Pass;
  151. if (top == "literal") {
  152. if (/^\{\/literal}/.test(textAfter)) indent -= config.indentUnit;
  153. } else {
  154. if (/^\s*\{\/(template|deltemplate)\b/.test(textAfter)) return 0;
  155. if (/^\{(\/|(fallbackmsg|elseif|else|ifempty)\b)/.test(textAfter)) indent -= config.indentUnit;
  156. if (state.tag != "switch" && /^\{(case|default)\b/.test(textAfter)) indent -= config.indentUnit;
  157. if (/^\{\/switch\b/.test(textAfter)) indent -= config.indentUnit;
  158. }
  159. if (indent && state.localMode.indent)
  160. indent += state.localMode.indent(state.localState, textAfter);
  161. return indent;
  162. },
  163. innerMode: function(state) {
  164. if (state.soyState.length && last(state.soyState) != "literal") return null;
  165. else return {state: state.localState, mode: state.localMode};
  166. },
  167. electricInput: /^\s*\{(\/|\/template|\/deltemplate|\/switch|fallbackmsg|elseif|else|case|default|ifempty|\/literal\})$/,
  168. lineComment: "//",
  169. blockCommentStart: "/*",
  170. blockCommentEnd: "*/",
  171. blockCommentContinue: " * ",
  172. fold: "indent"
  173. };
  174. }, "htmlmixed");
  175. CodeMirror.registerHelper("hintWords", "soy", indentingTags.concat(
  176. ["delpackage", "namespace", "alias", "print", "css", "debugger"]));
  177. CodeMirror.defineMIME("text/x-soy", "soy");
  178. });