Skip to content

Commit b7460f6

Browse files
authored
[jinja2 mode] Support line statements
1 parent 2114147 commit b7460f6

File tree

2 files changed

+68
-11
lines changed

2 files changed

+68
-11
lines changed

mode/jinja2/index.html

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,16 @@ <h2>Jinja2 mode</h2>
4545
})
4646
}}
4747
{% include (&#39;test.incl.html.twig&#39;) %}
48+
49+
# for item in seq:
50+
&lt;li&gt;{{ item }} &lt;/li&gt; ## this comment is ignored
51+
# endfor
52+
# set text = "Multiline
53+
text"
54+
# for href, caption in [('index.html', 'Index'),
55+
('about.html', 'About')]:
56+
&lt;li&gt;&lt;a href="{{ href }}"&gt;{{ caption }}&lt;/a&gt;&lt;/li&gt;
57+
# endfor
4858
</textarea></form>
4959
<script>
5060
var editor =

mode/jinja2/jinja2.js

Lines changed: 58 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,18 @@
1313

1414
CodeMirror.defineMode("jinja2", function() {
1515
var keywords = ["and", "as", "block", "endblock", "by", "cycle", "debug", "else", "elif",
16-
"extends", "filter", "endfilter", "firstof", "for",
16+
"extends", "filter", "endfilter", "firstof", "do", "for",
1717
"endfor", "if", "endif", "ifchanged", "endifchanged",
18-
"ifequal", "endifequal", "ifnotequal",
18+
"ifequal", "endifequal", "ifnotequal", "set", "raw", "endraw",
1919
"endifnotequal", "in", "include", "load", "not", "now", "or",
20-
"parsed", "regroup", "reversed", "spaceless",
21-
"endspaceless", "ssi", "templatetag", "openblock",
22-
"closeblock", "openvariable", "closevariable",
20+
"parsed", "regroup", "reversed", "spaceless", "call", "endcall", "macro",
21+
"endmacro", "endspaceless", "ssi", "templatetag", "openblock",
22+
"closeblock", "openvariable", "closevariable", "without", "context",
2323
"openbrace", "closebrace", "opencomment",
2424
"closecomment", "widthratio", "url", "with", "endwith",
2525
"get_current_language", "trans", "endtrans", "noop", "blocktrans",
2626
"endblocktrans", "get_available_languages",
27-
"get_current_language_bidi", "plural"],
27+
"get_current_language_bidi", "pluralize", "autoescape", "endautoescape"],
2828
operator = /^[+\-*&%=<>!?|~^]/,
2929
sign = /^[:\[\(\{]/,
3030
atom = ["true", "false"],
@@ -78,7 +78,24 @@
7878
state.instring = ch;
7979
stream.next();
8080
return "string";
81-
} else if(stream.match(state.intag + "}") || stream.eat("-") && stream.match(state.intag + "}")) {
81+
}
82+
else if (state.inbraces > 0 && ch ==")") {
83+
stream.next()
84+
state.inbraces--;
85+
}
86+
else if (ch == "(") {
87+
stream.next()
88+
state.inbraces++;
89+
}
90+
else if (state.inbrackets > 0 && ch =="]") {
91+
stream.next()
92+
state.inbrackets--;
93+
}
94+
else if (ch == "[") {
95+
stream.next()
96+
state.inbrackets++;
97+
}
98+
else if (!state.lineTag && (stream.match(state.intag + "}") || stream.eat("-") && stream.match(state.intag + "}"))) {
8299
state.intag = false;
83100
return "tag";
84101
} else if(stream.match(operator)) {
@@ -87,6 +104,10 @@
87104
} else if(stream.match(sign)) {
88105
state.sign = true;
89106
} else {
107+
if (stream.column() == 1 && state.lineTag && stream.match(keywords)) {
108+
//allow nospace after tag before the keyword
109+
return "keyword";
110+
}
90111
if(stream.eat(" ") || stream.sol()) {
91112
if(stream.match(keywords)) {
92113
return "keyword";
@@ -120,25 +141,51 @@
120141
} else if (ch = stream.eat(/\{|%/)) {
121142
//Cache close tag
122143
state.intag = ch;
144+
state.inbraces = 0;
145+
state.inbrackets = 0;
123146
if(ch == "{") {
124147
state.intag = "}";
125148
}
126149
stream.eat("-");
127150
return "tag";
128151
}
152+
//Line statements
153+
} else if (stream.eat('#')) {
154+
if (stream.peek() == '#') {
155+
stream.skipToEnd();
156+
return "comment"
157+
}
158+
else if (!stream.eol()) {
159+
state.intag = true;
160+
state.lineTag = true;
161+
state.inbraces = 0;
162+
state.inbrackets = 0;
163+
return "tag";
164+
}
129165
}
130166
stream.next();
131167
};
132168

133169
return {
134170
startState: function () {
135-
return {tokenize: tokenBase};
171+
return {
172+
tokenize: tokenBase,
173+
inbrackets:0,
174+
inbraces:0
175+
};
136176
},
137-
token: function (stream, state) {
138-
return state.tokenize(stream, state);
177+
token: function(stream, state) {
178+
var style = state.tokenize(stream, state);
179+
if (stream.eol() && state.lineTag && !state.instring && state.inbraces == 0 && state.inbrackets == 0) {
180+
//Close line statement at the EOL
181+
state.intag = false
182+
state.lineTag = false
183+
}
184+
return style;
139185
},
140186
blockCommentStart: "{#",
141-
blockCommentEnd: "#}"
187+
blockCommentEnd: "#}",
188+
lineComment: "##",
142189
};
143190
});
144191

0 commit comments

Comments
 (0)