From b98c119ba690afdcd21edb3b344834e42422df0a Mon Sep 17 00:00:00 2001 From: hi-rustin Date: Fri, 14 May 2021 10:36:12 +0800 Subject: Apply async semantic token modifier to async/await keywords Only async semantic token modifier --- crates/hir/src/lib.rs | 4 ++++ crates/ide/src/syntax_highlighting/highlight.rs | 7 +++++++ crates/ide/src/syntax_highlighting/tags.rs | 4 ++++ crates/rust-analyzer/src/semantic_tokens.rs | 1 + crates/rust-analyzer/src/to_proto.rs | 1 + 5 files changed, 17 insertions(+) diff --git a/crates/hir/src/lib.rs b/crates/hir/src/lib.rs index c9ef4b420..6233bca83 100644 --- a/crates/hir/src/lib.rs +++ b/crates/hir/src/lib.rs @@ -873,6 +873,10 @@ impl Function { db.function_data(self.id).is_unsafe() } + pub fn is_async(self, db: &dyn HirDatabase) -> bool { + db.function_data(self.id).is_async() + } + pub fn diagnostics(self, db: &dyn HirDatabase, sink: &mut DiagnosticSink) { let krate = self.module(db).id.krate(); hir_def::diagnostics::validate_body(db.upcast(), self.id.into(), sink); diff --git a/crates/ide/src/syntax_highlighting/highlight.rs b/crates/ide/src/syntax_highlighting/highlight.rs index b586dcc17..bea506544 100644 --- a/crates/ide/src/syntax_highlighting/highlight.rs +++ b/crates/ide/src/syntax_highlighting/highlight.rs @@ -255,6 +255,7 @@ pub(super) fn element( }) .map(|modifier| h | modifier) .unwrap_or(h), + T![async] | T![await] => h | HlMod::Async, _ => h, } } @@ -310,6 +311,9 @@ fn highlight_def(db: &RootDatabase, def: Definition) -> Highlight { if func.is_unsafe(db) { h |= HlMod::Unsafe; } + if func.is_async(db) { + h |= HlMod::Async; + } return h; } hir::ModuleDef::Adt(hir::Adt::Struct(_)) => HlTag::Symbol(SymbolKind::Struct), @@ -409,6 +413,9 @@ fn highlight_method_call( if func.is_unsafe(sema.db) || sema.is_unsafe_method_call(&method_call) { h |= HlMod::Unsafe; } + if func.is_async(sema.db) { + h |= HlMod::Async; + } if func.as_assoc_item(sema.db).and_then(|it| it.containing_trait(sema.db)).is_some() { h |= HlMod::Trait } diff --git a/crates/ide/src/syntax_highlighting/tags.rs b/crates/ide/src/syntax_highlighting/tags.rs index a304b3250..f4a2e7506 100644 --- a/crates/ide/src/syntax_highlighting/tags.rs +++ b/crates/ide/src/syntax_highlighting/tags.rs @@ -65,6 +65,8 @@ pub enum HlMod { Static, /// Used for items in traits and trait impls. Trait, + /// Used with keywords like `async` and `await`. + Async, // Keep this last! /// Used for unsafe functions, mutable statics, union accesses and unsafe operations. Unsafe, @@ -186,6 +188,7 @@ impl HlMod { HlMod::Mutable, HlMod::Static, HlMod::Trait, + HlMod::Async, HlMod::Unsafe, ]; @@ -203,6 +206,7 @@ impl HlMod { HlMod::Mutable => "mutable", HlMod::Static => "static", HlMod::Trait => "trait", + HlMod::Async => "async", HlMod::Unsafe => "unsafe", } } diff --git a/crates/rust-analyzer/src/semantic_tokens.rs b/crates/rust-analyzer/src/semantic_tokens.rs index ecab89b2a..c9d38693e 100644 --- a/crates/rust-analyzer/src/semantic_tokens.rs +++ b/crates/rust-analyzer/src/semantic_tokens.rs @@ -91,6 +91,7 @@ define_semantic_token_modifiers![ (INJECTED, "injected"), (MUTABLE, "mutable"), (CONSUMING, "consuming"), + (ASYNC, "async"), (UNSAFE, "unsafe"), (ATTRIBUTE_MODIFIER, "attribute"), (TRAIT_MODIFIER, "trait"), diff --git a/crates/rust-analyzer/src/to_proto.rs b/crates/rust-analyzer/src/to_proto.rs index ecf6fd12f..8ce4aa242 100644 --- a/crates/rust-analyzer/src/to_proto.rs +++ b/crates/rust-analyzer/src/to_proto.rs @@ -496,6 +496,7 @@ fn semantic_token_type_and_modifiers( HlMod::ControlFlow => semantic_tokens::CONTROL_FLOW, HlMod::Mutable => semantic_tokens::MUTABLE, HlMod::Consuming => semantic_tokens::CONSUMING, + HlMod::Async => semantic_tokens::ASYNC, HlMod::Unsafe => semantic_tokens::UNSAFE, HlMod::Callable => semantic_tokens::CALLABLE, HlMod::Static => lsp_types::SemanticTokenModifier::STATIC, -- cgit v1.2.3