2021-06-17 05:09:28 -05:00
|
|
|
use std::iter;
|
|
|
|
|
2021-05-12 08:06:54 -05:00
|
|
|
use ast::edit::IndentLevel;
|
2020-12-22 10:13:53 -06:00
|
|
|
use ide_db::base_db::AnchoredPathBuf;
|
2021-06-17 05:09:28 -05:00
|
|
|
use itertools::Itertools;
|
2021-02-20 11:11:04 -06:00
|
|
|
use stdx::format_to;
|
2020-12-07 10:17:54 -06:00
|
|
|
use syntax::{
|
2021-09-27 05:54:24 -05:00
|
|
|
ast::{self, edit::AstNodeEdit, HasName},
|
2021-01-06 07:24:47 -06:00
|
|
|
AstNode, TextRange,
|
2020-12-07 10:17:54 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
use crate::{AssistContext, AssistId, AssistKind, Assists};
|
|
|
|
|
2021-01-06 07:24:47 -06:00
|
|
|
// Assist: move_module_to_file
|
2020-12-07 10:17:54 -06:00
|
|
|
//
|
2021-01-06 07:24:47 -06:00
|
|
|
// Moves inline module's contents to a separate file.
|
2020-12-07 10:17:54 -06:00
|
|
|
//
|
|
|
|
// ```
|
2021-01-06 14:15:48 -06:00
|
|
|
// mod $0foo {
|
2020-12-07 10:17:54 -06:00
|
|
|
// fn t() {}
|
|
|
|
// }
|
|
|
|
// ```
|
|
|
|
// ->
|
|
|
|
// ```
|
|
|
|
// mod foo;
|
|
|
|
// ```
|
2021-01-06 07:24:47 -06:00
|
|
|
pub(crate) fn move_module_to_file(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
|
2020-12-07 10:17:54 -06:00
|
|
|
let module_ast = ctx.find_node_at_offset::<ast::Module>()?;
|
2021-01-06 07:24:47 -06:00
|
|
|
let module_items = module_ast.item_list()?;
|
|
|
|
|
|
|
|
let l_curly_offset = module_items.syntax().text_range().start();
|
|
|
|
if l_curly_offset <= ctx.offset() {
|
2021-03-08 14:19:44 -06:00
|
|
|
cov_mark::hit!(available_before_curly);
|
2021-01-06 07:24:47 -06:00
|
|
|
return None;
|
|
|
|
}
|
|
|
|
let target = TextRange::new(module_ast.syntax().text_range().start(), l_curly_offset);
|
|
|
|
|
2020-12-07 10:17:54 -06:00
|
|
|
let module_name = module_ast.name()?;
|
2020-12-22 10:13:53 -06:00
|
|
|
|
2021-06-17 05:09:28 -05:00
|
|
|
// get to the outermost module syntax so we can grab the module of file we are in
|
|
|
|
let outermost_mod_decl =
|
|
|
|
iter::successors(Some(module_ast.clone()), |module| module.parent()).last()?;
|
|
|
|
let module_def = ctx.sema.to_def(&outermost_mod_decl)?;
|
2020-12-22 10:13:53 -06:00
|
|
|
let parent_module = module_def.parent(ctx.db())?;
|
|
|
|
|
|
|
|
acc.add(
|
2021-01-06 07:24:47 -06:00
|
|
|
AssistId("move_module_to_file", AssistKind::RefactorExtract),
|
2020-12-22 10:13:53 -06:00
|
|
|
"Extract module to file",
|
|
|
|
target,
|
|
|
|
|builder| {
|
|
|
|
let path = {
|
2021-06-17 05:09:28 -05:00
|
|
|
let mut buf = String::from("./");
|
|
|
|
match parent_module.name(ctx.db()) {
|
|
|
|
Some(name) if !parent_module.is_mod_rs(ctx.db()) => {
|
|
|
|
format_to!(buf, "{}/", name)
|
|
|
|
}
|
|
|
|
_ => (),
|
|
|
|
}
|
|
|
|
let segments = iter::successors(Some(module_ast.clone()), |module| module.parent())
|
|
|
|
.filter_map(|it| it.name())
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
format_to!(buf, "{}", segments.into_iter().rev().format("/"));
|
|
|
|
format_to!(buf, ".rs");
|
|
|
|
buf
|
2020-12-22 10:13:53 -06:00
|
|
|
};
|
|
|
|
let contents = {
|
|
|
|
let items = module_items.dedent(IndentLevel(1)).to_string();
|
|
|
|
let mut items =
|
|
|
|
items.trim_start_matches('{').trim_end_matches('}').trim().to_string();
|
|
|
|
if !items.is_empty() {
|
|
|
|
items.push('\n');
|
|
|
|
}
|
|
|
|
items
|
|
|
|
};
|
|
|
|
|
2021-06-17 05:09:28 -05:00
|
|
|
let buf = format!("mod {};", module_name);
|
2021-02-20 11:11:04 -06:00
|
|
|
|
2021-06-17 05:09:28 -05:00
|
|
|
let replacement_start = match module_ast.mod_token() {
|
|
|
|
Some(mod_token) => mod_token.text_range(),
|
|
|
|
None => module_ast.syntax().text_range(),
|
|
|
|
}
|
|
|
|
.start();
|
2021-05-12 08:06:54 -05:00
|
|
|
|
|
|
|
builder.replace(
|
|
|
|
TextRange::new(replacement_start, module_ast.syntax().text_range().end()),
|
|
|
|
buf,
|
|
|
|
);
|
2020-12-22 10:13:53 -06:00
|
|
|
|
2021-10-13 07:39:37 -05:00
|
|
|
let dst = AnchoredPathBuf { anchor: ctx.file_id(), path };
|
2020-12-22 10:13:53 -06:00
|
|
|
builder.create_file(dst, contents);
|
|
|
|
},
|
|
|
|
)
|
2020-12-07 10:17:54 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2021-01-06 07:24:47 -06:00
|
|
|
use crate::tests::{check_assist, check_assist_not_applicable};
|
2020-12-07 10:17:54 -06:00
|
|
|
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
2020-12-22 10:13:53 -06:00
|
|
|
fn extract_from_root() {
|
2020-12-07 10:17:54 -06:00
|
|
|
check_assist(
|
2021-01-06 07:24:47 -06:00
|
|
|
move_module_to_file,
|
2020-12-07 10:17:54 -06:00
|
|
|
r#"
|
2021-01-06 14:15:48 -06:00
|
|
|
mod $0tests {
|
2020-12-07 10:17:54 -06:00
|
|
|
#[test] fn t() {}
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
r#"
|
2020-12-22 10:13:53 -06:00
|
|
|
//- /main.rs
|
2020-12-07 10:17:54 -06:00
|
|
|
mod tests;
|
2020-12-22 10:13:53 -06:00
|
|
|
//- /tests.rs
|
|
|
|
#[test] fn t() {}
|
2020-12-07 10:17:54 -06:00
|
|
|
"#,
|
2020-12-22 10:13:53 -06:00
|
|
|
);
|
2020-12-07 10:17:54 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2020-12-22 10:13:53 -06:00
|
|
|
fn extract_from_submodule() {
|
2020-12-07 10:17:54 -06:00
|
|
|
check_assist(
|
2021-01-06 07:24:47 -06:00
|
|
|
move_module_to_file,
|
2020-12-07 10:17:54 -06:00
|
|
|
r#"
|
|
|
|
//- /main.rs
|
2020-12-22 10:18:45 -06:00
|
|
|
mod submod;
|
|
|
|
//- /submod.rs
|
2021-01-06 14:15:48 -06:00
|
|
|
$0mod inner {
|
2020-12-22 10:13:53 -06:00
|
|
|
fn f() {}
|
2020-12-07 10:17:54 -06:00
|
|
|
}
|
2020-12-22 10:13:53 -06:00
|
|
|
fn g() {}
|
2020-12-07 10:17:54 -06:00
|
|
|
"#,
|
|
|
|
r#"
|
2020-12-22 10:18:45 -06:00
|
|
|
//- /submod.rs
|
2020-12-22 10:13:53 -06:00
|
|
|
mod inner;
|
|
|
|
fn g() {}
|
2020-12-22 10:18:45 -06:00
|
|
|
//- /submod/inner.rs
|
2020-12-22 10:13:53 -06:00
|
|
|
fn f() {}
|
|
|
|
"#,
|
|
|
|
);
|
2020-12-07 10:17:54 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2020-12-22 10:13:53 -06:00
|
|
|
fn extract_from_mod_rs() {
|
2020-12-07 10:17:54 -06:00
|
|
|
check_assist(
|
2021-01-06 07:24:47 -06:00
|
|
|
move_module_to_file,
|
2020-12-07 10:17:54 -06:00
|
|
|
r#"
|
2020-12-22 10:13:53 -06:00
|
|
|
//- /main.rs
|
|
|
|
mod submodule;
|
|
|
|
//- /submodule/mod.rs
|
2021-01-06 14:15:48 -06:00
|
|
|
mod inner$0 {
|
2020-12-22 10:13:53 -06:00
|
|
|
fn f() {}
|
2020-12-07 10:17:54 -06:00
|
|
|
}
|
2020-12-22 10:13:53 -06:00
|
|
|
fn g() {}
|
2020-12-07 10:17:54 -06:00
|
|
|
"#,
|
|
|
|
r#"
|
2020-12-22 10:13:53 -06:00
|
|
|
//- /submodule/mod.rs
|
|
|
|
mod inner;
|
|
|
|
fn g() {}
|
|
|
|
//- /submodule/inner.rs
|
|
|
|
fn f() {}
|
|
|
|
"#,
|
|
|
|
);
|
2020-12-07 10:17:54 -06:00
|
|
|
}
|
2021-01-06 07:24:47 -06:00
|
|
|
|
2021-02-20 10:51:16 -06:00
|
|
|
#[test]
|
|
|
|
fn extract_public() {
|
|
|
|
check_assist(
|
|
|
|
move_module_to_file,
|
|
|
|
r#"
|
|
|
|
pub mod $0tests {
|
|
|
|
#[test] fn t() {}
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
//- /main.rs
|
|
|
|
pub mod tests;
|
|
|
|
//- /tests.rs
|
|
|
|
#[test] fn t() {}
|
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn extract_public_crate() {
|
|
|
|
check_assist(
|
|
|
|
move_module_to_file,
|
|
|
|
r#"
|
|
|
|
pub(crate) mod $0tests {
|
|
|
|
#[test] fn t() {}
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
//- /main.rs
|
|
|
|
pub(crate) mod tests;
|
|
|
|
//- /tests.rs
|
|
|
|
#[test] fn t() {}
|
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-01-06 07:24:47 -06:00
|
|
|
#[test]
|
|
|
|
fn available_before_curly() {
|
2021-03-08 14:19:44 -06:00
|
|
|
cov_mark::check!(available_before_curly);
|
2021-01-06 14:15:48 -06:00
|
|
|
check_assist_not_applicable(move_module_to_file, r#"mod m { $0 }"#);
|
2021-01-06 07:24:47 -06:00
|
|
|
}
|
2021-05-12 08:06:54 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn keep_outer_comments_and_attributes() {
|
|
|
|
check_assist(
|
|
|
|
move_module_to_file,
|
|
|
|
r#"
|
|
|
|
/// doc comment
|
|
|
|
#[attribute]
|
|
|
|
mod $0tests {
|
|
|
|
#[test] fn t() {}
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
//- /main.rs
|
|
|
|
/// doc comment
|
|
|
|
#[attribute]
|
|
|
|
mod tests;
|
|
|
|
//- /tests.rs
|
|
|
|
#[test] fn t() {}
|
2021-06-17 05:09:28 -05:00
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn extract_nested() {
|
|
|
|
check_assist(
|
|
|
|
move_module_to_file,
|
|
|
|
r#"
|
|
|
|
//- /lib.rs
|
|
|
|
mod foo;
|
|
|
|
//- /foo.rs
|
|
|
|
mod bar {
|
|
|
|
mod baz {
|
|
|
|
mod qux$0 {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
//- /foo.rs
|
|
|
|
mod bar {
|
|
|
|
mod baz {
|
|
|
|
mod qux;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//- /foo/bar/baz/qux.rs
|
2021-05-12 08:06:54 -05:00
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
2020-12-07 10:17:54 -06:00
|
|
|
}
|