Skip to content

Commit e0fb5e0

Browse files
committed
Handle '|' and '&' type operators such that they can handle new lines better
With this change spaces followed by this operator will be consumed and that will help not tripping end of type regex in many scenarios We also handle { followed by these operators to handle constituent object type Fixes #421
1 parent 3e592ec commit e0fb5e0

10 files changed

+208
-194
lines changed

TypeScript.YAML-tmLanguage

Lines changed: 23 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -499,10 +499,6 @@ repository:
499499
endCaptures:
500500
'0': { name: punctuation.definition.block.ts }
501501
patterns:
502-
- include: '#type-object-members'
503-
504-
type-object-members:
505-
patterns:
506502
- include: '#comment'
507503
- include: '#method-declaration'
508504
- include: '#indexer-declaration'
@@ -518,6 +514,7 @@ repository:
518514
- include: '#punctuation-comma'
519515
- include: '#punctuation-semicolon'
520516
- include: '#type'
517+
521518

522519
field-declaration:
523520
name: meta.field.declaration.ts
@@ -841,15 +838,10 @@ repository:
841838
patterns:
842839
- include: '#comment'
843840
# Handle returning of object type specifically here so as to not confuse it with the start of function block
844-
- name: meta.object.type.ts
845-
begin: (?<=[:|&])\s*(\{)
846-
beginCaptures:
847-
'1': { name: punctuation.definition.block.ts }
848-
end: \}
849-
endCaptures:
850-
'0': { name: punctuation.definition.block.ts }
841+
- begin: (?<=[:])(?=\s*\{)
842+
end: (?<=\})
851843
patterns:
852-
- include: '#type-object-members'
844+
- include: '#type-object'
853845
- include: '#type-predicate-operator'
854846
- include: '#type'
855847

@@ -961,8 +953,19 @@ repository:
961953
type-operators:
962954
patterns:
963955
- include: '#typeof-operator'
964-
- name: keyword.operator.type.ts
965-
match: '[&|]'
956+
# Handle the object types followed by | or & operator as { can be used as end of Type in many places and this avoids tripping that
957+
- begin: ([&|])(?=\s*\{)
958+
beginCaptures:
959+
'0': { name: keyword.operator.type.ts }
960+
end: (?<=\})
961+
patterns:
962+
- include: '#type-object'
963+
# capture all white spaces followed by the | or & operator so that line break (which is end regex for many type patterns)
964+
# will be consumed and we will continue to be in type space
965+
- begin: '[&|]'
966+
beginCaptures:
967+
'0': { name: keyword.operator.type.ts }
968+
end: (?=\S)
966969
- name: keyword.operator.expression.keyof.ts
967970
match: (?<!\.|\$)\bkeyof\b(?!\$|\.)
968971

@@ -975,15 +978,10 @@ repository:
975978
patterns:
976979
- include: '#comment'
977980
# Handle returning of object type specifically here so as to not confuse it with the start of function block
978-
- name: meta.object.type.ts
979-
begin: (?:(?<==>)|(?<=[|&]))\s*(\{)
980-
beginCaptures:
981-
'1': { name: punctuation.definition.block.ts }
982-
end: \}
983-
endCaptures:
984-
'0': { name: punctuation.definition.block.ts }
981+
- begin: (?<==>)(?=\s*\{)
982+
end: (?<=\})
985983
patterns:
986-
- include: '#type-object-members'
984+
- include: '#type-object'
987985
- include: '#type-predicate-operator'
988986
- include: '#type'
989987

@@ -1672,15 +1670,10 @@ repository:
16721670
patterns:
16731671
# TODO: handle the fn and constructor type specifically.
16741672
# Handle returning of object type specifically here so as to not confuse it with the start of function block
1675-
- name: meta.object.type.ts
1676-
begin: (?<=[:|&])\s*(\{)
1677-
beginCaptures:
1678-
'1': { name: punctuation.definition.block.ts }
1679-
end: \}
1680-
endCaptures:
1681-
'0': { name: punctuation.definition.block.ts }
1673+
- begin: (?<=[:])(?=\s*\{)
1674+
end: (?<=\})
16821675
patterns:
1683-
- include: '#type-object-members'
1676+
- include: '#type-object'
16841677
- include: '#type-predicate-operator'
16851678
- include: '#type'
16861679

TypeScript.tmLanguage

Lines changed: 41 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -1581,16 +1581,6 @@
15811581
<string>punctuation.definition.block.ts</string>
15821582
</dict>
15831583
</dict>
1584-
<key>patterns</key>
1585-
<array>
1586-
<dict>
1587-
<key>include</key>
1588-
<string>#type-object-members</string>
1589-
</dict>
1590-
</array>
1591-
</dict>
1592-
<key>type-object-members</key>
1593-
<dict>
15941584
<key>patterns</key>
15951585
<array>
15961586
<dict>
@@ -2593,33 +2583,15 @@
25932583
<string>#comment</string>
25942584
</dict>
25952585
<dict>
2596-
<key>name</key>
2597-
<string>meta.object.type.ts</string>
25982586
<key>begin</key>
2599-
<string>(?&lt;=[:|&amp;])\s*(\{)</string>
2600-
<key>beginCaptures</key>
2601-
<dict>
2602-
<key>1</key>
2603-
<dict>
2604-
<key>name</key>
2605-
<string>punctuation.definition.block.ts</string>
2606-
</dict>
2607-
</dict>
2587+
<string>(?&lt;=[:])(?=\s*\{)</string>
26082588
<key>end</key>
2609-
<string>\}</string>
2610-
<key>endCaptures</key>
2611-
<dict>
2612-
<key>0</key>
2613-
<dict>
2614-
<key>name</key>
2615-
<string>punctuation.definition.block.ts</string>
2616-
</dict>
2617-
</dict>
2589+
<string>(?&lt;=\})</string>
26182590
<key>patterns</key>
26192591
<array>
26202592
<dict>
26212593
<key>include</key>
2622-
<string>#type-object-members</string>
2594+
<string>#type-object</string>
26232595
</dict>
26242596
</array>
26252597
</dict>
@@ -2912,10 +2884,39 @@
29122884
<string>#typeof-operator</string>
29132885
</dict>
29142886
<dict>
2915-
<key>name</key>
2916-
<string>keyword.operator.type.ts</string>
2917-
<key>match</key>
2887+
<key>begin</key>
2888+
<string>([&amp;|])(?=\s*\{)</string>
2889+
<key>beginCaptures</key>
2890+
<dict>
2891+
<key>0</key>
2892+
<dict>
2893+
<key>name</key>
2894+
<string>keyword.operator.type.ts</string>
2895+
</dict>
2896+
</dict>
2897+
<key>end</key>
2898+
<string>(?&lt;=\})</string>
2899+
<key>patterns</key>
2900+
<array>
2901+
<dict>
2902+
<key>include</key>
2903+
<string>#type-object</string>
2904+
</dict>
2905+
</array>
2906+
</dict>
2907+
<dict>
2908+
<key>begin</key>
29182909
<string>[&amp;|]</string>
2910+
<key>beginCaptures</key>
2911+
<dict>
2912+
<key>0</key>
2913+
<dict>
2914+
<key>name</key>
2915+
<string>keyword.operator.type.ts</string>
2916+
</dict>
2917+
</dict>
2918+
<key>end</key>
2919+
<string>(?=\S)</string>
29192920
</dict>
29202921
<dict>
29212922
<key>name</key>
@@ -2948,33 +2949,15 @@
29482949
<string>#comment</string>
29492950
</dict>
29502951
<dict>
2951-
<key>name</key>
2952-
<string>meta.object.type.ts</string>
29532952
<key>begin</key>
2954-
<string>(?:(?&lt;==&gt;)|(?&lt;=[|&amp;]))\s*(\{)</string>
2955-
<key>beginCaptures</key>
2956-
<dict>
2957-
<key>1</key>
2958-
<dict>
2959-
<key>name</key>
2960-
<string>punctuation.definition.block.ts</string>
2961-
</dict>
2962-
</dict>
2953+
<string>(?&lt;==&gt;)(?=\s*\{)</string>
29632954
<key>end</key>
2964-
<string>\}</string>
2965-
<key>endCaptures</key>
2966-
<dict>
2967-
<key>0</key>
2968-
<dict>
2969-
<key>name</key>
2970-
<string>punctuation.definition.block.ts</string>
2971-
</dict>
2972-
</dict>
2955+
<string>(?&lt;=\})</string>
29732956
<key>patterns</key>
29742957
<array>
29752958
<dict>
29762959
<key>include</key>
2977-
<string>#type-object-members</string>
2960+
<string>#type-object</string>
29782961
</dict>
29792962
</array>
29802963
</dict>
@@ -4582,33 +4565,15 @@
45824565
<key>patterns</key>
45834566
<array>
45844567
<dict>
4585-
<key>name</key>
4586-
<string>meta.object.type.ts</string>
45874568
<key>begin</key>
4588-
<string>(?&lt;=[:|&amp;])\s*(\{)</string>
4589-
<key>beginCaptures</key>
4590-
<dict>
4591-
<key>1</key>
4592-
<dict>
4593-
<key>name</key>
4594-
<string>punctuation.definition.block.ts</string>
4595-
</dict>
4596-
</dict>
4569+
<string>(?&lt;=[:])(?=\s*\{)</string>
45974570
<key>end</key>
4598-
<string>\}</string>
4599-
<key>endCaptures</key>
4600-
<dict>
4601-
<key>0</key>
4602-
<dict>
4603-
<key>name</key>
4604-
<string>punctuation.definition.block.ts</string>
4605-
</dict>
4606-
</dict>
4571+
<string>(?&lt;=\})</string>
46074572
<key>patterns</key>
46084573
<array>
46094574
<dict>
46104575
<key>include</key>
4611-
<string>#type-object-members</string>
4576+
<string>#type-object</string>
46124577
</dict>
46134578
</array>
46144579
</dict>

0 commit comments

Comments
 (0)