mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-28 21:05:02 +00:00
Improve extract_module_to_file assist
* simplify code * correctly handle crate roots and mod.rs files (nested inline modules are still mishandled) * make sure that new text contains a trailing newline
This commit is contained in:
parent
b98ee075ee
commit
41bc32368e
1 changed files with 66 additions and 103 deletions
|
@ -1,5 +1,5 @@
|
||||||
use ast::edit::IndentLevel;
|
use ast::edit::IndentLevel;
|
||||||
use ide_db::base_db::{AnchoredPathBuf, SourceDatabaseExt};
|
use ide_db::base_db::AnchoredPathBuf;
|
||||||
use syntax::{
|
use syntax::{
|
||||||
ast::{self, edit::AstNodeEdit, NameOwner},
|
ast::{self, edit::AstNodeEdit, NameOwner},
|
||||||
AstNode,
|
AstNode,
|
||||||
|
@ -21,43 +21,44 @@ use crate::{AssistContext, AssistId, AssistKind, Assists};
|
||||||
// mod foo;
|
// mod foo;
|
||||||
// ```
|
// ```
|
||||||
pub(crate) fn extract_module_to_file(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
|
pub(crate) fn extract_module_to_file(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
|
||||||
let assist_id = AssistId("extract_module_to_file", AssistKind::RefactorExtract);
|
|
||||||
let assist_label = "Extract module to file";
|
|
||||||
let db = ctx.db();
|
|
||||||
let module_ast = ctx.find_node_at_offset::<ast::Module>()?;
|
let module_ast = ctx.find_node_at_offset::<ast::Module>()?;
|
||||||
let module_items = module_ast.item_list()?;
|
|
||||||
let dedent_module_items_text = module_items.dedent(IndentLevel(1)).to_string();
|
|
||||||
let module_name = module_ast.name()?;
|
let module_name = module_ast.name()?;
|
||||||
|
|
||||||
|
let module_def = ctx.sema.to_def(&module_ast)?;
|
||||||
|
let parent_module = module_def.parent(ctx.db())?;
|
||||||
|
|
||||||
|
let module_items = module_ast.item_list()?;
|
||||||
let target = module_ast.syntax().text_range();
|
let target = module_ast.syntax().text_range();
|
||||||
let anchor_file_id = ctx.frange.file_id;
|
let anchor_file_id = ctx.frange.file_id;
|
||||||
let sr = db.file_source_root(anchor_file_id);
|
|
||||||
let sr = db.source_root(sr);
|
|
||||||
let file_path = sr.path_for_file(&anchor_file_id)?;
|
|
||||||
let (file_name, file_ext) = file_path.name_and_extension()?;
|
|
||||||
acc.add(assist_id, assist_label, target, |builder| {
|
|
||||||
builder.replace(target, format!("mod {};", module_name));
|
|
||||||
let path = if is_main_or_lib(file_name) {
|
|
||||||
format!("./{}.{}", module_name, file_ext.unwrap())
|
|
||||||
} else {
|
|
||||||
format!("./{}/{}.{}", file_name, module_name, file_ext.unwrap())
|
|
||||||
};
|
|
||||||
let dst = AnchoredPathBuf { anchor: anchor_file_id, path };
|
|
||||||
let contents = update_module_items_string(dedent_module_items_text);
|
|
||||||
builder.create_file(dst, contents);
|
|
||||||
})
|
|
||||||
}
|
|
||||||
fn is_main_or_lib(file_name: &str) -> bool {
|
|
||||||
file_name == "main".to_string() || file_name == "lib".to_string()
|
|
||||||
}
|
|
||||||
fn update_module_items_string(items_str: String) -> String {
|
|
||||||
let mut items_string_lines: Vec<&str> = items_str.lines().collect();
|
|
||||||
items_string_lines.pop(); // Delete last line
|
|
||||||
items_string_lines.reverse();
|
|
||||||
items_string_lines.pop(); // Delete first line
|
|
||||||
items_string_lines.reverse();
|
|
||||||
|
|
||||||
let string = items_string_lines.join("\n");
|
acc.add(
|
||||||
format!("{}", string)
|
AssistId("extract_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
|
||||||
|
};
|
||||||
|
|
||||||
|
builder.replace(target, format!("mod {};", module_name));
|
||||||
|
|
||||||
|
let dst = AnchoredPathBuf { anchor: anchor_file_id, path };
|
||||||
|
builder.create_file(dst, contents);
|
||||||
|
},
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
|
@ -67,104 +68,66 @@ mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn extract_module_to_file_with_basic_module() {
|
fn extract_from_root() {
|
||||||
check_assist(
|
check_assist(
|
||||||
extract_module_to_file,
|
extract_module_to_file,
|
||||||
r#"
|
r#"
|
||||||
//- /foo.rs crate:foo
|
|
||||||
mod tests {<|>
|
mod tests {<|>
|
||||||
#[test] fn t() {}
|
#[test] fn t() {}
|
||||||
}
|
}
|
||||||
"#,
|
"#,
|
||||||
r#"
|
r#"
|
||||||
//- /foo.rs
|
//- /main.rs
|
||||||
mod tests;
|
mod tests;
|
||||||
//- /foo/tests.rs
|
//- /tests.rs
|
||||||
#[test] fn t() {}"#,
|
#[test] fn t() {}
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn extract_module_to_file_with_file_path() {
|
|
||||||
check_assist(
|
|
||||||
extract_module_to_file,
|
|
||||||
r#"
|
|
||||||
//- /src/foo.rs crate:foo
|
|
||||||
mod bar {<|>
|
|
||||||
fn f() {
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
fn main() {
|
|
||||||
println!("Hello, world!");
|
|
||||||
}
|
|
||||||
"#,
|
"#,
|
||||||
r#"
|
);
|
||||||
//- /src/foo.rs
|
|
||||||
mod bar;
|
|
||||||
fn main() {
|
|
||||||
println!("Hello, world!");
|
|
||||||
}
|
|
||||||
//- /src/foo/bar.rs
|
|
||||||
fn f() {
|
|
||||||
|
|
||||||
}"#,
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn extract_module_to_file_with_main_filw() {
|
fn extract_from_submodule() {
|
||||||
check_assist(
|
check_assist(
|
||||||
extract_module_to_file,
|
extract_module_to_file,
|
||||||
r#"
|
r#"
|
||||||
//- /main.rs
|
//- /main.rs
|
||||||
mod foo {<|>
|
mod submodule;
|
||||||
fn f() {
|
//- /submodule.rs
|
||||||
|
mod inner<|> {
|
||||||
}
|
fn f() {}
|
||||||
}
|
|
||||||
fn main() {
|
|
||||||
println!("Hello, world!");
|
|
||||||
}
|
}
|
||||||
|
fn g() {}
|
||||||
"#,
|
"#,
|
||||||
r#"
|
r#"
|
||||||
//- /main.rs
|
//- /submodule.rs
|
||||||
mod foo;
|
mod inner;
|
||||||
fn main() {
|
fn g() {}
|
||||||
println!("Hello, world!");
|
//- /submodule/inner.rs
|
||||||
}
|
fn f() {}
|
||||||
//- /foo.rs
|
"#,
|
||||||
fn f() {
|
);
|
||||||
|
|
||||||
}"#,
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn extract_module_to_file_with_lib_file() {
|
fn extract_from_mod_rs() {
|
||||||
check_assist(
|
check_assist(
|
||||||
extract_module_to_file,
|
extract_module_to_file,
|
||||||
r#"
|
r#"
|
||||||
//- /lib.rs
|
//- /main.rs
|
||||||
mod foo {<|>
|
mod submodule;
|
||||||
fn f() {
|
//- /submodule/mod.rs
|
||||||
|
mod inner<|> {
|
||||||
}
|
fn f() {}
|
||||||
}
|
|
||||||
fn main() {
|
|
||||||
println!("Hello, world!");
|
|
||||||
}
|
}
|
||||||
|
fn g() {}
|
||||||
"#,
|
"#,
|
||||||
r#"
|
r#"
|
||||||
//- /lib.rs
|
//- /submodule/mod.rs
|
||||||
mod foo;
|
mod inner;
|
||||||
fn main() {
|
fn g() {}
|
||||||
println!("Hello, world!");
|
//- /submodule/inner.rs
|
||||||
}
|
fn f() {}
|
||||||
//- /foo.rs
|
"#,
|
||||||
fn f() {
|
);
|
||||||
|
|
||||||
}"#,
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue