Unnamed repository; edit this file 'description' to name the repository.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
use hir::{Adjust, Mutability};
use ide_db::assists::AssistId;
use itertools::Itertools;
use syntax::{
    AstNode, T,
    ast::{self, syntax_factory::SyntaxFactory},
};

use crate::{AssistContext, Assists};

// Assist: add_explicit_method_call_deref
//
// Insert explicit method call reference and dereferences.
//
// ```
// struct Foo;
// impl Foo { fn foo(&self) {} }
// fn test() {
//     Foo$0.$0foo();
// }
// ```
// ->
// ```
// struct Foo;
// impl Foo { fn foo(&self) {} }
// fn test() {
//     (&Foo).foo();
// }
// ```
pub(crate) fn add_explicit_method_call_deref(
    acc: &mut Assists,
    ctx: &AssistContext<'_>,
) -> Option<()> {
    if ctx.has_empty_selection() {
        return None;
    }
    let dot_token = ctx.find_token_syntax_at_offset(T![.])?;
    if ctx.selection_trimmed() != dot_token.text_range() {
        return None;
    }
    let method_call_expr = dot_token.parent().and_then(ast::MethodCallExpr::cast)?;
    let receiver = method_call_expr.receiver()?;

    let adjustments = ctx.sema.expr_adjustments(&receiver)?;
    let adjustments =
        adjustments.into_iter().filter_map(|adjust| simple_adjust_kind(adjust.kind)).collect_vec();
    if adjustments.is_empty() {
        return None;
    }

    acc.add(
        AssistId::refactor_rewrite("add_explicit_method_call_deref"),
        "Insert explicit method call derefs",
        dot_token.text_range(),
        |builder| {
            let mut edit = builder.make_editor(method_call_expr.syntax());
            let make = SyntaxFactory::without_mappings();
            let mut expr = receiver.clone();

            for adjust_kind in adjustments {
                expr = adjust_kind.wrap_expr(expr, &make);
            }

            expr = make.expr_paren(expr).into();
            edit.replace(receiver.syntax(), expr.syntax());

            builder.add_file_edits(ctx.vfs_file_id(), edit);
        },
    )
}

fn simple_adjust_kind(adjust: Adjust) -> Option<AdjustKind> {
    match adjust {
        Adjust::NeverToAny | Adjust::Pointer(_) => None,
        Adjust::Deref(_) => Some(AdjustKind::Deref),
        Adjust::Borrow(hir::AutoBorrow::Ref(mutability)) => Some(AdjustKind::Ref(mutability)),
        Adjust::Borrow(hir::AutoBorrow::RawPtr(mutability)) => Some(AdjustKind::RefRaw(mutability)),
    }
}

enum AdjustKind {
    Deref,
    Ref(Mutability),
    RefRaw(Mutability),
}

impl AdjustKind {
    fn wrap_expr(self, expr: ast::Expr, make: &SyntaxFactory) -> ast::Expr {
        match self {
            AdjustKind::Deref => make.expr_prefix(T![*], expr).into(),
            AdjustKind::Ref(mutability) => make.expr_ref(expr, mutability.is_mut()),
            AdjustKind::RefRaw(mutability) => make.expr_raw_ref(expr, mutability.is_mut()),
        }
    }
}

#[cfg(test)]
mod tests {
    use crate::tests::check_assist;

    use super::*;

    #[test]
    fn works_ref() {
        check_assist(
            add_explicit_method_call_deref,
            r#"
            struct Foo;
            impl Foo { fn foo(&self) {} }
            fn test() {
                Foo$0.$0foo();
            }"#,
            r#"
            struct Foo;
            impl Foo { fn foo(&self) {} }
            fn test() {
                (&Foo).foo();
            }"#,
        );
    }

    #[test]
    fn works_ref_mut() {
        check_assist(
            add_explicit_method_call_deref,
            r#"
            struct Foo;
            impl Foo { fn foo(&mut self) {} }
            fn test() {
                Foo$0.$0foo();
            }"#,
            r#"
            struct Foo;
            impl Foo { fn foo(&mut self) {} }
            fn test() {
                (&mut Foo).foo();
            }"#,
        );
    }

    #[test]
    fn works_deref() {
        check_assist(
            add_explicit_method_call_deref,
            r#"
            struct Foo;
            impl Foo { fn foo(self) {} }
            fn test() {
                let foo = &Foo;
                foo$0.$0foo();
            }"#,
            r#"
            struct Foo;
            impl Foo { fn foo(self) {} }
            fn test() {
                let foo = &Foo;
                (*foo).foo();
            }"#,
        );
    }

    #[test]
    fn works_reborrow() {
        check_assist(
            add_explicit_method_call_deref,
            r#"
            struct Foo;
            impl Foo { fn foo(&self) {} }
            fn test() {
                let foo = &mut Foo;
                foo$0.$0foo();
            }"#,
            r#"
            struct Foo;
            impl Foo { fn foo(&self) {} }
            fn test() {
                let foo = &mut Foo;
                (&*foo).foo();
            }"#,
        );
    }

    #[test]
    fn works_deref_reborrow() {
        check_assist(
            add_explicit_method_call_deref,
            r#"
            //- minicore: deref
            struct Foo;
            struct Bar;
            impl core::ops::Deref for Foo {
                type Target = Bar;
                fn deref(&self) -> &Self::Target {}
            }
            impl Bar { fn bar(&self) {} }
            fn test() {
                let foo = &mut Foo;
                foo$0.$0bar();
            }"#,
            r#"
            struct Foo;
            struct Bar;
            impl core::ops::Deref for Foo {
                type Target = Bar;
                fn deref(&self) -> &Self::Target {}
            }
            impl Bar { fn bar(&self) {} }
            fn test() {
                let foo = &mut Foo;
                (&**foo).bar();
            }"#,
        );
    }
}