Skip to content

Commit

Permalink
[Rust] Use keyword.declaration scope
Browse files Browse the repository at this point in the history
Following the example of Java, we're replacing the `storage.type` scope
with `keyword.declaration` without keeping the `storage.type` for
compatibility. Also, like in Java, the `use` keyword gets a
`keyword.declaration.import` scope to signal that it is introducing
names into the current namespace.

`type` aliases have slightly been refactored to properly match `where`
clauses. (I did a bit of work twice here because I forgot to `git pull`
before and included the remainder …)
  • Loading branch information
FichteFoll committed Jan 28, 2024
1 parent 0647aa3 commit 7f7708c
Show file tree
Hide file tree
Showing 15 changed files with 163 additions and 94 deletions.
109 changes: 66 additions & 43 deletions Rust/Rust.sublime-syntax
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,7 @@ contexts:

- include: visibility

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

- match: '(''(?:{{non_raw_ident}}))\s*(:)'
captures:
Expand Down Expand Up @@ -112,13 +111,11 @@ contexts:
modules:
- match: '\b(mod)\s+({{identifier}})\b'
captures:
1: storage.type.module.rust
1: keyword.declaration.module.rust
2: entity.name.module.rust
push:
- meta_scope: meta.module.rust
- match: ';'
scope: punctuation.terminator.rust
pop: true
- include: terminator
- include: module-block

module-block:
Expand All @@ -133,32 +130,24 @@ contexts:

- match: '\b(fn)\s+(?={{identifier}})'
captures:
1: storage.type.function.rust
1: keyword.declaration.function.rust
push: fn-definition

- match: '\bstruct\b'
scope: storage.type.struct.rust
scope: keyword.declaration.struct.rust
push: struct-identifier

- match: '\bunion\b(?!\s*\()'
scope: storage.type.union.rust
scope: keyword.declaration.union.rust
push: union-identifier

- match: '\b(type)\s+({{identifier}})\b'
captures:
1: storage.type.type.rust
2: entity.name.type.rust
push:
- match: (?=<)
push: generic-angles
- match: '=(?!=)'
scope: keyword.operator.assignment.rust
set: type-declaration-body
- include: else-pop
- match: '\btype\b'
scope: keyword.declaration.type.rust
push: type-declaration-identifier

- match: '\b(trait)\s+({{identifier}})\b'
captures:
1: storage.type.trait.rust
1: keyword.declaration.trait.rust
2: entity.name.trait.rust
push:
- meta_scope: meta.trait.rust
Expand All @@ -171,11 +160,11 @@ contexts:
- include: trait-block

- match: '\bimpl\b'
scope: storage.type.impl.rust
scope: keyword.declaration.impl.rust
push: impl-definition

- match: '\benum\b'
scope: storage.type.enum.rust
scope: keyword.declaration.enum.rust
push: enum-identifier

- include: raw-pointer
Expand All @@ -187,20 +176,20 @@ contexts:

- match: '\b(const)\s+({{identifier}})'
captures:
1: storage.type.rust
1: keyword.declaration.rust
2: entity.name.constant.rust
push: declaration-after-identifier

- match: '\b(static)\s+(?:(mut)\s+)?({{identifier}})'
captures:
1: storage.type.rust
1: keyword.declaration.rust
2: storage.modifier.rust
3: entity.name.constant.rust
push: declaration-after-identifier

- match: \b(let)\s+(?:(mut)\s+)?
captures:
1: storage.type.rust
1: keyword.declaration.rust
2: storage.modifier.rust
push: declaration-identifier

Expand Down Expand Up @@ -496,10 +485,6 @@ contexts:

##[ TYPES ]#################################################################

type-declaration-body:
- include: types
- include: else-pop

types-relaxed:
# only the most obvious type constructs (for fallback matching)
- match: (?=[!?]\s*{{type_identifier}})
Expand Down Expand Up @@ -866,6 +851,31 @@ contexts:
- meta_scope: meta.union.rust meta.block.rust
- include: struct-classic-body

##[ TYPE DECLARATIONS ]#####################################################

type-declaration-identifier:
- meta_scope: meta.type-declaration.rust
- include: comments
- include: type-declaration-wheres # deprecated
- match: '{{identifier}}'
scope: entity.name.type.rust
- match: '(?=<)'
push: generic-angles
- match: '=(?!=)'
scope: keyword.operator.assignment.rust
set: type-declaration-body
- include: else-pop

