Skip to content

Syntax: Improve visibility modifier. #299

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 30, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 44 additions & 36 deletions RustEnhanced.sublime-syntax
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ contexts:

statements:

- include: visibility

- match: ';'
scope: punctuation.terminator.rust

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

- match: '\b(?:(pub)\s+)?\b(mod)\s+({{identifier}})\b'
- match: '\b(mod)\s+({{identifier}})\b'
captures:
1: storage.modifier.rust
2: storage.type.module.rust
3: entity.name.module.rust
1: storage.type.module.rust
2: entity.name.module.rust
push:
- meta_scope: meta.module.rust
- match: ';'
Expand All @@ -53,44 +54,34 @@ contexts:
2: keyword.operator.rust
push: closure

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

- match: '\b(?:(pub)\s+)?(struct)\s+'
scope: meta.struct.rust
captures:
1: storage.modifier.rust
2: storage.type.struct.rust
- match: '\bstruct\b'
scope: storage.type.struct.rust
push: struct-identifier

- match: '\b(?:(pub)\s+)?(union)\s+'
scope: meta.union.rust
captures:
1: storage.modifier.rust
2: storage.type.union.rust
- match: '\bunion\b'
scope: storage.type.union.rust
push: union-identifier

- match: '\b(?:(pub)\s+)?(type)\s+({{identifier}})\b'
- match: '\b(type)\s+({{identifier}})\b'
captures:
1: storage.modifier.rust
2: storage.type.type.rust
3: entity.name.type.rust
1: storage.type.type.rust
2: entity.name.type.rust
push:
- match: '=(?!=)'
scope: keyword.operator.rust
push: after-operator
- match: '(?=\S)'
pop: true

- match: '\b(?:(pub)\s+)?(trait)\s+({{identifier}})\b'
- match: '\b(trait)\s+({{identifier}})\b'
captures:
1: storage.modifier.rust
2: storage.type.trait.rust
3: entity.name.trait.rust
1: storage.type.trait.rust
2: entity.name.trait.rust
push:
- meta_scope: meta.trait.rust
- include: statements-block
Expand All @@ -99,11 +90,10 @@ contexts:
scope: storage.type.impl.rust
push: impl-definition

- match: '\b(?:(pub)\s+)?(enum)\s+({{identifier}})\b'
- match: '\b(enum)\s+({{identifier}})\b'
captures:
1: storage.modifier.rust
2: storage.type.enum.rust
3: entity.name.enum.rust
1: storage.type.enum.rust
2: entity.name.enum.rust
push:
- meta_scope: meta.enum.rust
- include: statements-block
Expand Down Expand Up @@ -265,6 +255,25 @@ contexts:
- match: '[-=<>&|!~@?+*/%^''#$]'
scope: keyword.operator.rust

visibility:
- match: '\b(pub)\s*(\()'
captures:
1: storage.modifier.rust
2: punctuation.definition.group.begin.rust
push:
- include: comments
- match: '\)'
scope: punctuation.definition.group.end.rust
pop: true
- match: '(crate|in|self|super)'
scope: keyword.other.rust
- match: '::'
scope: meta.path.rust
- match: '{{identifier}}'
scope: meta.path.rust
- match: '\bpub\b'
scope: storage.modifier.rust

attribute:
- match: '#!?\['
push:
Expand Down Expand Up @@ -511,6 +520,7 @@ contexts:
scope: keyword.operator.rust

struct-identifier:
- meta_scope: meta.struct.rust
- match: '{{identifier}}(?=<)'
scope: entity.name.struct.rust
set:
Expand Down Expand Up @@ -542,14 +552,12 @@ contexts:
pop: true
- match: '\('
scope: punctuation.definition.group.begin.rust
# Ensure that we end the tuple at the next ) to
# prevent odd highlighting as the user is typing
with_prototype:
push:
- match: '(?=\))'
pop: true
push:
- meta_scope: meta.group.rust
- include: comments
- include: visibility
- include: type-any-identifier

struct-classic:
Expand All @@ -567,8 +575,7 @@ contexts:
pop: true
- include: comments
- include: attribute
- match: \bpub\b
scope: storage.modifier.rust
- include: visibility
- match: '{{identifier}}(?=\s*:)'
scope: variable.other.member.rust
push:
Expand All @@ -581,6 +588,7 @@ contexts:


union-identifier:
- meta_scope: meta.union.rust
- match: '{{identifier}}(?=<)'
scope: entity.name.union.rust
set:
Expand Down
95 changes: 89 additions & 6 deletions syntax_test_rust.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ extern extern crate simd_rng_derive;*/
// This one is just to visually confirm the testing comments don't intefere
#[macro_use]
extern crate std_web;
/*#[macro_use]
/*#[macro_use]
extern extern crate simd_rng_derive;*/

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

