1 use std::collections::{HashMap, HashSet};
3 use hir::{HasSource, ModuleSource};
5 assists::{AssistId, AssistKind},
7 defs::{Definition, NameClass, NameRefClass},
8 search::{FileReference, SearchScope},
12 algo::find_node_at_range,
15 edit::{AstNodeEdit, IndentLevel},
16 make, HasName, HasVisibility,
18 match_ast, ted, AstNode, SourceFile, SyntaxNode, TextRange,
21 use crate::{AssistContext, Assists};
23 use super::remove_unused_param::range_to_remove;
25 // Assist: extract_module
27 // Extracts a selected region as seperate module. All the references, visibility and imports are
31 // $0fn foo(name: i32) -> i32 {
35 // fn bar(name: i32) -> i32 {
42 // pub(crate) fn foo(name: i32) -> i32 {
47 // fn bar(name: i32) -> i32 {
51 pub(crate) fn extract_module(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
52 if ctx.has_empty_selection() {
56 let node = ctx.covering_element();
57 let node = match node {
58 syntax::NodeOrToken::Node(n) => n,
59 syntax::NodeOrToken::Token(t) => t.parent()?,
62 let mut curr_parent_module: Option<ast::Module> = None;
63 if let Some(mod_syn_opt) = node.ancestors().find(|it| ast::Module::can_cast(it.kind())) {
64 curr_parent_module = ast::Module::cast(mod_syn_opt);
67 let mut module = extract_target(&node, ctx.selection_trimmed())?;
68 if module.body_items.len() == 0 {
72 let old_item_indent = module.body_items[0].indent_level();
74 //This takes place in three steps:
76 //- Firstly, we will update the references(usages) e.g. converting a
77 // function call bar() to modname::bar(), and similarly for other items
79 //- Secondly, changing the visibility of each item inside the newly selected module
80 // i.e. making a fn a() {} to pub(crate) fn a() {}
82 //- Thirdly, resolving all the imports this includes removing paths from imports
83 // outside the module, shifting/cloning them inside new module, or shifting the imports, or making
84 // new import statemnts
86 //We are getting item usages and record_fields together, record_fields
87 //for change_visibility and usages for first point mentioned above in the process
88 let (usages_to_be_processed, record_fields) = module.get_usages_and_record_fields(ctx);
90 let import_paths_to_be_removed = module.resolve_imports(curr_parent_module, &ctx);
91 module.body_items = module.change_visibility(record_fields)?;
92 if module.body_items.len() == 0 {
97 AssistId("extract_module", AssistKind::RefactorExtract),
103 let mut body_items = Vec::new();
104 let new_item_indent = old_item_indent + 1;
105 for item in module.body_items {
106 let item = item.indent(IndentLevel(1));
107 let mut indented_item = String::new();
108 format_to!(indented_item, "{}{}", new_item_indent, item.to_string());
109 body_items.push(indented_item);
112 let body = body_items.join("\n\n");
114 let mut module_def = String::new();
116 format_to!(module_def, "mod {} {{\n{}\n{}}}", module.name, body, old_item_indent);
118 let mut usages_to_be_updated_for_curr_file = vec![];
119 for usages_to_be_updated_for_file in usages_to_be_processed {
120 if usages_to_be_updated_for_file.0 == ctx.file_id() {
121 usages_to_be_updated_for_curr_file = usages_to_be_updated_for_file.1;
124 builder.edit_file(usages_to_be_updated_for_file.0);
125 for usage_to_be_processed in usages_to_be_updated_for_file.1 {
126 builder.replace(usage_to_be_processed.0, usage_to_be_processed.1)
130 builder.edit_file(ctx.file_id());
131 for usage_to_be_processed in usages_to_be_updated_for_curr_file {
132 builder.replace(usage_to_be_processed.0, usage_to_be_processed.1)
135 for import_path_text_range in import_paths_to_be_removed {
136 builder.delete(import_path_text_range);
138 builder.replace(module.text_range, module_def)
145 text_range: TextRange,
147 body_items: Vec<ast::Item>,
150 fn extract_target(node: &SyntaxNode, selection_range: TextRange) -> Option<Module> {
151 let mut body_items: Vec<ast::Item> = node
153 .filter_map(|child| {
154 if let Some(item) = ast::Item::cast(child.clone()) {
155 if selection_range.contains_range(item.syntax().text_range()) {
164 if let Some(node_item) = ast::Item::cast(node.clone()) {
165 body_items.push(node_item);
168 Some(Module { text_range: selection_range, name: "modname".to_string(), body_items })
172 fn get_usages_and_record_fields(
175 ) -> (HashMap<FileId, Vec<(TextRange, String)>>, Vec<SyntaxNode>) {
176 let mut adt_fields = Vec::new();
177 let mut refs: HashMap<FileId, Vec<(TextRange, String)>> = HashMap::new();
179 //Here impl is not included as each item inside impl will be tied to the parent of
180 //implementing block(a struct, enum, etc), if the parent is in selected module, it will
181 //get updated by ADT section given below or if it is not, then we dont need to do any operation
182 self.body_items.clone().into_iter().for_each(|item| {
184 match (item.syntax()) {
186 if let Some( nod ) = ctx.sema.to_def(&it) {
187 let node_def = Definition::Adt(nod.into());
188 self.expand_and_group_usages_file_wise(ctx, node_def, &mut refs);
190 //Enum Fields are not allowed to explicitly specify pub, it is implied
192 ast::Adt::Struct(x) => {
193 if let Some(field_list) = x.field_list() {
195 ast::FieldList::RecordFieldList(record_field_list) => {
196 record_field_list.fields().for_each(|record_field| {
197 adt_fields.push(record_field.syntax().clone());
200 ast::FieldList::TupleFieldList(tuple_field_list) => {
201 tuple_field_list.fields().for_each(|tuple_field| {
202 adt_fields.push(tuple_field.syntax().clone());
208 ast::Adt::Union(x) => {
209 if let Some(record_field_list) = x.record_field_list() {
210 record_field_list.fields().for_each(|record_field| {
211 adt_fields.push(record_field.syntax().clone());
215 ast::Adt::Enum(_) => {},
219 ast::TypeAlias(it) => {
220 if let Some( nod ) = ctx.sema.to_def(&it) {
221 let node_def = Definition::TypeAlias(nod.into());
222 self.expand_and_group_usages_file_wise(ctx, node_def, &mut refs);
226 if let Some( nod ) = ctx.sema.to_def(&it) {
227 let node_def = Definition::Const(nod.into());
228 self.expand_and_group_usages_file_wise(ctx, node_def, &mut refs);
232 if let Some( nod ) = ctx.sema.to_def(&it) {
233 let node_def = Definition::Static(nod.into());
234 self.expand_and_group_usages_file_wise(ctx, node_def, &mut refs);
238 if let Some( nod ) = ctx.sema.to_def(&it) {
239 let node_def = Definition::Function(nod.into());
240 self.expand_and_group_usages_file_wise(ctx, node_def, &mut refs);
248 return (refs, adt_fields);
251 fn expand_and_group_usages_file_wise(
254 node_def: Definition,
255 refs: &mut HashMap<FileId, Vec<(TextRange, String)>>,
257 for (file_id, references) in node_def.usages(&ctx.sema).all() {
258 if let Some(file_refs) = refs.get_mut(&file_id) {
259 let mut usages = self.expand_ref_to_usages(references, ctx, file_id);
260 file_refs.append(&mut usages);
262 refs.insert(file_id, self.expand_ref_to_usages(references, ctx, file_id));
267 fn expand_ref_to_usages(
269 refs: Vec<FileReference>,
272 ) -> Vec<(TextRange, String)> {
273 let source_file = ctx.sema.parse(file_id);
275 let mut usages_to_be_processed_for_file = Vec::new();
277 if let Some(x) = self.get_usage_to_be_processed(&source_file, usage) {
278 usages_to_be_processed_for_file.push(x);
282 usages_to_be_processed_for_file
285 fn get_usage_to_be_processed(
287 source_file: &SourceFile,
288 FileReference { range, name, .. }: FileReference,
289 ) -> Option<(TextRange, String)> {
290 let path: Option<ast::Path> = find_node_at_range(source_file.syntax(), range);
294 for desc in path.syntax().descendants() {
295 if desc.to_string() == name.syntax().to_string()
296 && !self.text_range.contains_range(desc.text_range())
298 if let Some(name_ref) = ast::NameRef::cast(desc) {
300 name_ref.syntax().text_range(),
301 format!("{}::{}", self.name, name_ref.to_string()),
310 fn change_visibility(&self, record_fields: Vec<SyntaxNode>) -> Option<Vec<ast::Item>> {
311 let (body_items, mut replacements, record_field_parents, impls) =
312 get_replacements_for_visibilty_change(self.body_items.clone(), false);
314 let impl_items = impls.into_iter().fold(Vec::new(), |mut impl_items, x| {
315 let this_impl_items =
316 x.syntax().descendants().fold(Vec::new(), |mut this_impl_items, x| {
317 if let Some(item) = ast::Item::cast(x.clone()) {
318 this_impl_items.push(item);
320 return this_impl_items;
323 impl_items.append(&mut this_impl_items.clone());
327 let (_, mut impl_item_replacements, _, _) =
328 get_replacements_for_visibilty_change(impl_items.clone(), true);
330 replacements.append(&mut impl_item_replacements);
332 record_field_parents.into_iter().for_each(|x| {
333 x.1.descendants().filter_map(|x| ast::RecordField::cast(x)).for_each(|desc| {
334 let is_record_field_present = record_fields
337 .find(|x| x.to_string() == desc.to_string())
339 if is_record_field_present {
340 replacements.push((desc.visibility().clone(), desc.syntax().clone()));
345 replacements.into_iter().for_each(|(vis, syntax)| {
346 add_change_vis(vis, syntax.first_child_or_token());
354 curr_parent_module: Option<ast::Module>,
356 ) -> Vec<TextRange> {
357 let mut import_paths_to_be_removed: Vec<TextRange> = vec![];
358 let mut node_set: HashSet<String> = HashSet::new();
360 self.body_items.clone().into_iter().for_each(|item| {
361 item.syntax().descendants().for_each(|x| {
362 if let Some(name) = ast::Name::cast(x.clone()) {
363 if let Some(name_classify) = NameClass::classify(&ctx.sema, &name) {
364 //Necessary to avoid two same names going through
365 if !node_set.contains(&name.syntax().to_string()) {
366 node_set.insert(name.syntax().to_string());
367 let def_opt: Option<Definition> = match name_classify {
368 NameClass::Definition(def) => Some(def),
372 if let Some(def) = def_opt {
373 if let Some(import_path) = self
374 .process_names_and_namerefs_for_import_resolve(
381 import_paths_to_be_removed.push(import_path);
388 if let Some(name_ref) = ast::NameRef::cast(x) {
389 if let Some(name_classify) = NameRefClass::classify(&ctx.sema, &name_ref) {
390 //Necessary to avoid two same names going through
391 if !node_set.contains(&name_ref.syntax().to_string()) {
392 node_set.insert(name_ref.syntax().to_string());
393 let def_opt: Option<Definition> = match name_classify {
394 NameRefClass::Definition(def) => Some(def),
398 if let Some(def) = def_opt {
399 if let Some(import_path) = self
400 .process_names_and_namerefs_for_import_resolve(
407 import_paths_to_be_removed.push(import_path);
416 import_paths_to_be_removed
419 fn process_names_and_namerefs_for_import_resolve(
422 node_syntax: &SyntaxNode,
423 curr_parent_module: &Option<ast::Module>,
425 ) -> Option<TextRange> {
426 //We only need to find in the current file
427 let selection_range = ctx.selection_trimmed();
428 let curr_file_id = ctx.file_id();
429 let search_scope = SearchScope::single_file(curr_file_id);
430 let usage_res = def.usages(&ctx.sema).in_scope(search_scope).all();
431 let file = ctx.sema.parse(curr_file_id);
433 let mut exists_inside_sel = false;
434 let mut exists_outside_sel = false;
435 usage_res.clone().into_iter().for_each(|x| {
436 let mut non_use_nodes_itr = (&x.1).into_iter().filter_map(|x| {
437 if find_node_at_range::<ast::Use>(file.syntax(), x.range).is_none() {
438 let path_opt = find_node_at_range::<ast::Path>(file.syntax(), x.range);
447 .find(|x| !selection_range.contains_range(x.syntax().text_range()))
450 exists_outside_sel = true;
453 .find(|x| selection_range.contains_range(x.syntax().text_range()))
456 exists_inside_sel = true;
460 let source_exists_outside_sel_in_same_mod = does_source_exists_outside_sel_in_same_mod(
468 let use_stmt_opt: Option<ast::Use> = usage_res.into_iter().find_map(|x| {
470 let mut use_opt: Option<ast::Use> = None;
471 if file_id == curr_file_id {
472 (&x.1).into_iter().for_each(|x| {
473 let node_opt: Option<ast::Use> = find_node_at_range(file.syntax(), x.range);
474 if let Some(node) = node_opt {
475 use_opt = Some(node.clone());
482 let mut use_tree_str_opt: Option<Vec<ast::Path>> = None;
483 //Exists inside and outside selection
484 // - Use stmt for item is present -> get the use_tree_str and reconstruct the path in new
486 // - Use stmt for item is not present ->
487 //If it is not found, the definition is either ported inside new module or it stays
489 //- Def is inside: Nothing to import
490 //- Def is outside: Import it inside with super
492 //Exists inside selection but not outside -> Check for the import of it in original module,
493 //get the use_tree_str, reconstruct the use stmt in new module
495 let mut import_path_to_be_removed: Option<TextRange> = None;
496 if exists_inside_sel && exists_outside_sel {
497 //Changes to be made only inside new module
499 //If use_stmt exists, find the use_tree_str, reconstruct it inside new module
500 //If not, insert a use stmt with super and the given nameref
501 if let Some((use_tree_str, _)) =
502 self.process_use_stmt_for_import_resolve(use_stmt_opt, node_syntax)
504 use_tree_str_opt = Some(use_tree_str);
505 } else if source_exists_outside_sel_in_same_mod {
506 //Considered only after use_stmt is not present
507 //source_exists_outside_sel_in_same_mod | exists_outside_sel(exists_inside_sel =
508 //true for all cases)
509 // false | false -> Do nothing
510 // false | true -> If source is in selection -> nothing to do, If source is outside
511 // mod -> ust_stmt transversal
512 // true | false -> super import insertion
513 // true | true -> super import insertion
514 self.make_use_stmt_of_node_with_super(node_syntax);
516 } else if exists_inside_sel && !exists_outside_sel {
517 //Changes to be made inside new module, and remove import from outside
519 if let Some((use_tree_str, text_range_opt)) =
520 self.process_use_stmt_for_import_resolve(use_stmt_opt, node_syntax)
522 if let Some(text_range) = text_range_opt {
523 import_path_to_be_removed = Some(text_range);
525 use_tree_str_opt = Some(use_tree_str);
526 } else if source_exists_outside_sel_in_same_mod {
527 self.make_use_stmt_of_node_with_super(node_syntax);
531 if let Some(use_tree_str) = use_tree_str_opt {
532 let mut use_tree_str = use_tree_str.clone();
533 use_tree_str.reverse();
534 if use_tree_str[0].to_string().contains("super") {
535 let super_path = make::ext::ident_path("super");
536 use_tree_str.insert(0, super_path)
540 make::use_(None, make::use_tree(make::join_paths(use_tree_str), None, None, false));
541 if let Some(item) = ast::Item::cast(use_.syntax().clone()) {
542 self.body_items.insert(0, item);
546 import_path_to_be_removed
549 fn make_use_stmt_of_node_with_super(&mut self, node_syntax: &SyntaxNode) {
550 let super_path = make::ext::ident_path("super");
551 let node_path = make::ext::ident_path(&node_syntax.to_string());
552 let use_ = make::use_(
554 make::use_tree(make::join_paths(vec![super_path, node_path]), None, None, false),
556 if let Some(item) = ast::Item::cast(use_.syntax().clone()) {
557 self.body_items.insert(0, item);
561 fn process_use_stmt_for_import_resolve(
563 use_stmt_opt: Option<ast::Use>,
564 node_syntax: &SyntaxNode,
565 ) -> Option<(Vec<ast::Path>, Option<TextRange>)> {
566 if let Some(use_stmt) = use_stmt_opt {
567 for desc in use_stmt.syntax().descendants() {
568 if let Some(path_seg) = ast::PathSegment::cast(desc) {
569 if path_seg.syntax().to_string() == node_syntax.to_string() {
570 let mut use_tree_str = vec![path_seg.parent_path()];
571 get_use_tree_paths_from_path(path_seg.parent_path(), &mut use_tree_str);
572 for ancs in path_seg.syntax().ancestors() {
573 //Here we are looking for use_tree with same string value as node
574 //passed above as the range_to_remove function looks for a comma and
575 //then includes it in the text range to remove it. But the comma only
576 //appears at the use_tree level
577 if let Some(use_tree) = ast::UseTree::cast(ancs) {
578 if use_tree.syntax().to_string() == node_syntax.to_string() {
581 Some(range_to_remove(use_tree.syntax())),
587 return Some((use_tree_str, None));
597 fn does_source_exists_outside_sel_in_same_mod(
600 curr_parent_module: &Option<ast::Module>,
601 selection_range: TextRange,
602 curr_file_id: FileId,
604 let mut source_exists_outside_sel_in_same_mod = false;
606 Definition::Module(x) => {
607 let source = x.definition_source(ctx.db());
608 let have_same_parent;
609 if let Some(ast_module) = &curr_parent_module {
610 if let Some(hir_module) = x.parent(ctx.db()) {
612 compare_hir_and_ast_module(&ast_module, hir_module, ctx).is_some();
614 let source_file_id = source.file_id.original_file(ctx.db());
615 have_same_parent = source_file_id == curr_file_id;
618 let source_file_id = source.file_id.original_file(ctx.db());
619 have_same_parent = source_file_id == curr_file_id;
622 if have_same_parent {
624 ModuleSource::Module(module_) => {
625 source_exists_outside_sel_in_same_mod =
626 !selection_range.contains_range(module_.syntax().text_range());
632 Definition::Function(x) => {
633 if let Some(source) = x.source(ctx.db()) {
634 let have_same_parent;
635 if let Some(ast_module) = &curr_parent_module {
637 compare_hir_and_ast_module(&ast_module, x.module(ctx.db()), ctx).is_some();
639 let source_file_id = source.file_id.original_file(ctx.db());
640 have_same_parent = source_file_id == curr_file_id;
643 if have_same_parent {
644 source_exists_outside_sel_in_same_mod =
645 !selection_range.contains_range(source.value.syntax().text_range());
649 Definition::Adt(x) => {
650 if let Some(source) = x.source(ctx.db()) {
651 let have_same_parent;
652 if let Some(ast_module) = &curr_parent_module {
654 compare_hir_and_ast_module(&ast_module, x.module(ctx.db()), ctx).is_some();
656 let source_file_id = source.file_id.original_file(ctx.db());
657 have_same_parent = source_file_id == curr_file_id;
660 if have_same_parent {
661 source_exists_outside_sel_in_same_mod =
662 !selection_range.contains_range(source.value.syntax().text_range());
666 Definition::Variant(x) => {
667 if let Some(source) = x.source(ctx.db()) {
668 let have_same_parent;
669 if let Some(ast_module) = &curr_parent_module {
671 compare_hir_and_ast_module(&ast_module, x.module(ctx.db()), ctx).is_some();
673 let source_file_id = source.file_id.original_file(ctx.db());
674 have_same_parent = source_file_id == curr_file_id;
677 if have_same_parent {
678 source_exists_outside_sel_in_same_mod =
679 !selection_range.contains_range(source.value.syntax().text_range());
683 Definition::Const(x) => {
684 if let Some(source) = x.source(ctx.db()) {
685 let have_same_parent;
686 if let Some(ast_module) = &curr_parent_module {
688 compare_hir_and_ast_module(&ast_module, x.module(ctx.db()), ctx).is_some();
690 let source_file_id = source.file_id.original_file(ctx.db());
691 have_same_parent = source_file_id == curr_file_id;
694 if have_same_parent {
695 source_exists_outside_sel_in_same_mod =
696 !selection_range.contains_range(source.value.syntax().text_range());
700 Definition::Static(x) => {
701 if let Some(source) = x.source(ctx.db()) {
702 let have_same_parent;
703 if let Some(ast_module) = &curr_parent_module {
705 compare_hir_and_ast_module(&ast_module, x.module(ctx.db()), ctx).is_some();
707 let source_file_id = source.file_id.original_file(ctx.db());
708 have_same_parent = source_file_id == curr_file_id;
711 if have_same_parent {
712 source_exists_outside_sel_in_same_mod =
713 !selection_range.contains_range(source.value.syntax().text_range());
717 Definition::Trait(x) => {
718 if let Some(source) = x.source(ctx.db()) {
719 let have_same_parent;
720 if let Some(ast_module) = &curr_parent_module {
722 compare_hir_and_ast_module(&ast_module, x.module(ctx.db()), ctx).is_some();
724 let source_file_id = source.file_id.original_file(ctx.db());
725 have_same_parent = source_file_id == curr_file_id;
728 if have_same_parent {
729 source_exists_outside_sel_in_same_mod =
730 !selection_range.contains_range(source.value.syntax().text_range());
734 Definition::TypeAlias(x) => {
735 if let Some(source) = x.source(ctx.db()) {
736 let have_same_parent;
737 if let Some(ast_module) = &curr_parent_module {
739 compare_hir_and_ast_module(&ast_module, x.module(ctx.db()), ctx).is_some();
741 let source_file_id = source.file_id.original_file(ctx.db());
742 have_same_parent = source_file_id == curr_file_id;
745 if have_same_parent {
746 source_exists_outside_sel_in_same_mod =
747 !selection_range.contains_range(source.value.syntax().text_range());
754 return source_exists_outside_sel_in_same_mod;
757 fn get_replacements_for_visibilty_change(
758 items: Vec<ast::Item>,
759 is_clone_for_updated: bool,
762 Vec<(Option<ast::Visibility>, SyntaxNode)>,
763 Vec<(Option<ast::Visibility>, SyntaxNode)>,
766 let mut replacements = Vec::new();
767 let mut record_field_parents = Vec::new();
768 let mut impls = Vec::new();
769 let mut body_items = Vec::new();
771 items.into_iter().for_each(|item| {
773 if !is_clone_for_updated {
774 item = item.clone_for_update();
776 body_items.push(item.clone());
777 //Use stmts are ignored
779 ast::Item::Const(it) => {
780 replacements.push((it.visibility().clone(), it.syntax().clone()))
782 ast::Item::Enum(it) => {
783 replacements.push((it.visibility().clone(), it.syntax().clone()))
785 ast::Item::ExternCrate(it) => {
786 replacements.push((it.visibility().clone(), it.syntax().clone()))
788 ast::Item::Fn(it) => replacements.push((it.visibility().clone(), it.syntax().clone())),
789 ast::Item::Impl(it) => impls.push(it),
790 ast::Item::MacroRules(it) => {
791 replacements.push((it.visibility().clone(), it.syntax().clone()))
793 ast::Item::MacroDef(it) => {
794 replacements.push((it.visibility().clone(), it.syntax().clone()))
796 ast::Item::Module(it) => {
797 replacements.push((it.visibility().clone(), it.syntax().clone()))
799 ast::Item::Static(it) => {
800 replacements.push((it.visibility().clone(), it.syntax().clone()))
802 ast::Item::Struct(it) => {
803 replacements.push((it.visibility().clone(), it.syntax().clone()));
804 record_field_parents.push((it.visibility().clone(), it.syntax().clone()));
806 ast::Item::Trait(it) => {
807 replacements.push((it.visibility().clone(), it.syntax().clone()))
809 ast::Item::TypeAlias(it) => {
810 replacements.push((it.visibility().clone(), it.syntax().clone()))
812 ast::Item::Union(it) => {
813 replacements.push((it.visibility().clone(), it.syntax().clone()));
814 record_field_parents.push((it.visibility().clone(), it.syntax().clone()));
820 return (body_items, replacements, record_field_parents, impls);
823 fn get_use_tree_paths_from_path(
825 use_tree_str: &mut Vec<ast::Path>,
826 ) -> Option<&mut Vec<ast::Path>> {
827 path.syntax().ancestors().filter(|x| x.to_string() != path.to_string()).find_map(|x| {
828 if let Some(use_tree) = ast::UseTree::cast(x.clone()) {
829 if let Some(upper_tree_path) = use_tree.path() {
830 if upper_tree_path.to_string() != path.to_string() {
831 use_tree_str.push(upper_tree_path.clone());
832 get_use_tree_paths_from_path(upper_tree_path, use_tree_str);
833 return Some(use_tree);
844 vis: Option<ast::Visibility>,
845 node_or_token_opt: Option<syntax::SyntaxElement>,
847 if let Some(vis) = vis {
848 if vis.syntax().text() == "pub" {
849 ted::replace(vis.syntax(), make::visibility_pub_crate().syntax().clone_for_update());
852 if let Some(node_or_token) = node_or_token_opt {
853 let pub_crate_vis = make::visibility_pub_crate().clone_for_update();
854 if let Some(node) = node_or_token.as_node() {
855 ted::insert(ted::Position::before(node), pub_crate_vis.syntax());
857 if let Some(token) = node_or_token.as_token() {
858 ted::insert(ted::Position::before(token), pub_crate_vis.syntax());
866 fn compare_hir_and_ast_module(
867 ast_module: &ast::Module,
868 hir_module: hir::Module,
871 let hir_mod_name = hir_module.name(ctx.db())?;
872 let ast_mod_name = ast_module.name()?;
873 if hir_mod_name.to_string() != ast_mod_name.to_string() {
882 use crate::tests::{check_assist, check_assist_not_applicable};
887 fn test_not_applicable_without_selection() {
888 check_assist_not_applicable(
891 $0pub struct PublicStruct {
899 fn test_extract_module() {
903 mod thirdpartycrate {
906 pub struct SomeType2;
908 pub struct SomeType1;
912 use crate::thirdpartycrate::{nest::{SomeType, SomeType2}, SomeType1};
914 pub struct PublicStruct {
915 field: PrivateStruct,
920 pub fn new() -> Self {
921 Self { field: PrivateStruct::new(), field1: SomeType1 }
926 let _s = PrivateStruct::new();
930 $0struct PrivateStruct {
934 pub struct PrivateStruct1 {
940 PrivateStruct { inner: SomeType }
950 mod thirdpartycrate {
953 pub struct SomeType2;
955 pub struct SomeType1;
959 use crate::thirdpartycrate::{nest::{SomeType2}, SomeType1};
961 pub struct PublicStruct {
962 field: modname::PrivateStruct,
967 pub fn new() -> Self {
968 Self { field: modname::PrivateStruct::new(), field1: SomeType1 }
973 let _s = modname::PrivateStruct::new();
974 let _a = modname::bar();
978 use crate::thirdpartycrate::nest::SomeType;
980 pub(crate) struct PrivateStruct {
981 pub(crate) inner: SomeType,
984 pub(crate) struct PrivateStruct1 {
985 pub(crate) inner: i32,
989 pub(crate) fn new() -> Self {
990 PrivateStruct { inner: SomeType }
994 pub(crate) fn bar() -> i32 {
1004 fn test_extract_module_for_function_only() {
1008 $0fn foo(name: i32) -> i32 {
1012 fn bar(name: i32) -> i32 {
1018 pub(crate) fn foo(name: i32) -> i32 {
1023 fn bar(name: i32) -> i32 {
1031 fn test_extract_module_for_impl_having_corresponding_adt_in_selection() {
1039 pub fn new_a() -> i32 {
1045 let _a = A::new_a();
1052 pub(crate) struct A {}
1055 pub(crate) fn new_a() -> i32 {
1062 let _a = modname::A::new_a();
1070 fn test_import_resolve_when_its_only_inside_selection() {
1075 pub struct PrivateStruct;
1076 pub struct PrivateStruct1;
1080 use super::foo::{PrivateStruct, PrivateStruct1};
1083 field: PrivateStruct,
1087 field: PrivateStruct1,
1093 pub struct PrivateStruct;
1094 pub struct PrivateStruct1;
1098 use super::foo::{PrivateStruct1};
1101 use super::super::foo::PrivateStruct;
1103 pub(crate) struct Strukt {
1104 pub(crate) field: PrivateStruct,
1109 field: PrivateStruct1,
1117 fn test_import_resolve_when_its_inside_and_outside_selection_and_source_not_in_same_mod() {
1122 pub struct PrivateStruct;
1126 use super::foo::PrivateStruct;
1129 field: PrivateStruct,
1133 field: PrivateStruct,
1139 pub struct PrivateStruct;
1143 use super::foo::PrivateStruct;
1146 use super::super::foo::PrivateStruct;
1148 pub(crate) struct Strukt {
1149 pub(crate) field: PrivateStruct,
1154 field: PrivateStruct,
1162 fn test_import_resolve_when_its_inside_and_outside_selection_and_source_is_in_same_mod() {
1167 pub struct PrivateStruct;
1170 field: PrivateStruct,
1174 field: PrivateStruct,
1180 pub struct PrivateStruct;
1183 use super::PrivateStruct;
1185 pub(crate) struct Strukt {
1186 pub(crate) field: PrivateStruct,
1191 field: PrivateStruct,
1199 fn test_extract_module_for_correspoding_adt_of_impl_present_in_same_mod_but_not_in_selection() {
1207 pub fn new_a() -> i32 {
1213 let _a = A::new_a();
1225 pub(crate) fn new_a() -> i32 {
1232 let _a = A::new_a();
1240 fn test_extract_module_for_impl_not_having_corresponding_adt_in_selection_and_not_in_same_mod_but_with_super(
1252 pub fn new_a() -> i32 {
1258 let _a = A::new_a();
1270 use super::super::foo::A;
1273 pub(crate) fn new_a() -> i32 {
1280 let _a = A::new_a();
1288 fn test_import_resolve_for_trait_bounds_on_function() {
1297 fn foo<T: JustATrait>(arg: T) -> T {
1301 impl JustATrait for A {}
1314 use super::JustATrait;
1316 pub(crate) struct A {}
1318 pub(crate) fn foo<T: JustATrait>(arg: T) -> T {
1322 impl JustATrait for A {}
1324 pub(crate) fn bar() {
1335 fn test_extract_module_for_module() {
1348 pub(crate) mod impl_play {
1358 fn test_extract_module_with_multiple_files() {
1365 use foo::PrivateStruct;
1368 field: PrivateStruct,
1377 pub struct PrivateStruct;
1382 use foo::PrivateStruct;
1385 field: PrivateStruct,
1392 pub(crate) struct Strukt1 {
1393 pub(crate) field: Strukt,