Skip to content

Commit 66d38dc

Browse files
committed
Handle line endings in typennotation and return type little better
Fixes #472
1 parent 06d27e6 commit 66d38dc

File tree

5 files changed

+455
-107
lines changed

5 files changed

+455
-107
lines changed

TypeScript.YAML-tmLanguage

Lines changed: 62 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -874,15 +874,29 @@ repository:
874874
- include: '#punctuation-comma'
875875

876876
return-type:
877-
name: meta.return.type.ts
878-
begin: (?<=\))\s*(:)
879-
beginCaptures:
880-
'1': { name: keyword.operator.type.annotation.ts }
881-
end: (?<![:|&])(?=$|^|[{};,]|//)
877+
patterns:
878+
# if type starts on same line use end of line as end
879+
- name: meta.return.type.ts
880+
begin: (?<=\))\s*(:)(?=\s*\S)
881+
beginCaptures:
882+
'1': { name: keyword.operator.type.annotation.ts }
883+
end: (?<![:|&])(?=$|^|[{};,]|//)
884+
patterns:
885+
- include: '#return-type-core'
886+
# if type starts on next line use modified line endings as end of type annotation eg. complete empty line
887+
- name: meta.return.type.ts
888+
begin: (?<=\))\s*(:)
889+
beginCaptures:
890+
'1': { name: keyword.operator.type.annotation.ts }
891+
end: (?<![:|&])((?=[{};,]|//|^\s*$)|((?<=\S)(?=\s*$)))
892+
patterns:
893+
- include: '#return-type-core'
894+
895+
return-type-core:
882896
patterns:
883897
- include: '#comment'
884898
# Handle returning of object type specifically here so as to not confuse it with the start of function block
885-
- begin: (?<=[:])(?=\s*\{)
899+
- begin: (?<=[:|&])(?=\s*\{)
886900
end: (?<=\})
887901
patterns:
888902
- include: '#type-object'
@@ -894,14 +908,23 @@ repository:
894908
match: (?<!\.|\$)\bis\b(?!\$|\.)
895909

896910
type-annotation:
897-
name: meta.type.annotation.ts
898-
begin: ":"
899-
beginCaptures:
900-
'0': { name: keyword.operator.type.annotation.ts }
901-
end: (?=$|^|[,);\}\]]|//)|(?==[^>])|(?<=[\}>\]\)]|[_$[:alpha:]])\s*(?=\{)
902911
patterns:
903-
- include: '#comment'
904-
- include: '#type'
912+
# if type starts on same line use end of line as end
913+
- name: meta.type.annotation.ts
914+
begin: (:)(?=\s*\S)
915+
beginCaptures:
916+
'1': { name: keyword.operator.type.annotation.ts }
917+
end: (?<![:|&])((?=$|^|[,);\}\]]|//)|(?==[^>])|((?<=[\}>\]\)]|[_$[:alpha:]])\s*(?=\{)))
918+
patterns:
919+
- include: '#type'
920+
# if type starts on next line use modified line endings as end of type annotation eg. complete empty line
921+
- name: meta.type.annotation.ts
922+
begin: (:)
923+
beginCaptures:
924+
'1': { name: keyword.operator.type.annotation.ts }
925+
end: (?<![:|&])((?=[,);\}\]]|//)|(?==[^>])|(?=^\s*$)|((?<=\S)(?=\s*$))|((?<=[\}>\]\)]|[_$[:alpha:]])\s*(?=\{)))
926+
patterns:
927+
- include: '#type'
905928

906929
type:
907930
patterns:
@@ -1014,20 +1037,34 @@ repository:
10141037
match: (?<!\.|\$)\bkeyof\b(?!\$|\.)
10151038

10161039
type-function-return-type:
1017-
name: meta.type.function.return.ts
1018-
begin: '=>'
1019-
beginCaptures:
1020-
'0': { name: storage.type.function.arrow.ts }
1021-
end: (?<!=>)(?<![|&])(?=[,\]\)\{\}=;>]|//|$)
10221040
patterns:
1023-
- include: '#comment'
1024-
# Handle returning of object type specifically here so as to not confuse it with the start of function block
1025-
- begin: (?<==>)(?=\s*\{)
1026-
end: (?<=\})
1041+
# if type starts on same line use end of line as end
1042+
- name: meta.type.function.return.ts
1043+
begin: (=>)(?=\s*\S)
1044+
beginCaptures:
1045+
'1': { name: storage.type.function.arrow.ts }
1046+
end: (?<!=>)(?<![|&])(?=[,\]\)\{\}=;>]|//|$)
10271047
patterns:
1028-
- include: '#type-object'
1029-
- include: '#type-predicate-operator'
1030-
- include: '#type'
1048+
- include: '#type-function-return-type-core'
1049+
# if type starts on next line use modified line endings as end of type annotation eg. complete empty line
1050+
- name: meta.type.function.return.ts
1051+
begin: '=>'
1052+
beginCaptures:
1053+
'0': { name: storage.type.function.arrow.ts }
1054+
end: (?<!=>)(?<![|&])((?=[,\]\)\{\}=;>]|//|^\s*$)|((?<=\S)(?=\s*$)))
1055+
patterns:
1056+
- include: '#type-function-return-type-core'
1057+
1058+
type-function-return-type-core:
1059+
patterns:
1060+
- include: '#comment'
1061+
# Handle returning of object type specifically here so as to not confuse it with the start of function block
1062+
- begin: (?<==>)(?=\s*\{)
1063+
end: (?<=\})
1064+
patterns:
1065+
- include: '#type-object'
1066+
- include: '#type-predicate-operator'
1067+
- include: '#type'
10311068

