Skip to content

Commit 9066e28

Browse files
committed
Fix highlighting issues for incomplete functions.
Function definitions are incomplete while you type. This should not cause the highlighter to freak out and incorrectly highlight code after the new function. The issue was caused by incorrectly handling return annotations.
1 parent 848597d commit 9066e28

File tree

7 files changed

+37
-49
lines changed

7 files changed

+37
-49
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Magic Python [![Build Status](https://travis-ci.org/MagicStack/MagicPython.svg?branch=master)](https://travis-ci.org/MagicStack/MagicPython) [![apm](https://img.shields.io/apm/dm/magicpython.svg?label=Atom)](https://atom.io/packages/magicpython) [![Package Control](https://img.shields.io/packagecontrol/dt/MagicPython.svg?label=Sublime%20Text)](https://packagecontrol.io/packages/MagicPython) [![](https://img.shields.io/badge/Visual%20Studio%20Code-%3E20k-red.svg)](https://marketplace.visualstudio.com/items?itemName=magicstack.MagicPython)
1+
# Magic Python [![Build Status](https://travis-ci.org/MagicStack/MagicPython.svg?branch=master)](https://travis-ci.org/MagicStack/MagicPython) [![apm](https://img.shields.io/apm/dm/magicpython.svg?label=Atom)](https://atom.io/packages/magicpython) [![Package Control](https://img.shields.io/packagecontrol/dt/MagicPython.svg?label=Sublime%20Text)](https://packagecontrol.io/packages/MagicPython) [![](https://img.shields.io/badge/Visual%20Studio%20Code-%3E25k-red.svg)](https://marketplace.visualstudio.com/items?itemName=magicstack.MagicPython)
22

33
This is a package with preferences and syntax highlighter for cutting edge
44
Python 3, although Python 2 is well supported, too. The syntax is compatible

grammars/MagicPython.cson

+10-12
Original file line numberDiff line numberDiff line change
@@ -1168,7 +1168,7 @@ repository:
11681168
)
11691169
11701170
'''
1171-
end: "(:)"
1171+
end: "(:|(?=[#'\"\\n]))"
11721172
beginCaptures:
11731173
"1":
11741174
name: "storage.type.function.async.python"
@@ -1233,7 +1233,12 @@ repository:
12331233
include: "#parameter-special"
12341234
}
12351235
{
1236-
match: "([[:alpha:]_]\\w*)\\s*(?:(,)|(?=\\)))"
1236+
match: '''
1237+
(?x)
1238+
([[:alpha:]_]\\w*)
1239+
\\s* (?: (,) | (?=[)#\\n=]))
1240+
1241+
'''
12371242
captures:
12381243
"1":
12391244
name: "variable.parameter.function.language.python"
@@ -1244,7 +1249,7 @@ repository:
12441249
include: "#comments"
12451250
}
12461251
{
1247-
include: "#parameter-with-default"
1252+
include: "#loose-default"
12481253
}
12491254
{
12501255
include: "#annotated-parameter"
@@ -1265,18 +1270,11 @@ repository:
12651270
name: "variable.parameter.function.language.special.cls.python"
12661271
"4":
12671272
name: "punctuation.separator.parameters.python"
1268-
"parameter-with-default":
1269-
begin: '''
1270-
(?x)
1271-
\\b
1272-
([[:alpha:]_]\\w*) \\s* (=)
1273-
1274-
'''
1273+
"loose-default":
1274+
begin: "(=)"
12751275
end: "(,)|(?=\\))"
12761276
beginCaptures:
12771277
"1":
1278-
name: "variable.parameter.function.language.python"
1279-
"2":
12801278
name: "keyword.operator.python"
12811279
endCaptures:
12821280
"1":

grammars/MagicPython.tmLanguage

+8-13
Original file line numberDiff line numberDiff line change
@@ -1871,7 +1871,7 @@
18711871
)
18721872
</string>
18731873
<key>end</key>
1874-
<string>(:)</string>
1874+
<string>(:|(?=[#'"\n]))</string>
18751875
<key>beginCaptures</key>
18761876
<dict>
18771877
<key>1</key>
@@ -1981,7 +1981,10 @@
19811981
</dict>
19821982
<dict>
19831983
<key>match</key>
1984-
<string>([[:alpha:]_]\w*)\s*(?:(,)|(?=\)))</string>
1984+
<string>(?x)
1985+
([[:alpha:]_]\w*)
1986+
\s* (?: (,) | (?=[)#\n=]))
1987+
</string>
19851988
<key>captures</key>
19861989
<dict>
19871990
<key>1</key>
@@ -2002,7 +2005,7 @@
20022005
</dict>
20032006
<dict>
20042007
<key>include</key>
2005-
<string>#parameter-with-default</string>
2008+
<string>#loose-default</string>
20062009
</dict>
20072010
<dict>
20082011
<key>include</key>
@@ -2040,23 +2043,15 @@
20402043
</dict>
20412044
</dict>
20422045
</dict>
2043-
<key>parameter-with-default</key>
2046+
<key>loose-default</key>
20442047
<dict>
20452048
<key>begin</key>
2046-
<string>(?x)
2047-
\b
2048-
([[:alpha:]_]\w*) \s* (=)
2049-
</string>
2049+
<string>(=)</string>
20502050
<key>end</key>
20512051
<string>(,)|(?=\))</string>
20522052
<key>beginCaptures</key>
20532053
<dict>
20542054
<key>1</key>
2055-
<dict>
2056-
<key>name</key>
2057-
<string>variable.parameter.function.language.python</string>
2058-
</dict>
2059-
<key>2</key>
20602055
<dict>
20612056
<key>name</key>
20622057
<string>keyword.operator.python</string>

grammars/src/MagicPython.syntax.yaml

+9-10
Original file line numberDiff line numberDiff line change
@@ -795,7 +795,7 @@ repository:
795795
[[:alpha:]_][[:word:]]* \s* \(
796796
)
797797
798-
end: (:)
798+
end: (:|(?=[#'"\n]))
799799
beginCaptures:
800800
'1': {name: storage.type.function.async.python}
801801
'2': {name: storage.type.function.python}
@@ -833,13 +833,16 @@ repository:
833833
- include: '#illegal-names'
834834
- include: '#illegal-object-name'
835835
- include: '#parameter-special'
836-
- match: ([[:alpha:]_]\w*)\s*(?:(,)|(?=\)))
836+
- match: |
837+
(?x)
838+
([[:alpha:]_]\w*)
839+
\s* (?: (,) | (?=[)#\n=]))
837840
captures:
838841
'1': {name: variable.parameter.function.language.python}
839842
'2': {name: punctuation.separator.parameters.python}
840843
841844
- include: '#comments'
842-
- include: '#parameter-with-default'
845+
- include: '#loose-default'
843846
- include: '#annotated-parameter'
844847

845848
parameter-special:
@@ -852,15 +855,11 @@ repository:
852855
'3': {name: variable.parameter.function.language.special.cls.python}
853856
'4': {name: punctuation.separator.parameters.python}
854857

855-
parameter-with-default:
856-
begin: |
857-
(?x)
858-
\b
859-
([[:alpha:]_]\w*) \s* (=)
858+
loose-default:
859+
begin: (=)
860860
end: (,)|(?=\))
861861
beginCaptures:
862-
'1': {name: variable.parameter.function.language.python}
863-
'2': {name: keyword.operator.python}
862+
'1': {name: keyword.operator.python}
864863
endCaptures:
865864
'1': {name: punctuation.separator.parameters.python}
866865
patterns:

test/docstrings/mix1.py

+4-6
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,12 @@ class Foo:
44
# comment
55
R'''TEST'''
66

7-
def foo(self, a:'''TEST''')
8-
-> '''TEST''': #ok
7+
def foo(self, a:'''TEST''') -> '''TEST''': #ok
98
r'''TEST'''
109
with bar:
1110
pass
1211

13-
def bar(self, a:'''TEST''')
14-
-> '''TEST''': pass
12+
def bar(self, a:'''TEST''') -> '''TEST''': pass
1513
'''TEST''' # additional docstring
1614
with bar:
1715
pass
@@ -50,7 +48,7 @@ def : meta.function.python, source.python, storage.type.function.pytho
5048
TEST : meta.function.parameters.python, meta.function.python, source.python, string.quoted.multi.python
5149
''' : meta.function.parameters.python, meta.function.python, punctuation.definition.string.end.python, source.python, string.quoted.multi.python
5250
) : meta.function.parameters.python, meta.function.python, punctuation.definition.parameters.end.python, source.python
53-
: meta.function.python, source.python
51+
: meta.function.python, source.python
5452
-> : meta.function.python, punctuation.separator.annotation.result.python, source.python
5553
: meta.function.python, source.python
5654
''' : meta.function.python, punctuation.definition.string.begin.python, source.python, string.quoted.multi.python
@@ -87,7 +85,7 @@ def : meta.function.python, source.python, storage.type.function.pytho
8785
TEST : meta.function.parameters.python, meta.function.python, source.python, string.quoted.multi.python
8886
''' : meta.function.parameters.python, meta.function.python, punctuation.definition.string.end.python, source.python, string.quoted.multi.python
8987
) : meta.function.parameters.python, meta.function.python, punctuation.definition.parameters.end.python, source.python
90-
: meta.function.python, source.python
88+
: meta.function.python, source.python
9189
-> : meta.function.python, punctuation.separator.annotation.result.python, source.python
9290
: meta.function.python, source.python
9391
''' : meta.function.python, punctuation.definition.string.begin.python, source.python, string.quoted.multi.python

test/docstrings/mix2.py

+4-6
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,12 @@ class Foo:
44
# comment
55
R'TEST'
66

7-
def foo(self, a:'TEST')
8-
-> 'TEST': #ok
7+
def foo(self, a:'TEST') -> 'TEST': #ok
98
r'TEST'
109
with bar:
1110
pass
1211

13-
def bar(self, a:'TEST')
14-
-> 'TEST': pass
12+
def bar(self, a:'TEST') -> 'TEST': pass
1513
'TEST' # additional docstring
1614
with bar:
1715
pass
@@ -49,7 +47,7 @@ def : meta.function.python, source.python, storage.type.function.pytho
4947
TEST : meta.function.parameters.python, meta.function.python, source.python, string.quoted.single.python
5048
' : meta.function.parameters.python, meta.function.python, punctuation.definition.string.end.python, source.python, string.quoted.single.python
5149
) : meta.function.parameters.python, meta.function.python, punctuation.definition.parameters.end.python, source.python
52-
: meta.function.python, source.python
50+
: meta.function.python, source.python
5351
-> : meta.function.python, punctuation.separator.annotation.result.python, source.python
5452
: meta.function.python, source.python
5553
' : meta.function.python, punctuation.definition.string.begin.python, source.python, string.quoted.single.python
@@ -86,7 +84,7 @@ def : meta.function.python, source.python, storage.type.function.pytho
8684
TEST : meta.function.parameters.python, meta.function.python, source.python, string.quoted.single.python
8785
' : meta.function.parameters.python, meta.function.python, punctuation.definition.string.end.python, source.python, string.quoted.single.python
8886
) : meta.function.parameters.python, meta.function.python, punctuation.definition.parameters.end.python, source.python
89-
: meta.function.python, source.python
87+
: meta.function.python, source.python
9088
-> : meta.function.python, punctuation.separator.annotation.result.python, source.python
9189
: meta.function.python, source.python
9290
' : meta.function.python, punctuation.definition.string.begin.python, source.python, string.quoted.single.python

test/functions/decl2.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ def : meta.function.python, source.python, storage.type.function.pytho
99
lambda : meta.function.parameters.python, meta.function.python, source.python, storage.type.function.lambda.python
1010
, : meta.function.parameters.python, meta.function.python, source.python
1111
lambda : keyword.control.flow.python, meta.function.parameters.python, meta.function.python, source.python
12-
= : meta.function.parameters.python, meta.function.python, source.python
12+
= : keyword.operator.python, meta.function.parameters.python, meta.function.python, source.python
1313
) : meta.function.parameters.python, meta.function.python, punctuation.definition.parameters.end.python, source.python
1414
: meta.function.python, source.python
1515
-> : meta.function.python, punctuation.separator.annotation.result.python, source.python

0 commit comments

Comments
 (0)