Skip to content

Commit 9d473a0

Browse files
bors[bot]Veykril
andauthored
Merge #11616
11616: fix: Fix semantic highlighting breaking for lifetimes in macros r=Veykril a=Veykril bors r+ Co-authored-by: Lukas Wirth <[email protected]>
2 parents 59088d8 + 464dd81 commit 9d473a0

File tree

3 files changed

+88
-5
lines changed

3 files changed

+88
-5
lines changed

crates/ide/src/syntax_highlighting.rs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,7 @@ fn traverse(
206206

207207
let mut current_macro_call: Option<ast::MacroCall> = None;
208208
let mut current_attr_call = None;
209+
let mut current_derive_call = None;
209210
let mut current_macro: Option<ast::Macro> = None;
210211
let mut macro_highlighter = MacroHighlighter::default();
211212
let mut inside_attribute = false;
@@ -249,7 +250,7 @@ fn traverse(
249250
};
250251
match adt {
251252
Some(adt) if sema.is_derive_annotated(&adt) => {
252-
current_attr_call = Some(adt.into());
253+
current_derive_call = Some(ast::Item::from(adt));
253254
}
254255
_ => (),
255256
}
@@ -273,7 +274,10 @@ fn traverse(
273274
macro_highlighter = MacroHighlighter::default();
274275
}
275276
Some(item) if current_attr_call.as_ref().map_or(false, |it| *it == item) => {
276-
current_attr_call = None
277+
current_attr_call = None;
278+
}
279+
Some(item) if current_derive_call.as_ref().map_or(false, |it| *it == item) => {
280+
current_derive_call = None;
277281
}
278282
None if ast::Attr::can_cast(node.kind()) => inside_attribute = false,
279283
_ => (),
@@ -301,17 +305,19 @@ fn traverse(
301305
// as calling `descend_into_macros_single` gets rather expensive if done for every single token
302306
// additionally, do not descend into comments, descending maps down to doc attributes which get
303307
// tagged as string literals.
304-
let descend_token = (current_macro_call.is_some() || current_attr_call.is_some())
308+
let descend_token = (current_macro_call.is_some()
309+
|| current_attr_call.is_some()
310+
|| current_derive_call.is_some())
305311
&& element.kind() != COMMENT;
306312
let element_to_highlight = if descend_token {
307313
let token = match &element {
308314
NodeOrToken::Node(_) => continue,
309315
NodeOrToken::Token(tok) => tok.clone(),
310316
};
311-
let in_mcall_outside_tt = current_macro_call.is_some()
317+
let in_mcall_outside_tt = current_attr_call.is_none()
312318
&& token.parent().as_ref().map(SyntaxNode::kind) != Some(TOKEN_TREE);
313319
let token = match in_mcall_outside_tt {
314-
// not in the macros token tree, don't attempt to descend
320+
// not in the macros/derives token tree, don't attempt to descend
315321
true => token,
316322
false => sema.descend_into_macros_single(token),
317323
};
@@ -323,6 +329,7 @@ fn traverse(
323329
(T![ident], NAME | NAME_REF) => parent.into(),
324330
(T![self] | T![super] | T![crate], NAME_REF) => parent.into(),
325331
(INT_NUMBER, NAME_REF) => parent.into(),
332+
(LIFETIME_IDENT, LIFETIME) => parent.into(),
326333
_ => token.into(),
327334
}
328335
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
2+
<style>
3+
body { margin: 0; }
4+
pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padding: 0.4em; }
5+
6+
.lifetime { color: #DFAF8F; font-style: italic; }
7+
.label { color: #DFAF8F; font-style: italic; }
8+
.comment { color: #7F9F7F; }
9+
.documentation { color: #629755; }
10+
.intra_doc_link { font-style: italic; }
11+
.injected { opacity: 0.65 ; }
12+
.struct, .enum { color: #7CB8BB; }
13+
.enum_variant { color: #BDE0F3; }
14+
.string_literal { color: #CC9393; }
15+
.field { color: #94BFF3; }
16+
.function { color: #93E0E3; }
17+
.function.unsafe { color: #BC8383; }
18+
.trait.unsafe { color: #BC8383; }
19+
.operator.unsafe { color: #BC8383; }
20+
.mutable.unsafe { color: #BC8383; text-decoration: underline; }
21+
.keyword.unsafe { color: #BC8383; font-weight: bold; }
22+
.parameter { color: #94BFF3; }
23+
.text { color: #DCDCCC; }
24+
.type { color: #7CB8BB; }
25+
.builtin_type { color: #8CD0D3; }
26+
.type_param { color: #DFAF8F; }
27+
.attribute { color: #94BFF3; }
28+
.numeric_literal { color: #BFEBBF; }
29+
.bool_literal { color: #BFE6EB; }
30+
.macro { color: #94BFF3; }
31+
.derive { color: #94BFF3; font-style: italic; }
32+
.module { color: #AFD8AF; }
33+
.value_param { color: #DCDCCC; }
34+
.variable { color: #DCDCCC; }
35+
.format_specifier { color: #CC696B; }
36+
.mutable { text-decoration: underline; }
37+
.escape_sequence { color: #94BFF3; }
38+
.keyword { color: #F0DFAF; font-weight: bold; }
39+
.control { font-style: italic; }
40+
.reference { font-style: italic; font-weight: bold; }
41+
42+
.unresolved_reference { color: #FC5555; text-decoration: wavy underline; }
43+
</style>
44+
<pre><code>
45+
<span class="attribute_bracket attribute">#</span><span class="attribute_bracket attribute">[</span><span class="attribute attribute default_library library">derive</span><span class="parenthesis attribute">(</span><span class="parenthesis attribute">)</span><span class="attribute_bracket attribute">]</span>
46+
<span class="keyword">struct</span> <span class="struct declaration">Foo</span><span class="angle">&lt;</span><span class="lifetime declaration">'a</span><span class="comma">,</span> <span class="lifetime declaration">'b</span><span class="comma">,</span> <span class="lifetime declaration">'c</span><span class="angle">&gt;</span> <span class="keyword">where</span> <span class="lifetime">'a</span><span class="colon">:</span> <span class="lifetime">'a</span><span class="comma">,</span> <span class="lifetime">'static</span><span class="colon">:</span> <span class="lifetime">'static</span> <span class="brace">{</span>
47+
<span class="field declaration">field</span><span class="colon">:</span> <span class="operator">&</span><span class="lifetime">'a</span> <span class="parenthesis">(</span><span class="parenthesis">)</span><span class="comma">,</span>
48+
<span class="field declaration">field2</span><span class="colon">:</span> <span class="operator">&</span><span class="lifetime">'static</span> <span class="parenthesis">(</span><span class="parenthesis">)</span><span class="comma">,</span>
49+
<span class="brace">}</span>
50+
<span class="keyword">impl</span><span class="angle">&lt;</span><span class="lifetime declaration">'a</span><span class="angle">&gt;</span> <span class="struct">Foo</span><span class="angle">&lt;</span><span class="lifetime">'_</span><span class="comma">,</span> <span class="lifetime">'a</span><span class="comma">,</span> <span class="lifetime">'static</span><span class="angle">&gt;</span>
51+
<span class="keyword">where</span>
52+
<span class="lifetime">'a</span><span class="colon">:</span> <span class="lifetime">'a</span><span class="comma">,</span>
53+
<span class="lifetime">'static</span><span class="colon">:</span> <span class="lifetime">'static</span>
54+
<span class="brace">{</span><span class="brace">}</span></code></pre>

crates/ide/src/syntax_highlighting/tests.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,28 @@ macro_rules! die {
312312
);
313313
}
314314

315+
#[test]
316+
fn test_lifetime_highlighting() {
317+
check_highlighting(
318+
r#"
319+
//- minicore: derive
320+
321+
#[derive()]
322+
struct Foo<'a, 'b, 'c> where 'a: 'a, 'static: 'static {
323+
field: &'a (),
324+
field2: &'static (),
325+
}
326+
impl<'a> Foo<'_, 'a, 'static>
327+
where
328+
'a: 'a,
329+
'static: 'static
330+
{}
331+
"#,
332+
expect_file!["./test_data/highlight_lifetimes.html"],
333+
false,
334+
);
335+
}
336+
315337
#[test]
316338
fn test_string_highlighting() {
317339
// The format string detection is based on macro-expansion,

0 commit comments

Comments
 (0)