mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-28 21:05:02 +00:00
Hide Keyword Expression Hover For Units ()
Cleaned up the code for keyword expression hovers. Added a check to hide units `()` in keyword expression hovers.
This commit is contained in:
parent
261abbf45e
commit
768804f11d
1 changed files with 20 additions and 51 deletions
|
@ -240,11 +240,9 @@ pub(super) fn keyword(
|
||||||
let parent = token.parent()?;
|
let parent = token.parent()?;
|
||||||
let famous_defs = FamousDefs(sema, sema.scope(&parent).krate());
|
let famous_defs = FamousDefs(sema, sema.scope(&parent).krate());
|
||||||
|
|
||||||
// some keywords get fancy type tooltips if they are apart of an expression, which require some extra work
|
let KeywordHint { description, keyword_mod, actions } = keyword_hints(sema, token, parent);
|
||||||
// panic safety: we just checked that token is a keyword, and we have it's parent in scope, so it must have a parent
|
|
||||||
let KeywordHint { description, documentation, actions } = keyword_hints(sema, token);
|
|
||||||
|
|
||||||
let doc_owner = find_std_module(&famous_defs, &documentation)?;
|
let doc_owner = find_std_module(&famous_defs, &keyword_mod)?;
|
||||||
let docs = doc_owner.attrs(sema.db).docs()?;
|
let docs = doc_owner.attrs(sema.db).docs()?;
|
||||||
let markup = process_markup(
|
let markup = process_markup(
|
||||||
sema.db,
|
sema.db,
|
||||||
|
@ -501,29 +499,28 @@ fn local(db: &RootDatabase, it: hir::Local) -> Option<Markup> {
|
||||||
|
|
||||||
struct KeywordHint {
|
struct KeywordHint {
|
||||||
description: String,
|
description: String,
|
||||||
documentation: String,
|
keyword_mod: String,
|
||||||
actions: Vec<HoverAction>,
|
actions: Vec<HoverAction>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl KeywordHint {
|
impl KeywordHint {
|
||||||
fn new(description: String, documentation: String) -> Self {
|
fn new(description: String, keyword_mod: String) -> Self {
|
||||||
Self { description, documentation, actions: Vec::default() }
|
Self { description, keyword_mod, actions: Vec::default() }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Panics
|
fn keyword_hints(
|
||||||
/// ------
|
sema: &Semantics<RootDatabase>,
|
||||||
/// `token` is assumed to:
|
token: &SyntaxToken,
|
||||||
/// - have a parent, and
|
parent: syntax::SyntaxNode,
|
||||||
/// - be a keyword
|
) -> KeywordHint {
|
||||||
fn keyword_hints<'t>(sema: &Semantics<RootDatabase>, token: &'t SyntaxToken) -> KeywordHint {
|
match token.kind() {
|
||||||
let parent = token.parent().expect("token was assumed to have a parent, but had none");
|
T![await] | T![loop] | T![match] | T![unsafe] | T![as] | T![try] | T![if] | T![else] => {
|
||||||
|
let keyword_mod = format!("{}_keyword", token.text());
|
||||||
|
|
||||||
macro_rules! create_hint {
|
match ast::Expr::cast(parent).and_then(|site| sema.type_of_expr(&site)) {
|
||||||
($ty_info:expr, $doc:expr) => {{
|
// ignore the unit type ()
|
||||||
let documentation = $doc;
|
Some(ty) if !ty.adjusted.as_ref().unwrap_or(&ty.original).is_unit() => {
|
||||||
match $ty_info {
|
|
||||||
Some(ty) => {
|
|
||||||
let mut targets: Vec<hir::ModuleDef> = Vec::new();
|
let mut targets: Vec<hir::ModuleDef> = Vec::new();
|
||||||
let mut push_new_def = |item: hir::ModuleDef| {
|
let mut push_new_def = |item: hir::ModuleDef| {
|
||||||
if !targets.contains(&item) {
|
if !targets.contains(&item) {
|
||||||
|
@ -537,40 +534,16 @@ fn keyword_hints<'t>(sema: &Semantics<RootDatabase>, token: &'t SyntaxToken) ->
|
||||||
|
|
||||||
KeywordHint {
|
KeywordHint {
|
||||||
description,
|
description,
|
||||||
documentation,
|
keyword_mod,
|
||||||
actions: vec![HoverAction::goto_type_from_targets(sema.db, targets)],
|
actions: vec![HoverAction::goto_type_from_targets(sema.db, targets)],
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
None => KeywordHint {
|
_ => KeywordHint {
|
||||||
description: token.text().to_string(),
|
description: token.text().to_string(),
|
||||||
documentation,
|
keyword_mod,
|
||||||
actions: Vec::new(),
|
actions: Vec::new(),
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}};
|
|
||||||
}
|
|
||||||
|
|
||||||
match token.kind() {
|
|
||||||
T![await] | T![loop] | T![match] | T![unsafe] => {
|
|
||||||
let ty = ast::Expr::cast(parent).and_then(|site| sema.type_of_expr(&site));
|
|
||||||
create_hint!(ty, format!("{}_keyword", token.text()))
|
|
||||||
}
|
|
||||||
|
|
||||||
T![if] | T![else] => {
|
|
||||||
fn if_has_else(site: &ast::IfExpr) -> bool {
|
|
||||||
match site.else_branch() {
|
|
||||||
Some(ast::ElseBranch::IfExpr(inner)) => if_has_else(&inner),
|
|
||||||
Some(ast::ElseBranch::Block(_)) => true,
|
|
||||||
None => false,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// only include the type if there is an else branch; it isn't worth annotating
|
|
||||||
// an expression that always returns `()`, is it?
|
|
||||||
let ty = ast::IfExpr::cast(parent)
|
|
||||||
.and_then(|site| if_has_else(&site).then(|| site))
|
|
||||||
.and_then(|site| sema.type_of_expr(&ast::Expr::IfExpr(site)));
|
|
||||||
create_hint!(ty, format!("{}_keyword", token.text()))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
T![fn] => {
|
T![fn] => {
|
||||||
|
@ -582,10 +555,6 @@ fn keyword_hints<'t>(sema: &Semantics<RootDatabase>, token: &'t SyntaxToken) ->
|
||||||
KeywordHint::new(token.text().to_string(), module)
|
KeywordHint::new(token.text().to_string(), module)
|
||||||
}
|
}
|
||||||
|
|
||||||
kind if kind.is_keyword() => {
|
_ => KeywordHint::new(token.text().to_string(), format!("{}_keyword", token.text())),
|
||||||
KeywordHint::new(token.text().to_string(), format!("{}_keyword", token.text()))
|
|
||||||
}
|
|
||||||
|
|
||||||
_ => panic!("{} was assumed to be a keyword, but it wasn't", token),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue