Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644
  1. // CodeMirror, copyright (c) by Marijn Haverbeke and others
  2. // Distributed under an MIT license: https://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 paramData = { noEndTag: true, soyState: "param-def" };
  13. var tags = {
  14. "alias": { noEndTag: true },
  15. "delpackage": { noEndTag: true },
  16. "namespace": { noEndTag: true, soyState: "namespace-def" },
  17. "@attribute": paramData,
  18. "@attribute?": paramData,
  19. "@param": paramData,
  20. "@param?": paramData,
  21. "@inject": paramData,
  22. "@inject?": paramData,
  23. "@state": paramData,
  24. "template": { soyState: "templ-def", variableScope: true},
  25. "extern": {soyState: "param-def"},
  26. "export": {soyState: "param-def"},
  27. "literal": { },
  28. "msg": {},
  29. "fallbackmsg": { noEndTag: true, reduceIndent: true},
  30. "select": {},
  31. "plural": {},
  32. "let": { soyState: "var-def" },
  33. "if": {},
  34. "elseif": { noEndTag: true, reduceIndent: true},
  35. "else": { noEndTag: true, reduceIndent: true},
  36. "switch": {},
  37. "case": { noEndTag: true, reduceIndent: true},
  38. "default": { noEndTag: true, reduceIndent: true},
  39. "foreach": { variableScope: true, soyState: "for-loop" },
  40. "ifempty": { noEndTag: true, reduceIndent: true},
  41. "for": { variableScope: true, soyState: "for-loop" },
  42. "call": { soyState: "templ-ref" },
  43. "param": { soyState: "param-ref"},
  44. "print": { noEndTag: true },
  45. "deltemplate": { soyState: "templ-def", variableScope: true},
  46. "delcall": { soyState: "templ-ref" },
  47. "log": {},
  48. "element": { variableScope: true },
  49. "velog": {},
  50. };
  51. var indentingTags = Object.keys(tags).filter(function(tag) {
  52. return !tags[tag].noEndTag || tags[tag].reduceIndent;
  53. });
  54. CodeMirror.defineMode("soy", function(config) {
  55. var textMode = CodeMirror.getMode(config, "text/plain");
  56. var modes = {
  57. html: CodeMirror.getMode(config, {name: "text/html", multilineTagIndentFactor: 2, multilineTagIndentPastTag: false, allowMissingTagName: true}),
  58. attributes: textMode,
  59. text: textMode,
  60. uri: textMode,
  61. trusted_resource_uri: textMode,
  62. css: CodeMirror.getMode(config, "text/css"),
  63. js: CodeMirror.getMode(config, {name: "text/javascript", statementIndent: 2 * config.indentUnit})
  64. };
  65. function last(array) {
  66. return array[array.length - 1];
  67. }
  68. function tokenUntil(stream, state, untilRegExp) {
  69. if (stream.sol()) {
  70. for (var indent = 0; indent < state.indent; indent++) {
  71. if (!stream.eat(/\s/)) break;
  72. }
  73. if (indent) return null;
  74. }
  75. var oldString = stream.string;
  76. var match = untilRegExp.exec(oldString.substr(stream.pos));
  77. if (match) {
  78. // We don't use backUp because it backs up just the position, not the state.
  79. // This uses an undocumented API.
  80. stream.string = oldString.substr(0, stream.pos + match.index);
  81. }
  82. var result = stream.hideFirstChars(state.indent, function() {
  83. var localState = last(state.localStates);
  84. return localState.mode.token(stream, localState.state);
  85. });
  86. stream.string = oldString;
  87. return result;
  88. }
  89. function contains(list, element) {
  90. while (list) {
  91. if (list.element === element) return true;
  92. list = list.next;
  93. }
  94. return false;
  95. }
  96. function prepend(list, element) {
  97. return {
  98. element: element,
  99. next: list
  100. };
  101. }
  102. function popcontext(state) {
  103. if (!state.context) return;
  104. if (state.context.scope) {
  105. state.variables = state.context.scope;
  106. }
  107. state.context = state.context.previousContext;
  108. }
  109. // Reference a variable `name` in `list`.
  110. // Let `loose` be truthy to ignore missing identifiers.
  111. function ref(list, name, loose) {
  112. return contains(list, name) ? "variable-2" : (loose ? "variable" : "variable-2 error");
  113. }
  114. // Data for an open soy tag.
  115. function Context(previousContext, tag, scope) {
  116. this.previousContext = previousContext;
  117. this.tag = tag;
  118. this.kind = null;
  119. this.scope = scope;
  120. }
  121. function expression(stream, state) {
  122. var match;
  123. if (stream.match(/[[]/)) {
  124. state.soyState.push("list-literal");
  125. state.context = new Context(state.context, "list-literal", state.variables);
  126. state.lookupVariables = false;
  127. return null;
  128. } else if (stream.match(/map\b/)) {
  129. state.soyState.push("map-literal");
  130. return "keyword";
  131. } else if (stream.match(/record\b/)) {
  132. state.soyState.push("record-literal");
  133. return "keyword";
  134. } else if (stream.match(/([\w]+)(?=\()/)) {
  135. return "variable callee";
  136. } else if (match = stream.match(/^["']/)) {
  137. state.soyState.push("string");
  138. state.quoteKind = match[0];
  139. return "string";
  140. } else if (stream.match(/^[(]/)) {
  141. state.soyState.push("open-parentheses");
  142. return null;
  143. } else if (stream.match(/(null|true|false)(?!\w)/) ||
  144. stream.match(/0x([0-9a-fA-F]{2,})/) ||
  145. stream.match(/-?([0-9]*[.])?[0-9]+(e[0-9]*)?/)) {
  146. return "atom";
  147. } else if (stream.match(/(\||[+\-*\/%]|[=!]=|\?:|[<>]=?)/)) {
  148. // Tokenize filter, binary, null propagator, and equality operators.
  149. return "operator";
  150. } else if (match = stream.match(/^\$([\w]+)/)) {
  151. return ref(state.variables, match[1], !state.lookupVariables);
  152. } else if (match = stream.match(/^\w+/)) {
  153. return /^(?:as|and|or|not|in|if)$/.test(match[0]) ? "keyword" : null;
  154. }
  155. stream.next();
  156. return null;
  157. }
  158. return {
  159. startState: function() {
  160. return {
  161. soyState: [],
  162. variables: prepend(null, 'ij'),
  163. scopes: null,
  164. indent: 0,
  165. quoteKind: null,
  166. context: null,
  167. lookupVariables: true, // Is unknown variables considered an error
  168. localStates: [{
  169. mode: modes.html,
  170. state: CodeMirror.startState(modes.html)
  171. }]
  172. };
  173. },
  174. copyState: function(state) {
  175. return {
  176. tag: state.tag, // Last seen Soy tag.
  177. soyState: state.soyState.concat([]),
  178. variables: state.variables,
  179. context: state.context,
  180. indent: state.indent, // Indentation of the following line.
  181. quoteKind: state.quoteKind,
  182. lookupVariables: state.lookupVariables,
  183. localStates: state.localStates.map(function(localState) {
  184. return {
  185. mode: localState.mode,
  186. state: CodeMirror.copyState(localState.mode, localState.state)
  187. };
  188. })
  189. };
  190. },
  191. token: function(stream, state) {
  192. var match;
  193. switch (last(state.soyState)) {
  194. case "comment":
  195. if (stream.match(/^.*?\*\//)) {
  196. state.soyState.pop();
  197. } else {
  198. stream.skipToEnd();
  199. }
  200. if (!state.context || !state.context.scope) {
  201. var paramRe = /@param\??\s+(\S+)/g;
  202. var current = stream.current();
  203. for (var match; (match = paramRe.exec(current)); ) {
  204. state.variables = prepend(state.variables, match[1]);
  205. }
  206. }
  207. return "comment";
  208. case "string":
  209. var match = stream.match(/^.*?(["']|\\[\s\S])/);
  210. if (!match) {
  211. stream.skipToEnd();
  212. } else if (match[1] == state.quoteKind) {
  213. state.quoteKind = null;
  214. state.soyState.pop();
  215. }
  216. return "string";
  217. }
  218. if (!state.soyState.length || last(state.soyState) != "literal") {
  219. if (stream.match(/^\/\*/)) {
  220. state.soyState.push("comment");
  221. return "comment";
  222. } else if (stream.match(stream.sol() ? /^\s*\/\/.*/ : /^\s+\/\/.*/)) {
  223. return "comment";
  224. }
  225. }
  226. switch (last(state.soyState)) {
  227. case "templ-def":
  228. if (match = stream.match(/^\.?([\w]+(?!\.[\w]+)*)/)) {
  229. state.soyState.pop();
  230. return "def";
  231. }
  232. stream.next();
  233. return null;
  234. case "templ-ref":
  235. if (match = stream.match(/(\.?[a-zA-Z_][a-zA-Z_0-9]+)+/)) {
  236. state.soyState.pop();
  237. // If the first character is '.', it can only be a local template.
  238. if (match[0][0] == '.') {
  239. return "variable-2"
  240. }
  241. // Otherwise
  242. return "variable";
  243. }
  244. if (match = stream.match(/^\$([\w]+)/)) {
  245. state.soyState.pop();
  246. return ref(state.variables, match[1], !state.lookupVariables);
  247. }
  248. stream.next();
  249. return null;
  250. case "namespace-def":
  251. if (match = stream.match(/^\.?([\w\.]+)/)) {
  252. state.soyState.pop();
  253. return "variable";
  254. }
  255. stream.next();
  256. return null;
  257. case "param-def":
  258. if (match = stream.match(/^\*/)) {
  259. state.soyState.pop();
  260. state.soyState.push("param-type");
  261. return "type";
  262. }
  263. if (match = stream.match(/^\w+/)) {
  264. if (match[0] == 'extern') {
  265. return 'keyword';
  266. }
  267. state.variables = prepend(state.variables, match[0]);
  268. state.soyState.pop();
  269. state.soyState.push("param-type");
  270. return "def";
  271. }
  272. stream.next();
  273. return null;
  274. case "param-ref":
  275. if (match = stream.match(/^\w+/)) {
  276. state.soyState.pop();
  277. return "property";
  278. }
  279. stream.next();
  280. return null;
  281. case "open-parentheses":
  282. if (stream.match(/[)]/)) {
  283. state.soyState.pop();
  284. return null;
  285. }
  286. return expression(stream, state);
  287. case "param-type":
  288. var peekChar = stream.peek();
  289. if ("}]=>,".indexOf(peekChar) != -1) {
  290. state.soyState.pop();
  291. return null;
  292. } else if (peekChar == "[") {
  293. state.soyState.push('param-type-record');
  294. return null;
  295. } else if (peekChar == "(") {
  296. state.soyState.push('param-type-template');
  297. return null;
  298. } else if (peekChar == "<") {
  299. state.soyState.push('param-type-parameter');
  300. return null;
  301. } else if (match = stream.match(/^([\w]+|[?])/)) {
  302. return "type";
  303. }
  304. stream.next();
  305. return null;
  306. case "param-type-record":
  307. var peekChar = stream.peek();
  308. if (peekChar == "]") {
  309. state.soyState.pop();
  310. return null;
  311. }
  312. if (stream.match(/^\w+/)) {
  313. state.soyState.push('param-type');
  314. return "property";
  315. }
  316. stream.next();
  317. return null;
  318. case "param-type-parameter":
  319. if (stream.match(/^[>]/)) {
  320. state.soyState.pop();
  321. return null;
  322. }
  323. if (stream.match(/^[<,]/)) {
  324. state.soyState.push('param-type');
  325. return null;
  326. }
  327. stream.next();
  328. return null;
  329. case "param-type-template":
  330. if (stream.match(/[>]/)) {
  331. state.soyState.pop();
  332. state.soyState.push('param-type');
  333. return null;
  334. }
  335. if (stream.match(/^\w+/)) {
  336. state.soyState.push('param-type');
  337. return "def";
  338. }
  339. stream.next();
  340. return null;
  341. case "var-def":
  342. if (match = stream.match(/^\$([\w]+)/)) {
  343. state.variables = prepend(state.variables, match[1]);
  344. state.soyState.pop();
  345. return "def";
  346. }
  347. stream.next();
  348. return null;
  349. case "for-loop":
  350. if (stream.match(/\bin\b/)) {
  351. state.soyState.pop();
  352. return "keyword";
  353. }
  354. if (stream.peek() == "$") {
  355. state.soyState.push('var-def');
  356. return null;
  357. }
  358. stream.next();
  359. return null;
  360. case "record-literal":
  361. if (stream.match(/^[)]/)) {
  362. state.soyState.pop();
  363. return null;
  364. }
  365. if (stream.match(/[(,]/)) {
  366. state.soyState.push("map-value")
  367. state.soyState.push("record-key")
  368. return null;
  369. }
  370. stream.next()
  371. return null;
  372. case "map-literal":
  373. if (stream.match(/^[)]/)) {
  374. state.soyState.pop();
  375. return null;
  376. }
  377. if (stream.match(/[(,]/)) {
  378. state.soyState.push("map-value")
  379. state.soyState.push("map-value")
  380. return null;
  381. }
  382. stream.next()
  383. return null;
  384. case "list-literal":
  385. if (stream.match(']')) {
  386. state.soyState.pop();
  387. state.lookupVariables = true;
  388. popcontext(state);
  389. return null;
  390. }
  391. if (stream.match(/\bfor\b/)) {
  392. state.lookupVariables = true;
  393. state.soyState.push('for-loop');
  394. return "keyword";
  395. }
  396. return expression(stream, state);
  397. case "record-key":
  398. if (stream.match(/[\w]+/)) {
  399. return "property";
  400. }
  401. if (stream.match(/^[:]/)) {
  402. state.soyState.pop();
  403. return null;
  404. }
  405. stream.next();
  406. return null;
  407. case "map-value":
  408. if (stream.peek() == ")" || stream.peek() == "," || stream.match(/^[:)]/)) {
  409. state.soyState.pop();
  410. return null;
  411. }
  412. return expression(stream, state);
  413. case "import":
  414. if (stream.eat(";")) {
  415. state.soyState.pop();
  416. state.indent -= 2 * config.indentUnit;
  417. return null;
  418. }
  419. if (stream.match(/\w+(?=\s+as)/)) {
  420. return "variable";
  421. }
  422. if (match = stream.match(/\w+/)) {
  423. return /(from|as)/.test(match[0]) ? "keyword" : "def";
  424. }
  425. if (match = stream.match(/^["']/)) {
  426. state.soyState.push("string");
  427. state.quoteKind = match[0];
  428. return "string";
  429. }
  430. stream.next();
  431. return null;
  432. case "tag":
  433. var endTag;
  434. var tagName;
  435. if (state.tag === undefined) {
  436. endTag = true;
  437. tagName = '';
  438. } else {
  439. endTag = state.tag[0] == "/";
  440. tagName = endTag ? state.tag.substring(1) : state.tag;
  441. }
  442. var tag = tags[tagName];
  443. if (stream.match(/^\/?}/)) {
  444. var selfClosed = stream.current() == "/}";
  445. if (selfClosed && !endTag) {
  446. popcontext(state);
  447. }
  448. if (state.tag == "/template" || state.tag == "/deltemplate") {
  449. state.variables = prepend(null, 'ij');
  450. state.indent = 0;
  451. } else {
  452. state.indent -= config.indentUnit *
  453. (selfClosed || indentingTags.indexOf(state.tag) == -1 ? 2 : 1);
  454. }
  455. state.soyState.pop();
  456. return "keyword";
  457. } else if (stream.match(/^([\w?]+)(?==)/)) {
  458. if (state.context && state.context.tag == tagName && stream.current() == "kind" && (match = stream.match(/^="([^"]+)/, false))) {
  459. var kind = match[1];
  460. state.context.kind = kind;
  461. var mode = modes[kind] || modes.html;
  462. var localState = last(state.localStates);
  463. if (localState.mode.indent) {
  464. state.indent += localState.mode.indent(localState.state, "", "");
  465. }
  466. state.localStates.push({
  467. mode: mode,
  468. state: CodeMirror.startState(mode)
  469. });
  470. }
  471. return "attribute";
  472. }
  473. return expression(stream, state);
  474. case "template-call-expression":
  475. if (stream.match(/^([\w-?]+)(?==)/)) {
  476. return "attribute";
  477. } else if (stream.eat('>')) {
  478. state.soyState.pop();
  479. return "keyword";
  480. } else if (stream.eat('/>')) {
  481. state.soyState.pop();
  482. return "keyword";
  483. }
  484. return expression(stream, state);
  485. case "literal":
  486. if (stream.match('{/literal}', false)) {
  487. state.soyState.pop();
  488. return this.token(stream, state);
  489. }
  490. return tokenUntil(stream, state, /\{\/literal}/);
  491. }
  492. if (stream.match('{literal}')) {
  493. state.indent += config.indentUnit;
  494. state.soyState.push("literal");
  495. state.context = new Context(state.context, "literal", state.variables);
  496. return "keyword";
  497. // A tag-keyword must be followed by whitespace, comment or a closing tag.
  498. } else if (match = stream.match(/^\{([/@\\]?\w+\??)(?=$|[\s}]|\/[/*])/)) {
  499. var prevTag = state.tag;
  500. state.tag = match[1];
  501. var endTag = state.tag[0] == "/";
  502. var indentingTag = !!tags[state.tag];
  503. var tagName = endTag ? state.tag.substring(1) : state.tag;
  504. var tag = tags[tagName];
  505. if (state.tag != "/switch")
  506. state.indent += ((endTag || tag && tag.reduceIndent) && prevTag != "switch" ? 1 : 2) * config.indentUnit;
  507. state.soyState.push("tag");
  508. var tagError = false;
  509. if (tag) {
  510. if (!endTag) {
  511. if (tag.soyState) state.soyState.push(tag.soyState);
  512. }
  513. // If a new tag, open a new context.
  514. if (!tag.noEndTag && (indentingTag || !endTag)) {
  515. state.context = new Context(state.context, state.tag, tag.variableScope ? state.variables : null);
  516. // Otherwise close the current context.
  517. } else if (endTag) {
  518. var isBalancedForExtern = tagName == 'extern' && (state.context && state.context.tag == 'export');
  519. if (!state.context || ((state.context.tag != tagName) && !isBalancedForExtern)) {
  520. tagError = true;
  521. } else if (state.context) {
  522. if (state.context.kind) {
  523. state.localStates.pop();
  524. var localState = last(state.localStates);
  525. if (localState.mode.indent) {
  526. state.indent -= localState.mode.indent(localState.state, "", "");
  527. }
  528. }
  529. popcontext(state);
  530. }
  531. }
  532. } else if (endTag) {
  533. // Assume all tags with a closing tag are defined in the config.
  534. tagError = true;
  535. }
  536. return (tagError ? "error " : "") + "keyword";
  537. // Not a tag-keyword; it's an implicit print tag.
  538. } else if (stream.eat('{')) {
  539. state.tag = "print";
  540. state.indent += 2 * config.indentUnit;
  541. state.soyState.push("tag");
  542. return "keyword";
  543. } else if (!state.context && stream.match(/\bimport\b/)) {
  544. state.soyState.push("import");
  545. state.indent += 2 * config.indentUnit;
  546. return "keyword";
  547. } else if (match = stream.match('<{')) {
  548. state.soyState.push("template-call-expression");
  549. state.indent += 2 * config.indentUnit;
  550. state.soyState.push("tag");
  551. return "keyword";
  552. } else if (match = stream.match('</>')) {
  553. state.indent -= 1 * config.indentUnit;
  554. return "keyword";
  555. }
  556. return tokenUntil(stream, state, /\{|\s+\/\/|\/\*/);
  557. },
  558. indent: function(state, textAfter, line) {
  559. var indent = state.indent, top = last(state.soyState);
  560. if (top == "comment") return CodeMirror.Pass;
  561. if (top == "literal") {
  562. if (/^\{\/literal}/.test(textAfter)) indent -= config.indentUnit;
  563. } else {
  564. if (/^\s*\{\/(template|deltemplate)\b/.test(textAfter)) return 0;
  565. if (/^\{(\/|(fallbackmsg|elseif|else|ifempty)\b)/.test(textAfter)) indent -= config.indentUnit;
  566. if (state.tag != "switch" && /^\{(case|default)\b/.test(textAfter)) indent -= config.indentUnit;
  567. if (/^\{\/switch\b/.test(textAfter)) indent -= config.indentUnit;
  568. }
  569. var localState = last(state.localStates);
  570. if (indent && localState.mode.indent) {
  571. indent += localState.mode.indent(localState.state, textAfter, line);
  572. }
  573. return indent;
  574. },
  575. innerMode: function(state) {
  576. if (state.soyState.length && last(state.soyState) != "literal") return null;
  577. else return last(state.localStates);
  578. },
  579. electricInput: /^\s*\{(\/|\/template|\/deltemplate|\/switch|fallbackmsg|elseif|else|case|default|ifempty|\/literal\})$/,
  580. lineComment: "//",
  581. blockCommentStart: "/*",
  582. blockCommentEnd: "*/",
  583. blockCommentContinue: " * ",
  584. useInnerComments: false,
  585. fold: "indent"
  586. };
  587. }, "htmlmixed");
  588. CodeMirror.registerHelper("wordChars", "soy", /[\w$]/);
  589. CodeMirror.registerHelper("hintWords", "soy", Object.keys(tags).concat(
  590. ["css", "debugger"]));
  591. CodeMirror.defineMIME("text/x-soy", "soy");
  592. });