summaryrefslogtreecommitdiff
path: root/src/test/kotlin/org/rust/ide/hints/parameter/RsInlayParameterHintsProviderTest.kt
blob: 4b38937b0ab19f3655d7350f4974452163d32e4f (plain)
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
/*
 * Use of this source code is governed by the MIT license that can be
 * found in the LICENSE file.
 */

package org.rust.ide.hints.parameter

import com.intellij.codeInsight.daemon.impl.HintRenderer
import com.intellij.openapi.vfs.VirtualFileFilter
import org.intellij.lang.annotations.Language
import org.rust.*
import org.rust.ide.experiments.RsExperiments.PROC_MACROS
import org.rust.lang.core.psi.RsMethodCall

@WithExperimentalFeatures(PROC_MACROS)
@ProjectDescriptor(WithProcMacroRustProjectDescriptor::class)
class RsInlayParameterHintsProviderTest : RsTestBase() {
    fun `test fn args`() = checkByText("""
        fn foo(arg: u32, arg2: u32) {}
        fn main() { foo(/*hint text="arg:"*/0, /*hint text="arg2:"*/1); }
    """)

    fun `test arg out of bounds`() = checkByText("""
        fn foo(arg: u32) {}
        fn main() { foo(/*hint text="arg:"*/0, 1); }
    """)

    @MockAdditionalCfgOptions("intellij_rust")
    fun `test fn args with cfg`() = checkByText("""
        fn foo(
            #[cfg(not(intellij_rust))] arg1: u16,
            #[cfg(intellij_rust)]      arg2: u32,
            arg3: u64,
        ) {}
        fn main() { foo(/*hint text="arg2:"*/0, /*hint text="arg3:"*/1); }
    """)

    fun `test method args`() = checkByText("""
        struct S;
        impl S {
            fn foo(self, arg: u32, arg2: u32) {}
        }
        fn main() {
            let s = S;
            s.foo(/*hint text="arg:"*/0, /*hint text="arg2:"*/1);
        }
    """)

    fun `test struct fn arg`() = checkByText("""
        struct S;
        impl S {
            fn foo(self, arg: u32) {}
        }
        fn main() {
            let s = S;
            S::foo(/*hint text="self:"*/s, /*hint text="arg:"*/0);
        }
    """)

    fun `test smart hint same parameter name`() = checkByText("""
        fn foo(arg: u32, arg2: u32) {}
        fn main() {
            let arg = 0;
            foo(arg, /*hint text="arg2:"*/1);
        }
    """)

    fun `test smart hint method start with set`() = checkByText("""
        struct S;
        impl S {
            fn set_foo(self, arg: u32) {}
        }
        fn main() {
            let s = S;
            s.set_foo(1);
        }
    """)

    fun `test smart hint self call start with set`() = checkByText("""
        struct S;
        impl S {
            fn set_foo(self, arg: u32) {}
        }
        fn main() {
            let s = S;
            S::set_foo(s, 0);
        }
    """)

    fun `test smart hint same function name and single parameter`() = checkByText("""
        fn foo(arg: u32) {}
        fn main() {
            let foo = 0;
            foo(foo);
        }
    """)

    fun `test smart hint parameter name and ref input`() = checkByText("""
        fn foo(arg: &u32) {}
        fn main() {
            let arg = 0;
            foo(&arg);
        }
    """)

    fun `test smart hint same method name and single parameter`() = checkByText("""
        struct S;
        impl S {
            fn foo(self, foo: u32) {}
        }
        fn main() {
            let s = S;
            s.foo(10);
        }
    """)

    fun `test smart hint same method name (self call) and single parameter`() = checkByText("""
        struct S;
        impl S {
            fn foo(self, foo: u32) {}
        }
        fn main() {
            let s = S;
            S::foo(s, 10);
        }
    """)

    fun `test smart should not annotate tuple structs`() = checkByText("""
        struct TS(i32, f32);
        fn main() {
            let s = TS(5i32, 10.0f32);
        }
    """)

