rust/crates/assists/src/handlers/move_module_to_file.rs

146 lines
3.2 KiB
Rust
Raw Normal View History

2020-12-07 10:17:54 -06:00
use ast::edit::IndentLevel;
use ide_db::base_db::AnchoredPathBuf;
2020-12-07 10:17:54 -06:00
use syntax::{
ast::{self, edit::AstNodeEdit, NameOwner},
2021-01-06 07:24:47 -06:00
AstNode, TextRange,
2020-12-07 10:17:54 -06:00
};
2021-01-06 07:24:47 -06:00
use test_utils::mark;
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() {
mark::hit!(available_before_curly);
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()?;
let module_def = ctx.sema.to_def(&module_ast)?;
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),
"Extract module to file",
target,
|builder| {
let path = {
let dir = match parent_module.name(ctx.db()) {
Some(name) if !parent_module.is_mod_rs(ctx.db()) => format!("{}/", name),
_ => String::new(),
};
format!("./{}{}.rs", dir, module_name)
};
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-01-06 07:24:47 -06:00
builder.replace(module_ast.syntax().text_range(), format!("mod {};", module_name));
2021-01-06 07:24:47 -06:00
let dst = AnchoredPathBuf { anchor: ctx.frange.file_id, path };
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]
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#"
//- /main.rs
2020-12-07 10:17:54 -06:00
mod tests;
//- /tests.rs
#[test] fn t() {}
2020-12-07 10:17:54 -06:00
"#,
);
2020-12-07 10:17:54 -06:00
}
#[test]
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 {
fn f() {}
2020-12-07 10:17:54 -06:00
}
fn g() {}
2020-12-07 10:17:54 -06:00
"#,
r#"
2020-12-22 10:18:45 -06:00
//- /submod.rs
mod inner;
fn g() {}
2020-12-22 10:18:45 -06:00
//- /submod/inner.rs
fn f() {}
"#,
);
2020-12-07 10:17:54 -06:00
}
#[test]
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#"
//- /main.rs
mod submodule;
//- /submodule/mod.rs
2021-01-06 14:15:48 -06:00
mod inner$0 {
fn f() {}
2020-12-07 10:17:54 -06:00
}
fn g() {}
2020-12-07 10:17:54 -06:00
"#,
r#"
//- /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
#[test]
fn available_before_curly() {
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
}
2020-12-07 10:17:54 -06:00
}