X-Git-Url: https://git.lizzy.rs/?a=blobdiff_plain;f=crates%2Fide%2Fsrc%2Fhover%2Frender.rs;h=ce9055c0909fa02d5f1471f76a8a3db684c58aae;hb=0b53744f2d7e0694cd7207cca632fd6de1dc5bff;hp=2b53d4a27bfac3fc5fd744eff76e0dd272d772b2;hpb=47fad0ed7328f1a39defe9b0e857fec7e042d6ae;p=rust.git diff --git a/crates/ide/src/hover/render.rs b/crates/ide/src/hover/render.rs index 2b53d4a27bf..ce9055c0909 100644 --- a/crates/ide/src/hover/render.rs +++ b/crates/ide/src/hover/render.rs @@ -18,7 +18,7 @@ algo, ast, display::{fn_as_proc_macro_label, macro_label}, match_ast, AstNode, Direction, - SyntaxKind::{CONDITION, LET_STMT}, + SyntaxKind::{LET_EXPR, LET_STMT}, SyntaxToken, T, }; @@ -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 { @@ -360,7 +356,13 @@ pub(super) fn definition( Definition::Function(it) => label_and_docs(db, it), Definition::Adt(it) => label_and_docs(db, it), Definition::Variant(it) => label_and_docs(db, it), - Definition::Const(it) => label_value_and_docs(db, it, |it| it.value(db)), + Definition::Const(it) => label_value_and_docs(db, it, |it| { + let body = it.eval(db); + match body { + Ok(x) => Some(format!("{}", x)), + Err(_) => it.value(db).map(|x| format!("{}", x)), + } + }), Definition::Static(it) => label_value_and_docs(db, it, |it| it.value(db)), Definition::Trait(it) => label_and_docs(db, it), Definition::TypeAlias(it) => label_and_docs(db, it), @@ -387,7 +389,10 @@ fn render_builtin_attr(db: &RootDatabase, attr: hir::BuiltinAttr) -> Option template, + None => return Some(Markup::fenced_block(&attr.name(db))), + }; let mut docs = "Valid forms are:".to_owned(); if word { format_to!(docs, "\n - #\\[{}]", name); @@ -479,7 +484,7 @@ fn local(db: &RootDatabase, it: hir::Local) -> Option { let let_kw = if ident .syntax() .parent() - .map_or(false, |p| p.kind() == LET_STMT || p.kind() == CONDITION) + .map_or(false, |p| p.kind() == LET_STMT || p.kind() == LET_EXPR) { "let " } else { @@ -491,3 +496,65 @@ fn local(db: &RootDatabase, it: hir::Local) -> Option { }; markup(None, desc, None) } + +struct KeywordHint { + description: String, + keyword_mod: String, + actions: Vec, +} + +impl KeywordHint { + fn new(description: String, keyword_mod: String) -> Self { + Self { description, keyword_mod, actions: Vec::default() } + } +} + +fn keyword_hints( + sema: &Semantics, + 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 = 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())), + } +}