Skip to content

Commit 033f08a

Browse files
committed
fix: Fix switch/when statement syntax highlighting and brace matching
- Remove lookbehind end patterns from all when-* statements that prevented proper brace matching - Update end patterns to use lookahead that ends on closing brace or next when clause - Improve when-string pattern to support multiple comma-separated strings - Fix issue #2134: syntax highlighting and brace matching for switch/when statements All switch statement tests passing.
1 parent 4868efa commit 033f08a

File tree

4 files changed

+69
-28
lines changed

4 files changed

+69
-28
lines changed

grammars/apex.tmLanguage

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2893,7 +2893,7 @@
28932893
</dict>
28942894
</dict>
28952895
<key>end</key>
2896-
<string>(?&lt;=\})</string>
2896+
<string>(?=\})|(?=when\b)</string>
28972897
<key>patterns</key>
28982898
<array>
28992899
<dict>
@@ -2909,7 +2909,7 @@
29092909
<key>when-string</key>
29102910
<dict>
29112911
<key>begin</key>
2912-
<string>(when)\b\s*('[^'\n]*')(,)?</string>
2912+
<string>(when)\b\s*('[^'\n]*')(\s*(,)\s*('[^'\n]*'))*\s*</string>
29132913
<key>beginCaptures</key>
29142914
<dict>
29152915
<key>1</key>
@@ -2927,7 +2927,7 @@
29272927
</dict>
29282928
</array>
29292929
</dict>
2930-
<key>3</key>
2930+
<key>4</key>
29312931
<dict>
29322932
<key>patterns</key>
29332933
<array>
@@ -2937,9 +2937,19 @@
29372937
</dict>
29382938
</array>
29392939
</dict>
2940+
<key>5</key>
2941+
<dict>
2942+
<key>patterns</key>
2943+
<array>
2944+
<dict>
2945+
<key>include</key>
2946+
<string>#string-literal</string>
2947+
</dict>
2948+
</array>
2949+
</dict>
29402950
</dict>
29412951
<key>end</key>
2942-
<string>(?&lt;=\})</string>
2952+
<string>(?=\})|(?=when\b)</string>
29432953
<key>patterns</key>
29442954
<array>
29452955
<dict>
@@ -2970,7 +2980,7 @@
29702980
</dict>
29712981
</dict>
29722982
<key>end</key>
2973-
<string>(?&lt;=\})</string>
2983+
<string>(?=\})|(?=when\b)</string>
29742984
<key>patterns</key>
29752985
<array>
29762986
<dict>
@@ -2996,7 +3006,7 @@
29963006
</dict>
29973007
</dict>
29983008
<key>end</key>
2999-
<string>(?&lt;=\})</string>
3009+
<string>(?=\})|(?=when\b)</string>
30003010
<key>patterns</key>
30013011
<array>
30023012
<dict>
@@ -3007,6 +3017,10 @@
30073017
<key>include</key>
30083018
<string>#expression</string>
30093019
</dict>
3020+
<dict>
3021+
<key>include</key>
3022+
<string>#punctuation-comma</string>
3023+
</dict>
30103024
</array>
30113025
</dict>
30123026
<key>when-sobject-statement</key>
@@ -3032,7 +3046,7 @@
30323046
</dict>
30333047
</dict>
30343048
<key>end</key>
3035-
<string>(?&lt;=\})</string>
3049+
<string>(?=\})|(?=when\b)</string>
30363050
<key>patterns</key>
30373051
<array>
30383052
<dict>

