mirror of
https://github.com/rust-lang/rust-analyzer
synced 2024-12-27 05:23:24 +00:00
Merge #7178
7178: Better target for move module r=matklad a=matklad
bors r+
🤖
Co-authored-by: Aleksey Kladov <aleksey.kladov@gmail.com>
This commit is contained in:
commit
655ac47ee3
3 changed files with 47 additions and 35 deletions
|
@ -2,17 +2,18 @@ use ast::edit::IndentLevel;
|
||||||
use ide_db::base_db::AnchoredPathBuf;
|
use ide_db::base_db::AnchoredPathBuf;
|
||||||
use syntax::{
|
use syntax::{
|
||||||
ast::{self, edit::AstNodeEdit, NameOwner},
|
ast::{self, edit::AstNodeEdit, NameOwner},
|
||||||
AstNode,
|
AstNode, TextRange,
|
||||||
};
|
};
|
||||||
|
use test_utils::mark;
|
||||||
|
|
||||||
use crate::{AssistContext, AssistId, AssistKind, Assists};
|
use crate::{AssistContext, AssistId, AssistKind, Assists};
|
||||||
|
|
||||||
// Assist: extract_module_to_file
|
// Assist: move_module_to_file
|
||||||
//
|
//
|
||||||
// This assist extract module to file.
|
// Moves inline module's contents to a separate file.
|
||||||
//
|
//
|
||||||
// ```
|
// ```
|
||||||
// mod foo {<|>
|
// mod <|>foo {
|
||||||
// fn t() {}
|
// fn t() {}
|
||||||
// }
|
// }
|
||||||
// ```
|
// ```
|
||||||
|
@ -20,19 +21,24 @@ 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 move_module_to_file(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
|
||||||
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 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);
|
||||||
|
|
||||||
let module_name = module_ast.name()?;
|
let module_name = module_ast.name()?;
|
||||||
|
|
||||||
let module_def = ctx.sema.to_def(&module_ast)?;
|
let module_def = ctx.sema.to_def(&module_ast)?;
|
||||||
let parent_module = module_def.parent(ctx.db())?;
|
let parent_module = module_def.parent(ctx.db())?;
|
||||||
|
|
||||||
let module_items = module_ast.item_list()?;
|
|
||||||
let target = module_ast.syntax().text_range();
|
|
||||||
let anchor_file_id = ctx.frange.file_id;
|
|
||||||
|
|
||||||
acc.add(
|
acc.add(
|
||||||
AssistId("extract_module_to_file", AssistKind::RefactorExtract),
|
AssistId("move_module_to_file", AssistKind::RefactorExtract),
|
||||||
"Extract module to file",
|
"Extract module to file",
|
||||||
target,
|
target,
|
||||||
|builder| {
|
|builder| {
|
||||||
|
@ -53,9 +59,9 @@ pub(crate) fn extract_module_to_file(acc: &mut Assists, ctx: &AssistContext) ->
|
||||||
items
|
items
|
||||||
};
|
};
|
||||||
|
|
||||||
builder.replace(target, format!("mod {};", module_name));
|
builder.replace(module_ast.syntax().text_range(), format!("mod {};", module_name));
|
||||||
|
|
||||||
let dst = AnchoredPathBuf { anchor: anchor_file_id, path };
|
let dst = AnchoredPathBuf { anchor: ctx.frange.file_id, path };
|
||||||
builder.create_file(dst, contents);
|
builder.create_file(dst, contents);
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
@ -63,16 +69,16 @@ pub(crate) fn extract_module_to_file(acc: &mut Assists, ctx: &AssistContext) ->
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use crate::tests::check_assist;
|
use crate::tests::{check_assist, check_assist_not_applicable};
|
||||||
|
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn extract_from_root() {
|
fn extract_from_root() {
|
||||||
check_assist(
|
check_assist(
|
||||||
extract_module_to_file,
|
move_module_to_file,
|
||||||
r#"
|
r#"
|
||||||
mod tests {<|>
|
mod <|>tests {
|
||||||
#[test] fn t() {}
|
#[test] fn t() {}
|
||||||
}
|
}
|
||||||
"#,
|
"#,
|
||||||
|
@ -88,12 +94,12 @@ mod tests;
|
||||||
#[test]
|
#[test]
|
||||||
fn extract_from_submodule() {
|
fn extract_from_submodule() {
|
||||||
check_assist(
|
check_assist(
|
||||||
extract_module_to_file,
|
move_module_to_file,
|
||||||
r#"
|
r#"
|
||||||
//- /main.rs
|
//- /main.rs
|
||||||
mod submod;
|
mod submod;
|
||||||
//- /submod.rs
|
//- /submod.rs
|
||||||
mod inner<|> {
|
<|>mod inner {
|
||||||
fn f() {}
|
fn f() {}
|
||||||
}
|
}
|
||||||
fn g() {}
|
fn g() {}
|
||||||
|
@ -111,7 +117,7 @@ fn f() {}
|
||||||
#[test]
|
#[test]
|
||||||
fn extract_from_mod_rs() {
|
fn extract_from_mod_rs() {
|
||||||
check_assist(
|
check_assist(
|
||||||
extract_module_to_file,
|
move_module_to_file,
|
||||||
r#"
|
r#"
|
||||||
//- /main.rs
|
//- /main.rs
|
||||||
mod submodule;
|
mod submodule;
|
||||||
|
@ -130,4 +136,10 @@ fn f() {}
|
||||||
"#,
|
"#,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn available_before_curly() {
|
||||||
|
mark::check!(available_before_curly);
|
||||||
|
check_assist_not_applicable(move_module_to_file, r#"mod m { <|> }"#);
|
||||||
|
}
|
||||||
}
|
}
|
|
@ -116,7 +116,6 @@ mod handlers {
|
||||||
mod convert_integer_literal;
|
mod convert_integer_literal;
|
||||||
mod early_return;
|
mod early_return;
|
||||||
mod expand_glob_import;
|
mod expand_glob_import;
|
||||||
mod extract_module_to_file;
|
|
||||||
mod extract_struct_from_enum_variant;
|
mod extract_struct_from_enum_variant;
|
||||||
mod extract_variable;
|
mod extract_variable;
|
||||||
mod fill_match_arms;
|
mod fill_match_arms;
|
||||||
|
@ -139,6 +138,7 @@ mod handlers {
|
||||||
mod merge_match_arms;
|
mod merge_match_arms;
|
||||||
mod move_bounds;
|
mod move_bounds;
|
||||||
mod move_guard;
|
mod move_guard;
|
||||||
|
mod move_module_to_file;
|
||||||
mod pull_assignment_up;
|
mod pull_assignment_up;
|
||||||
mod qualify_path;
|
mod qualify_path;
|
||||||
mod raw_string;
|
mod raw_string;
|
||||||
|
@ -169,7 +169,7 @@ mod handlers {
|
||||||
convert_integer_literal::convert_integer_literal,
|
convert_integer_literal::convert_integer_literal,
|
||||||
early_return::convert_to_guarded_return,
|
early_return::convert_to_guarded_return,
|
||||||
expand_glob_import::expand_glob_import,
|
expand_glob_import::expand_glob_import,
|
||||||
extract_module_to_file::extract_module_to_file,
|
move_module_to_file::move_module_to_file,
|
||||||
extract_struct_from_enum_variant::extract_struct_from_enum_variant,
|
extract_struct_from_enum_variant::extract_struct_from_enum_variant,
|
||||||
extract_variable::extract_variable,
|
extract_variable::extract_variable,
|
||||||
fill_match_arms::fill_match_arms,
|
fill_match_arms::fill_match_arms,
|
||||||
|
|
|
@ -237,21 +237,6 @@ fn qux(bar: Bar, baz: Baz) {}
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn doctest_extract_module_to_file() {
|
|
||||||
check_doc_test(
|
|
||||||
"extract_module_to_file",
|
|
||||||
r#####"
|
|
||||||
mod foo {<|>
|
|
||||||
fn t() {}
|
|
||||||
}
|
|
||||||
"#####,
|
|
||||||
r#####"
|
|
||||||
mod foo;
|
|
||||||
"#####,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn doctest_extract_struct_from_enum_variant() {
|
fn doctest_extract_struct_from_enum_variant() {
|
||||||
check_doc_test(
|
check_doc_test(
|
||||||
|
@ -760,6 +745,21 @@ fn handle(action: Action) {
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn doctest_move_module_to_file() {
|
||||||
|
check_doc_test(
|
||||||
|
"move_module_to_file",
|
||||||
|
r#####"
|
||||||
|
mod <|>foo {
|
||||||
|
fn t() {}
|
||||||
|
}
|
||||||
|
"#####,
|
||||||
|
r#####"
|
||||||
|
mod foo;
|
||||||
|
"#####,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn doctest_pull_assignment_up() {
|
fn doctest_pull_assignment_up() {
|
||||||
check_doc_test(
|
check_doc_test(
|
||||||
|
|
Loading…
Reference in a new issue