2015-03-13 09:58:00 +00:00
|
|
|
// CodeMirror, copyright (c) by Marijn Haverbeke and others
|
|
|
|
// Distributed under an MIT license: http://codemirror.net/LICENSE
|
|
|
|
|
|
|
|
(function(mod) {
|
|
|
|
if (typeof exports == "object" && typeof module == "object") // CommonJS
|
|
|
|
mod(require("../../lib/codemirror"), require("../htmlmixed/htmlmixed"));
|
|
|
|
else if (typeof define == "function" && define.amd) // AMD
|
|
|
|
define(["../../lib/codemirror", "../htmlmixed/htmlmixed"], mod);
|
|
|
|
else // Plain browser env
|
|
|
|
mod(CodeMirror);
|
|
|
|
})(function(CodeMirror) {
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
var indentingTags = ["template", "literal", "msg", "fallbackmsg", "let", "if", "elseif",
|
|
|
|
"else", "switch", "case", "default", "foreach", "ifempty", "for",
|
|
|
|
"call", "param", "deltemplate", "delcall", "log"];
|
|
|
|
|
|
|
|
CodeMirror.defineMode("soy", function(config) {
|
|
|
|
var textMode = CodeMirror.getMode(config, "text/plain");
|
|
|
|
var modes = {
|
|
|
|
html: CodeMirror.getMode(config, {name: "text/html", multilineTagIndentFactor: 2, multilineTagIndentPastTag: false}),
|
|
|
|
attributes: textMode,
|
|
|
|
text: textMode,
|
|
|
|
uri: textMode,
|
|
|
|
css: CodeMirror.getMode(config, "text/css"),
|
|
|
|
js: CodeMirror.getMode(config, {name: "text/javascript", statementIndent: 2 * config.indentUnit})
|
|
|
|
};
|
|
|
|
|
|
|
|
function last(array) {
|
|
|
|
return array[array.length - 1];
|
|
|
|
}
|
|
|
|
|
|
|
|
function tokenUntil(stream, state, untilRegExp) {
|
2017-03-21 15:03:16 +00:00
|
|
|
if (stream.sol()) {
|
|
|
|
for (var indent = 0; indent < state.indent; indent++) {
|
|
|
|
if (!stream.eat(/\s/)) break;
|
|
|
|
}
|
|
|
|
if (indent) return null;
|
|
|
|
}
|
2015-03-13 09:58:00 +00:00
|
|
|
var oldString = stream.string;
|
|
|
|
var match = untilRegExp.exec(oldString.substr(stream.pos));
|
|
|
|
if (match) {
|
|
|
|
// We don't use backUp because it backs up just the position, not the state.
|
|
|
|
// This uses an undocumented API.
|
|
|
|
stream.string = oldString.substr(0, stream.pos + match.index);
|
|
|
|
}
|
|
|
|
var result = stream.hideFirstChars(state.indent, function() {
|
2017-03-21 15:03:16 +00:00
|
|
|
var localState = last(state.localStates);
|
|
|
|
return localState.mode.token(stream, localState.state);
|
2015-03-13 09:58:00 +00:00
|
|
|
});
|
|
|
|
stream.string = oldString;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2017-03-21 15:03:16 +00:00
|
|
|
function contains(list, element) {
|
|
|
|
while (list) {
|
|
|
|
if (list.element === element) return true;
|
|
|
|
list = list.next;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
function prepend(list, element) {
|
|
|
|
return {
|
|
|
|
element: element,
|
|
|
|
next: list
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reference a variable `name` in `list`.
|
|
|
|
// Let `loose` be truthy to ignore missing identifiers.
|
|
|
|
function ref(list, name, loose) {
|
|
|
|
return contains(list, name) ? "variable-2" : (loose ? "variable" : "variable-2 error");
|
|
|
|
}
|
|
|
|
|
|
|
|
function popscope(state) {
|
|
|
|
if (state.scopes) {
|
|
|
|
state.variables = state.scopes.element;
|
|
|
|
state.scopes = state.scopes.next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-13 09:58:00 +00:00
|
|
|
return {
|
|
|
|
startState: function() {
|
|
|
|
return {
|
|
|
|
kind: [],
|
|
|
|
kindTag: [],
|
|
|
|
soyState: [],
|
2017-03-21 15:03:16 +00:00
|
|
|
templates: null,
|
|
|
|
variables: null,
|
|
|
|
scopes: null,
|
2015-03-13 09:58:00 +00:00
|
|
|
indent: 0,
|
2017-09-05 03:39:06 +00:00
|
|
|
quoteKind: null,
|
2017-03-21 15:03:16 +00:00
|
|
|
localStates: [{
|
|
|
|
mode: modes.html,
|
|
|
|
state: CodeMirror.startState(modes.html)
|
|
|
|
}]
|
2015-03-13 09:58:00 +00:00
|
|
|
};
|
|
|
|
},
|
|
|
|
|
|
|
|
copyState: function(state) {
|
|
|
|
return {
|
|
|
|
tag: state.tag, // Last seen Soy tag.
|
|
|
|
kind: state.kind.concat([]), // Values of kind="" attributes.
|
|
|
|
kindTag: state.kindTag.concat([]), // Opened tags with kind="" attributes.
|
|
|
|
soyState: state.soyState.concat([]),
|
2017-03-21 15:03:16 +00:00
|
|
|
templates: state.templates,
|
|
|
|
variables: state.variables,
|
|
|
|
scopes: state.scopes,
|
2015-03-13 09:58:00 +00:00
|
|
|
indent: state.indent, // Indentation of the following line.
|
2017-09-05 03:39:06 +00:00
|
|
|
quoteKind: state.quoteKind,
|
2017-03-21 15:03:16 +00:00
|
|
|
localStates: state.localStates.map(function(localState) {
|
|
|
|
return {
|
|
|
|
mode: localState.mode,
|
|
|
|
state: CodeMirror.copyState(localState.mode, localState.state)
|
|
|
|
};
|
|
|
|
})
|
2015-03-13 09:58:00 +00:00
|
|
|
};
|
|
|
|
},
|
|
|
|
|
|
|
|
token: function(stream, state) {
|
|
|
|
var match;
|
|
|
|
|
|
|
|
switch (last(state.soyState)) {
|
|
|
|
case "comment":
|
|
|
|
if (stream.match(/^.*?\*\//)) {
|
|
|
|
state.soyState.pop();
|
|
|
|
} else {
|
|
|
|
stream.skipToEnd();
|
|
|
|
}
|
|
|
|
return "comment";
|
|
|
|
|
2017-03-21 15:03:16 +00:00
|
|
|
case "templ-def":
|
|
|
|
if (match = stream.match(/^\.?([\w]+(?!\.[\w]+)*)/)) {
|
|
|
|
state.templates = prepend(state.templates, match[1]);
|
|
|
|
state.scopes = prepend(state.scopes, state.variables);
|
2015-03-13 09:58:00 +00:00
|
|
|
state.soyState.pop();
|
2017-03-21 15:03:16 +00:00
|
|
|
return "def";
|
|
|
|
}
|
|
|
|
stream.next();
|
|
|
|
return null;
|
|
|
|
|
|
|
|
case "templ-ref":
|
|
|
|
if (match = stream.match(/^\.?([\w]+)/)) {
|
|
|
|
state.soyState.pop();
|
|
|
|
// If the first character is '.', try to match against a local template name.
|
|
|
|
if (match[0][0] == '.') {
|
|
|
|
return ref(state.templates, match[1], true);
|
|
|
|
}
|
|
|
|
// Otherwise
|
|
|
|
return "variable";
|
|
|
|
}
|
|
|
|
stream.next();
|
|
|
|
return null;
|
|
|
|
|
|
|
|
case "param-def":
|
2017-09-05 03:39:06 +00:00
|
|
|
if (match = stream.match(/^\w+/)) {
|
|
|
|
state.variables = prepend(state.variables, match[0]);
|
2017-03-21 15:03:16 +00:00
|
|
|
state.soyState.pop();
|
|
|
|
state.soyState.push("param-type");
|
|
|
|
return "def";
|
|
|
|
}
|
|
|
|
stream.next();
|
|
|
|
return null;
|
|
|
|
|
|
|
|
case "param-type":
|
|
|
|
if (stream.peek() == "}") {
|
|
|
|
state.soyState.pop();
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
if (stream.eatWhile(/^[\w]+/)) {
|
|
|
|
return "variable-3";
|
|
|
|
}
|
|
|
|
stream.next();
|
|
|
|
return null;
|
|
|
|
|
|
|
|
case "var-def":
|
|
|
|
if (match = stream.match(/^\$([\w]+)/)) {
|
|
|
|
state.variables = prepend(state.variables, match[1]);
|
|
|
|
state.soyState.pop();
|
|
|
|
return "def";
|
2015-03-13 09:58:00 +00:00
|
|
|
}
|
|
|
|
stream.next();
|
|
|
|
return null;
|
|
|
|
|
|
|
|
case "tag":
|
|
|
|
if (stream.match(/^\/?}/)) {
|
2017-03-21 15:03:16 +00:00
|
|
|
if (state.tag == "/template" || state.tag == "/deltemplate") {
|
|
|
|
popscope(state);
|
|
|
|
state.indent = 0;
|
|
|
|
} else {
|
|
|
|
if (state.tag == "/for" || state.tag == "/foreach") {
|
|
|
|
popscope(state);
|
|
|
|
}
|
|
|
|
state.indent -= config.indentUnit *
|
|
|
|
(stream.current() == "/}" || indentingTags.indexOf(state.tag) == -1 ? 2 : 1);
|
|
|
|
}
|
2015-03-13 09:58:00 +00:00
|
|
|
state.soyState.pop();
|
|
|
|
return "keyword";
|
2015-07-07 05:50:59 +00:00
|
|
|
} else if (stream.match(/^([\w?]+)(?==)/)) {
|
2015-03-13 09:58:00 +00:00
|
|
|
if (stream.current() == "kind" && (match = stream.match(/^="([^"]+)/, false))) {
|
|
|
|
var kind = match[1];
|
|
|
|
state.kind.push(kind);
|
|
|
|
state.kindTag.push(state.tag);
|
2017-03-21 15:03:16 +00:00
|
|
|
var mode = modes[kind] || modes.html;
|
|
|
|
var localState = last(state.localStates);
|
2017-09-05 03:39:06 +00:00
|
|
|
if (localState.mode.indent) {
|
|
|
|
state.indent += localState.mode.indent(localState.state, "");
|
|
|
|
}
|
2017-03-21 15:03:16 +00:00
|
|
|
state.localStates.push({
|
|
|
|
mode: mode,
|
|
|
|
state: CodeMirror.startState(mode)
|
|
|
|
});
|
2015-03-13 09:58:00 +00:00
|
|
|
}
|
|
|
|
return "attribute";
|
2017-09-05 03:39:06 +00:00
|
|
|
} else if (match = stream.match(/^["']/)) {
|
2015-03-13 09:58:00 +00:00
|
|
|
state.soyState.push("string");
|
2017-09-05 03:39:06 +00:00
|
|
|
state.quoteKind = match;
|
2015-03-13 09:58:00 +00:00
|
|
|
return "string";
|
|
|
|
}
|
2017-03-21 15:03:16 +00:00
|
|
|
if (match = stream.match(/^\$([\w]+)/)) {
|
|
|
|
return ref(state.variables, match[1]);
|
|
|
|
}
|
|
|
|
if (match = stream.match(/^\w+/)) {
|
|
|
|
return /^(?:as|and|or|not|in)$/.test(match[0]) ? "keyword" : null;
|
|
|
|
}
|
2015-03-13 09:58:00 +00:00
|
|
|
stream.next();
|
|
|
|
return null;
|
|
|
|
|
|
|
|
case "literal":
|
|
|
|
if (stream.match(/^(?=\{\/literal})/)) {
|
|
|
|
state.indent -= config.indentUnit;
|
|
|
|
state.soyState.pop();
|
|
|
|
return this.token(stream, state);
|
|
|
|
}
|
|
|
|
return tokenUntil(stream, state, /\{\/literal}/);
|
|
|
|
|
|
|
|
case "string":
|
2017-09-05 03:39:06 +00:00
|
|
|
var match = stream.match(/^.*?(["']|\\[\s\S])/);
|
2016-07-19 11:05:52 +00:00
|
|
|
if (!match) {
|
2015-03-13 09:58:00 +00:00
|
|
|
stream.skipToEnd();
|
2017-09-05 03:39:06 +00:00
|
|
|
} else if (match[1] == state.quoteKind) {
|
|
|
|
state.quoteKind = null;
|
2016-07-19 11:05:52 +00:00
|
|
|
state.soyState.pop();
|
2015-03-13 09:58:00 +00:00
|
|
|
}
|
|
|
|
return "string";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (stream.match(/^\/\*/)) {
|
|
|
|
state.soyState.push("comment");
|
|
|
|
return "comment";
|
|
|
|
} else if (stream.match(stream.sol() ? /^\s*\/\/.*/ : /^\s+\/\/.*/)) {
|
|
|
|
return "comment";
|
|
|
|
} else if (stream.match(/^\{literal}/)) {
|
|
|
|
state.indent += config.indentUnit;
|
|
|
|
state.soyState.push("literal");
|
|
|
|
return "keyword";
|
2017-09-05 03:39:06 +00:00
|
|
|
|
|
|
|
// A tag-keyword must be followed by whitespace or a closing tag.
|
|
|
|
} else if (match = stream.match(/^\{([\/@\\]?\w+\??)(?=[\s\}])/)) {
|
2015-03-13 09:58:00 +00:00
|
|
|
if (match[1] != "/switch")
|
2017-03-21 15:03:16 +00:00
|
|
|
state.indent += (/^(\/|(else|elseif|ifempty|case|fallbackmsg|default)$)/.test(match[1]) && state.tag != "switch" ? 1 : 2) * config.indentUnit;
|
2015-03-13 09:58:00 +00:00
|
|
|
state.tag = match[1];
|
|
|
|
if (state.tag == "/" + last(state.kindTag)) {
|
|
|
|
// We found the tag that opened the current kind="".
|
|
|
|
state.kind.pop();
|
|
|
|
state.kindTag.pop();
|
2017-03-21 15:03:16 +00:00
|
|
|
state.localStates.pop();
|
|
|
|
var localState = last(state.localStates);
|
2017-09-05 03:39:06 +00:00
|
|
|
if (localState.mode.indent) {
|
|
|
|
state.indent -= localState.mode.indent(localState.state, "");
|
|
|
|
}
|
2015-03-13 09:58:00 +00:00
|
|
|
}
|
|
|
|
state.soyState.push("tag");
|
2017-03-21 15:03:16 +00:00
|
|
|
if (state.tag == "template" || state.tag == "deltemplate") {
|
|
|
|
state.soyState.push("templ-def");
|
|
|
|
}
|
|
|
|
if (state.tag == "call" || state.tag == "delcall") {
|
|
|
|
state.soyState.push("templ-ref");
|
|
|
|
}
|
|
|
|
if (state.tag == "let") {
|
|
|
|
state.soyState.push("var-def");
|
|
|
|
}
|
|
|
|
if (state.tag == "for" || state.tag == "foreach") {
|
|
|
|
state.scopes = prepend(state.scopes, state.variables);
|
|
|
|
state.soyState.push("var-def");
|
|
|
|
}
|
|
|
|
if (state.tag.match(/^@(?:param\??|inject)/)) {
|
|
|
|
state.soyState.push("param-def");
|
|
|
|
}
|
2015-03-13 09:58:00 +00:00
|
|
|
return "keyword";
|
2017-09-05 03:39:06 +00:00
|
|
|
|
|
|
|
// Not a tag-keyword; it's an implicit print tag.
|
|
|
|
} else if (stream.eat('{')) {
|
|
|
|
state.tag = "print";
|
|
|
|
state.indent += 2 * config.indentUnit;
|
|
|
|
state.soyState.push("tag");
|
|
|
|
return "keyword";
|
2015-03-13 09:58:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return tokenUntil(stream, state, /\{|\s+\/\/|\/\*/);
|
|
|
|
},
|
|
|
|
|
|
|
|
indent: function(state, textAfter) {
|
|
|
|
var indent = state.indent, top = last(state.soyState);
|
|
|
|
if (top == "comment") return CodeMirror.Pass;
|
|
|
|
|
|
|
|
if (top == "literal") {
|
|
|
|
if (/^\{\/literal}/.test(textAfter)) indent -= config.indentUnit;
|
|
|
|
} else {
|
|
|
|
if (/^\s*\{\/(template|deltemplate)\b/.test(textAfter)) return 0;
|
|
|
|
if (/^\{(\/|(fallbackmsg|elseif|else|ifempty)\b)/.test(textAfter)) indent -= config.indentUnit;
|
|
|
|
if (state.tag != "switch" && /^\{(case|default)\b/.test(textAfter)) indent -= config.indentUnit;
|
|
|
|
if (/^\{\/switch\b/.test(textAfter)) indent -= config.indentUnit;
|
|
|
|
}
|
2017-03-21 15:03:16 +00:00
|
|
|
var localState = last(state.localStates);
|
|
|
|
if (indent && localState.mode.indent) {
|
|
|
|
indent += localState.mode.indent(localState.state, textAfter);
|
|
|
|
}
|
2015-03-13 09:58:00 +00:00
|
|
|
return indent;
|
|
|
|
},
|
|
|
|
|
|
|
|
innerMode: function(state) {
|
|
|
|
if (state.soyState.length && last(state.soyState) != "literal") return null;
|
2017-03-21 15:03:16 +00:00
|
|
|
else return last(state.localStates);
|
2015-03-13 09:58:00 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
electricInput: /^\s*\{(\/|\/template|\/deltemplate|\/switch|fallbackmsg|elseif|else|case|default|ifempty|\/literal\})$/,
|
|
|
|
lineComment: "//",
|
|
|
|
blockCommentStart: "/*",
|
|
|
|
blockCommentEnd: "*/",
|
|
|
|
blockCommentContinue: " * ",
|
2017-03-21 15:03:16 +00:00
|
|
|
useInnerComments: false,
|
2015-03-13 09:58:00 +00:00
|
|
|
fold: "indent"
|
|
|
|
};
|
|
|
|
}, "htmlmixed");
|
|
|
|
|
|
|
|
CodeMirror.registerHelper("hintWords", "soy", indentingTags.concat(
|
|
|
|
["delpackage", "namespace", "alias", "print", "css", "debugger"]));
|
|
|
|
|
|
|
|
CodeMirror.defineMIME("text/x-soy", "soy");
|
|
|
|
});
|