mirror of
https://github.com/rust-lang/rust-analyzer
synced 2024-12-27 05:23:24 +00:00
feat: Adding extract_module assist
This commit is contained in:
parent
6c7526d308
commit
32b95ea310
5 changed files with 1447 additions and 1 deletions
1409
crates/ide_assists/src/handlers/extract_module.rs
Normal file
1409
crates/ide_assists/src/handlers/extract_module.rs
Normal file
File diff suppressed because it is too large
Load diff
|
@ -140,7 +140,7 @@ fn process_usage(
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
|
|
||||||
fn range_to_remove(node: &SyntaxNode) -> TextRange {
|
pub(crate) fn range_to_remove(node: &SyntaxNode) -> TextRange {
|
||||||
let up_to_comma = next_prev().find_map(|dir| {
|
let up_to_comma = next_prev().find_map(|dir| {
|
||||||
node.siblings_with_tokens(dir)
|
node.siblings_with_tokens(dir)
|
||||||
.filter_map(|it| it.into_token())
|
.filter_map(|it| it.into_token())
|
||||||
|
|
|
@ -122,6 +122,7 @@ mod handlers {
|
||||||
mod destructure_tuple_binding;
|
mod destructure_tuple_binding;
|
||||||
mod expand_glob_import;
|
mod expand_glob_import;
|
||||||
mod extract_function;
|
mod extract_function;
|
||||||
|
mod extract_module;
|
||||||
mod extract_struct_from_enum_variant;
|
mod extract_struct_from_enum_variant;
|
||||||
mod extract_type_alias;
|
mod extract_type_alias;
|
||||||
mod extract_variable;
|
mod extract_variable;
|
||||||
|
@ -273,6 +274,7 @@ mod handlers {
|
||||||
//
|
//
|
||||||
extract_variable::extract_variable,
|
extract_variable::extract_variable,
|
||||||
extract_function::extract_function,
|
extract_function::extract_function,
|
||||||
|
extract_module::extract_module,
|
||||||
//
|
//
|
||||||
generate_getter::generate_getter,
|
generate_getter::generate_getter,
|
||||||
generate_getter::generate_getter_mut,
|
generate_getter::generate_getter_mut,
|
||||||
|
|
|
@ -526,6 +526,35 @@ fn $0fun_name(n: i32) {
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn doctest_extract_module() {
|
||||||
|
check_doc_test(
|
||||||
|
"extract_module",
|
||||||
|
r#####"
|
||||||
|
$0
|
||||||
|
fn foo(name: i32) -> i32 {
|
||||||
|
name + 1
|
||||||
|
}
|
||||||
|
$0
|
||||||
|
|
||||||
|
fn bar(name: i32) -> i32 {
|
||||||
|
name + 2
|
||||||
|
}
|
||||||
|
"#####,
|
||||||
|
r#####"
|
||||||
|
mod modname {
|
||||||
|
pub(crate) fn foo(name: i32) -> i32 {
|
||||||
|
name + 1
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn bar(name: i32) -> i32 {
|
||||||
|
name + 2
|
||||||
|
}
|
||||||
|
"#####,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn doctest_extract_struct_from_enum_variant() {
|
fn doctest_extract_struct_from_enum_variant() {
|
||||||
check_doc_test(
|
check_doc_test(
|
||||||
|
|
|
@ -196,6 +196,12 @@ pub fn path_from_segments(
|
||||||
format!("use {};", segments)
|
format!("use {};", segments)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn join_paths(paths: impl IntoIterator<Item = ast::Path>) -> ast::Path {
|
||||||
|
let paths = paths.into_iter().map(|it| it.syntax().clone()).join("::");
|
||||||
|
ast_from_text(&format!("use {};", paths))
|
||||||
|
}
|
||||||
|
|
||||||
// FIXME: should not be pub
|
// FIXME: should not be pub
|
||||||
pub fn path_from_text(text: &str) -> ast::Path {
|
pub fn path_from_text(text: &str) -> ast::Path {
|
||||||
ast_from_text(&format!("fn main() {{ let test = {}; }}", text))
|
ast_from_text(&format!("fn main() {{ let test = {}; }}", text))
|
||||||
|
|
Loading…
Reference in a new issue