From a6af0272f7bf129a3063cdd7096f685fc58438e6 Mon Sep 17 00:00:00 2001 From: Paul Daniel Faria Date: Thu, 23 Jul 2020 10:11:37 -0400 Subject: Move semantic logic into Semantics, fix missing tag for safe amp operator, using functional methods rather than clunky inline closure --- crates/ra_ide/src/syntax_highlighting.rs | 89 ++++++++---------------- crates/ra_ide/test_data/highlight_doctest.html | 2 +- crates/ra_ide/test_data/highlight_injection.html | 2 +- crates/ra_ide/test_data/highlight_unsafe.html | 4 +- crates/ra_ide/test_data/highlighting.html | 10 +-- 5 files changed, 37 insertions(+), 70 deletions(-) (limited to 'crates/ra_ide') diff --git a/crates/ra_ide/src/syntax_highlighting.rs b/crates/ra_ide/src/syntax_highlighting.rs index cf93205b6..e29f65a78 100644 --- a/crates/ra_ide/src/syntax_highlighting.rs +++ b/crates/ra_ide/src/syntax_highlighting.rs @@ -565,29 +565,21 @@ fn highlight_element( _ => h, } } - T![&] => { - let ref_expr = element.parent().and_then(ast::RefExpr::cast)?; - let expr = ref_expr.expr()?; - let field_expr = match expr { - ast::Expr::FieldExpr(fe) => fe, - _ => return None, - }; - - let expr = field_expr.expr()?; - let ty = sema.type_of_expr(&expr)?; - if !ty.is_packed(db) { - return None; - } - - // FIXME This needs layout computation to be correct. It will highlight - // more than it should with the current implementation. - - HighlightTag::Operator | HighlightModifier::Unsafe - } p if p.is_punct() => match p { - T![::] | T![->] | T![=>] | T![&] | T![..] | T![=] | T![@] => { - HighlightTag::Operator.into() + T![&] => { + let h = HighlightTag::Operator.into(); + let is_unsafe = element + .parent() + .and_then(ast::RefExpr::cast) + .map(|ref_expr| sema.is_unsafe_ref_expr(&ref_expr)) + .unwrap_or(false); + if is_unsafe { + h | HighlightModifier::Unsafe + } else { + h + } } + T![::] | T![->] | T![=>] | T![..] | T![=] | T![@] => HighlightTag::Operator.into(), T![!] if element.parent().and_then(ast::MacroCall::cast).is_some() => { HighlightTag::Macro.into() } @@ -668,22 +660,18 @@ fn highlight_element( HighlightTag::SelfKeyword.into() } } - T![ref] => { - let modifier: Option = (|| { - let bind_pat = element.parent().and_then(ast::BindPat::cast)?; - if sema.is_unsafe_pat(&ast::Pat::BindPat(bind_pat)) { + T![ref] => element + .parent() + .and_then(ast::BindPat::cast) + .and_then(|bind_pat| { + if sema.is_unsafe_bind_pat(&bind_pat) { Some(HighlightModifier::Unsafe) } else { None } - })(); - - if let Some(modifier) = modifier { - h | modifier - } else { - h - } - } + }) + .map(|modifier| h | modifier) + .unwrap_or(h), _ => h, } } @@ -713,31 +701,6 @@ fn is_child_of_impl(element: &SyntaxElement) -> bool { } } -fn is_method_call_unsafe( - sema: &Semantics, - method_call_expr: ast::MethodCallExpr, -) -> Option<()> { - let expr = method_call_expr.expr()?; - let field_expr = - if let ast::Expr::FieldExpr(field_expr) = expr { field_expr } else { return None }; - let ty = sema.type_of_expr(&field_expr.expr()?)?; - if !ty.is_packed(sema.db) { - return None; - } - - let func = sema.resolve_method_call(&method_call_expr)?; - if func.has_self_param(sema.db) { - let params = func.params(sema.db); - if matches!(params.into_iter().next(), Some(TypeRef::Reference(..))) { - Some(()) - } else { - None - } - } else { - None - } -} - fn highlight_name( sema: &Semantics, db: &RootDatabase, @@ -767,7 +730,7 @@ fn highlight_name( let is_unsafe = name_ref .and_then(|name_ref| name_ref.syntax().parent()) .and_then(ast::MethodCallExpr::cast) - .and_then(|method_call_expr| is_method_call_unsafe(sema, method_call_expr)); + .and_then(|method_call_expr| sema.is_unsafe_method_call(method_call_expr)); if is_unsafe.is_some() { h |= HighlightModifier::Unsafe; } @@ -846,7 +809,7 @@ fn highlight_name_ref_by_syntax(name: ast::NameRef, sema: &Semantics { let mut h = Highlight::new(HighlightTag::Function); let is_unsafe = ast::MethodCallExpr::cast(parent) - .and_then(|method_call_expr| is_method_call_unsafe(sema, method_call_expr)); + .and_then(|method_call_expr| sema.is_unsafe_method_call(method_call_expr)); if is_unsafe.is_some() { h |= HighlightModifier::Unsafe; @@ -866,7 +829,11 @@ fn highlight_name_ref_by_syntax(name: ast::NameRef, sema: &Semantics { let path = match parent.parent().and_then(ast::Path::cast) { diff --git a/crates/ra_ide/test_data/highlight_doctest.html b/crates/ra_ide/test_data/highlight_doctest.html index 46c1e0a11..6322d404f 100644 --- a/crates/ra_ide/test_data/highlight_doctest.html +++ b/crates/ra_ide/test_data/highlight_doctest.html @@ -87,7 +87,7 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd /// ```sh /// echo 1 /// ``` - pub fn foo(&self) -> bool { + pub fn foo(&self) -> bool { true } } diff --git a/crates/ra_ide/test_data/highlight_injection.html b/crates/ra_ide/test_data/highlight_injection.html index 60c394399..18addd00d 100644 --- a/crates/ra_ide/test_data/highlight_injection.html +++ b/crates/ra_ide/test_data/highlight_injection.html @@ -35,7 +35,7 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd .unresolved_reference { color: #FC5555; text-decoration: wavy underline; } -
fn fixture(ra_fixture: &str) {}
+
fn fixture(ra_fixture: &str) {}
 
 fn main() {
     fixture(r#"
diff --git a/crates/ra_ide/test_data/highlight_unsafe.html b/crates/ra_ide/test_data/highlight_unsafe.html
index 454ff6d5f..a2df2c27e 100644
--- a/crates/ra_ide/test_data/highlight_unsafe.html
+++ b/crates/ra_ide/test_data/highlight_unsafe.html
@@ -45,7 +45,7 @@ pre                 { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd
 struct HasUnsafeFn;
 
 impl HasUnsafeFn {
-    unsafe fn unsafe_method(&self) {}
+    unsafe fn unsafe_method(&self) {}
 }
 
 struct TypeForStaticMut {
@@ -55,7 +55,7 @@ pre                 { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd
 static mut global_mut: TypeForStaticMut = TypeForStaticMut { a: 0 };
 
 fn main() {
-    let x = &5 as *const _ as *const usize;
+    let x = &5 as *const _ as *const usize;
     let u = Union { b: 0 };
     unsafe {
         // unsafe fn and method calls
diff --git a/crates/ra_ide/test_data/highlighting.html b/crates/ra_ide/test_data/highlighting.html
index 678cf9bd3..8e0160eee 100644
--- a/crates/ra_ide/test_data/highlighting.html
+++ b/crates/ra_ide/test_data/highlighting.html
@@ -45,11 +45,11 @@ pre                 { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd
 }
 
 trait Bar {
-    fn bar(&self) -> i32;
+    fn bar(&self) -> i32;
 }
 
 impl Bar for Foo {
-    fn bar(&self) -> i32 {
+    fn bar(&self) -> i32 {
         self.x
     }
 }
@@ -59,7 +59,7 @@ pre                 { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd
         self.x
     }
 
-    fn qux(&mut self) {
+    fn qux(&mut self) {
         self.x = 0;
     }
 }
@@ -107,8 +107,8 @@ pre                 { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd
     noop!(noop!(1));
 
     let mut x = 42;
-    let y = &mut x;
-    let z = &y;
+    let y = &mut x;
+    let z = &y;
 
     let Foo { x: z, y } = Foo { x: z, y };
 
-- 
cgit v1.2.3