]> git.lizzy.rs Git - rust.git/commitdiff
Allow comments between newlines in chaining hints
authorPaco Soberón <unratito@gmail.com>
Tue, 16 Feb 2021 23:09:31 +0000 (00:09 +0100)
committerPaco Soberón <unratito@gmail.com>
Tue, 16 Feb 2021 23:09:56 +0000 (00:09 +0100)
crates/ide/src/inlay_hints.rs

index 42d0a38e8d5fe7b1819ed35cf8dcae2f645b74bc..4ceb2074209d62842ba53afb5bfdf801c6c6741c 100644 (file)
@@ -109,26 +109,31 @@ fn get_chaining_hints(
     // Chaining can be defined as an expression whose next sibling tokens are newline and dot
     // Ignoring extra whitespace and comments
     let next = tokens.next()?.kind();
-    let next_next = tokens.next()?.kind();
-    if next == SyntaxKind::WHITESPACE && next_next == T![.] {
-        let ty = sema.type_of_expr(&expr)?;
-        if ty.is_unknown() {
-            return None;
+    if next == SyntaxKind::WHITESPACE {
+        let mut next_next = tokens.next()?.kind();
+        while next_next == SyntaxKind::WHITESPACE {
+            next_next = tokens.next()?.kind();
         }
-        if matches!(expr, ast::Expr::PathExpr(_)) {
-            if let Some(hir::Adt::Struct(st)) = ty.as_adt() {
-                if st.fields(sema.db).is_empty() {
-                    return None;
+        if next_next == T![.] {
+            let ty = sema.type_of_expr(&expr)?;
+            if ty.is_unknown() {
+                return None;
+            }
+            if matches!(expr, ast::Expr::PathExpr(_)) {
+                if let Some(hir::Adt::Struct(st)) = ty.as_adt() {
+                    if st.fields(sema.db).is_empty() {
+                        return None;
+                    }
                 }
             }
+            acc.push(InlayHint {
+                range: expr.syntax().text_range(),
+                kind: InlayKind::ChainingHint,
+                label: hint_iterator(sema, &famous_defs, config, &ty).unwrap_or_else(|| {
+                    ty.display_truncated(sema.db, config.max_length).to_string().into()
+                }),
+            });
         }
-        acc.push(InlayHint {
-            range: expr.syntax().text_range(),
-            kind: InlayKind::ChainingHint,
-            label: hint_iterator(sema, &famous_defs, config, &ty).unwrap_or_else(|| {
-                ty.display_truncated(sema.db, config.max_length).to_string().into()
-            }),
-        });
     }
     Some(())
 }
@@ -983,6 +988,7 @@ impl B { fn into_c(self) -> C { self.0 } }
 fn main() {
     let c = A(B(C))
         .into_b() // This is a comment
+        // This is another comment
         .into_c();
 }
 "#,