|
| 1 | +//! Implementation of "closure return type" inlay hints. |
| 2 | +//! |
| 3 | +//! Tests live in [`bind_pat`][super::bind_pat] module. |
| 4 | +use ide_db::{base_db::FileId, famous_defs::FamousDefs}; |
| 5 | +use syntax::ast::{self, AstNode}; |
| 6 | +use text_edit::{TextRange, TextSize}; |
| 7 | + |
| 8 | +use crate::{InlayHint, InlayHintLabel, InlayHintsConfig, InlayKind}; |
| 9 | + |
| 10 | +pub(super) fn hints( |
| 11 | + acc: &mut Vec<InlayHint>, |
| 12 | + FamousDefs(sema, _): &FamousDefs<'_, '_>, |
| 13 | + config: &InlayHintsConfig, |
| 14 | + _file_id: FileId, |
| 15 | + closure: ast::ClosureExpr, |
| 16 | +) -> Option<()> { |
| 17 | + if !config.closure_capture_hints { |
| 18 | + return None; |
| 19 | + } |
| 20 | + let ty = &sema.type_of_expr(&closure.clone().into())?.original; |
| 21 | + let c = ty.as_closure()?; |
| 22 | + let captures = c.captured_items(sema.db); |
| 23 | + |
| 24 | + if captures.is_empty() { |
| 25 | + return None; |
| 26 | + } |
| 27 | + |
| 28 | + let move_kw_range = match closure.move_token() { |
| 29 | + Some(t) => t.text_range(), |
| 30 | + None => { |
| 31 | + let range = closure.syntax().first_token()?.prev_token()?.text_range(); |
| 32 | + let range = TextRange::new(range.end() - TextSize::from(1), range.end()); |
| 33 | + acc.push(InlayHint { |
| 34 | + range, |
| 35 | + kind: InlayKind::ClosureCapture, |
| 36 | + label: InlayHintLabel::simple("move", None, None), |
| 37 | + text_edit: None, |
| 38 | + }); |
| 39 | + range |
| 40 | + } |
| 41 | + }; |
| 42 | + acc.push(InlayHint { |
| 43 | + range: move_kw_range, |
| 44 | + kind: InlayKind::ClosureCapture, |
| 45 | + label: InlayHintLabel::from("("), |
| 46 | + text_edit: None, |
| 47 | + }); |
| 48 | + let last = captures.len() - 1; |
| 49 | + for (idx, capture) in captures.into_iter().enumerate() { |
| 50 | + let local = capture.local(); |
| 51 | + let source = local.primary_source(sema.db); |
| 52 | + |
| 53 | + // force cache the source file, otherwise sema lookup will potentially panic |
| 54 | + _ = sema.parse_or_expand(source.file()); |
| 55 | + |
| 56 | + acc.push(InlayHint { |
| 57 | + range: move_kw_range, |
| 58 | + kind: InlayKind::ClosureCapture, |
| 59 | + label: InlayHintLabel::simple( |
| 60 | + format!( |
| 61 | + "{}{}", |
| 62 | + match capture.kind() { |
| 63 | + hir::CaptureKind::SharedRef => "&", |
| 64 | + hir::CaptureKind::UniqueSharedRef => "&unique ", |
| 65 | + hir::CaptureKind::MutableRef => "&mut ", |
| 66 | + hir::CaptureKind::Move => "", |
| 67 | + }, |
| 68 | + local.name(sema.db) |
| 69 | + ), |
| 70 | + None, |
| 71 | + source.name().and_then(|name| sema.original_range_opt(name.syntax())), |
| 72 | + ), |
| 73 | + text_edit: None, |
| 74 | + }); |
| 75 | + |
| 76 | + if idx != last { |
| 77 | + acc.push(InlayHint { |
| 78 | + range: move_kw_range, |
| 79 | + kind: InlayKind::ClosureCapture, |
| 80 | + label: InlayHintLabel::simple(", ", None, None), |
| 81 | + text_edit: None, |
| 82 | + }); |
| 83 | + } |
| 84 | + } |
| 85 | + acc.push(InlayHint { |
| 86 | + range: move_kw_range, |
| 87 | + kind: InlayKind::ClosureCapture, |
| 88 | + label: InlayHintLabel::from(")"), |
| 89 | + text_edit: None, |
| 90 | + }); |
| 91 | + |
| 92 | + Some(()) |
| 93 | +} |
| 94 | + |
| 95 | +#[cfg(test)] |
| 96 | +mod tests { |
| 97 | + use crate::{ |
| 98 | + inlay_hints::tests::{check_with_config, DISABLED_CONFIG}, |
| 99 | + InlayHintsConfig, |
| 100 | + }; |
| 101 | + |
| 102 | + #[test] |
| 103 | + fn all_capture_kinds() { |
| 104 | + check_with_config( |
| 105 | + InlayHintsConfig { closure_capture_hints: true, ..DISABLED_CONFIG }, |
| 106 | + r#" |
| 107 | +//- minicore: copy, derive |
| 108 | +
|
| 109 | +
|
| 110 | +#[derive(Copy, Clone)] |
| 111 | +struct Copy; |
| 112 | +
|
| 113 | +struct NonCopy; |
| 114 | +
|
| 115 | +fn main() { |
| 116 | + let foo = Copy; |
| 117 | + let bar = NonCopy; |
| 118 | + let mut baz = NonCopy; |
| 119 | + let qux = &mut NonCopy; |
| 120 | + || { |
| 121 | +// ^ move |
| 122 | +// ^ ( |
| 123 | +// ^ &foo |
| 124 | +// ^ , |
| 125 | +// ^ bar |
| 126 | +// ^ , |
| 127 | +// ^ baz |
| 128 | +// ^ , |
| 129 | +// ^ qux |
| 130 | +// ^ ) |
| 131 | + foo; |
| 132 | + bar; |
| 133 | + baz; |
| 134 | + qux; |
| 135 | + }; |
| 136 | + || { |
| 137 | +// ^ move |
| 138 | +// ^ ( |
| 139 | +// ^ &foo |
| 140 | +// ^ , |
| 141 | +// ^ &bar |
| 142 | +// ^ , |
| 143 | +// ^ &baz |
| 144 | +// ^ , |
| 145 | +// ^ &qux |
| 146 | +// ^ ) |
| 147 | + &foo; |
| 148 | + &bar; |
| 149 | + &baz; |
| 150 | + &qux; |
| 151 | + }; |
| 152 | + || { |
| 153 | +// ^ move |
| 154 | +// ^ ( |
| 155 | +// ^ &mut baz |
| 156 | +// ^ ) |
| 157 | + &mut baz; |
| 158 | + }; |
| 159 | + // FIXME: &mut qux should be &unique qux |
| 160 | + || { |
| 161 | +// ^ move |
| 162 | +// ^ ( |
| 163 | +// ^ &mut baz |
| 164 | +// ^ , |
| 165 | +// ^ &mut qux |
| 166 | +// ^ ) |
| 167 | + baz = NonCopy; |
| 168 | + *qux = NonCopy; |
| 169 | + }; |
| 170 | +} |
| 171 | +"#, |
| 172 | + ); |
| 173 | + } |
| 174 | + |
| 175 | + #[test] |
| 176 | + fn move_token() { |
| 177 | + check_with_config( |
| 178 | + InlayHintsConfig { closure_capture_hints: true, ..DISABLED_CONFIG }, |
| 179 | + r#" |
| 180 | +//- minicore: copy, derive |
| 181 | +fn main() { |
| 182 | + let foo = u32; |
| 183 | + move || { |
| 184 | +// ^^^^ ( |
| 185 | +// ^^^^ foo |
| 186 | +// ^^^^ ) |
| 187 | + foo; |
| 188 | + }; |
| 189 | +} |
| 190 | +"#, |
| 191 | + ); |
| 192 | + } |
| 193 | +} |
0 commit comments