Skip to content

Commit c96aae3

Browse files
Axel Lewenhauptmarijnh
authored andcommitted
[soy mode] Fix indentation and restore previous local states
1 parent 2f43bcc commit c96aae3

File tree

2 files changed

+46
-13
lines changed

2 files changed

+46
-13
lines changed

mode/soy/soy.js

Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,12 @@
3131
}
3232

3333
function tokenUntil(stream, state, untilRegExp) {
34+
if (stream.sol()) {
35+
for (var indent = 0; indent < state.indent; indent++) {
36+
if (!stream.eat(/\s/)) break;
37+
}
38+
if (indent) return null;
39+
}
3440
var oldString = stream.string;
3541
var match = untilRegExp.exec(oldString.substr(stream.pos));
3642
if (match) {
@@ -39,7 +45,8 @@
3945
stream.string = oldString.substr(0, stream.pos + match.index);
4046
}
4147
var result = stream.hideFirstChars(state.indent, function() {
42-
return state.localMode.token(stream, state.localState);
48+
var localState = last(state.localStates);
49+
return localState.mode.token(stream, localState.state);
4350
});
4451
stream.string = oldString;
4552
return result;
@@ -83,8 +90,10 @@
8390
variables: null,
8491
scopes: null,
8592
indent: 0,
86-
localMode: modes.html,
87-
localState: CodeMirror.startState(modes.html)
93+
localStates: [{
94+
mode: modes.html,
95+
state: CodeMirror.startState(modes.html)
96+
}]
8897
};
8998
},
9099

@@ -98,8 +107,12 @@
98107
variables: state.variables,
99108
scopes: state.scopes,
100109
indent: state.indent, // Indentation of the following line.
101-
localMode: state.localMode,
102-
localState: CodeMirror.copyState(state.localMode, state.localState)
110+
localStates: state.localStates.map(function(localState) {
111+
return {
112+
mode: localState.mode,
113+
state: CodeMirror.copyState(localState.mode, localState.state)
114+
};
115+
})
103116
};
104117
},
105118

@@ -187,8 +200,13 @@
187200
var kind = match[1];
188201
state.kind.push(kind);
189202
state.kindTag.push(state.tag);
190-
state.localMode = modes[kind] || modes.html;
191-
state.localState = CodeMirror.startState(state.localMode);
203+
var mode = modes[kind] || modes.html;
204+
var localState = last(state.localStates);
205+
state.indent += localState.mode.indent(localState.state, "");
206+
state.localStates.push({
207+
mode: mode,
208+
state: CodeMirror.startState(mode)
209+
});
192210
}
193211
return "attribute";
194212
} else if (stream.match(/^"/)) {
@@ -233,14 +251,15 @@
233251
return "keyword";
234252
} else if (match = stream.match(/^\{([\/@\\]?[\w?]*)/)) {
235253
if (match[1] != "/switch")
236-
state.indent += (/^(\/|(else|elseif|ifempty|case|default)$)/.test(match[1]) && state.tag != "switch" ? 1 : 2) * config.indentUnit;
254+
state.indent += (/^(\/|(else|elseif|ifempty|case|fallbackmsg|default)$)/.test(match[1]) && state.tag != "switch" ? 1 : 2) * config.indentUnit;
237255
state.tag = match[1];
238256
if (state.tag == "/" + last(state.kindTag)) {
239257
// We found the tag that opened the current kind="".
240258
state.kind.pop();
241259
state.kindTag.pop();
242-
state.localMode = modes[last(state.kind)] || modes.html;
243-
state.localState = CodeMirror.startState(state.localMode);
260+
state.localStates.pop();
261+
var localState = last(state.localStates);
262+
state.indent -= localState.mode.indent(localState.state, "");
244263
}
245264
state.soyState.push("tag");
246265
if (state.tag == "template" || state.tag == "deltemplate") {
@@ -277,14 +296,16 @@
277296
if (state.tag != "switch" && /^\{(case|default)\b/.test(textAfter)) indent -= config.indentUnit;
278297
if (/^\{\/switch\b/.test(textAfter)) indent -= config.indentUnit;
279298
}
280-
if (indent && state.localMode.indent)
281-
indent += state.localMode.indent(state.localState, textAfter);
299+
var localState = last(state.localStates);
300+
if (indent && localState.mode.indent) {
301+
indent += localState.mode.indent(localState.state, textAfter);
302+
}
282303
return indent;
283304
},
284305

285306
innerMode: function(state) {
286307
if (state.soyState.length && last(state.soyState) != "literal") return null;
287-
else return {state: state.localState, mode: state.localMode};
308+
else return last(state.localStates);
288309
},
289310

290311
electricInput: /^\s*\{(\/|\/template|\/deltemplate|\/switch|fallbackmsg|elseif|else|case|default|ifempty|\/literal\})$/,

mode/soy/test.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,4 +80,16 @@
8080
' nothing',
8181
'[keyword {/foreach}]',
8282
'');
83+
84+
MT('nested-kind-test',
85+
'[keyword {template] [def .foo] [attribute kind]=[string "html"][keyword }]',
86+
' [tag&bracket <][tag div][tag&bracket >]',
87+
' [keyword {call] [variable .bar][keyword }]',
88+
' [keyword {param] [attribute kind]=[string "js"][keyword }]',
89+
' [keyword var] [def bar] [operator =] [number 5];',
90+
' [keyword {/param}]',
91+
' [keyword {/call}]',
92+
' [tag&bracket </][tag div][tag&bracket >]',
93+
'[keyword {/template}]',
94+
'');
8395
})();

0 commit comments

Comments
 (0)