Skip to content

Commit b4b2e6e

Browse files
authored
Fix for loop highlighting #211 (#212)
1 parent 2ca894f commit b4b2e6e

File tree

6 files changed

+176
-44
lines changed

6 files changed

+176
-44
lines changed

Diff for: grammars/MagicPython.cson

+2-2
Original file line numberDiff line numberDiff line change
@@ -1330,11 +1330,11 @@ repository:
13301330
correctly identify the "in" as a control flow keyword.
13311331
13321332
'''
1333-
begin: "for"
1333+
begin: "\\bfor\\b"
13341334
beginCaptures:
13351335
"0":
13361336
name: "keyword.control.flow.python"
1337-
end: "in"
1337+
end: "\\bin\\b"
13381338
endCaptures:
13391339
"0":
13401340
name: "keyword.control.flow.python"

Diff for: grammars/MagicPython.tmLanguage

+2-2
Original file line numberDiff line numberDiff line change
@@ -2061,7 +2061,7 @@ E.g. "arr[idx](args)"
20612061
correctly identify the "in" as a control flow keyword.
20622062
</string>
20632063
<key>begin</key>
2064-
<string>for</string>
2064+
<string>\bfor\b</string>
20652065
<key>beginCaptures</key>
20662066
<dict>
20672067
<key>0</key>
@@ -2071,7 +2071,7 @@ correctly identify the "in" as a control flow keyword.
20712071
</dict>
20722072
</dict>
20732073
<key>end</key>
2074-
<string>in</string>
2074+
<string>\bin\b</string>
20752075
<key>endCaptures</key>
20762076
<dict>
20772077
<key>0</key>

Diff for: grammars/src/MagicPython.syntax.yaml

+2-2
Original file line numberDiff line numberDiff line change
@@ -1046,10 +1046,10 @@ repository:
10461046
comment: |
10471047
Match "for ... in" construct used in generators and for loops to
10481048
correctly identify the "in" as a control flow keyword.
1049-
begin: for
1049+
begin: \bfor\b
10501050
beginCaptures:
10511051
'0': {name: keyword.control.flow.python}
1052-
end: in
1052+
end: \bin\b
10531053
endCaptures:
10541054
'0': {name: keyword.control.flow.python}
10551055
patterns:

Diff for: test/atom-spec/python-spec.js

+122-37
Original file line numberDiff line numberDiff line change
@@ -12192,11 +12192,90 @@ describe("Grammar Tests", function() {
1219212192

1219312193
it("test/statements/for2.py",
1219412194
function() {
12195-
tokens = grammar.tokenizeLines("for a, b, c in [2 in q, 2 in w]:\n pass")
12196-
expect(tokens[0][0].value).toBe("for");
12197-
expect(tokens[0][0].scopes).toEqual(["source.python","keyword.control.flow.python"]);
12195+
tokens = grammar.tokenizeLines("forvariable = None\nfor a, b, c, invariable in [2 in q, 2 in w]:\n pass")
12196+
expect(tokens[0][0].value).toBe("forvariable");
12197+
expect(tokens[0][0].scopes).toEqual(["source.python"]);
1219812198
expect(tokens[0][1].value).toBe(" ");
1219912199
expect(tokens[0][1].scopes).toEqual(["source.python"]);
12200+
expect(tokens[0][2].value).toBe("=");
12201+
expect(tokens[0][2].scopes).toEqual(["source.python","keyword.operator.assignment.python"]);
12202+
expect(tokens[0][3].value).toBe(" ");
12203+
expect(tokens[0][3].scopes).toEqual(["source.python"]);
12204+
expect(tokens[0][4].value).toBe("None");
12205+
expect(tokens[0][4].scopes).toEqual(["source.python","constant.language.python"]);
12206+
expect(tokens[1][0].value).toBe("for");
12207+
expect(tokens[1][0].scopes).toEqual(["source.python","keyword.control.flow.python"]);
12208+
expect(tokens[1][1].value).toBe(" ");
12209+
expect(tokens[1][1].scopes).toEqual(["source.python"]);
12210+
expect(tokens[1][2].value).toBe("a");
12211+
expect(tokens[1][2].scopes).toEqual(["source.python"]);
12212+
expect(tokens[1][3].value).toBe(",");
12213+
expect(tokens[1][3].scopes).toEqual(["source.python","punctuation.separator.element.python"]);
12214+
expect(tokens[1][4].value).toBe(" ");
12215+
expect(tokens[1][4].scopes).toEqual(["source.python"]);
12216+
expect(tokens[1][5].value).toBe("b");
12217+
expect(tokens[1][5].scopes).toEqual(["source.python"]);
12218+
expect(tokens[1][6].value).toBe(",");
12219+
expect(tokens[1][6].scopes).toEqual(["source.python","punctuation.separator.element.python"]);
12220+
expect(tokens[1][7].value).toBe(" ");
12221+
expect(tokens[1][7].scopes).toEqual(["source.python"]);
12222+
expect(tokens[1][8].value).toBe("c");
12223+
expect(tokens[1][8].scopes).toEqual(["source.python"]);
12224+
expect(tokens[1][9].value).toBe(",");
12225+
expect(tokens[1][9].scopes).toEqual(["source.python","punctuation.separator.element.python"]);
12226+
expect(tokens[1][10].value).toBe(" ");
12227+
expect(tokens[1][10].scopes).toEqual(["source.python"]);
12228+
expect(tokens[1][11].value).toBe("invariable");
12229+
expect(tokens[1][11].scopes).toEqual(["source.python"]);
12230+
expect(tokens[1][12].value).toBe(" ");
12231+
expect(tokens[1][12].scopes).toEqual(["source.python"]);
12232+
expect(tokens[1][13].value).toBe("in");
12233+
expect(tokens[1][13].scopes).toEqual(["source.python","keyword.control.flow.python"]);
12234+
expect(tokens[1][14].value).toBe(" ");
12235+
expect(tokens[1][14].scopes).toEqual(["source.python"]);
12236+
expect(tokens[1][15].value).toBe("[");
12237+
expect(tokens[1][15].scopes).toEqual(["source.python","punctuation.definition.list.begin.python"]);
12238+
expect(tokens[1][16].value).toBe("2");
12239+
expect(tokens[1][16].scopes).toEqual(["source.python","constant.numeric.dec.python"]);
12240+
expect(tokens[1][17].value).toBe(" ");
12241+
expect(tokens[1][17].scopes).toEqual(["source.python"]);
12242+
expect(tokens[1][18].value).toBe("in");
12243+
expect(tokens[1][18].scopes).toEqual(["source.python","keyword.operator.logical.python"]);
12244+
expect(tokens[1][19].value).toBe(" ");
12245+
expect(tokens[1][19].scopes).toEqual(["source.python"]);
12246+
expect(tokens[1][20].value).toBe("q");
12247+
expect(tokens[1][20].scopes).toEqual(["source.python"]);
12248+
expect(tokens[1][21].value).toBe(",");
12249+
expect(tokens[1][21].scopes).toEqual(["source.python","punctuation.separator.element.python"]);
12250+
expect(tokens[1][22].value).toBe(" ");
12251+
expect(tokens[1][22].scopes).toEqual(["source.python"]);
12252+
expect(tokens[1][23].value).toBe("2");
12253+
expect(tokens[1][23].scopes).toEqual(["source.python","constant.numeric.dec.python"]);
12254+
expect(tokens[1][24].value).toBe(" ");
12255+
expect(tokens[1][24].scopes).toEqual(["source.python"]);
12256+
expect(tokens[1][25].value).toBe("in");
12257+
expect(tokens[1][25].scopes).toEqual(["source.python","keyword.operator.logical.python"]);
12258+
expect(tokens[1][26].value).toBe(" ");
12259+
expect(tokens[1][26].scopes).toEqual(["source.python"]);
12260+
expect(tokens[1][27].value).toBe("w");
12261+
expect(tokens[1][27].scopes).toEqual(["source.python"]);
12262+
expect(tokens[1][28].value).toBe("]");
12263+
expect(tokens[1][28].scopes).toEqual(["source.python","punctuation.definition.list.end.python"]);
12264+
expect(tokens[1][29].value).toBe(":");
12265+
expect(tokens[1][29].scopes).toEqual(["source.python","punctuation.separator.colon.python"]);
12266+
expect(tokens[2][0].value).toBe(" ");
12267+
expect(tokens[2][0].scopes).toEqual(["source.python"]);
12268+
expect(tokens[2][1].value).toBe("pass");
12269+
expect(tokens[2][1].scopes).toEqual(["source.python","keyword.control.flow.python"]);
12270+
});
12271+
12272+
it("test/statements/for3.py",
12273+
function() {
12274+
tokens = grammar.tokenizeLines("for(a, b), c, invariable in[2 in q, 2 in w]:\n pass")
12275+
expect(tokens[0][0].value).toBe("for");
12276+
expect(tokens[0][0].scopes).toEqual(["source.python","keyword.control.flow.python"]);
12277+
expect(tokens[0][1].value).toBe("(");
12278+
expect(tokens[0][1].scopes).toEqual(["source.python","punctuation.parenthesis.begin.python"]);
1220012279
expect(tokens[0][2].value).toBe("a");
1220112280
expect(tokens[0][2].scopes).toEqual(["source.python"]);
1220212281
expect(tokens[0][3].value).toBe(",");
@@ -12205,48 +12284,54 @@ describe("Grammar Tests", function() {
1220512284
expect(tokens[0][4].scopes).toEqual(["source.python"]);
1220612285
expect(tokens[0][5].value).toBe("b");
1220712286
expect(tokens[0][5].scopes).toEqual(["source.python"]);
12208-
expect(tokens[0][6].value).toBe(",");
12209-
expect(tokens[0][6].scopes).toEqual(["source.python","punctuation.separator.element.python"]);
12210-
expect(tokens[0][7].value).toBe(" ");
12211-
expect(tokens[0][7].scopes).toEqual(["source.python"]);
12212-
expect(tokens[0][8].value).toBe("c");
12287+
expect(tokens[0][6].value).toBe(")");
12288+
expect(tokens[0][6].scopes).toEqual(["source.python","punctuation.parenthesis.end.python"]);
12289+
expect(tokens[0][7].value).toBe(",");
12290+
expect(tokens[0][7].scopes).toEqual(["source.python","punctuation.separator.element.python"]);
12291+
expect(tokens[0][8].value).toBe(" ");
1221312292
expect(tokens[0][8].scopes).toEqual(["source.python"]);
12214-
expect(tokens[0][9].value).toBe(" ");
12293+
expect(tokens[0][9].value).toBe("c");
1221512294
expect(tokens[0][9].scopes).toEqual(["source.python"]);
12216-
expect(tokens[0][10].value).toBe("in");
12217-
expect(tokens[0][10].scopes).toEqual(["source.python","keyword.control.flow.python"]);
12295+
expect(tokens[0][10].value).toBe(",");
12296+
expect(tokens[0][10].scopes).toEqual(["source.python","punctuation.separator.element.python"]);
1221812297
expect(tokens[0][11].value).toBe(" ");
1221912298
expect(tokens[0][11].scopes).toEqual(["source.python"]);
12220-
expect(tokens[0][12].value).toBe("[");
12221-
expect(tokens[0][12].scopes).toEqual(["source.python","punctuation.definition.list.begin.python"]);
12222-
expect(tokens[0][13].value).toBe("2");
12223-
expect(tokens[0][13].scopes).toEqual(["source.python","constant.numeric.dec.python"]);
12224-
expect(tokens[0][14].value).toBe(" ");
12225-
expect(tokens[0][14].scopes).toEqual(["source.python"]);
12226-
expect(tokens[0][15].value).toBe("in");
12227-
expect(tokens[0][15].scopes).toEqual(["source.python","keyword.operator.logical.python"]);
12228-
expect(tokens[0][16].value).toBe(" ");
12229-
expect(tokens[0][16].scopes).toEqual(["source.python"]);
12230-
expect(tokens[0][17].value).toBe("q");
12299+
expect(tokens[0][12].value).toBe("invariable");
12300+
expect(tokens[0][12].scopes).toEqual(["source.python"]);
12301+
expect(tokens[0][13].value).toBe(" ");
12302+
expect(tokens[0][13].scopes).toEqual(["source.python"]);
12303+
expect(tokens[0][14].value).toBe("in");
12304+
expect(tokens[0][14].scopes).toEqual(["source.python","keyword.control.flow.python"]);
12305+
expect(tokens[0][15].value).toBe("[");
12306+
expect(tokens[0][15].scopes).toEqual(["source.python","punctuation.definition.list.begin.python"]);
12307+
expect(tokens[0][16].value).toBe("2");
12308+
expect(tokens[0][16].scopes).toEqual(["source.python","constant.numeric.dec.python"]);
12309+
expect(tokens[0][17].value).toBe(" ");
1223112310
expect(tokens[0][17].scopes).toEqual(["source.python"]);
12232-
expect(tokens[0][18].value).toBe(",");
12233-
expect(tokens[0][18].scopes).toEqual(["source.python","punctuation.separator.element.python"]);
12311+
expect(tokens[0][18].value).toBe("in");
12312+
expect(tokens[0][18].scopes).toEqual(["source.python","keyword.operator.logical.python"]);
1223412313
expect(tokens[0][19].value).toBe(" ");
1223512314
expect(tokens[0][19].scopes).toEqual(["source.python"]);
12236-
expect(tokens[0][20].value).toBe("2");
12237-
expect(tokens[0][20].scopes).toEqual(["source.python","constant.numeric.dec.python"]);
12238-
expect(tokens[0][21].value).toBe(" ");
12239-
expect(tokens[0][21].scopes).toEqual(["source.python"]);
12240-
expect(tokens[0][22].value).toBe("in");
12241-
expect(tokens[0][22].scopes).toEqual(["source.python","keyword.operator.logical.python"]);
12242-
expect(tokens[0][23].value).toBe(" ");
12243-
expect(tokens[0][23].scopes).toEqual(["source.python"]);
12244-
expect(tokens[0][24].value).toBe("w");
12315+
expect(tokens[0][20].value).toBe("q");
12316+
expect(tokens[0][20].scopes).toEqual(["source.python"]);
12317+
expect(tokens[0][21].value).toBe(",");
12318+
expect(tokens[0][21].scopes).toEqual(["source.python","punctuation.separator.element.python"]);
12319+
expect(tokens[0][22].value).toBe(" ");
12320+
expect(tokens[0][22].scopes).toEqual(["source.python"]);
12321+
expect(tokens[0][23].value).toBe("2");
12322+
expect(tokens[0][23].scopes).toEqual(["source.python","constant.numeric.dec.python"]);
12323+
expect(tokens[0][24].value).toBe(" ");
1224512324
expect(tokens[0][24].scopes).toEqual(["source.python"]);
12246-
expect(tokens[0][25].value).toBe("]");
12247-
expect(tokens[0][25].scopes).toEqual(["source.python","punctuation.definition.list.end.python"]);
12248-
expect(tokens[0][26].value).toBe(":");
12249-
expect(tokens[0][26].scopes).toEqual(["source.python","punctuation.separator.colon.python"]);
12325+
expect(tokens[0][25].value).toBe("in");
12326+
expect(tokens[0][25].scopes).toEqual(["source.python","keyword.operator.logical.python"]);
12327+
expect(tokens[0][26].value).toBe(" ");
12328+
expect(tokens[0][26].scopes).toEqual(["source.python"]);
12329+
expect(tokens[0][27].value).toBe("w");
12330+
expect(tokens[0][27].scopes).toEqual(["source.python"]);
12331+
expect(tokens[0][28].value).toBe("]");
12332+
expect(tokens[0][28].scopes).toEqual(["source.python","punctuation.definition.list.end.python"]);
12333+
expect(tokens[0][29].value).toBe(":");
12334+
expect(tokens[0][29].scopes).toEqual(["source.python","punctuation.separator.colon.python"]);
1225012335
expect(tokens[1][0].value).toBe(" ");
1225112336
expect(tokens[1][0].scopes).toEqual(["source.python"]);
1225212337
expect(tokens[1][1].value).toBe("pass");

Diff for: test/statements/for2.py

+10-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
1-
for a, b, c in [2 in q, 2 in w]:
1+
forvariable = None
2+
for a, b, c, invariable in [2 in q, 2 in w]:
23
pass
34

45

56

67

8+
forvariable : source.python
9+
: source.python
10+
= : keyword.operator.assignment.python, source.python
11+
: source.python
12+
None : constant.language.python, source.python
713
for : keyword.control.flow.python, source.python
814
: source.python
915
a : source.python
@@ -13,6 +19,9 @@
1319
, : punctuation.separator.element.python, source.python
1420
: source.python
1521
c : source.python
22+
, : punctuation.separator.element.python, source.python
23+
: source.python
24+
invariable : source.python
1625
: source.python
1726
in : keyword.control.flow.python, source.python
1827
: source.python

Diff for: test/statements/for3.py

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
for(a, b), c, invariable in[2 in q, 2 in w]:
2+
pass
3+
4+
5+
6+
7+
for : keyword.control.flow.python, source.python
8+
( : punctuation.parenthesis.begin.python, source.python
9+
a : source.python
10+
, : punctuation.separator.element.python, source.python
11+
: source.python
12+
b : source.python
13+
) : punctuation.parenthesis.end.python, source.python
14+
, : punctuation.separator.element.python, source.python
15+
: source.python
16+
c : source.python
17+
, : punctuation.separator.element.python, source.python
18+
: source.python
19+
invariable : source.python
20+
: source.python
21+
in : keyword.control.flow.python, source.python
22+
[ : punctuation.definition.list.begin.python, source.python
23+
2 : constant.numeric.dec.python, source.python
24+
: source.python
25+
in : keyword.operator.logical.python, source.python
26+
: source.python
27+
q : source.python
28+
, : punctuation.separator.element.python, source.python
29+
: source.python
30+
2 : constant.numeric.dec.python, source.python
31+
: source.python
32+
in : keyword.operator.logical.python, source.python
33+
: source.python
34+
w : source.python
35+
] : punctuation.definition.list.end.python, source.python
36+
: : punctuation.separator.colon.python, source.python
37+
: source.python
38+
pass : keyword.control.flow.python, source.python

0 commit comments

Comments
 (0)