Skip to content

Commit ccfc27a

Browse files
ehussjasonwilliams
authored andcommitted
Syntax: Improve visibility modifier. (#299)
1 parent f653ba3 commit ccfc27a

File tree

2 files changed

+133
-42
lines changed

2 files changed

+133
-42
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.rust
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
@@ -265,6 +255,25 @@ contexts:
265255
- match: '[-=<>&|!~@?+*/%^''#$]'
266256
scope: keyword.operator.rust
267257

258+
visibility:
259+
- match: '\b(pub)\s*(\()'
260+
captures:
261+
1: storage.modifier.rust
262+
2: punctuation.definition.group.begin.rust
263+
push:
264+
- include: comments
265+
- match: '\)'
266+
scope: punctuation.definition.group.end.rust
267+
pop: true
268+
- match: '(crate|in|self|super)'
269+
scope: keyword.other.rust
270+
- match: '::'
271+
scope: meta.path.rust
272+
- match: '{{identifier}}'
273+
scope: meta.path.rust
274+
- match: '\bpub\b'
275+
scope: storage.modifier.rust
276+
268277
attribute:
269278
- match: '#!?\['
270279
push:
@@ -511,6 +520,7 @@ contexts:
511520
scope: keyword.operator.rust
512521

513522
struct-identifier:
523+
- meta_scope: meta.struct.rust
514524
- match: '{{identifier}}(?=<)'
515525
scope: entity.name.struct.rust
516526
set:
@@ -542,14 +552,12 @@ contexts:
542552
pop: true
543553
- match: '\('
544554
scope: punctuation.definition.group.begin.rust
545-
# Ensure that we end the tuple at the next ) to
546-
# prevent odd highlighting as the user is typing
547-
with_prototype:
555+
push:
548556
- match: '(?=\))'
549557
pop: true
550-
push:
551558
- meta_scope: meta.group.rust
552559
- include: comments
560+
- include: visibility
553561
- include: type-any-identifier
554562

555563
struct-classic:
@@ -567,8 +575,7 @@ contexts:
567575
pop: true
568576
- include: comments
569577
- include: attribute
570-
- match: \bpub\b
571-
scope: storage.modifier.rust
578+
- include: visibility
572579
- match: '{{identifier}}(?=\s*:)'
573580
scope: variable.other.member.rust
574581
push:
@@ -581,6 +588,7 @@ contexts:
581588

582589

583590
union-identifier:
591+
- meta_scope: meta.union.rust
584592
- match: '{{identifier}}(?=<)'
585593
scope: entity.name.union.rust
586594
set:

syntax_test_rust.rs

Lines changed: 89 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ extern extern crate simd_rng_derive;*/
3232
// This one is just to visually confirm the testing comments don't intefere
3333
#[macro_use]
3434
extern crate std_web;
35-
/*#[macro_use]
35+
/*#[macro_use]
3636
extern extern crate simd_rng_derive;*/
3737

3838
let c = 'c';
@@ -755,7 +755,7 @@ fn my_other_func(e: OperatingSystem) -> u32 {
755755
// Test highlighting/scope with struct field attributes
756756
// https://github.com/rust-lang/sublime-rust/issues/120
757757
pub struct Claim {
758-
// ^^^^^^^^^ meta.struct
758+
// ^^^^^^^^ meta.struct
759759
pub claim_id: String,
760760
// ^^^ storage.modifier.rust
761761
pub patient_id: String,
@@ -888,7 +888,7 @@ pub fn from_buf_reader<T>(s: io::BufReader<T>) -> Result<isize, &'static str>
888888
}
889889

890890
pub mod my_mod {
891-
//^^^^^^^^^^^^^^ meta.module
891+
// ^^^^^^^^^^^^ meta.module
892892
// <- storage.modifier
893893
// ^^^ storage.type.module
894894
// ^^^^^^ entity.name.module
@@ -953,7 +953,7 @@ impl<'a, T: MyTrait + OtherTrait> PrintInOption for T where
953953

954954
pub trait Animal {
955955
// <- storage.modifier
956-
//^^^^^^^^^^^^^^^^ meta.trait
956+
// ^^^^^^^^^^^^^^ meta.trait
957957
// ^ meta.block punctuation.definition.block.begin
958958
fn noise(quiet: bool) {
959959
// Comment
@@ -1265,7 +1265,7 @@ struct A<T>(T) where T: AsRef<str>;
12651265
// ^ punctuation.terminator
12661266
// ^^^^^ meta.struct meta.where keyword.other
12671267
pub struct A<T>(T)
1268-
//^^^^^^^^^^^^ meta.struct
1268+
// ^^^^^^^^^^ meta.struct
12691269
// ^^^^^^ meta.struct storage.type
12701270
where
12711271
//^^^ meta.struct meta.where keyword.other
@@ -1323,7 +1323,7 @@ union Union {
13231323

13241324
pub union Foo<'a, Y: Baz>
13251325
// <- storage.modifier
1326-
//^^^^^^^^^^^^^^^^^^^^^^^ meta.union
1326+
// ^^^^^^^^^^^^^^^^^^^^^ meta.union
13271327
// ^^^^^ meta.union storage.type.union
13281328
// ^^^ meta.union meta.generic entity.name.union
13291329
// ^ meta.union meta.generic meta.generic punctuation.definition.generic.begin
@@ -1352,3 +1352,86 @@ impl<A> Thing for &'a mut A {}
13521352
// ^^ meta.impl storage.modifier.lifetime
13531353
// ^^^ meta.impl storage.modifier
13541354
// ^ meta.impl entity.name.impl
1355+
1356+
pub ( crate ) struct S {}
1357+
// <- storage.modifier
1358+
// ^ punctuation.definition.group.begin
1359+
// ^^^^^ keyword.other
1360+
// ^ punctuation.definition.group.end
1361+
// ^^^^^^^^^^^ meta.struct
1362+
pub ( in foo::bar ) union U {}
1363+
// ^ punctuation.definition.group.begin
1364+
// ^^ keyword.other
1365+
// ^^^^^^^^ meta.path
1366+
// ^ punctuation.definition.group.end
1367+
// ^^^^^^^^^^ meta.union
1368+
pub ( in foo :: bar ) type T = i32;
1369+
// ^ punctuation.definition.group.begin
1370+
// ^^ keyword.other
1371+
// ^^^ meta.path
1372+
// ^^ meta.path
1373+
// ^^^ meta.path
1374+
// ^ punctuation.definition.group.end
1375+
// ^^^^ storage.type.type
1376+
pub ( in ::foo ) fn f() {}
1377+
// ^^^^^ meta.path
1378+
// ^^^^^^^^^ meta.function
1379+
pub ( self ) mod m {}
1380+
// ^^^^ keyword.other
1381+
// ^^^^^^^^ meta.module
1382+
pub ( super ) use a::b;
1383+
// ^^^^^ keyword.other
1384+
// ^^^ keyword.other
1385+
pub ( in self ) enum E {A,B}
1386+
// ^^ keyword.other
1387+
// ^^^^ keyword.other
1388+
// ^^^^^^^^^^^^ meta.enum
1389+
pub ( in super ) const CONST: i32 = 1;
1390+
// ^^ keyword.other
1391+
// ^^^^^ keyword.other
1392+
// ^^^^^ storage.type
1393+
pub ( in super::super ) static STATIC: i32 = 1;
1394+
// ^^ keyword.other
1395+
// ^^^^^ keyword.other
1396+
// ^^ meta.path
1397+
// ^^^^^ keyword.other
1398+
// ^^^^^^ storage.type
1399+
1400+
struct S {
1401+
pub f1: i32,
1402+
// ^^^ meta.struct storage.modifier
1403+
// ^^ meta.struct variable.other.member
1404+
pub(crate) f2: i32,
1405+
// ^^^ meta.struct storage.modifier
1406+
// ^ meta.struct punctuation.definition.group.begin
1407+
// ^^^^^ meta.struct keyword.other
1408+
// ^ meta.struct punctuation.definition.group.end
1409+
// ^^ meta.struct variable.other.member
1410+
pub(in super::foo) f3: i32,
1411+
// ^^^ meta.struct storage.modifier
1412+
// ^ meta.struct punctuation.definition.group.begin
1413+
// ^^ meta.struct keyword.other
1414+
// ^^^^^ meta.struct keyword.other
1415+
// ^^^^^ meta.struct meta.path
1416+
// ^ meta.struct punctuation.definition.group.end
1417+
// ^^ meta.struct variable.other.member
1418+
}
1419+
1420+
struct S (
1421+
pub i32,
1422+
// ^^^ meta.struct storage.modifier
1423+
// ^^^ meta.struct storage.type
1424+
pub(crate) i32,
1425+
// ^^^ meta.struct storage.modifier
1426+
// ^ meta.struct punctuation.definition.group.begin
1427+
// ^^^^^ meta.struct keyword.other
1428+
// ^ meta.struct punctuation.definition.group.end
1429+
// ^^^ meta.struct storage.type
1430+
pub(in super) i32,
1431+
// ^^^ meta.struct storage.modifier
1432+
// ^ meta.struct punctuation.definition.group.begin
1433+
// ^^ meta.struct keyword.other
1434+
// ^^^^^ meta.struct keyword.other
1435+
// ^ meta.struct punctuation.definition.group.end
1436+
// ^^^ meta.struct storage.type
1437+
);

0 commit comments

Comments
 (0)