    fun `test smart should not annotate single lambda argument`() = checkByText("""
        fn foo(bar: impl Fn(i32) -> i32) {}
        fn main() {
            foo(|x| x);
        }
    """)

    fun `test fn arg with mut ident`() = checkByText("""
        fn foo(mut arg: u32) {}
        fn main() { foo(/*hint text="arg:"*/0); }
    """)

    fun `test fn arg with mut array`() = checkByText("""
        fn foo([mut x, y]: [i32; 2]) {}
        fn main() { foo(/*hint text="[x, y]:"*/0); }
    """)

    fun `test fn arg with mut tuple`() = checkByText("""
        fn foo((mut x, y): (i32, i32)) {}
        fn main() { foo(/*hint text="(x, y):"*/0); }
    """)

    fun `test fn arg with mut struct`() = checkByText("""
        struct S { x: i32, y: i32 }
        fn foo(S { mut x, y }: S) {}
        fn main() { foo(/*hint text="S {x, y}:"*/0); }
    """)

    fun `test fn arg with mut tuple struct`() = checkByText("""
        struct S(i32, i32);
        fn foo(S(mut x, y): S) {}
        fn main() { foo(/*hint text="S(x, y):"*/0); }
    """)

    fun `test generic enum variant single parameter smart mode disabled`() = checkByText("""
        enum Result<T, E> {
            Ok(T),
            Err(E)
        }

        fn main() {
            Result::Ok(/*hint text="T:"*/0);
            Result::Err(/*hint text="E:"*/0);
        }
    """, smart = false)

    fun `test generic enum variant single parameter smart mode enabled`() = checkByText("""
        enum Result<T, E> {
            Ok(T),
            Err(E)
        }

        fn main() {
            Result::Ok(0);
            Result::Err(0);
        }
    """, smart = true)

    fun `test generic enum variant multiple parameters smart mode disabled`() = checkByText("""
        enum Foo<T, E> {
            Bar(T, E),
        }

        fn main() {
            Foo::Bar(/*hint text="T:"*/0, /*hint text="E:"*/0);
        }
    """, smart = false)

    fun `test generic enum variant multiple parameters smart mode enabled`() = checkByText("""
        enum Foo<T, E> {
            Bar(T, E),
        }

        fn main() {
            Foo::Bar(/*hint text="T:"*/0, /*hint text="E:"*/0);
        }
    """, smart = true)

    fun `test don't touch ast`() {
        fileTreeFromText("""
        //- main.rs
            mod foo;
            use foo::Foo;

            fn main() {
                Foo.bar(92)
            }     //^
        //- foo.rs
            struct Foo;
            impl Foo { fn bar(&self, x: i32) {} }
        """).createAndOpenFileWithCaretMarker()

        val handler = RsInlayParameterHintsProvider()
        val target = findElementInEditor<RsMethodCall>("^")
        checkAstNotLoaded(VirtualFileFilter.ALL)
        val inlays = handler.getParameterHints(target)
        check(inlays.size == 1)
    }

    private fun checkByText(@Language("Rust") code: String, smart: Boolean = true) {
        check("fn main() {" in code) { "Please use hints inside `fn main` function - needed for testing with attr macros" }

        doTest(code, smart)
        doTest(code.replace("fn main() {", "#[test_proc_macros::attr_as_is] fn main() {"), smart)
    }

    @Suppress("UnstableApiUsage")
    private fun doTest(@Language("Rust") code: String, smart: Boolean) {
        InlineFile(code.replace(HINT_COMMENT_PATTERN, "<$1/>"))

        RsInlayParameterHints.smartOption.set(smart)

        myFixture.testInlays({ (it.renderer as HintRenderer).text }) { it.renderer is HintRenderer }
    }

    companion object {
        private val HINT_COMMENT_PATTERN = Regex("""/\*(hint.*?)\*/""")
    }
}