-
1
-
2
-
3
-
4
-
5
-
6
-
7
-
8
-
9
-
10
-
11
-
12
-
13
-
14
-
15
-
16
-
17
-
18
-
19
-
20
-
21
-
22
-
23
-
24
-
25
-
26
-
27
-
28
-
29
-
30
-
31
-
32
-
33
-
34
-
35
-
36
-
37
-
38
-
39
-
40
-
41
-
42
-
43
-
44
-
45
-
46
-
47
-
48
-
49
-
50
-
51
-
52
-
53
-
54
-
55
-
56
-
57
-
58
-
59
-
60
-
61
-
62
-
63
-
64
-
65
-
66
-
67
-
68
-
69
-
70
-
71
-
72
-
73
-
74
-
75
-
76
-
77
-
78
-
79
-
80
-
81
-
82
-
83
-
84
-
85
-
86
-
87
-
88
-
89
-
90
-
91
-
92
-
93
-
94
-
95
-
96
-
97
-
98
-
99
-
100
-
101
-
102
-
103
-
104
-
105
-
106
-
107
-
108
-
109
-
110
-
111
-
112
-
113
-
114
-
115
-
116
-
117
-
118
-
119
-
120
-
121
-
122
-
123
-
124
-
125
-
126
-
127
-
128
-
129
-
130
-
131
-
132
-
133
-
134
-
135
-
136
-
137
-
138
-
139
-
140
-
141
-
142
-
143
-
144
-
145
-
146
-
147
-
148
-
149
-
150
-
151
-
152
-
153
-
154
-
155
-
156
-
157
-
158
-
159
-
160
-
161
-
162
-
163
-
164
-
165
-
166
-
167
-
168
-
169
-
170
-
171
-
172
-
173
-
174
-
175
-
176
-
177
-
178
-
179
-
180
-
181
-
182
-
183
-
184
-
185
-
186
-
187
-
188
-
189
-
190
-
191
-
192
-
193
-
194
-
195
-
196
-
197
-
198
-
199
-
200
-
201
-
202
-
203
-
204
-
205
-
206
-
207
-
208
-
209
-
210
-
211
-
212
-
213
-
214
-
215
-
216
-
217
-
218
-
219
-
220
-
221
-
222
-
223
-
224
-
225
-
226
-
227
-
228
-
229
-
230
-
231
-
232
-
233
-
234
-
235
-
236
-
237
-
238
-
239
-
240
-
241
-
242
-
243
-
244
-
245
-
246
-
247
-
248
-
249
-
250
-
251
-
252
-
253
-
254
-
255
-
256
-
257
-
258
-
259
-
260
-
261
-
262
-
263
-
264
-
265
-
266
-
267
-
268
-
269
-
270
-
271
-
272
-
273
-
274
-
275
-
276
-
277
-
278
-
279
-
280
-
281
-
282
-
283
-
284
-
285
-
286
-
287
-
288
-
289
-
290
-
291
-
292
-
293
-
294
-
295
-
296
-
297
-
298
-
299
-
300
-
301
-
302
-
303
-
304
-
305
-
306
-
307
-
308
-
309
-
310
-
311
-
312
-
313
-
314
-
315
-
316
-
317
-
318
-
319
-
320
-
321
-
322
-
323
-
324
-
325
-
326
-
327
-
328
-
329
-
330
-
331
-
332
-
333
-
334
-
335
-
336
-
337
-
338
-
339
-
340
-
341
-
342
-
343
-
344
-
345
-
346
-
347
-
348
-
349
-
350
-
351
-
352
-
353
-
354
-
355
-
356
-
357
-
358
-
359
-
360
-
361
-
362
-
363
-
364
-
365
-
366
-
367
-
368
-
369
-
370
-
371
-
372
-
373
-
374
-
375
-
376
-
377
-
378
-
379
-
380
-
381
-
382
-
383
-
384
-
385
-
386
-
387
-
388
-
389
-
390
-
391
-
392
-
393
-
394
-
395
-
396
-
397
-
398
-
399
-
400
-
401
-
402
-
403
-
404
-
405
-
406
-
407
-
408
-
409
-
410
-
411
-
412
-
413
-
414
-
415
-
416
-
417
-
418
-
419
-
420
-
421
-
422
-
423
-
424
-
425
-
426
-
427
-
428
-
429
-
430
-
431
-
432
-
433
-
434
-
435
-
436
-
437
// CodeMirror, copyright (c) by Marijn Haverbeke and others
// Distributed under an MIT license: https://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 paramData = { noEndTag: true, soyState: "param-def" };
var tags = {
"alias": { noEndTag: true },
"delpackage": { noEndTag: true },
"namespace": { noEndTag: true, soyState: "namespace-def" },
"@param": paramData,
"@param?": paramData,
"@inject": paramData,
"@inject?": paramData,
"@state": paramData,
"@state?": paramData,
"template": { soyState: "templ-def", variableScope: true},
"literal": { },
"msg": {},
"fallbackmsg": { noEndTag: true, reduceIndent: true},
"select": {},
"plural": {},
"let": { soyState: "var-def" },
"if": {},
"elseif": { noEndTag: true, reduceIndent: true},
"else": { noEndTag: true, reduceIndent: true},
"switch": {},
"case": { noEndTag: true, reduceIndent: true},
"default": { noEndTag: true, reduceIndent: true},
"foreach": { variableScope: true, soyState: "var-def" },
"ifempty": { noEndTag: true, reduceIndent: true},
"for": { variableScope: true, soyState: "var-def" },
"call": { soyState: "templ-ref" },
"param": { soyState: "param-ref"},
"print": { noEndTag: true },
"deltemplate": { soyState: "templ-def", variableScope: true},
"delcall": { soyState: "templ-ref" },
"log": {},
"element": { variableScope: true },
};
var indentingTags = Object.keys(tags).filter(function(tag) {
return !tags[tag].noEndTag || tags[tag].reduceIndent;
});
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,
trusted_resource_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) {
if (stream.sol()) {
for (var indent = 0; indent < state.indent; indent++) {
if (!stream.eat(/\s/)) break;
}
if (indent) return null;
}
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() {
var localState = last(state.localStates);
return localState.mode.token(stream, localState.state);
});
stream.string = oldString;
return result;
}
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
};
}
function popcontext(state) {
if (!state.context) return;
if (state.context.scope) {
state.variables = state.context.scope;
}
state.context = state.context.previousContext;
}
// 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");
}
// Data for an open soy tag.
function Context(previousContext, tag, scope) {
this.previousContext = previousContext;
this.tag = tag;
this.kind = null;
this.scope = scope;
}
return {
startState: function() {
return {
soyState: [],
templates: null,
variables: prepend(null, 'ij'),
scopes: null,
indent: 0,
quoteKind: null,
context: null,
localStates: [{
mode: modes.html,
state: CodeMirror.startState(modes.html)
}]
};
},
copyState: function(state) {
return {
tag: state.tag, // Last seen Soy tag.
soyState: state.soyState.concat([]),
templates: state.templates,
variables: state.variables,
context: state.context,
indent: state.indent, // Indentation of the following line.
quoteKind: state.quoteKind,
localStates: state.localStates.map(function(localState) {
return {
mode: localState.mode,
state: CodeMirror.copyState(localState.mode, localState.state)
};
})
};
},
token: function(stream, state) {
var match;
switch (last(state.soyState)) {
case "comment":
if (stream.match(/^.*?\*\//)) {
state.soyState.pop();
} else {
stream.skipToEnd();
}
if (!state.context || !state.context.scope) {
var paramRe = /@param\??\s+(\S+)/g;
var current = stream.current();
for (var match; (match = paramRe.exec(current)); ) {
state.variables = prepend(state.variables, match[1]);
}
}
return "comment";
case "string":
var match = stream.match(/^.*?(["']|\\[\s\S])/);
if (!match) {
stream.skipToEnd();
} else if (match[1] == state.quoteKind) {
state.quoteKind = null;
state.soyState.pop();
}
return "string";
}
if (!state.soyState.length || last(state.soyState) != "literal") {
if (stream.match(/^\/\*/)) {
state.soyState.push("comment");
return "comment";
} else if (stream.match(stream.sol() ? /^\s*\/\/.*/ : /^\s+\/\/.*/)) {
return "comment";
}
}
switch (last(state.soyState)) {
case "templ-def":
if (match = stream.match(/^\.?([\w]+(?!\.[\w]+)*)/)) {
state.templates = prepend(state.templates, match[1]);
state.soyState.pop();
return "def";
}
stream.next();
return null;
case "templ-ref":
if (match = stream.match(/(\.?[a-zA-Z_][a-zA-Z_0-9]+)+/)) {
state.soyState.pop();
// If the first character is '.', it can only be a local template.
if (match[0][0] == '.') {
return "variable-2"
}
// Otherwise
return "variable";
}
stream.next();
return null;
case "namespace-def":
if (match = stream.match(/^\.?([\w\.]+)/)) {
state.soyState.pop();
return "variable";
}
stream.next();
return null;
case "param-def":
if (match = stream.match(/^\w+/)) {
state.variables = prepend(state.variables, match[0]);
state.soyState.pop();
state.soyState.push("param-type");
return "def";
}
stream.next();
return null;
case "param-ref":
if (match = stream.match(/^\w+/)) {
state.soyState.pop();
return "property";
}
stream.next();
return null;
case "param-type":
if (stream.peek() == "}") {
state.soyState.pop();
return null;
}
if (stream.eatWhile(/^([\w]+|[?])/)) {
return "type";
}
stream.next();
return null;
case "var-def":
if (match = stream.match(/^\$([\w]+)/)) {
state.variables = prepend(state.variables, match[1]);
state.soyState.pop();
return "def";
}
stream.next();
return null;
case "tag":
var endTag = state.tag[0] == "/";
var tagName = endTag ? state.tag.substring(1) : state.tag;
var tag = tags[tagName];
if (stream.match(/^\/?}/)) {
var selfClosed = stream.current() == "/}";
if (selfClosed && !endTag) {
popcontext(state);
}
if (state.tag == "/template" || state.tag == "/deltemplate") {
state.variables = prepend(null, 'ij');
state.indent = 0;
} else {
state.indent -= config.indentUnit *
(selfClosed || indentingTags.indexOf(state.tag) == -1 ? 2 : 1);
}
state.soyState.pop();
return "keyword";
} else if (stream.match(/^([\w?]+)(?==)/)) {
if (state.context && state.context.tag == tagName && stream.current() == "kind" && (match = stream.match(/^="([^"]+)/, false))) {
var kind = match[1];
state.context.kind = kind;
var mode = modes[kind] || modes.html;
var localState = last(state.localStates);
if (localState.mode.indent) {
state.indent += localState.mode.indent(localState.state, "", "");
}
state.localStates.push({
mode: mode,
state: CodeMirror.startState(mode)
});
}
return "attribute";
} else if (match = stream.match(/([\w]+)(?=\()/)) {
return "variable callee";
} else if (match = stream.match(/^["']/)) {
state.soyState.push("string");
state.quoteKind = match;
return "string";
}
if (stream.match(/(null|true|false)(?!\w)/) ||
stream.match(/0x([0-9a-fA-F]{2,})/) ||
stream.match(/-?([0-9]*[.])?[0-9]+(e[0-9]*)?/)) {
return "atom";
}
if (stream.match(/(\||[+\-*\/%]|[=!]=|\?:|[<>]=?)/)) {
// Tokenize filter, binary, null propagator, and equality operators.
return "operator";
}
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;
}
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}/);
}
if (stream.match(/^\{literal}/)) {
state.indent += config.indentUnit;
state.soyState.push("literal");
state.context = new Context(state.context, "literal", state.variables);
return "keyword";
// A tag-keyword must be followed by whitespace, comment or a closing tag.
} else if (match = stream.match(/^\{([/@\\]?\w+\??)(?=$|[\s}]|\/[/*])/)) {
var prevTag = state.tag;
state.tag = match[1];
var endTag = state.tag[0] == "/";
var indentingTag = !!tags[state.tag];
var tagName = endTag ? state.tag.substring(1) : state.tag;
var tag = tags[tagName];
if (state.tag != "/switch")
state.indent += ((endTag || tag && tag.reduceIndent) && prevTag != "switch" ? 1 : 2) * config.indentUnit;
state.soyState.push("tag");
var tagError = false;
if (tag) {
if (!endTag) {
if (tag.soyState) state.soyState.push(tag.soyState);
}
// If a new tag, open a new context.
if (!tag.noEndTag && (indentingTag || !endTag)) {
state.context = new Context(state.context, state.tag, tag.variableScope ? state.variables : null);
// Otherwise close the current context.
} else if (endTag) {
if (!state.context || state.context.tag != tagName) {
tagError = true;
} else if (state.context) {
if (state.context.kind) {
state.localStates.pop();
var localState = last(state.localStates);
if (localState.mode.indent) {
state.indent -= localState.mode.indent(localState.state, "", "");
}
}
popcontext(state);
}
}
} else if (endTag) {
// Assume all tags with a closing tag are defined in the config.
tagError = true;
}
return (tagError ? "error " : "") + "keyword";
// 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";
}
return tokenUntil(stream, state, /\{|\s+\/\/|\/\*/);
},
indent: function(state, textAfter, line) {
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;
}
var localState = last(state.localStates);
if (indent && localState.mode.indent) {
indent += localState.mode.indent(localState.state, textAfter, line);
}
return indent;
},
innerMode: function(state) {
if (state.soyState.length && last(state.soyState) != "literal") return null;
else return last(state.localStates);
},
electricInput: /^\s*\{(\/|\/template|\/deltemplate|\/switch|fallbackmsg|elseif|else|case|default|ifempty|\/literal\})$/,
lineComment: "//",
blockCommentStart: "/*",
blockCommentEnd: "*/",
blockCommentContinue: " * ",
useInnerComments: false,
fold: "indent"
};
}, "htmlmixed");
CodeMirror.registerHelper("wordChars", "soy", /[\w$]/);
CodeMirror.registerHelper("hintWords", "soy", Object.keys(tags).concat(
["css", "debugger"]));
CodeMirror.defineMIME("text/x-soy", "soy");
});