Skip to content

Commit e46428f

Browse files
committed
Add distinct scopes for self & cls variables and parameters
1 parent 13a1c69 commit e46428f

File tree

11 files changed

+274
-7
lines changed

11 files changed

+274
-7
lines changed

Diff for: grammars/MagicPython.cson

+35
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,9 @@ repository:
307307
{
308308
include: "#illegal-names"
309309
}
310+
{
311+
include: "#special-variables"
312+
}
310313
{
311314
include: "#ellipsis"
312315
}
@@ -1068,6 +1071,9 @@ repository:
10681071
{
10691072
include: "#illegal-names"
10701073
}
1074+
{
1075+
include: "#parameter-special"
1076+
}
10711077
{
10721078
match: "([[:alpha:]_]\\w*)\\s*(?:(,)|(?=\\)))"
10731079
captures:
@@ -1086,6 +1092,19 @@ repository:
10861092
include: "#annotated-parameter"
10871093
}
10881094
]
1095+
"parameter-special":
1096+
match: '''
1097+
(?x)
1098+
(?: \\b (self)|(cls) \\b) \\s*(?:(,)|(?=\\)))
1099+
1100+
'''
1101+
captures:
1102+
"1":
1103+
name: "variable.parameter.function.language.python variable.parameter.function.language.special.self.python"
1104+
"2":
1105+
name: "variable.parameter.function.language.python variable.parameter.function.language.special.cls.python"
1106+
"3":
1107+
name: "punctuation.separator.parameters.python"
10891108
"parameter-with-default":
10901109
begin: '''
10911110
(?x)
@@ -1313,6 +1332,9 @@ repository:
13131332
"1":
13141333
name: "punctuation.definition.arguments.end.python"
13151334
patterns: [
1335+
{
1336+
include: "#special-variables"
1337+
}
13161338
{
13171339
include: "#function-name"
13181340
}
@@ -1520,6 +1542,19 @@ repository:
15201542
)\\b
15211543
15221544
'''
1545+
"special-variables":
1546+
match: '''
1547+
(?x)
1548+
\\b (?<!\\.) (?:
1549+
(self) | (cls)
1550+
)\\b
1551+
1552+
'''
1553+
captures:
1554+
"1":
1555+
name: "variable.language.special.self.python"
1556+
"2":
1557+
name: "variable.language.special.cls.python"
15231558
"member-access":
15241559
comment: "this rule exists to highlight errors"
15251560
begin: "\\.(?=\\s*\\w+\\s)"

Diff for: grammars/MagicPython.syntax.yaml

+24
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,7 @@ repository:
221221
- include: '#special-names'
222222
- include: '#illegal-operator'
223223
- include: '#illegal-names'
224+
- include: '#special-variables'
224225
- include: '#ellipsis'
225226

226227
special-names:
@@ -728,6 +729,7 @@ repository:
728729
- match: \*
729730
- include: '#lambda-incomplete'
730731
- include: '#illegal-names'
732+
- include: '#parameter-special'
731733
- match: ([[:alpha:]_]\w*)\s*(?:(,)|(?=\)))
732734
captures:
733735
'1': {name: variable.parameter.function.language.python}
@@ -737,6 +739,17 @@ repository:
737739
- include: '#parameter-with-default'
738740
- include: '#annotated-parameter'
739741

742+
parameter-special:
743+
match: |
744+
(?x)
745+
(?: \b (self)|(cls) \b) \s*(?:(,)|(?=\)))
746+
captures:
747+
'1': {name: variable.parameter.function.language.python
748+
variable.parameter.function.language.special.self.python}
749+
'2': {name: variable.parameter.function.language.python
750+
variable.parameter.function.language.special.cls.python}
751+
'3': {name: punctuation.separator.parameters.python}
752+
740753
parameter-with-default:
741754
begin: |
742755
(?x)
@@ -880,6 +893,7 @@ repository:
880893
endCaptures:
881894
'1': {name: punctuation.definition.arguments.end.python}
882895
patterns:
896+
- include: '#special-variables'
883897
- include: '#function-name'
884898
- include: '#function-arguments'
885899