pub mod my_mod {
//^^^^^^^^^^^^^^ meta.module
// ^^^^^^^^^^^^ meta.module
// <- storage.modifier
// ^^^ storage.type.module
// ^^^^^^ entity.name.module
Expand Down Expand Up @@ -953,7 +953,7 @@ impl<'a, T: MyTrait + OtherTrait> PrintInOption for T where

pub trait Animal {
// <- storage.modifier
//^^^^^^^^^^^^^^^^ meta.trait
// ^^^^^^^^^^^^^^ meta.trait
// ^ meta.block punctuation.definition.block.begin
fn noise(quiet: bool) {
// Comment
Expand Down Expand Up @@ -1265,7 +1265,7 @@ struct A<T>(T) where T: AsRef<str>;
// ^ punctuation.terminator
// ^^^^^ meta.struct meta.where keyword.other
pub struct A<T>(T)
//^^^^^^^^^^^^ meta.struct
// ^^^^^^^^^^ meta.struct
// ^^^^^^ meta.struct storage.type
where
//^^^ meta.struct meta.where keyword.other
Expand Down Expand Up @@ -1323,7 +1323,7 @@ union Union {

pub union Foo<'a, Y: Baz>
// <- storage.modifier
//^^^^^^^^^^^^^^^^^^^^^^^ meta.union
// ^^^^^^^^^^^^^^^^^^^^^ meta.union
// ^^^^^ meta.union storage.type.union
// ^^^ meta.union meta.generic entity.name.union
// ^ meta.union meta.generic meta.generic punctuation.definition.generic.begin
Expand Down Expand Up @@ -1352,3 +1352,86 @@ impl<A> Thing for &'a mut A {}
// ^^ meta.impl storage.modifier.lifetime
// ^^^ meta.impl storage.modifier
// ^ meta.impl entity.name.impl

pub ( crate ) struct S {}
// <- storage.modifier
// ^ punctuation.definition.group.begin
// ^^^^^ keyword.other
// ^ punctuation.definition.group.end
// ^^^^^^^^^^^ meta.struct
pub ( in foo::bar ) union U {}
// ^ punctuation.definition.group.begin
// ^^ keyword.other
// ^^^^^^^^ meta.path
// ^ punctuation.definition.group.end
// ^^^^^^^^^^ meta.union
pub ( in foo :: bar ) type T = i32;
// ^ punctuation.definition.group.begin
// ^^ keyword.other
// ^^^ meta.path
// ^^ meta.path
// ^^^ meta.path
// ^ punctuation.definition.group.end
// ^^^^ storage.type.type
pub ( in ::foo ) fn f() {}
// ^^^^^ meta.path
// ^^^^^^^^^ meta.function
pub ( self ) mod m {}
// ^^^^ keyword.other
// ^^^^^^^^ meta.module
pub ( super ) use a::b;
// ^^^^^ keyword.other
// ^^^ keyword.other
pub ( in self ) enum E {A,B}
// ^^ keyword.other
// ^^^^ keyword.other
// ^^^^^^^^^^^^ meta.enum
pub ( in super ) const CONST: i32 = 1;
// ^^ keyword.other
// ^^^^^ keyword.other
// ^^^^^ storage.type
pub ( in super::super ) static STATIC: i32 = 1;
// ^^ keyword.other
// ^^^^^ keyword.other
// ^^ meta.path
// ^^^^^ keyword.other
// ^^^^^^ storage.type

struct S {
pub f1: i32,
// ^^^ meta.struct storage.modifier
// ^^ meta.struct variable.other.member
pub(crate) f2: i32,
// ^^^ meta.struct storage.modifier
// ^ meta.struct punctuation.definition.group.begin
// ^^^^^ meta.struct keyword.other
// ^ meta.struct punctuation.definition.group.end
// ^^ meta.struct variable.other.member
pub(in super::foo) f3: i32,
// ^^^ meta.struct storage.modifier
// ^ meta.struct punctuation.definition.group.begin
// ^^ meta.struct keyword.other
// ^^^^^ meta.struct keyword.other
// ^^^^^ meta.struct meta.path
// ^ meta.struct punctuation.definition.group.end
// ^^ meta.struct variable.other.member
}

struct S (
pub i32,
// ^^^ meta.struct storage.modifier
// ^^^ meta.struct storage.type
pub(crate) i32,
// ^^^ meta.struct storage.modifier
// ^ meta.struct punctuation.definition.group.begin
// ^^^^^ meta.struct keyword.other
// ^ meta.struct punctuation.definition.group.end
// ^^^ meta.struct storage.type
pub(in super) i32,
// ^^^ meta.struct storage.modifier
// ^ meta.struct punctuation.definition.group.begin
// ^^ meta.struct keyword.other
// ^^^^^ meta.struct keyword.other
// ^ meta.struct punctuation.definition.group.end
// ^^^ meta.struct storage.type
);