Skip to content

Commit 7b3be8b

Browse files
authored
[Python] Fix broken highlighting after case statements (#4436)
* [Python] Fix function definitions in first case branch This commit removes `^\s*` restriction from function definitions' patterns to ensure those are matched after first case statements. Leading whitespace of a line after first case statement is already consumed and scoped `meta.disable-dedentation` to support ST's indentation engine. For consistency the restriction is globally removed, so space before `def` keyword is never scoped `meta.function`. * [Python] Fix class definitions in first case branch This commit removes `^\s*` restriction from class definitions' patterns to ensure those are matched after first case statements. Leading whitespace of a line after first case statement is already consumed and scoped `meta.disable-dedentation` to support ST's indentation engine. For consistency the restriction is globally removed, so space before `class` keyword is never scoped `meta.class`. * [Python] Fix type definitions in first case branch This commit removes `^\s*` restriction from type definitions' patterns to ensure those are matched after first case statements. Leading whitespace of a line after first case statement is already consumed and scoped `meta.disable-dedentation` to support ST's indentation engine. For consistency the restriction is globally removed, so space before `type` keyword is never scoped `meta.type-alias`. * [Python] Fix decorators in first case branch This commit pushes a special context for special patterns right after first case statement in order to consume decorators. A `@` denotes an decorator only at the beginning of a line. In any other position it is an operator. * [Python] Verify conflict markers after case statements
1 parent 2021276 commit 7b3be8b

File tree

3 files changed

+106
-14
lines changed

3 files changed

+106
-14
lines changed

Python/Python.sublime-syntax

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -789,10 +789,8 @@ contexts:
789789
###[ CLASS DEFINITIONS ]######################################################
790790

791791
class-definitions:
792-
- match: ^\s*(class)\b
793-
scope: meta.class.python
794-
captures:
795-
1: keyword.declaration.class.python
792+
- match: class\b
793+
scope: meta.class.python keyword.declaration.class.python
796794
push:
797795
- class-definition-base-list
798796
- type-parameter-list
@@ -860,7 +858,7 @@ contexts:
860858
###[ FUNCTION DEFINITIONS ]###################################################
861859

862860
function-definitions:
863-
- match: ^\s*(?:(async)\s+)?(def)\b
861+
- match: (?:(async)\s+)?(def)\b
864862
scope: meta.function.python
865863
captures:
866864
1: keyword.declaration.async.python
@@ -1074,17 +1072,16 @@ contexts:
10741072
###[ TYPE DEFINITIONS ]#######################################################
10751073

10761074
type-definitions:
1077-
- match: ^(?=\s*type\b)
1075+
- match: (?=type\b)
10781076
branch_point: type-definitions
10791077
branch:
10801078
- type-definition
10811079
- type-definition-fallback
10821080

10831081
type-definition:
10841082
- meta_include_prototype: false
1085-
- match: \s*(type)\b
1086-
captures:
1087-
1: keyword.declaration.class.python
1083+
- match: type\b
1084+
scope: keyword.declaration.class.python
10881085
set:
10891086
- type-definition-meta
10901087
- type-definition-value-assignment
@@ -1621,12 +1618,20 @@ contexts:
16211618
meta.statement.conditional.case.python
16221619
keyword.control.conditional.case.python
16231620
set:
1621+
- after-first-case-statement
16241622
- meta-disable-dedentation
16251623
- case-statement-pattern
16261624
- allow-unpack-operators
16271625
- include: comments
16281626
- include: else-pop
16291627

1628+
after-first-case-statement:
1629+
- meta_include_prototype: false
1630+
- match: \@
1631+
scope: meta.annotation.python punctuation.definition.annotation.python
1632+
set: decorator-body
1633+
- include: immediately-pop
1634+
16301635
meta-disable-dedentation:
16311636
- meta_include_prototype: false
16321637
- meta_scope: meta.disable-dedentation.python

Python/tests/syntax_test_conflicts.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -721,3 +721,43 @@
721721
# ^^^^^^ meta.block.conflict.end.diff entity.name.section.diff
722722
# ^ meta.block.conflict.end.diff - entity - punctuation
723723
'''
724+
725+
match test:
726+
case "before":
727+
<<<<<<< HEAD
728+
# <- meta.block.conflict.begin.diff punctuation.section.block.begin.diff
729+
# ^^^^^ meta.block.conflict.begin.diff punctuation.section.block.begin.diff
730+
# ^ meta.block.conflict.begin.diff - entity - punctuation
731+
# ^^^^ meta.block.conflict.begin.diff entity.name.section.diff
732+
# ^ meta.block.conflict.begin.diff - entity - punctuation
733+
case "ours":
734+
# ^^^^ meta.statement.conditional.case.python keyword.control.conditional.case.python
735+
# ^^^^^^^ meta.statement.conditional.case.patterns.python
736+
# ^^^^^^ meta.string.python string.quoted.double.python
737+
# ^ punctuation.definition.string.begin.python
738+
# ^ punctuation.definition.string.end.python
739+
# ^ meta.statement.conditional.case.python punctuation.section.block.begin.python
740+
=======
741+
# <- meta.block.conflict.separator.diff punctuation.section.block.diff
742+
# ^^^^^ meta.block.conflict.separator.diff punctuation.section.block.diff
743+
# ^ meta.block.conflict.separator.diff - punctuation
744+
case "theirs":
745+
# ^^^^ meta.statement.conditional.case.python keyword.control.conditional.case.python
746+
# ^^^^^^^^^ meta.statement.conditional.case.patterns.python
747+
# ^^^^^^^^ meta.string.python string.quoted.double.python
748+
# ^ punctuation.definition.string.begin.python
749+
# ^ punctuation.definition.string.end.python
750+
# ^ meta.statement.conditional.case.python punctuation.section.block.begin.python
751+
>>>>>>> master
752+
# <- meta.block.conflict.end.diff punctuation.section.block.end.diff
753+
# ^^^^^ meta.block.conflict.end.diff punctuation.section.block.end.diff
754+
# ^ meta.block.conflict.end.diff - entity - punctuation
755+
# ^^^^^^ meta.block.conflict.end.diff entity.name.section.diff
756+
# ^ meta.block.conflict.end.diff - entity - punctuation
757+
case "after":
758+
# ^^^^ meta.statement.conditional.case.python keyword.control.conditional.case.python
759+
# ^^^^^^^^ meta.statement.conditional.case.patterns.python
760+
# ^^^^^^^ meta.string.python string.quoted.double.python
761+
# ^ punctuation.definition.string.begin.python
762+
# ^ punctuation.definition.string.end.python
763+
# ^ meta.statement.conditional.case.python punctuation.section.block.begin.python

Python/tests/syntax_test_python.py

Lines changed: 52 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2146,6 +2146,20 @@ def f[
21462146
# ^ meta.type.python meta.path.python meta.generic-name.python
21472147
# ^ meta.function.python punctuation.section.block.begin.python
21482148

2149+
match test:
2150+
case "func":
2151+
def func(arg): pass
2152+
# <- meta.disable-dedentation.python - meta.function
2153+
#^^^^^^^ meta.disable-dedentation.python - meta.function
2154+
# ^^^^^^^^ meta.function.python
2155+
# ^^^ keyword.declaration.function.python
2156+
# ^^^^ entity.name.function.python
2157+
# ^^^^^ meta.function.parameters.python
2158+
# ^ punctuation.section.parameters.begin.python
2159+
# ^^^ variable.parameter.python
2160+
# ^ punctuation.section.parameters.end.python
2161+
# ^ meta.function.python punctuation.section.block.begin.python
2162+
# ^^^^ keyword.control.flow.pass.python
21492163

21502164
##################
21512165
# Class definitions
@@ -2310,6 +2324,19 @@ def method(arg: T):
23102324
# ^^^^^^^^ meta.function.parameters
23112325
# ^ meta.type.python meta.path.python meta.generic-name.python
23122326

2327+
match test:
2328+
case "class":
2329+
class name(): pass
2330+
# <- meta.disable-dedentation.python - meta.class
2331+
#^^^^^^^ meta.disable-dedentation.python - meta.class
2332+
# ^^^^^^^^^^ meta.class.python
2333+
# ^^^^^ keyword.declaration.class.python
2334+
# ^^^^ entity.name.class.python
2335+
# ^^ meta.class.inheritance.python
2336+
# ^ punctuation.section.inheritance.begin.python
2337+
# ^ punctuation.section.inheritance.end.python
2338+
# ^ meta.class.python punctuation.section.block.begin.python
2339+
# ^^^^ keyword.control.flow.pass.python
23132340

23142341
##################
23152342
# Type definitions
@@ -2389,8 +2416,9 @@ def method(arg: T):
23892416
# ^ punctuation.section.brackets.end.python
23902417

23912418
type Alias
2392-
# <- meta.type-alias.python
2393-
#^^^^^^^^^^^ meta.type-alias.python
2419+
# <- - meta.type-alias
2420+
#^ - meta.type-alias
2421+
# ^^^^^^^^^^ meta.type-alias.python
23942422
# ^^^^ keyword.declaration.class.python
23952423
# ^^^^^ entity.name.type.alias.python
23962424

@@ -2429,7 +2457,7 @@ def __init__(self, type: int):
24292457
# ^^^^ variable.parameter.python
24302458
self.type = type
24312459
# ^^^^ meta.path.python meta.generic-name.python
2432-
# ^^^^ meta.path.python meta.generic-name.python
2460+
# ^^^^ meta.generic-name.python
24332461

24342462
self.me = type(type)
24352463
# ^^^^ meta.function-call.identifier.python support.function.builtin.python
@@ -2438,6 +2466,14 @@ def __init__(self, type: int):
24382466
# ^^^^ meta.path.python meta.generic-name.python
24392467
# ^ punctuation.section.arguments.end.python
24402468

2469+
match test:
2470+
case "type":
2471+
type foo
2472+
#^^^^^^^ meta.disable-dedentation.python - meta.type
2473+
# ^^^^^^^^ meta.type-alias.python
2474+
# ^^^^ keyword.declaration.class.python
2475+
# ^^^ entity.name.type.alias.python
2476+
24412477
##################
24422478
# Decorators
24432479
##################
@@ -2592,6 +2628,14 @@ def f(): pass
25922628
# ^^^^^^^ variable.annotation.python
25932629
#
25942630

2631+
match test:
2632+
case "func":
2633+
@guarded
2634+
#^^^^^^^ meta.disable-dedentation.python - meta.annotation
2635+
# ^^^^^^^^ meta.annotation.python
2636+
# ^ punctuation.definition.annotation.python
2637+
# ^^^^^^^ variable.annotation.python
2638+
25952639
##################
25962640
# Collection literals and generators
25972641
##################
@@ -3087,8 +3131,11 @@ class Cls:
30873131
__slots__ = "item",
30883132

30893133
def method():
3090-
# <- meta.function.python
3091-
#^^^^^^^^^^^^^^^^ meta.function
3134+
# <- - meta.function
3135+
#^^^ - meta.function
3136+
# ^^^^^^^^^^ meta.function.python
3137+
# ^^ meta.function.parameters.python
3138+
# ^ meta.function.python
30923139
# ^^^ keyword.declaration.function.python
30933140
# ^^^^^^ entity.name.function.python
30943141
# ^ punctuation.section.parameters.begin.python

0 commit comments

Comments
 (0)