@@ -31,6 +31,7 @@ variables:
31
31
matchingBraces : (\{([^\{\}]|(\{[^\{\}]*\}))*\})
32
32
matchingBrackets : (\[([^\[\]]|(\[[^\[\]]*\]))*\])
33
33
inlineComment : \/\*([^\*]|(\*[^\/]))*\*\/
34
+ lookBehindOfPossiblyMultilineArrow : (?<=[(=,]|=>)
34
35
# Identifier start | matching braces | matching parenthesis | matching square brackets
35
36
typeParamersStart : ([_$[:alpha:]]|{{matchingBraces}}|{{matchingParenthesis}}|{{matchingBrackets}})
36
37
typeParameters : (<\s*{{typeParamersStart}}([^=<>]|=[^<]|\<\s*{{typeParamersStart}}([^=<>]|=[^<])*\>)*>\s*)
@@ -39,6 +40,7 @@ variables:
39
40
typeArgumentsInnerExpressionPart : ' [^<>\(]|{{matchingParenthesis}}|(?<==)\>'
40
41
typeArguments : ' <\s*{{typeArgumentsStart}}({{typeArgumentsInnerExpressionPart}}|\<\s*{{typeArgumentsStart}}({{typeArgumentsInnerExpressionPart}})*(?!=)\>)*(?!=)>'
41
42
functionCallLookup : \s*(\?\.\s*)?({{typeArguments}}\s*)?\(
43
+ returnTypeOfArrow : \s*([^<>\(\)]|\<[^<>]+\>|\([^\(\)]+\))+
42
44
arrowLookup : |-
43
45
# sure shot arrow functions even if => is on new line
44
46
(
@@ -55,9 +57,10 @@ variables:
55
57
(
56
58
{{typeParameters}}? # typeparameters
57
59
\(\s*(([_$[:alpha:]]|{{matchingBraces}}|{{matchingBrackets}})([^()]|{{matchingParenthesis}})*)?\) # parameters
58
- (\s*:\s*([^<>\(\)]|\<[^<>]+\>|\([^\(\)]+\))+)? # return type
60
+ (\s*:{{returnTypeOfArrow}})? # return type
59
61
\s*=> # arrow operator
60
62
)
63
+ possiblyMultilineArrow : ({{typeParameters}}?[\(]\s*$) # during lookup treat <typeparameters>?( followed by line end as arrow
61
64
functionOrArrowLookup : |-
62
65
\s*(
63
66
((async\s+)?(
@@ -66,6 +69,7 @@ variables:
66
69
({{identifier}}\s*=>)
67
70
)) |
68
71
((async\s*)?(
72
+ {{possiblyMultilineArrow}} |
69
73
{{arrowLookup}}
70
74
))
71
75
)
@@ -86,6 +90,7 @@ variables:
86
90
))
87
91
)) |
88
92
(:\s*(=>|{{matchingParenthesis}}|(<[^<>]*>)|[^<>(),=])+={{functionOrArrowLookup}})
93
+ arrowFunctionEnd : (?==>|\{|(^\s*(export|function|class|interface|let|var|const|import|enum|namespace|module|type|abstract|declare)\s+))
89
94
90
95
patterns :
91
96
- include : ' #directives'
@@ -159,6 +164,7 @@ repository:
159
164
- include : ' #function-expression'
160
165
- include : ' #class-expression'
161
166
- include : ' #arrow-function'
167
+ - include : ' #paren-expression-possibly-arrow'
162
168
- include : ' #cast'
163
169
- include : ' #ternary-expression'
164
170
- include : ' #new-expr'
@@ -580,7 +586,7 @@ repository:
580
586
)
581
587
beginCaptures :
582
588
' 1 ' : { name: storage.modifier.async.ts }
583
- end : (?==>|\{|(^\s*(export|function|class|interface|let|var|const|import|enum|namespace|module|type|abstract|declare)\s+))
589
+ end : ' {{arrowFunctionEnd}} '
584
590
patterns :
585
591
- include : ' #comment'
586
592
- include : ' #type-parameters'
@@ -637,6 +643,10 @@ repository:
637
643
endCaptures :
638
644
' 0 ' : { name: punctuation.definition.parameters.end.ts }
639
645
patterns :
646
+ - include : ' #function-parameters-body'
647
+
648
+ function-parameters-body :
649
+ patterns :
640
650
- include : ' #comment'
641
651
- include : ' #decorator'
642
652
- include : ' #destructuring-parameter'
@@ -1098,9 +1108,9 @@ repository:
1098
1108
begin : (?!\?\.\s*[^[:digit:]])(\?)
1099
1109
beginCaptures :
1100
1110
' 1 ' : { name: keyword.operator.ternary.ts }
1101
- end : (:)
1111
+ end : \s* (:)
1102
1112
endCaptures :
1103
- ' 0 ' : { name: keyword.operator.ternary.ts }
1113
+ ' 1 ' : { name: keyword.operator.ternary.ts }
1104
1114
patterns :
1105
1115
- include : ' #expression'
1106
1116
@@ -1146,6 +1156,42 @@ repository:
1146
1156
patterns :
1147
1157
- include : ' #type'
1148
1158
1159
+ # when ( is followed by end of line, it could be arrow parameter decarations, so inside this match as if parameter falling back to expression
1160
+ # but do this only from expression and as last resort
1161
+ paren-expression-possibly-arrow :
1162
+ patterns :
1163
+ - begin : ' {{lookBehindOfPossiblyMultilineArrow}}\s*(async)?(?=\s*{{typeParameters}}\(\s*$)'
1164
+ beginCaptures :
1165
+ ' 1 ' : { name: storage.modifier.async.ts }
1166
+ end : (?<=\))
1167
+ patterns :
1168
+ - include : ' #type-parameters'
1169
+ - begin : \(
1170
+ beginCaptures :
1171
+ ' 0 ' : { name: meta.brace.round.ts }
1172
+ end : \)
1173
+ endCaptures :
1174
+ ' 0 ' : { name: meta.brace.round.ts }
1175
+ patterns :
1176
+ - include : ' #expression-inside-possibly-arrow-parens'
1177
+ - begin : ' {{lookBehindOfPossiblyMultilineArrow}}\s*(async)?\s*(\()(?=\s*$)'
1178
+ beginCaptures :
1179
+ ' 1 ' : { name: storage.modifier.async.ts }
1180
+ ' 2 ' : { name: meta.brace.round.ts }
1181
+ end : \)
1182
+ endCaptures :
1183
+ ' 0 ' : { name: meta.brace.round.ts }
1184
+ patterns :
1185
+ - include : ' #expression-inside-possibly-arrow-parens'
1186
+ - include : ' #possibly-arrow-return-type'
1187
+
1188
+ expression-inside-possibly-arrow-parens :
1189
+ patterns :
1190
+ - include : ' #expressionWithoutIdentifiers'
1191
+ - include : ' #function-parameters-body'
1192
+ - include : ' #identifiers'
1193
+ - include : ' #expressionPunctuations'
1194
+
1149
1195
paren-expression :
1150
1196
begin : \(
1151
1197
beginCaptures :
@@ -1155,7 +1201,6 @@ repository:
1155
1201
' 0 ' : {name: meta.brace.round.ts }
1156
1202
patterns :
1157
1203
- include : ' #expression'
1158
- - include : ' #punctuation-comma'
1159
1204
1160
1205
# cast expression
1161
1206
cast :
@@ -1735,7 +1780,21 @@ repository:
1735
1780
begin : ' (?<=\))\s*(:)'
1736
1781
beginCaptures :
1737
1782
' 1 ' : { name: keyword.operator.type.annotation.ts }
1738
- end : (?==>|\{|(^\s*(export|function|class|interface|let|var|const|import|enum|namespace|module|type|abstract|declare)\s+))
1783
+ end : ' {{arrowFunctionEnd}}'
1784
+ patterns :
1785
+ - include : ' #arrow-return-type-body'
1786
+
1787
+ # This is the check if the exoression '): something =>'' can be matched as return type of arrow
1788
+ possibly-arrow-return-type :
1789
+ begin : (?<=\))\s*(:)(?={{returnTypeOfArrow}}\s*=>)
1790
+ beginCaptures :
1791
+ ' 1 ' : { name: meta.arrow.ts meta.return.type.arrow.ts keyword.operator.type.annotation.ts }
1792
+ end : ' {{arrowFunctionEnd}}'
1793
+ contentName : meta.arrow.ts meta.return.type.arrow.ts
1794
+ patterns :
1795
+ - include : ' #arrow-return-type-body'
1796
+
1797
+ arrow-return-type-body :
1739
1798
patterns :
1740
1799
# TODO: handle the fn and constructor type specifically.
1741
1800
# Handle returning of object type specifically here so as to not confuse it with the start of function block
0 commit comments