|
| 1 | +//! Implementation of trait bound hints. |
| 2 | +//! |
| 3 | +//! Currently this renders the implied `Sized` bound. |
| 4 | +use ide_db::{famous_defs::FamousDefs, FileRange}; |
| 5 | + |
| 6 | +use span::EditionedFileId; |
| 7 | +use syntax::ast::{self, AstNode, HasTypeBounds}; |
| 8 | + |
| 9 | +use crate::{ |
| 10 | + InlayHint, InlayHintLabel, InlayHintLabelPart, InlayHintPosition, InlayHintsConfig, InlayKind, |
| 11 | + TryToNav, |
| 12 | +}; |
| 13 | + |
| 14 | +pub(super) fn hints( |
| 15 | + acc: &mut Vec<InlayHint>, |
| 16 | + famous_defs @ FamousDefs(sema, _): &FamousDefs<'_, '_>, |
| 17 | + config: &InlayHintsConfig, |
| 18 | + _file_id: EditionedFileId, |
| 19 | + params: ast::GenericParamList, |
| 20 | +) -> Option<()> { |
| 21 | + if !config.sized_bound { |
| 22 | + return None; |
| 23 | + } |
| 24 | + |
| 25 | + let linked_location = |
| 26 | + famous_defs.core_marker_Sized().and_then(|it| it.try_to_nav(sema.db)).map(|it| { |
| 27 | + let n = it.call_site(); |
| 28 | + FileRange { file_id: n.file_id, range: n.focus_or_full_range() } |
| 29 | + }); |
| 30 | + |
| 31 | + for param in params.type_or_const_params() { |
| 32 | + match param { |
| 33 | + ast::TypeOrConstParam::Type(type_param) => { |
| 34 | + let c = type_param.colon_token().map(|it| it.text_range()); |
| 35 | + let has_bounds = |
| 36 | + type_param.type_bound_list().is_some_and(|it| it.bounds().next().is_some()); |
| 37 | + acc.push(InlayHint { |
| 38 | + range: c.unwrap_or_else(|| type_param.syntax().text_range()), |
| 39 | + kind: InlayKind::Type, |
| 40 | + label: { |
| 41 | + let mut hint = InlayHintLabel::default(); |
| 42 | + if c.is_none() { |
| 43 | + hint.parts.push(InlayHintLabelPart { |
| 44 | + text: ": ".to_owned(), |
| 45 | + linked_location: None, |
| 46 | + tooltip: None, |
| 47 | + }); |
| 48 | + } |
| 49 | + hint.parts.push(InlayHintLabelPart { |
| 50 | + text: "Sized".to_owned(), |
| 51 | + linked_location, |
| 52 | + tooltip: None, |
| 53 | + }); |
| 54 | + if has_bounds { |
| 55 | + hint.parts.push(InlayHintLabelPart { |
| 56 | + text: " +".to_owned(), |
| 57 | + linked_location: None, |
| 58 | + tooltip: None, |
| 59 | + }); |
| 60 | + } |
| 61 | + hint |
| 62 | + }, |
| 63 | + text_edit: None, |
| 64 | + position: InlayHintPosition::After, |
| 65 | + pad_left: c.is_some(), |
| 66 | + pad_right: has_bounds, |
| 67 | + resolve_parent: Some(params.syntax().text_range()), |
| 68 | + }); |
| 69 | + } |
| 70 | + ast::TypeOrConstParam::Const(_) => (), |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + Some(()) |
| 75 | +} |
| 76 | + |
| 77 | +#[cfg(test)] |
| 78 | +mod tests { |
| 79 | + use expect_test::expect; |
| 80 | + |
| 81 | + use crate::inlay_hints::InlayHintsConfig; |
| 82 | + |
| 83 | + use crate::inlay_hints::tests::{check_expect, check_with_config, DISABLED_CONFIG}; |
| 84 | + |
| 85 | + #[track_caller] |
| 86 | + fn check(ra_fixture: &str) { |
| 87 | + check_with_config(InlayHintsConfig { sized_bound: true, ..DISABLED_CONFIG }, ra_fixture); |
| 88 | + } |
| 89 | + |
| 90 | + #[test] |
| 91 | + fn smoke() { |
| 92 | + check( |
| 93 | + r#" |
| 94 | +fn foo<T>() {} |
| 95 | + // ^ : Sized |
| 96 | +"#, |
| 97 | + ); |
| 98 | + } |
| 99 | + |
| 100 | + #[test] |
| 101 | + fn with_colon() { |
| 102 | + check( |
| 103 | + r#" |
| 104 | +fn foo<T:>() {} |
| 105 | + // ^ Sized |
| 106 | +"#, |
| 107 | + ); |
| 108 | + } |
| 109 | + |
| 110 | + #[test] |
| 111 | + fn with_colon_and_bounds() { |
| 112 | + check( |
| 113 | + r#" |
| 114 | +fn foo<T: 'static>() {} |
| 115 | + // ^ Sized + |
| 116 | +"#, |
| 117 | + ); |
| 118 | + } |
| 119 | + |
| 120 | + #[test] |
| 121 | + fn location_works() { |
| 122 | + check_expect( |
| 123 | + InlayHintsConfig { sized_bound: true, ..DISABLED_CONFIG }, |
| 124 | + r#" |
| 125 | +//- minicore: sized |
| 126 | +fn foo<T>() {} |
| 127 | +"#, |
| 128 | + expect![[r#" |
| 129 | + [ |
| 130 | + ( |
| 131 | + 7..8, |
| 132 | + [ |
| 133 | + ": ", |
| 134 | + InlayHintLabelPart { |
| 135 | + text: "Sized", |
| 136 | + linked_location: Some( |
| 137 | + FileRangeWrapper { |
| 138 | + file_id: FileId( |
| 139 | + 1, |
| 140 | + ), |
| 141 | + range: 135..140, |
| 142 | + }, |
| 143 | + ), |
| 144 | + tooltip: "", |
| 145 | + }, |
| 146 | + ], |
| 147 | + ), |
| 148 | + ] |
| 149 | + "#]], |
| 150 | + ); |
| 151 | + } |
| 152 | +} |
0 commit comments