Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 768804f

Browse files
committed
Hide Keyword Expression Hover For Units ()
Cleaned up the code for keyword expression hovers. Added a check to hide units `()` in keyword expression hovers.
1 parent 261abbf commit 768804f

File tree

1 file changed

+21
-52
lines changed

1 file changed

+21
-52
lines changed

crates/ide/src/hover/render.rs

Lines changed: 21 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -240,11 +240,9 @@ pub(super) fn keyword(
240240
let parent = token.parent()?;
241241
let famous_defs = FamousDefs(sema, sema.scope(&parent).krate());
242242

243-
// some keywords get fancy type tooltips if they are apart of an expression, which require some extra work
244-
// panic safety: we just checked that token is a keyword, and we have it's parent in scope, so it must have a parent
245-
let KeywordHint { description, documentation, actions } = keyword_hints(sema, token);
243+
let KeywordHint { description, keyword_mod, actions } = keyword_hints(sema, token, parent);
246244

247-
let doc_owner = find_std_module(&famous_defs, &documentation)?;
245+
let doc_owner = find_std_module(&famous_defs, &keyword_mod)?;
248246
let docs = doc_owner.attrs(sema.db).docs()?;
249247
let markup = process_markup(
250248
sema.db,
@@ -501,29 +499,28 @@ fn local(db: &RootDatabase, it: hir::Local) -> Option<Markup> {
501499

502500
struct KeywordHint {
503501
description: String,
504-
documentation: String,
502+
keyword_mod: String,
505503
actions: Vec<HoverAction>,
506504
}
507505

508506
impl KeywordHint {
509-
fn new(description: String, documentation: String) -> Self {
510-
Self { description, documentation, actions: Vec::default() }
507+
fn new(description: String, keyword_mod: String) -> Self {
508+
Self { description, keyword_mod, actions: Vec::default() }
511509
}
512510
}
513511

514-
/// Panics
515-
/// ------
516-
/// `token` is assumed to:
517-
/// - have a parent, and
518-
/// - be a keyword
519-
fn keyword_hints<'t>(sema: &Semantics<RootDatabase>, token: &'t SyntaxToken) -> KeywordHint {
520-
let parent = token.parent().expect("token was assumed to have a parent, but had none");
521-
522-
macro_rules! create_hint {
523-
($ty_info:expr, $doc:expr) => {{
524-
let documentation = $doc;
525-
match $ty_info {
526-
Some(ty) => {
512+
fn keyword_hints(
513+
sema: &Semantics<RootDatabase>,
514+
token: &SyntaxToken,
515+
parent: syntax::SyntaxNode,
516+
) -> KeywordHint {
517+
match token.kind() {
518+
T![await] | T![loop] | T![match] | T![unsafe] | T![as] | T![try] | T![if] | T![else] => {
519+
let keyword_mod = format!("{}_keyword", token.text());
520+
521+
match ast::Expr::cast(parent).and_then(|site| sema.type_of_expr(&site)) {
522+
// ignore the unit type ()
523+
Some(ty) if !ty.adjusted.as_ref().unwrap_or(&ty.original).is_unit() => {
527524
let mut targets: Vec<hir::ModuleDef> = Vec::new();
528525
let mut push_new_def = |item: hir::ModuleDef| {
529526
if !targets.contains(&item) {
@@ -537,40 +534,16 @@ fn keyword_hints<'t>(sema: &Semantics<RootDatabase>, token: &'t SyntaxToken) ->
537534

538535
KeywordHint {
539536
description,
540-
documentation,
537+
keyword_mod,
541538
actions: vec![HoverAction::goto_type_from_targets(sema.db, targets)],
542539
}
543540
}
544-
None => KeywordHint {
541+
_ => KeywordHint {
545542
description: token.text().to_string(),
546-
documentation,
543+
keyword_mod,
547544
actions: Vec::new(),
548545
},
549546
}
550-
}};
551-
}
552-
553-
match token.kind() {
554-
T![await] | T![loop] | T![match] | T![unsafe] => {
555-
let ty = ast::Expr::cast(parent).and_then(|site| sema.type_of_expr(&site));
556-
create_hint!(ty, format!("{}_keyword", token.text()))
557-
}
558-
559-
T![if] | T![else] => {
560-
fn if_has_else(site: &ast::IfExpr) -> bool {
561-
match site.else_branch() {
562-
Some(ast::ElseBranch::IfExpr(inner)) => if_has_else(&inner),
563-
Some(ast::ElseBranch::Block(_)) => true,
564-
None => false,
565-
}
566-
}
567-
568-
// only include the type if there is an else branch; it isn't worth annotating
569-
// an expression that always returns `()`, is it?
570-
let ty = ast::IfExpr::cast(parent)
571-
.and_then(|site| if_has_else(&site).then(|| site))
572-
.and_then(|site| sema.type_of_expr(&ast::Expr::IfExpr(site)));
573-
create_hint!(ty, format!("{}_keyword", token.text()))
574547
}
575548

576549
T![fn] => {
@@ -582,10 +555,6 @@ fn keyword_hints<'t>(sema: &Semantics<RootDatabase>, token: &'t SyntaxToken) ->
582555
KeywordHint::new(token.text().to_string(), module)
583556
}
584557

585-
kind if kind.is_keyword() => {
586-
KeywordHint::new(token.text().to_string(), format!("{}_keyword", token.text()))
587-
}
588-
589-
_ => panic!("{} was assumed to be a keyword, but it wasn't", token),
558+
_ => KeywordHint::new(token.text().to_string(), format!("{}_keyword", token.text())),
590559
}
591560
}

0 commit comments

Comments
 (0)