grammars/apex.tmLanguage.cson

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1704,7 +1704,7 @@ repository:
17041704
include: '#expression'
17051705
}
17061706
]
1707-
end: '(?<=\\})'
1707+
end: '(?=\\})|(?=when\\b)'
17081708
patterns: [
17091709
{
17101710
include: '#block'
@@ -1714,7 +1714,7 @@ repository:
17141714
}
17151715
]
17161716
'when-string':
1717-
begin: "(when)\\b\\s*('[^'\\n]*')(,)?"
1717+
begin: "(when)\\b\\s*('[^'\\n]*')(\\s*(,)\\s*('[^'\\n]*'))*\\s*"
17181718
beginCaptures:
17191719
'1':
17201720
name: 'keyword.control.switch.when.apex'
@@ -1724,13 +1724,19 @@ repository:
17241724
include: '#string-literal'
17251725
}
17261726
]
1727-
'3':
1727+
'4':
17281728
patterns: [
17291729
{
17301730
include: '#punctuation-comma'
17311731
}
17321732
]
1733-
end: '(?<=\\})'
1733+
'5':
1734+
patterns: [
1735+
{
1736+
include: '#string-literal'
1737+
}
1738+
]
1739+
end: '(?=\\})|(?=when\\b)'
17341740
patterns: [
17351741
{
17361742
include: '#block'
@@ -1746,7 +1752,7 @@ repository:
17461752
name: 'keyword.control.switch.when.apex'
17471753
'2':
17481754
name: 'keyword.control.switch.else.apex'
1749-
end: '(?<=\\})'
1755+
end: '(?=\\})|(?=when\\b)'
17501756
patterns: [
17511757
{
17521758
include: '#block'
@@ -1760,14 +1766,17 @@ repository:
17601766
beginCaptures:
17611767
'1':
17621768
name: 'keyword.control.switch.when.apex'
1763-
end: '(?<=\\})'
1769+
end: '(?=\\})|(?=when\\b)'
17641770
patterns: [
17651771
{
17661772
include: '#block'
17671773
}
17681774
{
17691775
include: '#expression'
17701776
}
1777+
{
1778+
include: '#punctuation-comma'
1779+
}
17711780
]
17721781
'when-sobject-statement':
17731782
begin: '(when)\\b\\s+([_[:alnum:]]+)\\s+([_[:alnum:]]+)\\s*'
@@ -1778,7 +1787,7 @@ repository:
17781787
name: 'storage.type.apex'
17791788
'3':
17801789
name: 'entity.name.variable.local.apex'
1781-
end: '(?<=\\})'
1790+
end: '(?=\\})|(?=when\\b)'
17821791
patterns: [
17831792
{
17841793
include: '#block'

grammars/soql.tmLanguage

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2875,7 +2875,7 @@
28752875
</dict>
28762876
</dict>
28772877
<key>end</key>
2878-
<string>(?&lt;=\})</string>
2878+
<string>(?=\})|(?=when\b)</string>
28792879
<key>patterns</key>
28802880
<array>
28812881
<dict>
@@ -2891,7 +2891,7 @@
28912891
<key>when-string</key>
28922892
<dict>
28932893
<key>begin</key>
2894-
<string>(when)\b\s*('[^'\n]*')(,)?</string>
2894+
<string>(when)\b\s*('[^'\n]*')(\s*(,)\s*('[^'\n]*'))*\s*</string>
28952895
<key>beginCaptures</key>
28962896
<dict>
28972897
<key>1</key>
@@ -2909,7 +2909,7 @@
29092909
</dict>
29102910
</array>
29112911
</dict>
2912-
<key>3</key>
2912+
<key>4</key>
29132913
<dict>
29142914
<key>patterns</key>
29152915
<array>
@@ -2919,9 +2919,19 @@
29192919
</dict>
29202920
</array>
29212921
</dict>
2922+
<key>5</key>
2923+
<dict>
2924+
<key>patterns</key>
2925+
<array>
2926+
<dict>
2927+
<key>include</key>
2928+
<string>#string-literal</string>
2929+
</dict>
2930+
</array>
2931+
</dict>
29222932
</dict>
29232933
<key>end</key>
2924-
<string>(?&lt;=\})</string>
2934+
<string>(?=\})|(?=when\b)</string>
29252935
<key>patterns</key>
29262936
<array>
29272937
<dict>
@@ -2952,7 +2962,7 @@
29522962
</dict>
29532963
</dict>
29542964
<key>end</key>
2955-
<string>(?&lt;=\})</string>
2965+
<string>(?=\})|(?=when\b)</string>
29562966
<key>patterns</key>
29572967
<array>
29582968
<dict>
@@ -2978,7 +2988,7 @@
29782988
</dict>
29792989
</dict>
29802990
<key>end</key>
2981-
<string>(?&lt;=\})</string>
2991+
<string>(?=\})|(?=when\b)</string>
29822992
<key>patterns</key>
29832993
<array>
29842994
<dict>
@@ -2989,6 +2999,10 @@
29892999
<key>include</key>
29903000
<string>#expression</string>
29913001
</dict>
3002+
<dict>
3003+
<key>include</key>
3004+
<string>#punctuation-comma</string>
3005+
</dict>
29923006
</array>
29933007
</dict>
29943008
<key>when-sobject-statement</key>
@@ -3014,7 +3028,7 @@
30143028
</dict>
30153029
</dict>
30163030
<key>end</key>
3017-
<string>(?&lt;=\})</string>
3031+
<string>(?=\})|(?=when\b)</string>
30183032
<key>patterns</key>
30193033
<array>
30203034
<dict>

src/apex.tmLanguage.yml

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -985,22 +985,25 @@ repository:
985985
'2':
986986
patterns:
987987
- include: '#expression'
988-
end: (?<=\})
988+
end: (?=\})|(?=when\b)
989989
patterns:
990990
- include: '#block'
991991
- include: '#expression'
992992

993993
when-string:
994-
begin: (when)\b\s*('[^'\n]*')(,)?
994+
begin: (when)\b\s*('[^'\n]*')(\s*(,)\s*('[^'\n]*'))*\s*
995995
beginCaptures:
996996
'1': { name: keyword.control.switch.when.apex }
997997
'2':
998998
patterns:
999999
- include: '#string-literal'
1000-
'3':
1000+
'4':
10011001
patterns:
10021002
- include: '#punctuation-comma'
1003-
end: (?<=\})
1003+
'5':
1004+
patterns:
1005+
- include: '#string-literal'
1006+
end: (?=\})|(?=when\b)
10041007
patterns:
10051008
- include: '#block'
10061009
- include: '#expression'
@@ -1010,7 +1013,7 @@ repository:
10101013
beginCaptures:
10111014
'1': { name: keyword.control.switch.when.apex }
10121015
'2': { name: keyword.control.switch.else.apex }
1013-
end: (?<=\})
1016+
end: (?=\})|(?=when\b)
10141017
patterns:
10151018
- include: '#block'
10161019
- include: '#expression'
@@ -1019,18 +1022,19 @@ repository:
10191022
begin: (when)\b\s*
10201023
beginCaptures:
10211024
'1': { name: keyword.control.switch.when.apex }
1022-
end: (?<=\})
1025+
end: (?=\})|(?=when\b)
10231026
patterns:
10241027
- include: '#block'
10251028
- include: '#expression'
1029+
- include: '#punctuation-comma'
10261030

10271031
when-sobject-statement:
10281032
begin: (when)\b\s+([_[:alnum:]]+)\s+([_[:alnum:]]+)\s*
10291033
beginCaptures:
10301034
'1': { name: keyword.control.switch.when.apex }
10311035
'2': { name: storage.type.apex }
10321036
'3': { name: entity.name.variable.local.apex }
1033-
end: (?<=\})
1037+
end: (?=\})|(?=when\b)
10341038
patterns:
10351039
- include: '#block'
10361040
- include: '#expression'

0 commit comments

Comments
 (0)