@@ -81,38 +81,34 @@ pub(crate) fn extract_module(acc: &mut Assists, ctx: &AssistContext) -> Option<(
81
81
}
82
82
83
83
let mut module = extract_target ( & node, ctx. selection_trimmed ( ) ) ?;
84
- if module. body_items . len ( ) == 0 {
84
+ if module. body_items . is_empty ( ) {
85
85
return None ;
86
86
}
87
87
88
88
let old_item_indent = module. body_items [ 0 ] . indent_level ( ) ;
89
89
90
- //This takes place in three steps:
91
- //
92
- //- Firstly, we will update the references(usages) e.g. converting a
93
- // function call bar() to modname::bar(), and similarly for other items
94
- //
95
- //- Secondly, changing the visibility of each item inside the newly selected module
96
- // i.e. making a fn a() {} to pub(crate) fn a() {}
97
- //
98
- //- Thirdly, resolving all the imports this includes removing paths from imports
99
- // outside the module, shifting/cloning them inside new module, or shifting the imports, or making
100
- // new import statemnts
101
-
102
- //We are getting item usages and record_fields together, record_fields
103
- //for change_visibility and usages for first point mentioned above in the process
104
- let ( usages_to_be_processed, record_fields) = module. get_usages_and_record_fields ( ctx) ;
105
-
106
- let import_paths_to_be_removed = module. resolve_imports ( curr_parent_module, ctx) ;
107
- if module. body_items . len ( ) == 0 {
108
- return None ;
109
- }
110
-
111
90
acc. add (
112
91
AssistId ( "extract_module" , AssistKind :: RefactorExtract ) ,
113
92
"Extract Module" ,
114
93
module. text_range ,
115
94
|builder| {
95
+ //This takes place in three steps:
96
+ //
97
+ //- Firstly, we will update the references(usages) e.g. converting a
98
+ // function call bar() to modname::bar(), and similarly for other items
99
+ //
100
+ //- Secondly, changing the visibility of each item inside the newly selected module
101
+ // i.e. making a fn a() {} to pub(crate) fn a() {}
102
+ //
103
+ //- Thirdly, resolving all the imports this includes removing paths from imports
104
+ // outside the module, shifting/cloning them inside new module, or shifting the imports, or making
105
+ // new import statemnts
106
+
107
+ //We are getting item usages and record_fields together, record_fields
108
+ //for change_visibility and usages for first point mentioned above in the process
109
+ let ( usages_to_be_processed, record_fields) = module. get_usages_and_record_fields ( ctx) ;
110
+
111
+ let import_paths_to_be_removed = module. resolve_imports ( curr_parent_module, ctx) ;
116
112
module. change_visibility ( record_fields) ;
117
113
118
114
let mut body_items: Vec < String > = Vec :: new ( ) ;
@@ -221,19 +217,13 @@ fn extract_target(node: &SyntaxNode, selection_range: TextRange) -> Option<Modul
221
217
222
218
let mut body_items: Vec < ast:: Item > = node
223
219
. children ( )
224
- . filter_map ( |child| {
225
- if selection_range. contains_range ( child. text_range ( ) ) {
226
- let child_kind = child. kind ( ) ;
227
- if let Some ( item) = ast:: Item :: cast ( child) {
228
- if ast:: Use :: can_cast ( child_kind) {
229
- use_items. push ( item) ;
230
- } else {
231
- return Some ( item) ;
232
- }
233
- }
234
- return None ;
220
+ . filter ( |child| selection_range. contains_range ( child. text_range ( ) ) )
221
+ . filter_map ( |child| match ast:: Item :: cast ( child) {
222
+ Some ( it @ ast:: Item :: Use ( _) ) => {
223
+ use_items. push ( it) ;
224
+ None
235
225
}
236
- None
226
+ item => item ,
237
227
} )
238
228
. collect ( ) ;
239
229
@@ -368,9 +358,7 @@ impl Module {
368
358
source_file : & SourceFile ,
369
359
FileReference { range, name, .. } : FileReference ,
370
360
) -> Option < ( TextRange , String ) > {
371
- let path: Option < ast:: Path > = find_node_at_range ( source_file. syntax ( ) , range) ;
372
-
373
- let path = path?;
361
+ let path: ast:: Path = find_node_at_range ( source_file. syntax ( ) , range) ?;
374
362
375
363
for desc in path. syntax ( ) . descendants ( ) {
376
364
if desc. to_string ( ) == name. syntax ( ) . to_string ( )
@@ -609,9 +597,8 @@ impl Module {
609
597
610
598
let use_ =
611
599
make:: use_ ( None , make:: use_tree ( make:: join_paths ( use_tree_str) , None , None , false ) ) ;
612
- if let Some ( item) = ast:: Item :: cast ( use_. syntax ( ) . clone ( ) ) {
613
- self . use_items . insert ( 0 , item) ;
614
- }
600
+ let item = ast:: Item :: from ( use_) ;
601
+ self . use_items . insert ( 0 , item) ;
615
602
}
616
603
617
604
import_path_to_be_removed
@@ -825,7 +812,6 @@ fn get_replacements_for_visibilty_change(
825
812
let mut impls = Vec :: new ( ) ;
826
813
827
814
items. into_iter ( ) . for_each ( |item| {
828
- let item = item;
829
815
if !is_clone_for_updated {
830
816
* item = item. clone_for_update ( ) ;
831
817
}
0 commit comments