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
use ide_db::{
    assists::{AssistId, AssistKind},
    base_db::AnchoredPathBuf,
};
use syntax::{
    ast::{self, Whitespace},
    AstNode, AstToken, SourceFile, TextRange, TextSize,
};

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

/// Trim(remove leading and trailing whitespace) `initial_range` in `source_file`, return the trimmed range.
fn trimmed_text_range(source_file: &SourceFile, initial_range: TextRange) -> TextRange {
    let mut trimmed_range = initial_range;
    while source_file
        .syntax()
        .token_at_offset(trimmed_range.start())
        .find_map(Whitespace::cast)
        .is_some()
        && trimmed_range.start() < trimmed_range.end()
    {
        let start = trimmed_range.start() + TextSize::from(1);
        trimmed_range = TextRange::new(start, trimmed_range.end());
    }
    while source_file
        .syntax()
        .token_at_offset(trimmed_range.end())
        .find_map(Whitespace::cast)
        .is_some()
        && trimmed_range.start() < trimmed_range.end()
    {
        let end = trimmed_range.end() - TextSize::from(1);
        trimmed_range = TextRange::new(trimmed_range.start(), end);
    }
    trimmed_range
}

// Assist: move_to_mod_rs
//
// Moves xxx.rs to xxx/mod.rs.
//
// ```
// //- /main.rs
// mod a;
// //- /a.rs
// $0fn t() {}$0
// ```
// ->
// ```
// fn t() {}
// ```
pub(crate) fn move_to_mod_rs(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
    let source_file = ctx.find_node_at_offset::<ast::SourceFile>()?;
    let module = ctx.sema.to_module_def(ctx.frange.file_id)?;
    // Enable this assist if the user select all "meaningful" content in the source file
    let trimmed_selected_range = trimmed_text_range(&source_file, ctx.frange.range);
    let trimmed_file_range = trimmed_text_range(&source_file, source_file.syntax().text_range());
    if module.is_mod_rs(ctx.db()) {
        cov_mark::hit!(already_mod_rs);
        return None;
    }
    if trimmed_selected_range != trimmed_file_range {
        cov_mark::hit!(not_all_selected);
        return None;
    }

    let target = TextRange::new(
        source_file.syntax().text_range().start(),
        source_file.syntax().text_range().end(),
    );
    let module_name = module.name(ctx.db())?.to_string();
    let path = format!("./{}/mod.rs", module_name);
    let dst = AnchoredPathBuf { anchor: ctx.frange.file_id, path };
    acc.add(
        AssistId("move_to_mod_rs", AssistKind::Refactor),
        format!("Turn {}.rs to {}/mod.rs", module_name, module_name),
        target,
        |builder| {
            builder.move_file(ctx.frange.file_id, dst);
        },
    )
}

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

    use super::*;

    #[test]
    fn trivial() {
        check_assist(
            move_to_mod_rs,
            r#"
//- /main.rs
mod a;
//- /a.rs
$0fn t() {}
$0"#,
            r#"
//- /a/mod.rs
fn t() {}
"#,
        );
    }

    #[test]
    fn must_select_all_file() {
        cov_mark::check!(not_all_selected);
        check_assist_not_applicable(
            move_to_mod_rs,
            r#"
//- /main.rs
mod a;
//- /a.rs
fn t() {}$0
"#,
        );
        cov_mark::check!(not_all_selected);
        check_assist_not_applicable(
            move_to_mod_rs,
            r#"
//- /main.rs
mod a;
//- /a.rs
$0fn$0 t() {}
"#,
        );
    }

    #[test]
    fn cannot_promote_mod_rs() {
        cov_mark::check!(already_mod_rs);
        check_assist_not_applicable(
            move_to_mod_rs,
            r#"//- /main.rs
mod a;
//- /a/mod.rs
$0fn t() {}$0
"#,
        );
    }

    #[test]
    fn cannot_promote_main_and_lib_rs() {
        check_assist_not_applicable(
            move_to_mod_rs,
            r#"//- /main.rs
$0fn t() {}$0
"#,
        );
        check_assist_not_applicable(
            move_to_mod_rs,
            r#"//- /lib.rs
$0fn t() {}$0
"#,
        );
    }

    #[test]
    fn works_in_mod() {
        // note: /a/b.rs remains untouched
        check_assist(
            move_to_mod_rs,
            r#"//- /main.rs
mod a;
//- /a.rs
$0mod b;
fn t() {}$0
//- /a/b.rs
fn t1() {}
"#,
            r#"
//- /a/mod.rs
mod b;
fn t() {}
"#,
        );
    }
}