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 {
let lint_hover = (|| {
// FIXME: Definition should include known lints and the like instead of having this special case here // FIXME: Definition should include known lints and the like instead of having this special case here
let attr = token.parent_ancestors().find_map(ast::Attr::cast)?; let attr = token.parent_ancestors().find_map(ast::Attr::cast)?;
render::try_for_lint(&attr, token) render::try_for_lint(&attr, &token)
}) })();
}) if let Some(lint_hover) = lint_hover {
// try definitions res.push(lint_hover);
.or_else(|| { continue;
descended() }
.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,37 +245,36 @@ 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(
multizip((class.definitions(), iter::repeat(None), iter::repeat(node)))
.collect::<Vec<_>>(),
),
} }
})
.flatten() class => {
multizip((class.definitions(), iter::repeat(None), iter::repeat(node)))
.collect::<Vec<_>>()
}
}
}
.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
.or_else(|| {
descended().find_map(|token| {
if token.kind() != DOT2 { if token.kind() != DOT2 {
return None; return None;
} }
@ -285,11 +287,8 @@ fn hover_simple(
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
.or_else(|| {
descended().find_map(|token| {
if token.kind() != T!['('] && token.kind() != T![')'] { if token.kind() != T!['('] && token.kind() != T![')'] {
return None; return None;
} }
@ -302,25 +301,37 @@ fn hover_simple(
} }
}; };
render::type_info_of(sema, config, &Either::Left(call_expr), edition) render::type_info_of(sema, config, &Either::Left(call_expr), edition)
}) };
}) let closure = || {
// try closure
.or_else(|| {
descended().find_map(|token| {
if token.kind() != T![|] { if token.kind() != T![|] {
return None; return None;
} }
let c = token.parent().and_then(|x| x.parent()).and_then(ast::ClosureExpr::cast)?; let c = token.parent().and_then(|x| x.parent()).and_then(ast::ClosureExpr::cast)?;
render::closure_expr(sema, config, c, edition) render::closure_expr(sema, config, c, edition)
}) };
}) let literal = || {
// 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()
.unique()
.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); res.actions = dedupe_or_merge_hover_actions(res.actions);
RangeInfo::new(original_token.text_range(), res) RangeInfo::new(original_token.text_range(), res)
}) })

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,
} }