Skip to content

Commit 761796f

Browse files
ehussjasonwilliams
authored andcommitted
Add union to syntax. (rust-lang#260)
* Add union to syntax. * Add test to verify that `union` is not a keyword.
1 parent 65a5c32 commit 761796f

File tree

2 files changed

+87
-14
lines changed

2 files changed

+87
-14
lines changed

RustEnhanced.sublime-syntax

Lines changed: 51 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,13 @@ contexts:
6767
2: storage.type.struct.rust
6868
push: struct-identifier
6969

70+
- match: '\b(?:(pub)\s+)?(union)\s+'
71+
scope: meta.union.rust
72+
captures:
73+
1: storage.modifier.rust
74+
2: storage.type.union.rust
75+
push: union-identifier
76+
7077
- match: '\b(?:(pub)\s+)?(type)\s+({{identifier}})\b'
7178
captures:
7279
1: storage.modifier.rust
@@ -547,23 +554,53 @@ contexts:
547554
pop: true
548555
- match: '\{'
549556
scope: punctuation.definition.block.begin.rust
557+
push: struct-classic-body
558+
559+
struct-classic-body:
560+
- meta_scope: meta.block.rust
561+
- match: '(?=\})'
562+
pop: true
563+
- include: comments
564+
- include: attribute
565+
- match: \bpub\b
566+
scope: storage.modifier.rust
567+
- match: '{{identifier}}(?=\s*:)'
568+
scope: variable.other.member.rust
550569
push:
551-
- meta_scope: meta.block.rust
552-
- match: '(?=\})'
570+
- match: ',|(?=\})'
553571
pop: true
554572
- include: comments
555-
- include: attribute
556-
- match: \bpub\b
557-
scope: storage.modifier.rust
558-
- match: '{{identifier}}(?=\s*:)'
559-
scope: variable.other.member.rust
560-
push:
561-
- match: ',|(?=\})'
562-
pop: true
563-
- include: comments
564-
- match: ':'
565-
scope: punctuation.separator.rust
566-
- include: type-any-identifier
573+
- match: ':'
574+
scope: punctuation.separator.rust
575+
- include: type-any-identifier
576+
577+
578+
union-identifier:
579+
- match: '{{identifier}}(?=<)'
580+
scope: entity.name.union.rust
581+
set:
582+
- meta_scope: meta.union.rust meta.generic.rust
583+
- match: '(?=<)'
584+
push: generic-angles
585+
- match: ''
586+
set: union-body
587+
- match: '{{identifier}}'
588+
scope: entity.name.union.rust
589+
set: union-body
590+
591+
union-body:
592+
- meta_scope: meta.union.rust
593+
- include: comments
594+
- match: '(?=\bwhere\b)'
595+
push: impl-where
596+
- match: '\{'
597+
scope: punctuation.definition.block.begin.rust
598+
push: struct-classic-body
599+
- match: '\}'
600+
scope: meta.block.rust punctuation.definition.block.end.rust
601+
pop: true
602+
- match: '(?=;)'
603+
pop: true
567604

568605
macro-block:
569606
- meta_scope: meta.macro.rust

syntax_test_rust.rs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1167,3 +1167,39 @@ fn main() {
11671167
}.collect::<Vec<_>>();
11681168
println!("{:?}", l);
11691169
}
1170+
1171+
union Union {
1172+
//^^^ meta.union storage.type.union
1173+
//^^^^^^^^^^^ meta.union
1174+
// ^^^^^ entity.name.union
1175+
// ^ meta.block punctuation.definition.block.begin
1176+
f: u32,
1177+
// ^ meta.union meta.block variable.other.member
1178+
// ^ meta.union meta.block punctuation.separator
1179+
// ^^^ meta.union meta.block storage.type
1180+
}
1181+
// <- meta.union meta.block punctuation.definition.block.end
1182+
1183+
pub union Foo<'a, Y: Baz>
1184+
// <- storage.modifier
1185+
//^^^^^^^^^^^^^^^^^^^^^^^ meta.union
1186+
// ^^^^^ meta.union storage.type.union
1187+
// ^^^ meta.union meta.generic entity.name.union
1188+
// ^ meta.union meta.generic meta.generic punctuation.definition.generic.begin
1189+
// ^^ meta.union meta.generic meta.generic storage.modifier.lifetime
1190+
where X: Whatever,
1191+
// ^^^^^ meta.union meta.where keyword.other
1192+
// ^ meta.union meta.where
1193+
// ^ meta.union meta.where punctuation.separator
1194+
// ^^^^^^^^^^ meta.union meta.where
1195+
{
1196+
// <- meta.union meta.block punctuation.definition.block.begin
1197+
f: SomeType, // Comment beside a field
1198+
// ^^^^^^^^^^^^^^^^^^^^^^^^^^ meta.union meta.block comment.line.double-slash
1199+
}
1200+
// <- meta.union meta.block punctuation.definition.block.end
1201+
1202+
// Union was implemented in such a way that `union` is not a keyword. Verify
1203+
// that we don't accidentally interpret it as a keyword.
1204+
fn union() {}
1205+
// ^^^^^ meta.function entity.name.function

0 commit comments

Comments
 (0)