@@ -1034,6 +1048,16 @@ repository:
10341048
| yield
10351049
)\b
10361050
1051+
special-variables:
1052+
match: |
1053+
(?x)
1054+
\b (?<!\.) (?:
1055+
(self) | (cls)
1056+
)\b
1057+
captures:
1058+
'1': {name: variable.language.special.self.python}
1059+
'2': {name: variable.language.special.cls.python}
1060+
10371061
member-access:
10381062
comment: this rule exists to highlight errors
10391063
begin: \.(?=\s*\w+\s)

Diff for: grammars/MagicPython.tmLanguage

+59
Original file line numberDiff line numberDiff line change
@@ -481,6 +481,10 @@
481481
<key>include</key>
482482
<string>#illegal-names</string>
483483
</dict>
484+
<dict>
485+
<key>include</key>
486+
<string>#special-variables</string>
487+
</dict>
484488
<dict>
485489
<key>include</key>
486490
<string>#ellipsis</string>
@@ -1733,6 +1737,10 @@ it&apos;s not tokenized as ellipsis.
17331737
<key>include</key>
17341738
<string>#illegal-names</string>
17351739
</dict>
1740+
<dict>
1741+
<key>include</key>
1742+
<string>#parameter-special</string>
1743+
</dict>
17361744
<dict>
17371745
<key>match</key>
17381746
<string>([[:alpha:]_]\w*)\s*(?:(,)|(?=\)))</string>
@@ -1764,6 +1772,31 @@ it&apos;s not tokenized as ellipsis.
17641772
</dict>
17651773
</array>
17661774
</dict>
1775+
<key>parameter-special</key>
1776+
<dict>
1777+
<key>match</key>
1778+
<string>(?x)
1779+
(?: \b (self)|(cls) \b) \s*(?:(,)|(?=\)))
1780+
</string>
1781+
<key>captures</key>
1782+
<dict>
1783+
<key>1</key>
1784+
<dict>
1785+
<key>name</key>
1786+
<string>variable.parameter.function.language.python variable.parameter.function.language.special.self.python</string>
1787+
</dict>
1788+
<key>2</key>
1789+
<dict>
1790+
<key>name</key>
1791+
<string>variable.parameter.function.language.python variable.parameter.function.language.special.cls.python</string>
1792+
</dict>
1793+
<key>3</key>
1794+
<dict>
1795+
<key>name</key>
1796+
<string>punctuation.separator.parameters.python</string>
1797+
</dict>
1798+
</dict>
1799+
</dict>
17671800
<key>parameter-with-default</key>
17681801
<dict>
17691802
<key>begin</key>
@@ -2124,6 +2157,10 @@ it&apos;s not tokenized as ellipsis.
21242157
</dict>
21252158
<key>patterns</key>
21262159
<array>
2160+
<dict>
2161+
<key>include</key>
2162+
<string>#special-variables</string>
2163+
</dict>
21272164
<dict>
21282165
<key>include</key>
21292166
<string>#function-name</string>
@@ -2406,6 +2443,28 @@ indirectly through syntactic constructs
24062443
)\b
24072444
</string>
24082445
</dict>
2446+
<key>special-variables</key>
2447+
<dict>
2448+
<key>match</key>
2449+
<string>(?x)
2450+
\b (?&lt;!\.) (?:
2451+
(self) | (cls)
2452+
)\b
2453+
</string>
2454+
<key>captures</key>
2455+
<dict>
2456+
<key>1</key>
2457+
<dict>
2458+
<key>name</key>
2459+
<string>variable.language.special.self.python</string>
2460+
</dict>
2461+
<key>2</key>
2462+
<dict>
2463+
<key>name</key>
2464+
<string>variable.language.special.cls.python</string>
2465+
</dict>
2466+
</dict>
2467+
</dict>
24092468
<key>member-access</key>
24102469
<dict>
24112470
<key>comment</key>

Diff for: misc/scopes

+4
Original file line numberDiff line numberDiff line change
@@ -138,5 +138,9 @@ support.type.exception.python
138138
support.type.metaclass.python
139139
support.type.python
140140
support.variable.magic.python
141+
variable.language.special.cls.python
142+
variable.language.special.self.python
141143
variable.parameter.function-call.python
142144
variable.parameter.function.language.python
145+
variable.parameter.function.language.special.cls.python
146+
variable.parameter.function.language.special.self.python

