ide_assists/handlers/
normalize_import.rs

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
use ide_db::imports::merge_imports::try_normalize_import;
use syntax::{ast, AstNode};

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

// Assist: normalize_import
//
// Normalizes an import.
//
// ```
// use$0 std::{io, {fmt::Formatter}};
// ```
// ->
// ```
// use std::{fmt::Formatter, io};
// ```
pub(crate) fn normalize_import(acc: &mut Assists, ctx: &AssistContext<'_>) -> Option<()> {
    let use_item = if ctx.has_empty_selection() {
        ctx.find_node_at_offset()?
    } else {
        ctx.covering_element().ancestors().find_map(ast::Use::cast)?
    };

    let target = use_item.syntax().text_range();
    let normalized_use_item =
        try_normalize_import(&use_item, ctx.config.insert_use.granularity.into())?;

    acc.add(
        AssistId("normalize_import", AssistKind::RefactorRewrite),
        "Normalize import",
        target,
        |builder| {
            builder.replace_ast(use_item, normalized_use_item);
        },
    )
}

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

    use super::*;

    macro_rules! check_assist_variations {
        ($fixture: literal, $expected: literal) => {
            check_assist(
                normalize_import,
                concat!("use $0", $fixture, ";"),
                concat!("use ", $expected, ";"),
            );
            check_assist(
                normalize_import,
                concat!("$0use ", $fixture, ";"),
                concat!("use ", $expected, ";"),
            );

            check_assist_import_one(
                normalize_import,
                concat!("use $0", $fixture, ";"),
                concat!("use {", $expected, "};"),
            );
            check_assist_import_one(
                normalize_import,
                concat!("$0use ", $fixture, ";"),
                concat!("use {", $expected, "};"),
            );

            check_assist_import_one(
                normalize_import,
                concat!("use $0{", $fixture, "};"),
                concat!("use {", $expected, "};"),
            );
            check_assist_import_one(
                normalize_import,
                concat!("$0use {", $fixture, "};"),
                concat!("use {", $expected, "};"),
            );

            check_assist(
                normalize_import,
                concat!("use $0", $fixture, "$0;"),
                concat!("use ", $expected, ";"),
            );
            check_assist(
                normalize_import,
                concat!("$0use ", $fixture, ";$0"),
                concat!("use ", $expected, ";"),
            );
        };
    }

    macro_rules! check_assist_not_applicable_variations {
        ($fixture: literal) => {
            check_assist_not_applicable(normalize_import, concat!("use $0", $fixture, ";"));
            check_assist_not_applicable(normalize_import, concat!("$0use ", $fixture, ";"));

            check_assist_not_applicable_for_import_one(
                normalize_import,
                concat!("use $0{", $fixture, "};"),
            );
            check_assist_not_applicable_for_import_one(
                normalize_import,
                concat!("$0use {", $fixture, "};"),
            );
        };
    }

    #[test]
    fn test_order() {
        check_assist_variations!(
            "foo::{*, Qux, bar::{Quux, Bar}, baz, FOO_BAZ, self, Baz}",
            "foo::{self, bar::{Bar, Quux}, baz, Baz, Qux, FOO_BAZ, *}"
        );
    }

    #[test]
    fn test_braces_kept() {
        check_assist_not_applicable_variations!("foo::bar::{$0self}");

        // This code compiles but transforming "bar::{self}" into "bar" causes a
        // compilation error (the name `bar` is defined multiple times).
        // Therefore, the normalize_input assist must not apply here.
        check_assist_not_applicable(
            normalize_import,
            r"
mod foo {

    pub mod bar {}

    pub const bar: i32 = 8;
}

use foo::bar::{$0self};

const bar: u32 = 99;

fn main() {
    let local_bar = bar;
}

",
        );
    }

    #[test]
    fn test_redundant_braces() {
        check_assist_variations!("foo::{bar::{baz, Qux}}", "foo::bar::{baz, Qux}");
        check_assist_variations!("foo::{bar::{self}}", "foo::bar::{self}");
        check_assist_variations!("foo::{bar::{*}}", "foo::bar::*");
        check_assist_variations!("foo::{bar::{Qux as Quux}}", "foo::bar::Qux as Quux");
        check_assist_variations!(
            "foo::bar::{{FOO_BAZ, Qux, self}, {*, baz}}",
            "foo::bar::{self, baz, Qux, FOO_BAZ, *}"
        );
        check_assist_variations!(
            "foo::bar::{{{FOO_BAZ}, {{Qux}, {self}}}, {{*}, {baz}}}",
            "foo::bar::{self, baz, Qux, FOO_BAZ, *}"
        );
    }

    #[test]
    fn test_merge() {
        check_assist_variations!(
            "foo::{*, bar, {FOO_BAZ, qux}, bar::{*, baz}, {Quux}}",
            "foo::{bar::{self, baz, *}, qux, Quux, FOO_BAZ, *}"
        );
        check_assist_variations!(
            "foo::{*, bar, {FOO_BAZ, qux}, bar::{*, baz}, {Quux, bar::{baz::Foo}}}",
            "foo::{bar::{self, baz::{self, Foo}, *}, qux, Quux, FOO_BAZ, *}"
        );
    }

    #[test]
    fn test_merge_self() {
        check_assist_variations!("std::{fmt, fmt::Display}", "std::fmt::{self, Display}");
    }

    #[test]
    fn test_merge_nested() {
        check_assist_variations!("std::{fmt::Debug, fmt::Display}", "std::fmt::{Debug, Display}");
    }

    #[test]
    fn test_merge_nested2() {
        check_assist_variations!("std::{fmt::Debug, fmt::Display}", "std::fmt::{Debug, Display}");
    }

    #[test]
    fn test_merge_self_with_nested_self_item() {
        check_assist_variations!(
            "std::{fmt::{self, Debug}, fmt::{Write, Display}}",
            "std::fmt::{self, Debug, Display, Write}"
        );
    }

    #[test]
    fn works_with_trailing_comma() {
        check_assist(
            normalize_import,
            r"
use $0{
    foo::bar,
    foo::baz,
};
        ",
            r"
use foo::{bar, baz};
        ",
        );
        check_assist_import_one(
            normalize_import,
            r"
use $0{
    foo::bar,
    foo::baz,
};
",
            r"
use {
    foo::{bar, baz},
};
",
        );
    }

    #[test]
    fn not_applicable_to_normalized_import() {
        check_assist_not_applicable_variations!("foo::bar");
        check_assist_not_applicable_variations!("foo::bar::*");
        check_assist_not_applicable_variations!("foo::bar::Qux as Quux");
        check_assist_not_applicable_variations!("foo::bar::{self, baz, Qux, FOO_BAZ, *}");
        check_assist_not_applicable_variations!(
            "foo::{self, bar::{Bar, Quux}, baz, Baz, Qux, FOO_BAZ, *}"
        );
        check_assist_not_applicable_variations!(
            "foo::{bar::{self, baz, *}, qux, Quux, FOO_BAZ, *}"
        );
        check_assist_not_applicable_variations!(
            "foo::{bar::{self, baz::{self, Foo}, *}, qux, Quux, FOO_BAZ, *}"
        );
    }
}