ide/
hover.rs

1mod render;
2
3#[cfg(test)]
4mod tests;
5
6use std::{iter, ops::Not};
7
8use either::Either;
9use hir::{
10    DisplayTarget, GenericDef, GenericSubstitution, HasCrate, HasSource, LangItem, Semantics,
11    db::DefDatabase,
12};
13use ide_db::{
14    FileRange, FxIndexSet, MiniCore, Ranker, RootDatabase,
15    defs::{Definition, IdentClass, NameRefClass, OperatorClass},
16    famous_defs::FamousDefs,
17    helpers::pick_best_token,
18    ra_fixture::UpmapFromRaFixture,
19};
20use itertools::{Itertools, multizip};
21use macros::UpmapFromRaFixture;
22use span::{Edition, TextRange};
23use syntax::{
24    AstNode, AstToken,
25    SyntaxKind::{self, *},
26    SyntaxNode, T, ast,
27};
28
29use crate::{
30    Analysis, FileId, FilePosition, NavigationTarget, RangeInfo, Runnable, TryToNav,
31    doc_links::token_as_doc_comment,
32    markdown_remove::remove_markdown,
33    markup::Markup,
34    navigation_target::UpmappingResult,
35    runnables::{runnable_fn, runnable_mod},
36};
37
38#[derive(Clone, Debug)]
39pub struct HoverConfig<'a> {
40    pub links_in_hover: bool,
41    pub memory_layout: Option<MemoryLayoutHoverConfig>,
42    pub documentation: bool,
43    pub keywords: bool,
44    pub format: HoverDocFormat,
45    pub max_trait_assoc_items_count: Option<usize>,
46    pub max_fields_count: Option<usize>,
47    pub max_enum_variants_count: Option<usize>,
48    pub max_subst_ty_len: SubstTyLen,
49    pub show_drop_glue: bool,
50    pub minicore: MiniCore<'a>,
51}
52
53#[derive(Clone, Debug, PartialEq, Eq)]
54pub enum SubstTyLen {
55    Unlimited,
56    LimitTo(usize),
57    Hide,
58}
59
60#[derive(Copy, Clone, Debug, PartialEq, Eq)]
61pub struct MemoryLayoutHoverConfig {
62    pub size: Option<MemoryLayoutHoverRenderKind>,
63    pub offset: Option<MemoryLayoutHoverRenderKind>,
64    pub alignment: Option<MemoryLayoutHoverRenderKind>,
65    pub padding: Option<MemoryLayoutHoverRenderKind>,
66    pub niches: bool,
67}
68
69#[derive(Copy, Clone, Debug, PartialEq, Eq)]
70pub enum MemoryLayoutHoverRenderKind {
71    Decimal,
72    Hexadecimal,
73    Both,
74}
75
76#[derive(Clone, Debug, PartialEq, Eq)]
77pub enum HoverDocFormat {
78    Markdown,
79    PlainText,
80}
81
82#[derive(Debug, Clone, Hash, PartialEq, Eq, UpmapFromRaFixture)]
83pub enum HoverAction {
84    Runnable(Runnable),
85    Implementation(FilePosition),
86    Reference(FilePosition),
87    GoToType(Vec<HoverGotoTypeData>),
88}
89
90impl HoverAction {
91    fn goto_type_from_targets(
92        sema: &Semantics<'_, RootDatabase>,
93        targets: Vec<hir::ModuleDef>,
94        edition: Edition,
95    ) -> Option<Self> {
96        let db = sema.db;
97        let targets = targets
98            .into_iter()
99            .filter_map(|it| {
100                Some(HoverGotoTypeData {
101                    mod_path: render::path(
102                        db,
103                        it.module(db)?,
104                        it.name(db).map(|name| name.display(db, edition).to_string()),
105                        edition,
106                    ),
107                    nav: it.try_to_nav(sema)?.call_site(),
108                })
109            })
110            .collect::<Vec<_>>();
111        targets.is_empty().not().then_some(HoverAction::GoToType(targets))
112    }
113}
114
115#[derive(Debug, Clone, Eq, PartialEq, Hash, UpmapFromRaFixture)]
116pub struct HoverGotoTypeData {
117    pub mod_path: String,
118    pub nav: NavigationTarget,
119}
120
121/// Contains the results when hovering over an item
122#[derive(Clone, Debug, Default, Hash, PartialEq, Eq, UpmapFromRaFixture)]
123pub struct HoverResult {
124    pub markup: Markup,
125    pub actions: Vec<HoverAction>,
126}
127
128// Feature: Hover
129//
130// Shows additional information, like the type of an expression or the documentation for a definition when "focusing" code.
131// Focusing is usually hovering with a mouse, but can also be triggered with a shortcut.
132//
133// ![Hover](https://user-images.githubusercontent.com/48062697/113020658-b5f98b80-917a-11eb-9f88-3dbc27320c95.gif)
134pub(crate) fn hover(
135    db: &RootDatabase,
136    frange @ FileRange { file_id, range }: FileRange,
137    config: &HoverConfig<'_>,
138) -> Option<RangeInfo<HoverResult>> {
139    let sema = &hir::Semantics::new(db);
140    let file = sema.parse_guess_edition(file_id).syntax().clone();
141    let edition =
142        sema.attach_first_edition(file_id).map(|it| it.edition(db)).unwrap_or(Edition::CURRENT);
143    let display_target = sema.first_crate(file_id)?.to_display_target(db);
144    let mut res = if range.is_empty() {
145        hover_offset(
146            sema,
147            FilePosition { file_id, offset: range.start() },
148            file,
149            config,
150            edition,
151            display_target,
152        )
153    } else {
154        hover_ranged(sema, frange, file, config, edition, display_target)
155    }?;
156
157    if let HoverDocFormat::PlainText = config.format {
158        res.info.markup = remove_markdown(res.info.markup.as_str()).into();
159    }
160    Some(res)
161}
162
163#[allow(clippy::field_reassign_with_default)]
164fn hover_offset(
165    sema: &Semantics<'_, RootDatabase>,
166    FilePosition { file_id, offset }: FilePosition,
167    file: SyntaxNode,
168    config: &HoverConfig<'_>,
169    edition: Edition,
170    display_target: DisplayTarget,
171) -> Option<RangeInfo<HoverResult>> {
172    let original_token = pick_best_token(file.token_at_offset(offset), |kind| match kind {
173        IDENT
174        | INT_NUMBER
175        | LIFETIME_IDENT
176        | T![self]
177        | T![super]
178        | T![crate]
179        | T![Self]
180        | T![_] => 4,
181        // index and prefix ops and closure pipe
182        T!['['] | T![']'] | T![?] | T![*] | T![-] | T![!] | T![|] => 3,
183        kind if kind.is_keyword(edition) => 2,
184        T!['('] | T![')'] => 2,
185        kind if kind.is_trivia() => 0,
186        _ => 1,
187    })?;
188
189    if let Some(doc_comment) = token_as_doc_comment(&original_token) {
190        cov_mark::hit!(no_highlight_on_comment_hover);
191        return doc_comment.get_definition_with_descend_at(sema, offset, |def, node, range| {
192            let res = hover_for_definition(
193                sema,
194                file_id,
195                def,
196                None,
197                &node,
198                None,
199                false,
200                config,
201                edition,
202                display_target,
203            );
204            Some(RangeInfo::new(range, res))
205        });
206    }
207
208    if let Some((range, _, _, resolution)) =
209        sema.check_for_format_args_template(original_token.clone(), offset)
210    {
211        let res = hover_for_definition(
212            sema,
213            file_id,
214            Definition::from(resolution?),
215            None,
216            &original_token.parent()?,
217            None,
218            false,
219            config,
220            edition,
221            display_target,
222        );
223        return Some(RangeInfo::new(range, res));
224    }
225
226    if let Some(literal) = ast::String::cast(original_token.clone())
227        && let Some((analysis, fixture_analysis)) =
228            Analysis::from_ra_fixture(sema, literal.clone(), &literal, config.minicore)
229    {
230        let (virtual_file_id, virtual_offset) = fixture_analysis.map_offset_down(offset)?;
231        return analysis
232            .hover(
233                config,
234                FileRange { file_id: virtual_file_id, range: TextRange::empty(virtual_offset) },
235            )
236            .ok()??
237            .upmap_from_ra_fixture(&fixture_analysis, virtual_file_id, file_id)
238            .ok();
239    }
240
241    // prefer descending the same token kind in attribute expansions, in normal macros text
242    // equivalency is more important
243    let mut descended = sema.descend_into_macros(original_token.clone());
244
245    let ranker = Ranker::from_token(&original_token);
246
247    descended.sort_by_cached_key(|tok| !ranker.rank_token(tok));
248
249    let mut res = vec![];
250    for token in descended {
251        let is_same_kind = token.kind() == ranker.kind;
252        let lint_hover = (|| {
253            // FIXME: Definition should include known lints and the like instead of having this special case here
254            let attr = token.parent_ancestors().find_map(ast::Attr::cast)?;
255            render::try_for_lint(&attr, &token)
256        })();
257        if let Some(lint_hover) = lint_hover {
258            res.push(lint_hover);
259            continue;
260        }
261        let definitions = (|| {
262            Some(
263                'a: {
264                    let node = token.parent()?;
265
266                    // special case macro calls, we wanna render the invoked arm index
267                    if let Some(name) = ast::NameRef::cast(node.clone())
268                        && let Some(path_seg) =
269                            name.syntax().parent().and_then(ast::PathSegment::cast)
270                            && let Some(macro_call) = path_seg
271                                .parent_path()
272                                .syntax()
273                                .parent()
274                                .and_then(ast::MacroCall::cast)
275                                && let Some(macro_) = sema.resolve_macro_call(&macro_call) {
276                                    break 'a vec![(
277                                        (Definition::Macro(macro_), None),
278                                        sema.resolve_macro_call_arm(&macro_call),
279                                        false,
280                                        node,
281                                    )];
282                                }
283
284                    match IdentClass::classify_node(sema, &node)? {
285                        // It's better for us to fall back to the keyword hover here,
286                        // rendering poll is very confusing
287                        IdentClass::Operator(OperatorClass::Await(_)) => return None,
288
289                        IdentClass::NameRefClass(NameRefClass::ExternCrateShorthand {
290                            decl,
291                            ..
292                        }) => {
293                            vec![((Definition::ExternCrateDecl(decl), None), None, false, node)]
294                        }
295
296                        class => {
297                            let render_extras = matches!(class, IdentClass::NameClass(_))
298                                // Render extra information for `Self` keyword as well
299                                || ast::NameRef::cast(node.clone()).is_some_and(|name_ref| name_ref.token_kind() == SyntaxKind::SELF_TYPE_KW);
300                            multizip((
301                                class.definitions(),
302                                iter::repeat(None),
303                                iter::repeat(render_extras),
304                                iter::repeat(node),
305                            ))
306                            .collect::<Vec<_>>()
307                        }
308                    }
309                }
310                .into_iter()
311                .unique_by(|&((def, _), _, _, _)| def)
312                .map(|((def, subst), macro_arm, hovered_definition, node)| {
313                    hover_for_definition(
314                        sema,
315                        file_id,
316                        def,
317                        subst,
318                        &node,
319                        macro_arm,
320                        hovered_definition,
321                        config,
322                        edition,
323                        display_target,
324                    )
325                })
326                .collect::<Vec<_>>(),
327            )
328        })();
329        if let Some(definitions) = definitions {
330            res.extend(definitions);
331            continue;
332        }
333        let keywords = || render::keyword(sema, config, &token, edition, display_target);
334        let underscore = || {
335            if !is_same_kind {
336                return None;
337            }
338            render::underscore(sema, config, &token, edition, display_target)
339        };
340        let rest_pat = || {
341            if !is_same_kind || token.kind() != DOT2 {
342                return None;
343            }
344
345            let rest_pat = token.parent().and_then(ast::RestPat::cast)?;
346            let record_pat_field_list =
347                rest_pat.syntax().parent().and_then(ast::RecordPatFieldList::cast)?;
348
349            let record_pat =
350                record_pat_field_list.syntax().parent().and_then(ast::RecordPat::cast)?;
351
352            Some(render::struct_rest_pat(sema, config, &record_pat, edition, display_target))
353        };
354        let call = || {
355            if !is_same_kind || token.kind() != T!['('] && token.kind() != T![')'] {
356                return None;
357            }
358            let arg_list = token.parent().and_then(ast::ArgList::cast)?.syntax().parent()?;
359            let call_expr = syntax::match_ast! {
360                match arg_list {
361                    ast::CallExpr(expr) => expr.into(),
362                    ast::MethodCallExpr(expr) => expr.into(),
363                    _ => return None,
364                }
365            };
366            render::type_info_of(sema, config, &Either::Left(call_expr), edition, display_target)
367        };
368        let closure = || {
369            if !is_same_kind || token.kind() != T![|] {
370                return None;
371            }
372            let c = token.parent().and_then(|x| x.parent()).and_then(ast::ClosureExpr::cast)?;
373            render::closure_expr(sema, config, c, edition, display_target)
374        };
375        let literal = || {
376            render::literal(sema, original_token.clone(), display_target)
377                .map(|markup| HoverResult { markup, actions: vec![] })
378        };
379        if let Some(result) = keywords()
380            .or_else(underscore)
381            .or_else(rest_pat)
382            .or_else(call)
383            .or_else(closure)
384            .or_else(literal)
385        {
386            res.push(result)
387        }
388    }
389
390    res.into_iter()
391        .unique()
392        .reduce(|mut acc: HoverResult, HoverResult { markup, actions }| {
393            acc.actions.extend(actions);
394            acc.markup = Markup::from(format!("{}\n\n---\n{markup}", acc.markup));
395            acc
396        })
397        .map(|mut res: HoverResult| {
398            res.actions = dedupe_or_merge_hover_actions(res.actions);
399            RangeInfo::new(original_token.text_range(), res)
400        })
401}
402
403fn hover_ranged(
404    sema: &Semantics<'_, RootDatabase>,
405    FileRange { file_id, range }: FileRange,
406    file: SyntaxNode,
407    config: &HoverConfig<'_>,
408    edition: Edition,
409    display_target: DisplayTarget,
410) -> Option<RangeInfo<HoverResult>> {
411    // FIXME: make this work in attributes
412    let expr_or_pat = file
413        .covering_element(range)
414        .ancestors()
415        .take_while(|it| ast::MacroCall::can_cast(it.kind()) || !ast::Item::can_cast(it.kind()))
416        .find_map(Either::<ast::Expr, ast::Pat>::cast)?;
417    let res = match &expr_or_pat {
418        Either::Left(ast::Expr::TryExpr(try_expr)) => {
419            render::try_expr(sema, config, try_expr, edition, display_target)
420        }
421        Either::Left(ast::Expr::PrefixExpr(prefix_expr))
422            if prefix_expr.op_kind() == Some(ast::UnaryOp::Deref) =>
423        {
424            render::deref_expr(sema, config, prefix_expr, edition, display_target)
425        }
426        Either::Left(ast::Expr::Literal(literal)) => {
427            if let Some(literal) = ast::String::cast(literal.token())
428                && let Some((analysis, fixture_analysis)) =
429                    Analysis::from_ra_fixture(sema, literal.clone(), &literal, config.minicore)
430            {
431                let (virtual_file_id, virtual_range) = fixture_analysis.map_range_down(range)?;
432                return analysis
433                    .hover(config, FileRange { file_id: virtual_file_id, range: virtual_range })
434                    .ok()??
435                    .upmap_from_ra_fixture(&fixture_analysis, virtual_file_id, file_id)
436                    .ok();
437            }
438            None
439        }
440        _ => None,
441    };
442    let res =
443        res.or_else(|| render::type_info_of(sema, config, &expr_or_pat, edition, display_target));
444    res.map(|it| {
445        let range = match expr_or_pat {
446            Either::Left(it) => it.syntax().text_range(),
447            Either::Right(it) => it.syntax().text_range(),
448        };
449        RangeInfo::new(range, it)
450    })
451}
452
453// FIXME: Why is this pub(crate)?
454pub(crate) fn hover_for_definition(
455    sema: &Semantics<'_, RootDatabase>,
456    file_id: FileId,
457    def: Definition,
458    subst: Option<GenericSubstitution<'_>>,
459    scope_node: &SyntaxNode,
460    macro_arm: Option<u32>,
461    render_extras: bool,
462    config: &HoverConfig<'_>,
463    edition: Edition,
464    display_target: DisplayTarget,
465) -> HoverResult {
466    let famous_defs = match &def {
467        Definition::BuiltinType(_) => sema.scope(scope_node).map(|it| FamousDefs(sema, it.krate())),
468        _ => None,
469    };
470
471    let db = sema.db;
472    let def_ty = match def {
473        Definition::Local(it) => Some(it.ty(db)),
474        Definition::GenericParam(hir::GenericParam::ConstParam(it)) => Some(it.ty(db)),
475        Definition::GenericParam(hir::GenericParam::TypeParam(it)) => Some(it.ty(db)),
476        Definition::Field(field) => Some(field.ty(db).to_type(db)),
477        Definition::TupleField(it) => Some(it.ty(db)),
478        Definition::Function(it) => Some(it.ty(db)),
479        Definition::Adt(it) => Some(it.ty(db)),
480        Definition::Const(it) => Some(it.ty(db)),
481        Definition::Static(it) => Some(it.ty(db)),
482        Definition::TypeAlias(it) => Some(it.ty(db)),
483        Definition::BuiltinType(it) => Some(it.ty(db)),
484        _ => None,
485    };
486    let notable_traits = def_ty.map(|ty| notable_traits(db, &ty)).unwrap_or_default();
487    let subst_types = subst.map(|subst| subst.types(db));
488
489    let (markup, range_map) = render::definition(
490        sema.db,
491        def,
492        famous_defs.as_ref(),
493        &notable_traits,
494        macro_arm,
495        render_extras,
496        subst_types.as_ref(),
497        config,
498        edition,
499        display_target,
500    );
501    HoverResult {
502        markup: render::process_markup(sema.db, def, &markup, range_map, config),
503        actions: [
504            show_fn_references_action(sema, def),
505            show_implementations_action(sema, def),
506            runnable_action(sema, def, file_id),
507            goto_type_action_for_def(sema, def, &notable_traits, subst_types, edition),
508        ]
509        .into_iter()
510        .flatten()
511        .collect(),
512    }
513}
514
515fn notable_traits<'db>(
516    db: &'db RootDatabase,
517    ty: &hir::Type<'db>,
518) -> Vec<(hir::Trait, Vec<(Option<hir::Type<'db>>, hir::Name)>)> {
519    if ty.is_unknown() {
520        // The trait solver returns "yes" to the question whether the error type
521        // impls any trait, and we don't want to show it as having any notable trait.
522        return Vec::new();
523    }
524
525    db.notable_traits_in_deps(ty.krate(db).into())
526        .iter()
527        .flat_map(|it| &**it)
528        .filter_map(move |&trait_| {
529            let trait_ = trait_.into();
530            ty.impls_trait(db, trait_, &[]).then(|| {
531                (
532                    trait_,
533                    trait_
534                        .items(db)
535                        .into_iter()
536                        .filter_map(hir::AssocItem::as_type_alias)
537                        .map(|alias| {
538                            (ty.normalize_trait_assoc_type(db, &[], alias), alias.name(db))
539                        })
540                        .collect::<Vec<_>>(),
541                )
542            })
543        })
544        .sorted_by_cached_key(|(trait_, _)| trait_.name(db))
545        .collect::<Vec<_>>()
546}
547
548fn show_implementations_action(
549    sema: &Semantics<'_, RootDatabase>,
550    def: Definition,
551) -> Option<HoverAction> {
552    fn to_action(nav_target: NavigationTarget) -> HoverAction {
553        HoverAction::Implementation(FilePosition {
554            file_id: nav_target.file_id,
555            offset: nav_target.focus_or_full_range().start(),
556        })
557    }
558
559    let adt = match def {
560        Definition::Trait(it) => {
561            return it.try_to_nav(sema).map(UpmappingResult::call_site).map(to_action);
562        }
563        Definition::Adt(it) => Some(it),
564        Definition::SelfType(it) => it.self_ty(sema.db).as_adt(),
565        _ => None,
566    }?;
567    adt.try_to_nav(sema).map(UpmappingResult::call_site).map(to_action)
568}
569
570fn show_fn_references_action(
571    sema: &Semantics<'_, RootDatabase>,
572    def: Definition,
573) -> Option<HoverAction> {
574    match def {
575        Definition::Function(it) => {
576            it.try_to_nav(sema).map(UpmappingResult::call_site).map(|nav_target| {
577                HoverAction::Reference(FilePosition {
578                    file_id: nav_target.file_id,
579                    offset: nav_target.focus_or_full_range().start(),
580                })
581            })
582        }
583        _ => None,
584    }
585}
586
587fn runnable_action(
588    sema: &hir::Semantics<'_, RootDatabase>,
589    def: Definition,
590    file_id: FileId,
591) -> Option<HoverAction> {
592    match def {
593        Definition::Module(it) => runnable_mod(sema, it).map(HoverAction::Runnable),
594        Definition::Function(func) => {
595            let src = func.source(sema.db)?;
596            if src.file_id.file_id().is_none_or(|f| f.file_id(sema.db) != file_id) {
597                cov_mark::hit!(hover_macro_generated_struct_fn_doc_comment);
598                cov_mark::hit!(hover_macro_generated_struct_fn_doc_attr);
599                return None;
600            }
601
602            runnable_fn(sema, func).map(HoverAction::Runnable)
603        }
604        _ => None,
605    }
606}
607
608fn goto_type_action_for_def(
609    sema: &Semantics<'_, RootDatabase>,
610    def: Definition,
611    notable_traits: &[(hir::Trait, Vec<(Option<hir::Type<'_>>, hir::Name)>)],
612    subst_types: Option<Vec<(hir::Symbol, hir::Type<'_>)>>,
613    edition: Edition,
614) -> Option<HoverAction> {
615    let db = sema.db;
616    let mut targets: Vec<hir::ModuleDef> = Vec::new();
617    let mut push_new_def = |item: hir::ModuleDef| {
618        if !targets.contains(&item) {
619            targets.push(item);
620        }
621    };
622
623    for &(trait_, ref assocs) in notable_traits {
624        push_new_def(trait_.into());
625        assocs.iter().filter_map(|(ty, _)| ty.as_ref()).for_each(|ty| {
626            walk_and_push_ty(db, ty, &mut push_new_def);
627        });
628    }
629
630    if let Ok(generic_def) = GenericDef::try_from(def) {
631        generic_def.type_or_const_params(db).into_iter().for_each(|it| {
632            walk_and_push_ty(db, &it.ty(db), &mut push_new_def);
633        });
634    }
635
636    let ty = match def {
637        Definition::Local(it) => Some(it.ty(db)),
638        Definition::Field(field) => Some(field.ty(db).to_type(db)),
639        Definition::TupleField(field) => Some(field.ty(db)),
640        Definition::Const(it) => Some(it.ty(db)),
641        Definition::Static(it) => Some(it.ty(db)),
642        Definition::Function(func) => {
643            for param in func.assoc_fn_params(db) {
644                walk_and_push_ty(db, param.ty(), &mut push_new_def);
645            }
646            Some(func.ret_type(db))
647        }
648        Definition::GenericParam(hir::GenericParam::ConstParam(it)) => Some(it.ty(db)),
649        Definition::GenericParam(hir::GenericParam::TypeParam(it)) => Some(it.ty(db)),
650        _ => None,
651    };
652    if let Some(ty) = ty {
653        walk_and_push_ty(db, &ty, &mut push_new_def);
654    }
655
656    if let Some(subst_types) = subst_types {
657        for (_, ty) in subst_types {
658            walk_and_push_ty(db, &ty, &mut push_new_def);
659        }
660    }
661
662    HoverAction::goto_type_from_targets(sema, targets, edition)
663}
664
665fn walk_and_push_ty(
666    db: &RootDatabase,
667    ty: &hir::Type<'_>,
668    push_new_def: &mut dyn FnMut(hir::ModuleDef),
669) {
670    ty.walk(db, |t| {
671        if let Some(adt) = t.as_adt() {
672            push_new_def(adt.into());
673        } else if let Some(trait_) = t.as_dyn_trait() {
674            push_new_def(trait_.into());
675        } else if let Some(traits) = t.as_impl_traits(db) {
676            traits.for_each(|it| push_new_def(it.into()));
677        } else if let Some(trait_) = t.as_associated_type_parent_trait(db) {
678            push_new_def(trait_.into());
679        } else if let Some(tp) = t.as_type_param(db) {
680            let sized_trait = LangItem::Sized.resolve_trait(db, t.krate(db).into());
681            tp.trait_bounds(db)
682                .into_iter()
683                .filter(|&it| Some(it.into()) != sized_trait)
684                .for_each(|it| push_new_def(it.into()));
685        }
686    });
687}
688
689fn dedupe_or_merge_hover_actions(actions: Vec<HoverAction>) -> Vec<HoverAction> {
690    let mut deduped_actions = Vec::with_capacity(actions.len());
691    let mut go_to_type_targets = FxIndexSet::default();
692
693    let mut seen_implementation = false;
694    let mut seen_reference = false;
695    let mut seen_runnable = false;
696    for action in actions {
697        match action {
698            HoverAction::GoToType(targets) => {
699                go_to_type_targets.extend(targets);
700            }
701            HoverAction::Implementation(..) => {
702                if !seen_implementation {
703                    seen_implementation = true;
704                    deduped_actions.push(action);
705                }
706            }
707            HoverAction::Reference(..) => {
708                if !seen_reference {
709                    seen_reference = true;
710                    deduped_actions.push(action);
711                }
712            }
713            HoverAction::Runnable(..) => {
714                if !seen_runnable {
715                    seen_runnable = true;
716                    deduped_actions.push(action);
717                }
718            }
719        };
720    }
721
722    if !go_to_type_targets.is_empty() {
723        deduped_actions.push(HoverAction::GoToType(
724            go_to_type_targets.into_iter().sorted_by(|a, b| a.mod_path.cmp(&b.mod_path)).collect(),
725        ));
726    }
727
728    deduped_actions
729}