Diff for: test/classes/class11.py

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
class F:
2+
def __init__(self, a, b=1):
3+
self.a = a
4+
self.b = b
5+
print(self)
6+
self()
7+
a.self = 1
8+
a.self.bar = 2
9+
10+
11+
12+
class : meta.class.python, source.python, storage.type.class.python
13+
: meta.class.python, source.python
14+
F : entity.name.type.class.python, meta.class.python, source.python
15+
: : meta.class.python, punctuation.section.class.begin.python, source.python
16+
: source.python
17+
def : meta.function.python, source.python, storage.type.function.python
18+
: meta.function.python, source.python
19+
__init__ : meta.function.python, source.python, support.function.magic.python
20+
( : meta.function.parameters.python, meta.function.python, punctuation.definition.parameters.begin.python, source.python
21+
self : meta.function.parameters.python, meta.function.python, source.python, variable.parameter.function.language.python, variable.parameter.function.language.special.self.python
22+
, : meta.function.parameters.python, meta.function.python, punctuation.separator.parameters.python, source.python
23+
: meta.function.parameters.python, meta.function.python, source.python
24+
a : meta.function.parameters.python, meta.function.python, source.python, variable.parameter.function.language.python
25+
, : meta.function.parameters.python, meta.function.python, punctuation.separator.parameters.python, source.python
26+
: meta.function.parameters.python, meta.function.python, source.python
27+
b : meta.function.parameters.python, meta.function.python, source.python, variable.parameter.function.language.python
28+
= : keyword.operator.python, meta.function.parameters.python, meta.function.python, source.python
29+
1 : constant.numeric.dec.python, meta.function.parameters.python, meta.function.python, source.python
30+
) : meta.function.parameters.python, meta.function.python, punctuation.definition.parameters.end.python, source.python
31+
: : meta.function.python, punctuation.section.function.begin.python, source.python
32+
: source.python
33+
self : source.python, variable.language.special.self.python
34+
. : source.python
35+
a : source.python
36+
: source.python
37+
= : keyword.operator.assignment.python, source.python
38+
a : source.python
39+
: source.python
40+
self : source.python, variable.language.special.self.python
41+
. : source.python
42+
b : source.python
43+
: source.python
44+
= : keyword.operator.assignment.python, source.python
45+
b : source.python
46+
: source.python
47+
print : meta.function-call.python, source.python, support.function.builtin.python
48+
( : meta.function-call.arguments.python, meta.function-call.python, punctuation.definition.arguments.begin.python, source.python
49+
self : meta.function-call.arguments.python, meta.function-call.python, source.python, variable.language.special.self.python
50+
) : meta.function-call.python, punctuation.definition.arguments.end.python, source.python
51+
: source.python
52+
self : meta.function-call.python, source.python, variable.language.special.self.python
53+
( : meta.function-call.arguments.python, meta.function-call.python, punctuation.definition.arguments.begin.python, source.python
54+
) : meta.function-call.python, punctuation.definition.arguments.end.python, source.python
55+
a : source.python
56+
. : source.python
57+
self : source.python
58+
: source.python
59+
= : keyword.operator.assignment.python, source.python
60+
: source.python
61+
1 : constant.numeric.dec.python, source.python
62+
a.self : source.python
63+
. : source.python
64+
bar : source.python
65+
: source.python
66+
= : keyword.operator.assignment.python, source.python
67+
: source.python
68+
2 : constant.numeric.dec.python, source.python

