Skip to content

Commit 464dd81

Browse files
committed
slightly improve highlighting performance for derive annotated items
1 parent 8ded3ec commit 464dd81

File tree

3 files changed

+13
-12
lines changed

3 files changed

+13
-12
lines changed

crates/ide/src/syntax_highlighting.rs

Lines changed: 11 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
};

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,4 @@
5151
<span class="keyword">where</span>
5252
<span class="lifetime">'a</span><span class="colon">:</span> <span class="lifetime">'a</span><span class="comma">,</span>
5353
<span class="lifetime">'static</span><span class="colon">:</span> <span class="lifetime">'static</span>
54-
<span class="brace">{</span>
55-
56-
<span class="brace">}</span>
57-
</code></pre>
54+
<span class="brace">{</span><span class="brace">}</span></code></pre>

crates/ide/src/syntax_highlighting/tests.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -327,9 +327,7 @@ impl<'a> Foo<'_, 'a, 'static>
327327
where
328328
'a: 'a,
329329
'static: 'static
330-
{
331-
332-
}
330+
{}
333331
"#,
334332
expect_file!["./test_data/highlight_lifetimes.html"],
335333
false,

0 commit comments

Comments
 (0)