aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_ide/src/syntax_highlighting.rs
diff options
context:
space:
mode:
authorbors[bot] <26634292+bors[bot]@users.noreply.github.com>2020-07-15 11:00:10 +0100
committerGitHub <[email protected]>2020-07-15 11:00:10 +0100
commit6f3c8dc11d477a6d680ce018e93f007c31499bd5 (patch)
treeca759aa18be3bb4d30e42ae6e11e14dd673bfe1f /crates/ra_ide/src/syntax_highlighting.rs
parentb8b41c5f41a0cf8bf9c15a3bbcdd8d626eb41772 (diff)
parent91b35d882776d7ae8891b3aecf9879164ef183b5 (diff)
Merge #5345
5345: Semantic Highlighting: Emit mutable modifier for 'self' when applicable r=matklad a=Veykril This PR implements emitting the mutable modifier for the self keyword when applicable for semantic highlighting as mentioned in #5041. The rendered highlighting test html file: ![firefox_5lbsFNBqsT](https://user-images.githubusercontent.com/3757771/87346245-a5a07280-c551-11ea-9051-e5901255f8c9.png) As you can see it does not emit the modifier when `self` is not used in a mutable context even if it is declared mutably in the enclosing function. I'm not sure if this is actually something wanted or not. Co-authored-by: Lukas Wirth <[email protected]>
Diffstat (limited to 'crates/ra_ide/src/syntax_highlighting.rs')
-rw-r--r--crates/ra_ide/src/syntax_highlighting.rs27
1 files changed, 24 insertions, 3 deletions
diff --git a/crates/ra_ide/src/syntax_highlighting.rs b/crates/ra_ide/src/syntax_highlighting.rs
index 5bb6f9642..b3236e821 100644
--- a/crates/ra_ide/src/syntax_highlighting.rs
+++ b/crates/ra_ide/src/syntax_highlighting.rs
@@ -566,10 +566,31 @@ fn highlight_element(
566 | T![return] 566 | T![return]
567 | T![while] 567 | T![while]
568 | T![in] => h | HighlightModifier::ControlFlow, 568 | T![in] => h | HighlightModifier::ControlFlow,
569 T![for] if !is_child_of_impl(element) => h | HighlightModifier::ControlFlow, 569 T![for] if !is_child_of_impl(&element) => h | HighlightModifier::ControlFlow,
570 T![unsafe] => h | HighlightModifier::Unsafe, 570 T![unsafe] => h | HighlightModifier::Unsafe,
571 T![true] | T![false] => HighlightTag::BoolLiteral.into(), 571 T![true] | T![false] => HighlightTag::BoolLiteral.into(),
572 T![self] => HighlightTag::SelfKeyword.into(), 572 T![self] => {
573 let self_param_is_mut = element
574 .parent()
575 .and_then(ast::SelfParam::cast)
576 .and_then(|p| p.mut_token())
577 .is_some();
578 // closure to enforce lazyness
579 let self_path = || {
580 sema.resolve_path(&element.parent()?.parent().and_then(ast::Path::cast)?)
581 };
582 if self_param_is_mut
583 || matches!(self_path(),
584 Some(hir::PathResolution::Local(local))
585 if local.is_self(db)
586 && (local.is_mut(db) || local.ty(db).is_mutable_reference())
587 )
588 {
589 HighlightTag::SelfKeyword | HighlightModifier::Mutable
590 } else {
591 HighlightTag::SelfKeyword.into()
592 }
593 }
573 _ => h, 594 _ => h,
574 } 595 }
575 } 596 }
@@ -592,7 +613,7 @@ fn highlight_element(
592 } 613 }
593} 614}
594 615
595fn is_child_of_impl(element: SyntaxElement) -> bool { 616fn is_child_of_impl(element: &SyntaxElement) -> bool {
596 match element.parent() { 617 match element.parent() {
597 Some(e) => e.kind() == IMPL_DEF, 618 Some(e) => e.kind() == IMPL_DEF,
598 _ => false, 619 _ => false,