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/ide/src/syntax_highlighting/highlight.rs | 7 +++++++ crates/ide/src/syntax_highlighting/tags.rs | 4 ++++ 2 files changed, 11 insertions(+) (limited to 'crates/ide/src/syntax_highlighting') 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", } } -- cgit v1.2.3 From 765ccf2eca2c21af4e2d76b006809446d0034bc7 Mon Sep 17 00:00:00 2001 From: hi-rustin Date: Fri, 14 May 2021 17:30:00 +0800 Subject: Address comments --- crates/ide/src/syntax_highlighting/highlight.rs | 6 +++--- .../ide/src/syntax_highlighting/test_data/highlighting.html | 11 +++++++++++ crates/ide/src/syntax_highlighting/tests.rs | 11 +++++++++++ 3 files changed, 25 insertions(+), 3 deletions(-) (limited to 'crates/ide/src/syntax_highlighting') diff --git a/crates/ide/src/syntax_highlighting/highlight.rs b/crates/ide/src/syntax_highlighting/highlight.rs index bea506544..baed8e217 100644 --- a/crates/ide/src/syntax_highlighting/highlight.rs +++ b/crates/ide/src/syntax_highlighting/highlight.rs @@ -227,8 +227,8 @@ pub(super) fn element( k if k.is_keyword() => { let h = Highlight::new(HlTag::Keyword); match k { - T![await] - | T![break] + T![await] => h | HlMod::Async | HlMod::ControlFlow, + T![break] | T![continue] | T![else] | T![if] @@ -255,7 +255,7 @@ pub(super) fn element( }) .map(|modifier| h | modifier) .unwrap_or(h), - T![async] | T![await] => h | HlMod::Async, + T![async] => h | HlMod::Async, _ => h, } } diff --git a/crates/ide/src/syntax_highlighting/test_data/highlighting.html b/crates/ide/src/syntax_highlighting/test_data/highlighting.html index df4192194..33bc6b0f3 100644 --- a/crates/ide/src/syntax_highlighting/test_data/highlighting.html +++ b/crates/ide/src/syntax_highlighting/test_data/highlighting.html @@ -234,4 +234,15 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd Nope => Nope, } } +} + +async fn learn_and_sing() { + let song = learn_song().await; + sing_song(song).await; +} + +async fn async_main() { + let f1 = learn_and_sing(); + let f2 = dance(); + futures::join!(f1, f2); } \ No newline at end of file diff --git a/crates/ide/src/syntax_highlighting/tests.rs b/crates/ide/src/syntax_highlighting/tests.rs index b6e952b08..32f2d9038 100644 --- a/crates/ide/src/syntax_highlighting/tests.rs +++ b/crates/ide/src/syntax_highlighting/tests.rs @@ -208,6 +208,17 @@ impl Option { } } } + +async fn learn_and_sing() { + let song = learn_song().await; + sing_song(song).await; +} + +async fn async_main() { + let f1 = learn_and_sing(); + let f2 = dance(); + futures::join!(f1, f2); +} "# .trim(), expect_file!["./test_data/highlighting.html"], -- cgit v1.2.3