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
215
216
217
218
219
220
221
222
223
224
225
//! Term search assist
use hir::term_search::TermSearchCtx;
use ide_db::assists::{AssistId, AssistKind, GroupLabel};

use itertools::Itertools;
use syntax::{ast, AstNode};

use crate::assist_context::{AssistContext, Assists};

pub(crate) fn term_search(acc: &mut Assists, ctx: &AssistContext<'_>) -> Option<()> {
    let unexpanded = ctx.find_node_at_offset::<ast::MacroCall>()?;
    let syntax = unexpanded.syntax();
    let goal_range = syntax.text_range();

    let excl = unexpanded.excl_token()?;
    let macro_name_token = excl.prev_token()?;
    let name = macro_name_token.text();
    if name != "todo" {
        return None;
    }

    let parent = syntax.parent()?;
    let target_ty = ctx.sema.type_of_expr(&ast::Expr::cast(parent.clone())?)?.adjusted();

    let scope = ctx.sema.scope(&parent)?;

    let term_search_ctx = TermSearchCtx {
        sema: &ctx.sema,
        scope: &scope,
        goal: target_ty,
        config: Default::default(),
    };
    let paths = hir::term_search::term_search(term_search_ctx);

    if paths.is_empty() {
        return None;
    }

    let mut formatter = |_: &hir::Type| String::from("todo!()");
    for path in paths.iter().unique() {
        let code = path.gen_source_code(&scope, &mut formatter);
        acc.add_group(
            &GroupLabel(String::from("Term search")),
            AssistId("term_search", AssistKind::Generate),
            format!("Replace todo!() with {code}"),
            goal_range,
            |builder| {
                builder.replace(goal_range, code);
            },
        );
    }

    Some(())
}

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

    use super::*;

    #[test]
    fn test_complete_local() {
        check_assist(
            term_search,
            "macro_rules! todo { () => (_) }; fn f() { let a: u128 = 1; let b: u128 = todo$0!() }",
            "macro_rules! todo { () => (_) }; fn f() { let a: u128 = 1; let b: u128 = a }",
        )
    }

    #[test]
    fn test_complete_todo_with_msg() {
        check_assist(
            term_search,
            "macro_rules! todo { ($($arg:tt)+) => (_) }; fn f() { let a: u128 = 1; let b: u128 = todo$0!(\"asd\") }",
            "macro_rules! todo { ($($arg:tt)+) => (_) }; fn f() { let a: u128 = 1; let b: u128 = a }",
        )
    }

    #[test]
    fn test_complete_struct_field() {
        check_assist(
            term_search,
            r#"macro_rules! todo { () => (_) };
            struct A { pub x: i32, y: bool }
            fn f() { let a = A { x: 1, y: true }; let b: i32 = todo$0!(); }"#,
            r#"macro_rules! todo { () => (_) };
            struct A { pub x: i32, y: bool }
            fn f() { let a = A { x: 1, y: true }; let b: i32 = a.x; }"#,
        )
    }

    #[test]
    fn test_enum_with_generics() {
        check_assist(
            term_search,
            r#"macro_rules! todo { () => (_) };
            enum Option<T> { Some(T), None }
            fn f() { let a: i32 = 1; let b: Option<i32> = todo$0!(); }"#,
            r#"macro_rules! todo { () => (_) };
            enum Option<T> { Some(T), None }
            fn f() { let a: i32 = 1; let b: Option<i32> = Option::None; }"#,
        )
    }

    #[test]
    fn test_enum_with_generics2() {
        check_assist(
            term_search,
            r#"macro_rules! todo { () => (_) };
            enum Option<T> { None, Some(T) }
            fn f() { let a: i32 = 1; let b: Option<i32> = todo$0!(); }"#,
            r#"macro_rules! todo { () => (_) };
            enum Option<T> { None, Some(T) }
            fn f() { let a: i32 = 1; let b: Option<i32> = Option::Some(a); }"#,
        )
    }

    #[test]
    fn test_enum_with_generics3() {
        check_assist(
            term_search,
            r#"macro_rules! todo { () => (_) };
            enum Option<T> { None, Some(T) }
            fn f() { let a: Option<i32> = Option::None; let b: Option<Option<i32>> = todo$0!(); }"#,
            r#"macro_rules! todo { () => (_) };
            enum Option<T> { None, Some(T) }
            fn f() { let a: Option<i32> = Option::None; let b: Option<Option<i32>> = Option::Some(a); }"#,
        )
    }

    #[test]
    fn test_enum_with_generics4() {
        check_assist(
            term_search,
            r#"macro_rules! todo { () => (_) };
            enum Foo<T = i32> { Foo(T) }
            fn f() { let a = 0; let b: Foo = todo$0!(); }"#,
            r#"macro_rules! todo { () => (_) };
            enum Foo<T = i32> { Foo(T) }
            fn f() { let a = 0; let b: Foo = Foo::Foo(a); }"#,
        );

        check_assist(
            term_search,
            r#"macro_rules! todo { () => (_) };
            enum Foo<T = i32> { Foo(T) }
            fn f() { let a: Foo<u32> = Foo::Foo(0); let b: Foo<u32> = todo$0!(); }"#,
            r#"macro_rules! todo { () => (_) };
            enum Foo<T = i32> { Foo(T) }
            fn f() { let a: Foo<u32> = Foo::Foo(0); let b: Foo<u32> = a; }"#,
        )
    }

    #[test]
    fn test_newtype() {
        check_assist(
            term_search,
            r#"macro_rules! todo { () => (_) };
            struct Foo(i32);
            fn f() { let a: i32 = 1; let b: Foo = todo$0!(); }"#,
            r#"macro_rules! todo { () => (_) };
            struct Foo(i32);
            fn f() { let a: i32 = 1; let b: Foo = Foo(a); }"#,
        )
    }

    #[test]
    fn test_shadowing() {
        check_assist(
            term_search,
            r#"macro_rules! todo { () => (_) };
            fn f() { let a: i32 = 1; let b: i32 = 2; let a: u32 = 0; let c: i32 = todo$0!(); }"#,
            r#"macro_rules! todo { () => (_) };
            fn f() { let a: i32 = 1; let b: i32 = 2; let a: u32 = 0; let c: i32 = b; }"#,
        )
    }

    #[test]
    fn test_famous_bool() {
        check_assist(
            term_search,
            r#"macro_rules! todo { () => (_) };
            fn f() { let a: bool = todo$0!(); }"#,
            r#"macro_rules! todo { () => (_) };
            fn f() { let a: bool = false; }"#,
        )
    }

    #[test]
    fn test_fn_with_reference_types() {
        check_assist(
            term_search,
            r#"macro_rules! todo { () => (_) };
            fn f(a: &i32) -> f32 { a as f32 }
            fn g() { let a = 1; let b: f32 = todo$0!(); }"#,
            r#"macro_rules! todo { () => (_) };
            fn f(a: &i32) -> f32 { a as f32 }
            fn g() { let a = 1; let b: f32 = f(&a); }"#,
        )
    }

    #[test]
    fn test_fn_with_reference_types2() {
        check_assist(
            term_search,
            r#"macro_rules! todo { () => (_) };
            fn f(a: &i32) -> f32 { a as f32 }
            fn g() { let a = &1; let b: f32 = todo$0!(); }"#,
            r#"macro_rules! todo { () => (_) };
            fn f(a: &i32) -> f32 { a as f32 }
            fn g() { let a = &1; let b: f32 = f(a); }"#,
        )
    }

    #[test]
    fn test_fn_with_reference_types3() {
        check_assist_not_applicable(
            term_search,
            r#"macro_rules! todo { () => (_) };
            fn f(a: &i32) -> f32 { a as f32 }
            fn g() { let a = &mut 1; let b: f32 = todo$0!(); }"#,
        )
    }
}