10321069
type-tuple:
10331070
name: meta.type.tuple.ts

TypeScript.tmLanguage

Lines changed: 141 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -2709,20 +2709,58 @@
27092709
</dict>
27102710
<key>return-type</key>
27112711
<dict>
2712-
<key>name</key>
2713-
<string>meta.return.type.ts</string>
2714-
<key>begin</key>
2715-
<string>(?&lt;=\))\s*(:)</string>
2716-
<key>beginCaptures</key>
2717-
<dict>
2718-
<key>1</key>
2712+
<key>patterns</key>
2713+
<array>
27192714
<dict>
27202715
<key>name</key>
2721-
<string>keyword.operator.type.annotation.ts</string>
2716+
<string>meta.return.type.ts</string>
2717+
<key>begin</key>
2718+
<string>(?&lt;=\))\s*(:)(?=\s*\S)</string>
2719+
<key>beginCaptures</key>
2720+
<dict>
2721+
<key>1</key>
2722+
<dict>
2723+
<key>name</key>
2724+
<string>keyword.operator.type.annotation.ts</string>
2725+
</dict>
2726+
</dict>
2727+
<key>end</key>
2728+
<string>(?&lt;![:|&amp;])(?=$|^|[{};,]|//)</string>
2729+
<key>patterns</key>
2730+
<array>
2731+
<dict>
2732+
<key>include</key>
2733+
<string>#return-type-core</string>
2734+
</dict>
2735+
</array>
27222736
</dict>
2723-
</dict>
2724-
<key>end</key>
2725-
<string>(?&lt;![:|&amp;])(?=$|^|[{};,]|//)</string>
2737+
<dict>
2738+
<key>name</key>
2739+
<string>meta.return.type.ts</string>
2740+
<key>begin</key>
2741+
<string>(?&lt;=\))\s*(:)</string>
2742+
<key>beginCaptures</key>
2743+
<dict>
2744+
<key>1</key>
2745+
<dict>
2746+
<key>name</key>
2747+
<string>keyword.operator.type.annotation.ts</string>
2748+
</dict>
2749+
</dict>
2750+
<key>end</key>
2751+
<string>(?&lt;![:|&amp;])((?=[{};,]|//|^\s*$)|((?&lt;=\S)(?=\s*$)))</string>
2752+
<key>patterns</key>
2753+
<array>
2754+
<dict>
2755+
<key>include</key>
2756+
<string>#return-type-core</string>
2757+
</dict>
2758+
</array>
2759+
</dict>
2760+
</array>
2761+
</dict>
2762+
<key>return-type-core</key>
2763+
<dict>
27262764
<key>patterns</key>
27272765
<array>
27282766
<dict>
@@ -2731,7 +2769,7 @@
27312769
</dict>
27322770
<dict>
27332771
<key>begin</key>
2734-
<string>(?&lt;=[:])(?=\s*\{)</string>
2772+
<string>(?&lt;=[:|&amp;])(?=\s*\{)</string>
27352773
<key>end</key>
27362774
<string>(?&lt;=\})</string>
27372775
<key>patterns</key>
@@ -2761,29 +2799,53 @@
27612799
</dict>
27622800
<key>type-annotation</key>
27632801
<dict>
2764-
<key>name</key>
2765-
<string>meta.type.annotation.ts</string>
2766-
<key>begin</key>
2767-
<string>:</string>
2768-
<key>beginCaptures</key>
2769-
<dict>
2770-
<key>0</key>
2771-
<dict>
2772-
<key>name</key>
2773-
<string>keyword.operator.type.annotation.ts</string>
2774-
</dict>
2775-
</dict>
2776-
<key>end</key>
2777-
<string>(?=$|^|[,);\}\]]|//)|(?==[^&gt;])|(?&lt;=[\}&gt;\]\)]|[_$[:alpha:]])\s*(?=\{)</string>
27782802
<key>patterns</key>
27792803
<array>
27802804
<dict>
2781-
<key>include</key>
2782-
<string>#comment</string>
2805+
<key>name</key>
2806+
<string>meta.type.annotation.ts</string>
2807+
<key>begin</key>
2808+
<string>(:)(?=\s*\S)</string>
2809+
<key>beginCaptures</key>
2810+
<dict>
2811+
<key>1</key>
2812+
<dict>
2813+
<key>name</key>
2814+
<string>keyword.operator.type.annotation.ts</string>
2815+
</dict>
2816+
</dict>
2817+
<key>end</key>
2818+
<string>(?&lt;![:|&amp;])((?=$|^|[,);\}\]]|//)|(?==[^&gt;])|((?&lt;=[\}&gt;\]\)]|[_$[:alpha:]])\s*(?=\{)))</string>
2819+
<key>patterns</key>
2820+
<array>
2821+
<dict>
2822+
<key>include</key>
2823+
<string>#type</string>
2824+
</dict>
2825+
</array>
27832826
</dict>
27842827
<dict>
2785-
<key>include</key>
2786-
<string>#type</string>
2828+
<key>name</key>
2829+
<string>meta.type.annotation.ts</string>
2830+
<key>begin</key>
2831+
<string>(:)</string>
2832+
<key>beginCaptures</key>
2833+
<dict>
2834+
<key>1</key>
2835+
<dict>
2836+
<key>name</key>
2837+
<string>keyword.operator.type.annotation.ts</string>
2838+
</dict>
2839+
</dict>
2840+
<key>end</key>
2841+
<string>(?&lt;![:|&amp;])((?=[,);\}\]]|//)|(?==[^&gt;])|(?=^\s*$)|((?&lt;=\S)(?=\s*$))|((?&lt;=[\}&gt;\]\)]|[_$[:alpha:]])\s*(?=\{)))</string>
2842+
<key>patterns</key>
2843+
<array>
2844+
<dict>
2845+
<key>include</key>
2846+
<string>#type</string>
2847+
</dict>
2848+
</array>
27872849
</dict>
27882850
</array>
27892851
</dict>
@@ -3075,20 +3137,58 @@
30753137
</dict>
30763138
<key>type-function-return-type</key>
30773139
<dict>
3078-
<key>name</key>
3079-
<string>meta.type.function.return.ts</string>
3080-
<key>begin</key>
3081-
<string>=&gt;</string>
3082-
<key>beginCaptures</key>
3083-
<dict>
3084-
<key>0</key>
3140+
<key>patterns</key>
3141+
<array>
30853142
<dict>
30863143
<key>name</key>
3087-
<string>storage.type.function.arrow.ts</string>
3144+
<string>meta.type.function.return.ts</string>
3145+
<key>begin</key>
3146+
<string>(=&gt;)(?=\s*\S)</string>
3147+
<key>beginCaptures</key>
3148+
<dict>
3149+
<key>1</key>
3150+
<dict>
3151+
<key>name</key>
3152+
<string>storage.type.function.arrow.ts</string>
3153+
</dict>
3154+
</dict>
3155+
<key>end</key>
3156+
<string>(?&lt;!=&gt;)(?&lt;![|&amp;])(?=[,\]\)\{\}=;&gt;]|//|$)</string>
3157+
<key>patterns</key>
3158+
<array>
3159+
<dict>
3160+
<key>include</key>
3161+
<string>#type-function-return-type-core</string>
3162+
</dict>
3163+
</array>
30883164
</dict>
3089-
</dict>
3090-
<key>end</key>
3091-
<string>(?&lt;!=&gt;)(?&lt;![|&amp;])(?=[,\]\)\{\}=;&gt;]|//|$)</string>
3165+
<dict>
3166+
<key>name</key>
3167+
<string>meta.type.function.return.ts</string>
3168+
<key>begin</key>
3169+
<string>=&gt;</string>
3170+
<key>beginCaptures</key>
3171+
<dict>
3172+
<key>0</key>
3173+
<dict>
3174+
<key>name</key>
3175+
<string>storage.type.function.arrow.ts</string>
3176+
</dict>
3177+
</dict>
3178+
<key>end</key>
3179+
<string>(?&lt;!=&gt;)(?&lt;![|&amp;])((?=[,\]\)\{\}=;&gt;]|//|^\s*$)|((?&lt;=\S)(?=\s*$)))</string>
3180+
<key>patterns</key>
3181+
<array>
3182+
<dict>
3183+
<key>include</key>
3184+
<string>#type-function-return-type-core</string>
3185+
</dict>
3186+
</array>
3187+
</dict>
3188+
</array>
3189+
</dict>
3190+
<key>type-function-return-type-core</key>
3191+
<dict>
30923192
<key>patterns</key>
30933193
<array>
30943194
<dict>

0 commit comments

Comments
 (0)