Skip to content

Commit 2c35d11

Browse files
committed
Syntax: Improve visibility modifier.
1 parent 18edc8a commit 2c35d11

File tree

2 files changed

+132
-41
lines changed

2 files changed

+132
-41
lines changed

RustEnhanced.sublime-syntax

Lines changed: 44 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ contexts:
2525

2626
statements:
2727

28+
- include: visibility
29+
2830
- match: ';'
2931
scope: punctuation.terminator
3032

@@ -36,11 +38,10 @@ contexts:
3638
- match: '''{{identifier}}(?!\'')\b'
3739
scope: storage.modifier.lifetime.rust
3840

39-
- match: '\b(?:(pub)\s+)?\b(mod)\s+({{identifier}})\b'
41+
- match: '\b(mod)\s+({{identifier}})\b'
4042
captures:
41-
1: storage.modifier.rust
42-
2: storage.type.module.rust
43-
3: entity.name.module.rust
43+
1: storage.type.module.rust
44+
2: entity.name.module.rust
4445
push:
4546
- meta_scope: meta.module.rust
4647
- match: ';'
@@ -53,44 +54,34 @@ contexts:
5354
2: keyword.operator.rust
5455
push: closure
5556

56-
- match: '\b(?:(pub)\s+)?\b(fn)\s+(?={{identifier}})'
57-
scope: meta.function.rust
57+
- match: '\b(fn)\s+(?={{identifier}})'
5858
captures:
59-
1: storage.modifier.rust
60-
2: storage.type.function.rust
59+
1: storage.type.function.rust
6160
push: fn-definition
6261

63-
- match: '\b(?:(pub)\s+)?(struct)\s+'
64-
scope: meta.struct.rust
65-
captures:
66-
1: storage.modifier.rust
67-
2: storage.type.struct.rust
62+
- match: '\bstruct\b'
63+
scope: storage.type.struct.rust
6864
push: struct-identifier
6965

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
66+
- match: '\bunion\b'
67+
scope: storage.type.union.rust
7568
push: union-identifier
7669

77-
- match: '\b(?:(pub)\s+)?(type)\s+({{identifier}})\b'
70+
- match: '\b(type)\s+({{identifier}})\b'
7871
captures:
79-
1: storage.modifier.rust
80-
2: storage.type.type.rust
81-
3: entity.name.type.rust
72+
1: storage.type.type.rust
73+
2: entity.name.type.rust
8274
push:
8375
- match: '=(?!=)'
8476
scope: keyword.operator.rust
8577
push: after-operator
8678
- match: '(?=\S)'
8779
pop: true
8880

89-
- match: '\b(?:(pub)\s+)?(trait)\s+({{identifier}})\b'
81+
- match: '\b(trait)\s+({{identifier}})\b'
9082
captures:
91-
1: storage.modifier.rust
92-
2: storage.type.trait.rust
93-
3: entity.name.trait.rust
83+
1: storage.type.trait.rust
84+
2: entity.name.trait.rust
9485
push:
9586
- meta_scope: meta.trait.rust
9687
- include: statements-block
@@ -99,11 +90,10 @@ contexts:
9990
scope: storage.type.impl.rust
10091
push: impl-definition
10192

102-
- match: '\b(?:(pub)\s+)?(enum)\s+({{identifier}})\b'
93+
- match: '\b(enum)\s+({{identifier}})\b'
10394
captures:
104-
1: storage.modifier.rust
105-
2: storage.type.enum.rust
106-
3: entity.name.enum.rust
95+
1: storage.type.enum.rust
96+
2: entity.name.enum.rust
10797
push:
10898
- meta_scope: meta.enum.rust
10999
- include: statements-block
@@ -260,6 +250,25 @@ contexts:
260250
- match: '[-=<>&|!~@?+*/%^''#$]'
261251
scope: keyword.operator.rust
262252

253+
visibility:
254+
- match: '\b(pub)\s*(\()'
255+
captures:
256+
1: storage.modifier.rust
257+
2: punctuation.definition.group.begin.rust
258+
push:
259+
- include: comments
260+
- match: '\)'
261+
scope: punctuation.definition.group.end.rust
262+
pop: true
263+
- match: '(crate|in|self|super)'
264+
scope: keyword.other.rust
265+
- match: '::'
266+
scope: meta.path.rust
267+
- match: '{{identifier}}'
268+
scope: meta.path.rust
269+
- match: '\bpub\b'
270+
scope: storage.modifier.rust
271+
263272
attribute:
264273
- match: '#!?\['
265274
push:
@@ -506,6 +515,7 @@ contexts:
506515
scope: keyword.operator.rust
507516

508517
struct-identifier:
518+
- meta_scope: meta.struct.rust
509519
- match: '{{identifier}}(?=<)'
510520
scope: entity.name.struct.rust
511521
set:
@@ -537,14 +547,12 @@ contexts:
537547
pop: true
538548
- match: '\('
539549
scope: punctuation.definition.group.begin.rust
540-
# Ensure that we end the tuple at the next ) to
541-
# prevent odd highlighting as the user is typing
542-
with_prototype:
550+
push:
543551
- match: '(?=\))'
544552
pop: true
545-
push:
546553
- meta_scope: meta.group.rust
547554
- include: comments
555+
- include: visibility
548556
- include: type-any-identifier
549557

550558
struct-classic:
@@ -562,8 +570,7 @@ contexts:
562570
pop: true
563571
- include: comments
564572
- include: attribute
565-
- match: \bpub\b
566-
scope: storage.modifier.rust
573+
- include: visibility
567574
- match: '{{identifier}}(?=\s*:)'
568575
scope: variable.other.member.rust
569576
push:
@@ -576,6 +583,7 @@ contexts:
576583

577584

578585
union-identifier:
586+
- meta_scope: meta.union.rust
579587
- match: '{{identifier}}(?=<)'
580588
scope: entity.name.union.rust
581589
set:

syntax_test_rust.rs

Lines changed: 88 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -619,7 +619,7 @@ fn my_other_func(e: OperatingSystem) -> u32 {
619619
// Test highlighting/scope with struct field attributes
620620
// https://github.com/rust-lang/sublime-rust/issues/120
621621
pub struct Claim {
622-
// ^^^^^^^^^ meta.struct
622+
// ^^^^^^^^ meta.struct
623623
pub claim_id: String,
624624
// ^^^ storage.modifier.rust
625625
pub patient_id: String,
@@ -747,7 +747,7 @@ pub fn from_buf_reader<T>(s: io::BufReader<T>) -> Result<isize, &'static str>
747747
}
748748

749749
pub mod my_mod {
750-
//^^^^^^^^^^^^^^ meta.module
750+
// ^^^^^^^^^^^^ meta.module
751751
// <- storage.modifier
752752
// ^^^ storage.type.module
753753
// ^^^^^^ entity.name.module
@@ -812,7 +812,7 @@ impl<'a, T: MyTrait + OtherTrait> PrintInOption for T where
812812

813813
pub trait Animal {
814814
// <- storage.modifier
815-
//^^^^^^^^^^^^^^^^ meta.trait
815+
// ^^^^^^^^^^^^^^ meta.trait
816816
// ^ meta.block punctuation.definition.block.begin
817817
fn noise(quiet: bool) {
818818
// Comment
@@ -1124,7 +1124,7 @@ struct A<T>(T) where T: AsRef<str>;
11241124
// ^ punctuation.terminator
11251125
// ^^^^^ meta.struct meta.where keyword.other
11261126
pub struct A<T>(T)
1127-
//^^^^^^^^^^^^ meta.struct
1127+
// ^^^^^^^^^^ meta.struct
11281128
// ^^^^^^ meta.struct storage.type
11291129
where
11301130
//^^^ meta.struct meta.where keyword.other
@@ -1182,7 +1182,7 @@ union Union {
11821182

11831183
pub union Foo<'a, Y: Baz>
11841184
// <- storage.modifier
1185-
//^^^^^^^^^^^^^^^^^^^^^^^ meta.union
1185+
// ^^^^^^^^^^^^^^^^^^^^^ meta.union
11861186
// ^^^^^ meta.union storage.type.union
11871187
// ^^^ meta.union meta.generic entity.name.union
11881188
// ^ meta.union meta.generic meta.generic punctuation.definition.generic.begin
@@ -1203,3 +1203,86 @@ pub union Foo<'a, Y: Baz>
12031203
// that we don't accidentally interpret it as a keyword.
12041204
fn union() {}
12051205
// ^^^^^ meta.function entity.name.function
1206+
1207+
pub ( crate ) struct S {}
1208+
// <- storage.modifier
1209+
// ^ punctuation.definition.group.begin
1210+
// ^^^^^ keyword.other
1211+
// ^ punctuation.definition.group.end
1212+
// ^^^^^^^^^^^ meta.struct
1213+
pub ( in foo::bar ) union U {}
1214+
// ^ punctuation.definition.group.begin
1215+
// ^^ keyword.other
1216+
// ^^^^^^^^ meta.path
1217+
// ^ punctuation.definition.group.end
1218+
// ^^^^^^^^^^ meta.union
1219+
pub ( in foo :: bar ) type T = i32;
1220+
// ^ punctuation.definition.group.begin
1221+
// ^^ keyword.other
1222+
// ^^^ meta.path
1223+
// ^^ meta.path
1224+
// ^^^ meta.path
1225+
// ^ punctuation.definition.group.end
1226+
// ^^^^ storage.type.type
1227+
pub ( in ::foo ) fn f() {}
1228+
// ^^^^^ meta.path
1229+
// ^^^^^^^^^ meta.function
1230+
pub ( self ) mod m {}
1231+
// ^^^^ keyword.other
1232+
// ^^^^^^^^ meta.module
1233+
pub ( super ) use a::b;
1234+
// ^^^^^ keyword.other
1235+
// ^^^ keyword.other
1236+
pub ( in self ) enum E {A,B}
1237+
// ^^ keyword.other
1238+
// ^^^^ keyword.other
1239+
// ^^^^^^^^^^^^ meta.enum
1240+
pub ( in super ) const CONST: i32 = 1;
1241+
// ^^ keyword.other
1242+
// ^^^^^ keyword.other
1243+
// ^^^^^ storage.type
1244+
pub ( in super::super ) static STATIC: i32 = 1;
1245+
// ^^ keyword.other
1246+
// ^^^^^ keyword.other
1247+
// ^^ meta.path
1248+
// ^^^^^ keyword.other
1249+
// ^^^^^^ storage.type
1250+
1251+
struct S {
1252+
pub f1: i32,
1253+
// ^^^ meta.struct storage.modifier
1254+
// ^^ meta.struct variable.other.member
1255+
pub(crate) f2: i32,
1256+
// ^^^ meta.struct storage.modifier
1257+
// ^ meta.struct punctuation.definition.group.begin
1258+
// ^^^^^ meta.struct keyword.other
1259+
// ^ meta.struct punctuation.definition.group.end
1260+
// ^^ meta.struct variable.other.member
1261+
pub(in super::foo) f3: i32,
1262+
// ^^^ meta.struct storage.modifier
1263+
// ^ meta.struct punctuation.definition.group.begin
1264+
// ^^ meta.struct keyword.other
1265+
// ^^^^^ meta.struct keyword.other
1266+
// ^^^^^ meta.struct meta.path
1267+
// ^ meta.struct punctuation.definition.group.end
1268+
// ^^ meta.struct variable.other.member
1269+
}
1270+
1271+
struct S (
1272+
pub i32,
1273+
// ^^^ meta.struct storage.modifier
1274+
// ^^^ meta.struct storage.type
1275+
pub(crate) i32,
1276+
// ^^^ meta.struct storage.modifier
1277+
// ^ meta.struct punctuation.definition.group.begin
1278+
// ^^^^^ meta.struct keyword.other
1279+
// ^ meta.struct punctuation.definition.group.end
1280+
// ^^^ meta.struct storage.type
1281+
pub(in super) i32,
1282+
// ^^^ meta.struct storage.modifier
1283+
// ^ meta.struct punctuation.definition.group.begin
1284+
// ^^ meta.struct keyword.other
1285+
// ^^^^^ meta.struct keyword.other
1286+
// ^ meta.struct punctuation.definition.group.end
1287+
// ^^^ meta.struct storage.type
1288+
);

0 commit comments

Comments
 (0)