Diff for: test/classes/class12.py

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
class F:
2+
@classmethod
3+
def meth(cls, a, b=1):
4+
cls.a = a
5+
cls.b = b
6+
print(cls)
7+
cls()
8+
cls + 1
9+
a.cls = 1
10+
a.cls.__name__
11+
12+
13+
14+
class : meta.class.python, source.python, storage.type.class.python
15+
: meta.class.python, source.python
16+
F : entity.name.type.class.python, meta.class.python, source.python
17+
: : meta.class.python, punctuation.section.class.begin.python, source.python
18+
: meta.function.decorator.python, source.python
19+
@ : entity.name.function.decorator.python, meta.function.decorator.python, source.python
20+
classmethod : meta.function.decorator.python, source.python, support.type.python
21+
: meta.function.python, source.python
22+
def : meta.function.python, source.python, storage.type.function.python
23+
: meta.function.python, source.python
24+
meth : entity.name.function.python, meta.function.python, source.python
25+
( : meta.function.parameters.python, meta.function.python, punctuation.definition.parameters.begin.python, source.python
26+
cls : meta.function.parameters.python, meta.function.python, source.python, variable.parameter.function.language.python, variable.parameter.function.language.special.cls.python
27+
, : meta.function.parameters.python, meta.function.python, punctuation.separator.parameters.python, source.python
28+
: meta.function.parameters.python, meta.function.python, source.python
29+
a : meta.function.parameters.python, meta.function.python, source.python, variable.parameter.function.language.python
30+
, : meta.function.parameters.python, meta.function.python, punctuation.separator.parameters.python, source.python
31+
: meta.function.parameters.python, meta.function.python, source.python
32+
b : meta.function.parameters.python, meta.function.python, source.python, variable.parameter.function.language.python
33+
= : keyword.operator.python, meta.function.parameters.python, meta.function.python, source.python
34+
1 : constant.numeric.dec.python, meta.function.parameters.python, meta.function.python, source.python
35+
) : meta.function.parameters.python, meta.function.python, punctuation.definition.parameters.end.python, source.python
36+
: : meta.function.python, punctuation.section.function.begin.python, source.python
37+
: source.python
38+
cls : source.python, variable.language.special.cls.python
39+
. : source.python
40+
a : source.python
41+
: source.python
42+
= : keyword.operator.assignment.python, source.python
43+
a : source.python
44+
: source.python
45+
cls : source.python, variable.language.special.cls.python
46+
. : source.python
47+
b : source.python
48+
: source.python
49+
= : keyword.operator.assignment.python, source.python
50+
b : source.python
51+
: source.python
52+
print : meta.function-call.python, source.python, support.function.builtin.python
53+
( : meta.function-call.arguments.python, meta.function-call.python, punctuation.definition.arguments.begin.python, source.python
54+
cls : meta.function-call.arguments.python, meta.function-call.python, source.python, variable.language.special.cls.python
55+
) : meta.function-call.python, punctuation.definition.arguments.end.python, source.python
56+
: source.python
57+
cls : meta.function-call.python, source.python, variable.language.special.cls.python
58+
( : meta.function-call.arguments.python, meta.function-call.python, punctuation.definition.arguments.begin.python, source.python
59+
) : meta.function-call.python, punctuation.definition.arguments.end.python, source.python
60+
: source.python
61+
cls : source.python, variable.language.special.cls.python
62+
: source.python
63+
+ : keyword.operator.python, source.python
64+
: source.python
65+
1 : constant.numeric.dec.python, source.python
66+
a : source.python
67+
. : source.python
68+
cls : source.python
69+
: source.python
70+
= : keyword.operator.assignment.python, source.python
71+
: source.python
72+
1 : constant.numeric.dec.python, source.python
73+
a.cls : source.python
74+
. : source.python
75+
__name__ : source.python, support.variable.magic.python

Diff for: test/docstrings/mix1.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ def : meta.function.python, source.python, storage.type.function.pytho
4141
: meta.function.python, source.python
4242
foo : entity.name.function.python, meta.function.python, source.python
4343
( : meta.function.parameters.python, meta.function.python, punctuation.definition.parameters.begin.python, source.python
44-
self : meta.function.parameters.python, meta.function.python, source.python, variable.parameter.function.language.python
44+
self : meta.function.parameters.python, meta.function.python, source.python, variable.parameter.function.language.python, variable.parameter.function.language.special.self.python
4545
, : meta.function.parameters.python, meta.function.python, punctuation.separator.parameters.python, source.python
4646
: meta.function.parameters.python, meta.function.python, source.python
4747
a : meta.function.parameters.python, meta.function.python, source.python, variable.parameter.function.language.python
@@ -76,7 +76,7 @@ def : meta.function.python, source.python, storage.type.function.pytho
7676
: meta.function.python, source.python
7777
bar : entity.name.function.python, meta.function.python, source.python
7878
( : meta.function.parameters.python, meta.function.python, punctuation.definition.parameters.begin.python, source.python
79-
self : meta.function.parameters.python, meta.function.python, source.python, variable.parameter.function.language.python
79+
self : meta.function.parameters.python, meta.function.python, source.python, variable.parameter.function.language.python, variable.parameter.function.language.special.self.python
8080
, : meta.function.parameters.python, meta.function.python, punctuation.separator.parameters.python, source.python
8181
: meta.function.parameters.python, meta.function.python, source.python
8282
a : meta.function.parameters.python, meta.function.python, source.python, variable.parameter.function.language.python

0 commit comments

Comments
 (0)