|
| 1 | +//! Implementation of trait bound hints. |
| 2 | +//! |
| 3 | +//! Currently this renders the implied `Sized` bound. |
| 4 | +use either::Either; |
| 5 | +use ide_db::{famous_defs::FamousDefs, text_edit::TextEdit}; |
| 6 | + |
| 7 | +use syntax::ast::{self, AstNode}; |
| 8 | + |
| 9 | +use crate::{InlayHint, InlayHintLabel, InlayHintPosition, InlayHintsConfig, InlayKind}; |
| 10 | + |
| 11 | +pub(super) fn hints( |
| 12 | + acc: &mut Vec<InlayHint>, |
| 13 | + FamousDefs(sema, _): &FamousDefs<'_, '_>, |
| 14 | + config: &InlayHintsConfig, |
| 15 | + path: Either<ast::PathType, ast::DynTraitType>, |
| 16 | +) -> Option<()> { |
| 17 | + let parent = path.syntax().parent()?; |
| 18 | + let range = match path { |
| 19 | + Either::Left(path) => { |
| 20 | + let paren = |
| 21 | + parent.ancestors().take_while(|it| ast::ParenType::can_cast(it.kind())).last(); |
| 22 | + let parent = paren.as_ref().and_then(|it| it.parent()).unwrap_or(parent); |
| 23 | + if ast::TypeBound::can_cast(parent.kind()) |
| 24 | + || ast::TypeAnchor::can_cast(parent.kind()) |
| 25 | + || ast::Impl::cast(parent) |
| 26 | + .and_then(|it| it.trait_()) |
| 27 | + .is_some_and(|it| it.syntax() == path.syntax()) |
| 28 | + { |
| 29 | + return None; |
| 30 | + } |
| 31 | + sema.resolve_trait(&path.path()?)?; |
| 32 | + paren.map_or_else(|| path.syntax().text_range(), |it| it.text_range()) |
| 33 | + } |
| 34 | + Either::Right(dyn_) => { |
| 35 | + if dyn_.dyn_token().is_some() { |
| 36 | + return None; |
| 37 | + } |
| 38 | + |
| 39 | + dyn_.syntax().text_range() |
| 40 | + } |
| 41 | + }; |
| 42 | + |
| 43 | + acc.push(InlayHint { |
| 44 | + range, |
| 45 | + kind: InlayKind::Dyn, |
| 46 | + label: InlayHintLabel::simple("dyn", None, None), |
| 47 | + text_edit: Some( |
| 48 | + config.lazy_text_edit(|| TextEdit::insert(range.start(), "dyn ".to_owned())), |
| 49 | + ), |
| 50 | + position: InlayHintPosition::Before, |
| 51 | + pad_left: false, |
| 52 | + pad_right: true, |
| 53 | + resolve_parent: Some(range), |
| 54 | + }); |
| 55 | + |
| 56 | + Some(()) |
| 57 | +} |
| 58 | + |
| 59 | +#[cfg(test)] |
| 60 | +mod tests { |
| 61 | + |
| 62 | + use expect_test::expect; |
| 63 | + |
| 64 | + use crate::inlay_hints::InlayHintsConfig; |
| 65 | + |
| 66 | + use crate::inlay_hints::tests::{DISABLED_CONFIG, check_edit, check_with_config}; |
| 67 | + |
| 68 | + #[track_caller] |
| 69 | + fn check(#[rust_analyzer::rust_fixture] ra_fixture: &str) { |
| 70 | + check_with_config(InlayHintsConfig { sized_bound: true, ..DISABLED_CONFIG }, ra_fixture); |
| 71 | + } |
| 72 | + |
| 73 | + #[test] |
| 74 | + fn path_works() { |
| 75 | + check( |
| 76 | + r#" |
| 77 | +struct S {} |
| 78 | +trait T {} |
| 79 | +fn foo(_: T, _: dyn T, _: S) {} |
| 80 | + // ^ dyn |
| 81 | +fn foo(_: &T, _: for<'a> T) {} |
| 82 | + // ^ dyn |
| 83 | + // ^ dyn |
| 84 | +impl T {} |
| 85 | + // ^ dyn |
| 86 | +impl T for (T) {} |
| 87 | + // ^^^ dyn |
| 88 | +"#, |
| 89 | + ); |
| 90 | + } |
| 91 | + |
| 92 | + #[test] |
| 93 | + fn missing_dyn_bounds() { |
| 94 | + check( |
| 95 | + r#" |
| 96 | +trait T {} |
| 97 | +fn foo( |
| 98 | + _: T + T, |
| 99 | + // ^^^^^ dyn |
| 100 | + _: T + 'a, |
| 101 | + // ^^^^^^ dyn |
| 102 | + _: 'a + T, |
| 103 | + // ^^^^^^ dyn |
| 104 | + _: &(T + T) |
| 105 | + // ^^^^^ dyn |
| 106 | + _: &mut (T + T) |
| 107 | + // ^^^^^ dyn |
| 108 | + _: *mut (T), |
| 109 | + // ^^^ dyn |
| 110 | +) {} |
| 111 | +"#, |
| 112 | + ); |
| 113 | + } |
| 114 | + |
| 115 | + #[test] |
| 116 | + fn edit() { |
| 117 | + check_edit( |
| 118 | + DISABLED_CONFIG, |
| 119 | + r#" |
| 120 | +trait T {} |
| 121 | +fn foo( |
| 122 | + _: &mut T |
| 123 | +) {} |
| 124 | +"#, |
| 125 | + expect![[r#" |
| 126 | + trait T {} |
| 127 | + fn foo( |
| 128 | + _: &mut dyn T |
| 129 | + ) {} |
| 130 | + "#]], |
| 131 | + ); |
| 132 | + } |
| 133 | +} |
0 commit comments