Consider interleaving hover kinds

This commit is contained in:
Lukas Wirth 2024-08-22 17:29:32 +02:00
parent 01fc1e57d2
commit 46feeb3f05
2 changed files with 95 additions and 84 deletions

View file

@ -58,7 +58,7 @@ pub enum HoverDocFormat {
PlainText, PlainText,
} }
#[derive(Debug, Clone)] #[derive(Debug, Clone, Hash, PartialEq, Eq)]
pub enum HoverAction { pub enum HoverAction {
Runnable(Runnable), Runnable(Runnable),
Implementation(FilePosition), Implementation(FilePosition),
@ -97,7 +97,7 @@ pub struct HoverGotoTypeData {
} }
/// Contains the results when hovering over an item /// Contains the results when hovering over an item
#[derive(Debug, Default)] #[derive(Clone, Debug, Default, Hash, PartialEq, Eq)]
pub struct HoverResult { pub struct HoverResult {
pub markup: Markup, pub markup: Markup,
pub actions: Vec<HoverAction>, pub actions: Vec<HoverAction>,
@ -200,21 +200,24 @@ fn hover_simple(
| ((no_tt_parent as usize) << 3) | ((no_tt_parent as usize) << 3)
}); });
// TODO: WE should not try these step by step, instead to accommodate for macros we should run let mut res = vec![];
// all of these in "parallel" and rank their results // let mut merge_result = |next: HoverResult| {
let result = None // res.markup = Markup::from(format!("{}\n---\n{}", res.markup, next.markup));
// try lint hover // res.actions.extend(next.actions);
.or_else(|| { // };
descended().find_map(|token| { for token in descended {
// FIXME: Definition should include known lints and the like instead of having this special case here let lint_hover = (|| {
let attr = token.parent_ancestors().find_map(ast::Attr::cast)?; // FIXME: Definition should include known lints and the like instead of having this special case here
render::try_for_lint(&attr, token) let attr = token.parent_ancestors().find_map(ast::Attr::cast)?;
}) render::try_for_lint(&attr, &token)
}) })();
// try definitions if let Some(lint_hover) = lint_hover {
.or_else(|| { res.push(lint_hover);
descended() continue;
.filter_map(|token| { }
let definitions = (|| {
Some(
'a: {
let node = token.parent()?; let node = token.parent()?;
// special case macro calls, we wanna render the invoked arm index // special case macro calls, we wanna render the invoked arm index
@ -229,11 +232,11 @@ fn hover_simple(
.and_then(ast::MacroCall::cast) .and_then(ast::MacroCall::cast)
{ {
if let Some(macro_) = sema.resolve_macro_call(&macro_call) { if let Some(macro_) = sema.resolve_macro_call(&macro_call) {
return Some(vec![( break 'a vec![(
Definition::Macro(macro_), Definition::Macro(macro_),
sema.resolve_macro_call_arm(&macro_call), sema.resolve_macro_call_arm(&macro_call),
node, node,
)]); )];
} }
} }
} }
@ -242,88 +245,96 @@ fn hover_simple(
match IdentClass::classify_node(sema, &node)? { match IdentClass::classify_node(sema, &node)? {
// It's better for us to fall back to the keyword hover here, // It's better for us to fall back to the keyword hover here,
// rendering poll is very confusing // rendering poll is very confusing
IdentClass::Operator(OperatorClass::Await(_)) => None, IdentClass::Operator(OperatorClass::Await(_)) => return None,
IdentClass::NameRefClass(NameRefClass::ExternCrateShorthand { IdentClass::NameRefClass(NameRefClass::ExternCrateShorthand {
decl, decl,
.. ..
}) => Some(vec![(Definition::ExternCrateDecl(decl), None, node)]), }) => {
vec![(Definition::ExternCrateDecl(decl), None, node)]
}
class => Some( class => {
multizip((class.definitions(), iter::repeat(None), iter::repeat(node))) multizip((class.definitions(), iter::repeat(None), iter::repeat(node)))
.collect::<Vec<_>>(), .collect::<Vec<_>>()
), }
} }
}) }
.flatten() .into_iter()
.unique_by(|&(def, _, _)| def) .unique_by(|&(def, _, _)| def)
.map(|(def, macro_arm, node)| { .map(|(def, macro_arm, node)| {
hover_for_definition(sema, file_id, def, &node, macro_arm, config, edition) hover_for_definition(sema, file_id, def, &node, macro_arm, config, edition)
}) })
.reduce(|mut acc: HoverResult, HoverResult { markup, actions }| { .collect::<Vec<_>>(),
acc.actions.extend(actions); )
acc.markup = Markup::from(format!("{}\n---\n{markup}", acc.markup)); })();
acc if let Some(definitions) = definitions {
}) res.extend(definitions);
}) continue;
// try keywords }
.or_else(|| descended().find_map(|token| render::keyword(sema, config, token, edition))) let keywords = || render::keyword(sema, config, &token, edition);
// try _ hovers let underscore = || render::underscore(sema, config, &token, edition);
.or_else(|| descended().find_map(|token| render::underscore(sema, config, token, edition))) let rest_pat = || {
// try rest pattern hover if token.kind() != DOT2 {
.or_else(|| { return None;
descended().find_map(|token| { }
if token.kind() != DOT2 {
return None;
}
let rest_pat = token.parent().and_then(ast::RestPat::cast)?; let rest_pat = token.parent().and_then(ast::RestPat::cast)?;
let record_pat_field_list = let record_pat_field_list =
rest_pat.syntax().parent().and_then(ast::RecordPatFieldList::cast)?; rest_pat.syntax().parent().and_then(ast::RecordPatFieldList::cast)?;
let record_pat = let record_pat =
record_pat_field_list.syntax().parent().and_then(ast::RecordPat::cast)?; record_pat_field_list.syntax().parent().and_then(ast::RecordPat::cast)?;
Some(render::struct_rest_pat(sema, config, &record_pat, edition)) Some(render::struct_rest_pat(sema, config, &record_pat, edition))
}) };
}) let call = || {
// try () call hovers if token.kind() != T!['('] && token.kind() != T![')'] {
.or_else(|| { return None;
descended().find_map(|token| { }
if token.kind() != T!['('] && token.kind() != T![')'] { let arg_list = token.parent().and_then(ast::ArgList::cast)?.syntax().parent()?;
return None; let call_expr = syntax::match_ast! {
match arg_list {
ast::CallExpr(expr) => expr.into(),
ast::MethodCallExpr(expr) => expr.into(),
_ => return None,
} }
let arg_list = token.parent().and_then(ast::ArgList::cast)?.syntax().parent()?; };
let call_expr = syntax::match_ast! { render::type_info_of(sema, config, &Either::Left(call_expr), edition)
match arg_list { };
ast::CallExpr(expr) => expr.into(), let closure = || {
ast::MethodCallExpr(expr) => expr.into(), if token.kind() != T![|] {
_ => return None, return None;
} }
}; let c = token.parent().and_then(|x| x.parent()).and_then(ast::ClosureExpr::cast)?;
render::type_info_of(sema, config, &Either::Left(call_expr), edition) render::closure_expr(sema, config, c, edition)
}) };
}) let literal = || {
// try closure
.or_else(|| {
descended().find_map(|token| {
if token.kind() != T![|] {
return None;
}
let c = token.parent().and_then(|x| x.parent()).and_then(ast::ClosureExpr::cast)?;
render::closure_expr(sema, config, c, edition)
})
})
// tokens
.or_else(|| {
render::literal(sema, original_token.clone(), edition) render::literal(sema, original_token.clone(), edition)
.map(|markup| HoverResult { markup, actions: vec![] }) .map(|markup| HoverResult { markup, actions: vec![] })
}); };
if let Some(result) = keywords()
.or_else(underscore)
.or_else(rest_pat)
.or_else(call)
.or_else(closure)
.or_else(literal)
{
res.push(result)
}
}
result.map(|mut res: HoverResult| { res.into_iter()
res.actions = dedupe_or_merge_hover_actions(res.actions); .unique()
RangeInfo::new(original_token.text_range(), res) .reduce(|mut acc: HoverResult, HoverResult { markup, actions }| {
}) acc.actions.extend(actions);
acc.markup = Markup::from(format!("{}\n---\n{markup}", acc.markup));
acc
})
.map(|mut res: HoverResult| {
res.actions = dedupe_or_merge_hover_actions(res.actions);
RangeInfo::new(original_token.text_range(), res)
})
} }
fn hover_ranged( fn hover_ranged(

View file

@ -5,7 +5,7 @@
//! what is used by LSP, so let's keep it simple. //! what is used by LSP, so let's keep it simple.
use std::fmt; use std::fmt;
#[derive(Default, Debug)] #[derive(Clone, Default, Debug, Hash, PartialEq, Eq)]
pub struct Markup { pub struct Markup {
text: String, text: String,
} }