type-declaration-body:
- meta_content_scope: meta.type-declaration.rust
- include: type-declaration-wheres
- include: types
- include: else-pop

type-declaration-wheres:
- match: '(?=\bwhere\b)'
push: impl-where

##[ ENUMS ]#################################################################

enum-identifier:
Expand Down Expand Up @@ -1109,9 +1119,7 @@ contexts:

macro-semi-sep:
- include: comments
- match: ';'
scope: punctuation.terminator.rust
pop: true
- include: terminator
- match: '(?=[})\]])'
pop: true
- match: '\S'
Expand Down Expand Up @@ -1171,7 +1179,7 @@ contexts:
impl-where:
- meta_scope: meta.where.rust
- include: comments
- match: '(?=(\{|;))'
- match: '(?=(\{|;|=))'
pop: true
- match: \bwhere\b
scope: keyword.other.rust
Expand Down Expand Up @@ -1735,36 +1743,42 @@ contexts:
# specific rules.
- include: bool

- match: \b(let|const|static)\b
- match: \b(let|static)\b
scope: keyword.declaration.rust

- match: \bconst\b
scope: storage.type.rust

- match: \bfn\b
scope: storage.type.function.rust
scope: keyword.declaration.function.rust

- match: \bmod\b
scope: storage.type.module.rust
scope: keyword.declaration.module.rust

- match: \bstruct\b
scope: storage.type.struct.rust
scope: keyword.declaration.struct.rust

- match: \bimpl\b
scope: storage.type.impl.rust
scope: keyword.declaration.impl.rust

- match: \benum\b
scope: storage.type.enum.rust
scope: keyword.declaration.enum.rust

- match: \btype\b
scope: storage.type.type.rust
scope: keyword.declaration.type.rust

- match: \btrait\b
scope: storage.type.trait.rust
scope: keyword.declaration.trait.rust

- match: \b(mut|pub|unsafe|move|ref)\b
scope: storage.modifier.rust

- match: \b(crate|extern|use|where)\b
- match: \b(crate|extern|where)\b
scope: keyword.other.rust

- match: \buse\b
scope: keyword.declaration.import.rust

- match: \b(async|await|else|for|if|loop|match|try|while|yield)\b
scope: keyword.control.rust

Expand All @@ -1789,6 +1803,15 @@ contexts:
- match: \b(virtual|become|priv|typeof|unsized|do|abstract|final|override|macro|for)\b
scope: invalid.illegal.rust

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

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

