Skip to content

Commit ab528e8

Browse files
bors[bot]0xPoe
andauthored
Merge #8831
8831: Apply async semantic token modifier to async/await keywords r=Veykril a=hi-rustin close #8633 Co-authored-by: hi-rustin <[email protected]>
2 parents 9803a9a + 765ccf2 commit ab528e8

File tree

7 files changed

+41
-2
lines changed

7 files changed

+41
-2
lines changed

crates/hir/src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -873,6 +873,10 @@ impl Function {
873873
db.function_data(self.id).is_unsafe()
874874
}
875875

876+
pub fn is_async(self, db: &dyn HirDatabase) -> bool {
877+
db.function_data(self.id).is_async()
878+
}
879+
876880
pub fn diagnostics(self, db: &dyn HirDatabase, sink: &mut DiagnosticSink) {
877881
let krate = self.module(db).id.krate();
878882
hir_def::diagnostics::validate_body(db.upcast(), self.id.into(), sink);

crates/ide/src/syntax_highlighting/highlight.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -227,8 +227,8 @@ pub(super) fn element(
227227
k if k.is_keyword() => {
228228
let h = Highlight::new(HlTag::Keyword);
229229
match k {
230-
T![await]
231-
| T![break]
230+
T![await] => h | HlMod::Async | HlMod::ControlFlow,
231+
T![break]
232232
| T![continue]
233233
| T![else]
234234
| T![if]
@@ -255,6 +255,7 @@ pub(super) fn element(
255255
})
256256
.map(|modifier| h | modifier)
257257
.unwrap_or(h),
258+
T![async] => h | HlMod::Async,
258259
_ => h,
259260
}
260261
}
@@ -310,6 +311,9 @@ fn highlight_def(db: &RootDatabase, def: Definition) -> Highlight {
310311
if func.is_unsafe(db) {
311312
h |= HlMod::Unsafe;
312313
}
314+
if func.is_async(db) {
315+
h |= HlMod::Async;
316+
}
313317
return h;
314318
}
315319
hir::ModuleDef::Adt(hir::Adt::Struct(_)) => HlTag::Symbol(SymbolKind::Struct),
@@ -409,6 +413,9 @@ fn highlight_method_call(
409413
if func.is_unsafe(sema.db) || sema.is_unsafe_method_call(&method_call) {
410414
h |= HlMod::Unsafe;
411415
}
416+
if func.is_async(sema.db) {
417+
h |= HlMod::Async;
418+
}
412419
if func.as_assoc_item(sema.db).and_then(|it| it.containing_trait(sema.db)).is_some() {
413420
h |= HlMod::Trait
414421
}

crates/ide/src/syntax_highlighting/tags.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ pub enum HlMod {
6565
Static,
6666
/// Used for items in traits and trait impls.
6767
Trait,
68+
/// Used with keywords like `async` and `await`.
69+
Async,
6870
// Keep this last!
6971
/// Used for unsafe functions, mutable statics, union accesses and unsafe operations.
7072
Unsafe,
@@ -186,6 +188,7 @@ impl HlMod {
186188
HlMod::Mutable,
187189
HlMod::Static,
188190
HlMod::Trait,
191+
HlMod::Async,
189192
HlMod::Unsafe,
190193
];
191194

@@ -203,6 +206,7 @@ impl HlMod {
203206
HlMod::Mutable => "mutable",
204207
HlMod::Static => "static",
205208
HlMod::Trait => "trait",
209+
HlMod::Async => "async",
206210
HlMod::Unsafe => "unsafe",
207211
}
208212
}

crates/ide/src/syntax_highlighting/test_data/highlighting.html

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,4 +234,15 @@
234234
<span class="variable declaration">Nope</span> <span class="operator">=&gt;</span> <span class="variable">Nope</span><span class="comma">,</span>
235235
<span class="brace">}</span>
236236
<span class="brace">}</span>
237+
<span class="brace">}</span>
238+
239+
<span class="keyword async">async</span> <span class="keyword">fn</span> <span class="function declaration async">learn_and_sing</span><span class="parenthesis">(</span><span class="parenthesis">)</span> <span class="brace">{</span>
240+
<span class="keyword">let</span> <span class="variable declaration">song</span> <span class="operator">=</span> <span class="unresolved_reference">learn_song</span><span class="parenthesis">(</span><span class="parenthesis">)</span><span class="operator">.</span><span class="keyword control async">await</span><span class="semicolon">;</span>
241+
<span class="unresolved_reference">sing_song</span><span class="parenthesis">(</span><span class="variable consuming">song</span><span class="parenthesis">)</span><span class="operator">.</span><span class="keyword control async">await</span><span class="semicolon">;</span>
242+
<span class="brace">}</span>
243+
244+
<span class="keyword async">async</span> <span class="keyword">fn</span> <span class="function declaration async">async_main</span><span class="parenthesis">(</span><span class="parenthesis">)</span> <span class="brace">{</span>
245+
<span class="keyword">let</span> <span class="variable declaration">f1</span> <span class="operator">=</span> <span class="function async">learn_and_sing</span><span class="parenthesis">(</span><span class="parenthesis">)</span><span class="semicolon">;</span>
246+
<span class="keyword">let</span> <span class="variable declaration">f2</span> <span class="operator">=</span> <span class="unresolved_reference">dance</span><span class="parenthesis">(</span><span class="parenthesis">)</span><span class="semicolon">;</span>
247+
futures::<span class="macro">join!</span><span class="parenthesis">(</span>f1<span class="comma">,</span> f2<span class="parenthesis">)</span><span class="semicolon">;</span>
237248
<span class="brace">}</span></code></pre>

crates/ide/src/syntax_highlighting/tests.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,17 @@ impl<T> Option<T> {
208208
}
209209
}
210210
}
211+
212+
async fn learn_and_sing() {
213+
let song = learn_song().await;
214+
sing_song(song).await;
215+
}
216+
217+
async fn async_main() {
218+
let f1 = learn_and_sing();
219+
let f2 = dance();
220+
futures::join!(f1, f2);
221+
}
211222
"#
212223
.trim(),
213224
expect_file!["./test_data/highlighting.html"],

crates/rust-analyzer/src/semantic_tokens.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ define_semantic_token_modifiers![
9191
(INJECTED, "injected"),
9292
(MUTABLE, "mutable"),
9393
(CONSUMING, "consuming"),
94+
(ASYNC, "async"),
9495
(UNSAFE, "unsafe"),
9596
(ATTRIBUTE_MODIFIER, "attribute"),
9697
(TRAIT_MODIFIER, "trait"),

crates/rust-analyzer/src/to_proto.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -496,6 +496,7 @@ fn semantic_token_type_and_modifiers(
496496
HlMod::ControlFlow => semantic_tokens::CONTROL_FLOW,
497497
HlMod::Mutable => semantic_tokens::MUTABLE,
498498
HlMod::Consuming => semantic_tokens::CONSUMING,
499+
HlMod::Async => semantic_tokens::ASYNC,
499500
HlMod::Unsafe => semantic_tokens::UNSAFE,
500501
HlMod::Callable => semantic_tokens::CALLABLE,
501502
HlMod::Static => lsp_types::SemanticTokenModifier::STATIC,

0 commit comments

Comments
 (0)