about summary refs log tree commit diff
diff options
context:
space:
mode:
authorbors[bot] <26634292+bors[bot]@users.noreply.github.com>2022-02-14 21:07:41 +0000
committerGitHub <noreply@github.com>2022-02-14 21:07:41 +0000
commitaafa40cebd7a79d149ea31a069d42225f6fe0272 (patch)
tree14b5c50aec79641699a81be0679fac66cbd5eb7d
parent014d3ef1a4b69c4329aba9e0c27cd7a8649e33c2 (diff)
parent768804f11d165a73bb56d87b284e86456913aef6 (diff)
downloadrust-aafa40cebd7a79d149ea31a069d42225f6fe0272.tar.gz
rust-aafa40cebd7a79d149ea31a069d42225f6fe0272.zip
Merge #11369
11369: feat: Add type hint for keyword expression hovers r=Veykril a=danii

Adds the return type of keywords to tool-tips where it makes sense. This applies to: `if`, `else`, `match`, `loop`, `unsafe` and `await`. Thanks to `@Veykril` for sharing the idea of putting return type highlighting on other keywords!
![image](https://user-images.githubusercontent.com/39541871/151611737-12325c23-a1f9-4fca-ae48-279b374bdcdf.png)

Closes #11359

Co-authored-by: Daniel Conley <himself@danii.dev>
-rw-r--r--crates/ide/src/hover/render.rs76
1 files changed, 67 insertions, 9 deletions
diff --git a/crates/ide/src/hover/render.rs b/crates/ide/src/hover/render.rs
index d29833a65b3..f94348ec581 100644
--- a/crates/ide/src/hover/render.rs
+++ b/crates/ide/src/hover/render.rs
@@ -239,22 +239,18 @@ pub(super) fn keyword(
     }
     let parent = token.parent()?;
     let famous_defs = FamousDefs(sema, sema.scope(&parent).krate());
-    let keyword_mod = if token.kind() == T![fn] && ast::FnPtrType::cast(parent).is_some() {
-        // treat fn keyword inside function pointer type as primitive
-        format!("prim_{}", token.text())
-    } else {
-        // std exposes {}_keyword modules with docstrings on the root to document keywords
-        format!("{}_keyword", token.text())
-    };
+
+    let KeywordHint { description, keyword_mod, actions } = keyword_hints(sema, token, parent);
+
     let doc_owner = find_std_module(&famous_defs, &keyword_mod)?;
     let docs = doc_owner.attrs(sema.db).docs()?;
     let markup = process_markup(
         sema.db,
         Definition::Module(doc_owner),
-        &markup(Some(docs.into()), token.text().into(), None)?,
+        &markup(Some(docs.into()), description, None)?,
         config,
     );
-    Some(HoverResult { markup, actions: Default::default() })
+    Some(HoverResult { markup, actions })
 }
 
 pub(super) fn try_for_lint(attr: &ast::Attr, token: &SyntaxToken) -> Option<HoverResult> {
@@ -500,3 +496,65 @@ fn local(db: &RootDatabase, it: hir::Local) -> Option<Markup> {
     };
     markup(None, desc, None)
 }
+
+struct KeywordHint {
+    description: String,
+    keyword_mod: String,
+    actions: Vec<HoverAction>,
+}
+
+impl KeywordHint {
+    fn new(description: String, keyword_mod: String) -> Self {
+        Self { description, keyword_mod, actions: Vec::default() }
+    }
+}
+
+fn keyword_hints(
+    sema: &Semantics<RootDatabase>,
+    token: &SyntaxToken,
+    parent: syntax::SyntaxNode,
+) -> KeywordHint {
+    match token.kind() {
+        T![await] | T![loop] | T![match] | T![unsafe] | T![as] | T![try] | T![if] | T![else] => {
+            let keyword_mod = format!("{}_keyword", token.text());
+
+            match ast::Expr::cast(parent).and_then(|site| sema.type_of_expr(&site)) {
+                // ignore the unit type ()
+                Some(ty) if !ty.adjusted.as_ref().unwrap_or(&ty.original).is_unit() => {
+                    let mut targets: Vec<hir::ModuleDef> = Vec::new();
+                    let mut push_new_def = |item: hir::ModuleDef| {
+                        if !targets.contains(&item) {
+                            targets.push(item);
+                        }
+                    };
+                    walk_and_push_ty(sema.db, &ty.original, &mut push_new_def);
+
+                    let ty = ty.adjusted();
+                    let description = format!("{}: {}", token.text(), ty.display(sema.db));
+
+                    KeywordHint {
+                        description,
+                        keyword_mod,
+                        actions: vec![HoverAction::goto_type_from_targets(sema.db, targets)],
+                    }
+                }
+                _ => KeywordHint {
+                    description: token.text().to_string(),
+                    keyword_mod,
+                    actions: Vec::new(),
+                },
+            }
+        }
+
+        T![fn] => {
+            let module = match ast::FnPtrType::cast(parent) {
+                // treat fn keyword inside function pointer type as primitive
+                Some(_) => format!("prim_{}", token.text()),
+                None => format!("{}_keyword", token.text()),
+            };
+            KeywordHint::new(token.text().to_string(), module)
+        }
+
+        _ => KeywordHint::new(token.text().to_string(), format!("{}_keyword", token.text())),
+    }
+}