else-pop:
- match: (?=\S)
pop: true
Expand Down
6 changes: 4 additions & 2 deletions Rust/tests/syntax_test_attributes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@
// Test highlighting/scope with struct field attributes
// https://github.com/rust-lang/sublime-rust/issues/120
pub struct Claim {
// ^^^^^^^^ meta.struct
// ^^^^^^^^^^^^^^^ meta.struct.rust
// ^^^^^^ keyword.declaration.struct.rust
pub claim_id: String,
// ^^^ storage.modifier
pub patient_id: String,
Expand Down Expand Up @@ -74,6 +75,7 @@ enum E {

// Generic parameters.
unsafe impl<#[may_dangle] T: ?Sized> Drop for Box<T> { }
// ^^^^ keyword.declaration.impl
// ^^^^^^^^^^^^^ meta.annotation
// ^^^^^^^^^^^^^^^^^^^^^^^^^ meta.impl meta.generic

Expand Down Expand Up @@ -120,7 +122,7 @@ unsafe impl<#[may_dangle] T: ?Sized> Drop for Box<T> { }
true,
// ^^^^ constant.language
struct,
// ^^^^^^ storage.type.struct
// ^^^^^^ keyword.declaration.struct
1 + 1,
// ^ constant.numeric.integer.decimal
// ^ keyword.operator.arithmetic
Expand Down
4 changes: 2 additions & 2 deletions Rust/tests/syntax_test_control_flow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ if n < 0 {
// <- meta.block punctuation.section.block.end

if let BasicStruct(i) = j {
// ^^^ storage.type
// ^^^ keyword.declaration
// ^ keyword.operator.assignment
// ^ meta.block punctuation.section.block.begin
println!("Basic value: {}", i);
Expand All @@ -80,7 +80,7 @@ if let BasicStruct(i) = j {

while let BasicStruct(k) = j {
//^^^ keyword.control
// ^^^ storage.type
// ^^^ keyword.declaration
// ^ keyword.operator.assignment
// ^ meta.block punctuation.section.block.begin
println!("Constructed example: {}", j)
Expand Down
2 changes: 1 addition & 1 deletion Rust/tests/syntax_test_enum.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// SYNTAX TEST "Packages/Rust/Rust.sublime-syntax"

enum OperatingSystem
// <- storage.type.enum
// <- keyword.declaration.enum
// ^^^^^^^^^^^^^^^^^ meta.enum
// ^^^^^^^^^^^^^^^ entity.name.enum
{
Expand Down
3 changes: 3 additions & 0 deletions Rust/tests/syntax_test_expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
// specific categories.

let big_n =
// <- keyword.declaration
// <- keyword.declaration
//^ keyword.declaration
// ^ keyword.operator.assignment
if n < 10 && n > -10 {
// ^ keyword.operator.comparison
Expand Down
14 changes: 7 additions & 7 deletions Rust/tests/syntax_test_functions.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// SYNTAX TEST "Packages/Rust/Rust.sublime-syntax"

fn my_func(x: i32)
// <- storage.type.function
// <- keyword.declaration.function
// ^^^^^^^ entity.name.function
// ^^^^^^^^ meta.function.parameters
// ^ punctuation.section.parameters.begin
Expand All @@ -15,7 +15,7 @@ fn my_func(x: i32)
// <- meta.function meta.block punctuation.section.block.end

fn foo<A>(i: u32, b: i64) -> u32 {
// <- storage.type.function
// <- keyword.declaration.function
// ^^^ entity.name.function
// ^ punctuation.definition.generic.begin
// ^ punctuation.definition.generic.end
Expand Down Expand Up @@ -47,7 +47,7 @@ fn my_other_func(e: OperatingSystem) -> &'a Type {

pub fn pub_function() -> bool
// <- storage.modifier
// ^^ storage.type.function
// ^^ keyword.declaration.function
// ^^^^^^^^^^^^ entity.name.function
{
// <- meta.function
Expand All @@ -61,7 +61,7 @@ pub unsafe extern "C" fn __sync_synchronize() { }
// ^^^^^^ keyword.other
// ^^^ string.quoted.double
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^ meta.function
// ^^ storage.type.function
// ^^ keyword.declaration.function
// ^^^^^^^^^^^^^^^^^^ entity.name.function
// ^ meta.function.parameters punctuation.section.parameters.begin
// ^ meta.function.parameters punctuation.section.parameters.end
Expand Down Expand Up @@ -117,20 +117,20 @@ fn f(self,

const fn f() {}
// <- storage.modifier
// ^^ meta.function storage.type.function
// ^^ meta.function keyword.declaration.function
// ^ meta.function entity.name.function

const unsafe fn f() {}
// <- storage.modifier
// ^^^^^^ storage.modifier
// ^^ meta.function storage.type.function
// ^^ meta.function keyword.declaration.function
// ^ meta.function entity.name.function

const extern "C" fn f() {}
// <- storage.modifier
// ^^^^^^ keyword.other
// ^^^ string.quoted.double
// ^^ meta.function storage.type.function
// ^^ meta.function keyword.declaration.function
// ^ meta.function entity.name.function

fn foo(&'a self) {}
Expand Down
4 changes: 2 additions & 2 deletions Rust/tests/syntax_test_generics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ struct A<T>(T) where T: AsRef<str>;
// ^^^^^ meta.struct meta.where keyword.other
pub struct A<T>(T)
// ^^^^^^^^^^ meta.struct
// ^^^^^^ meta.struct storage.type
// ^^^^^^ meta.struct keyword.declaration
where
//^^^ meta.struct meta.where keyword.other
T: AsRef<str>;
Expand Down Expand Up @@ -342,7 +342,7 @@ fn f(a: for<'a, 'b> fn() -> String) {}

// Function in type path with return type.
fn factory() -> Box<Fn(i32) -> i32> {
// <- storage.type.function
// <- keyword.declaration.function
// ^^^^^^^ entity.name.function
// ^^^^^^^^^^^^^^^^ meta.generic
// ^^ support.type
Expand Down
Loading

0 comments on commit 7f7708c

Please sign in to comment.