@@ -81,6 +81,7 @@ struct FunctionBuilder {
81
81
type_params : Option < ast:: TypeParamList > ,
82
82
params : ast:: ParamList ,
83
83
file : AssistFile ,
84
+ needs_pub : bool ,
84
85
}
85
86
86
87
impl FunctionBuilder {
@@ -90,24 +91,29 @@ impl FunctionBuilder {
90
91
ctx : & AssistCtx ,
91
92
call : & ast:: CallExpr ,
92
93
path : & ast:: Path ,
93
- generate_in : Option < hir:: InFile < hir:: ModuleSource > > ,
94
+ target_module : Option < hir:: InFile < hir:: ModuleSource > > ,
94
95
) -> Option < Self > {
96
+ let needs_pub = target_module. is_some ( ) ;
95
97
let mut file = AssistFile :: default ( ) ;
96
- let target = if let Some ( generate_in_module ) = generate_in {
97
- let ( in_file, target) = next_space_for_fn_in_module ( ctx. sema . db , generate_in_module ) ?;
98
+ let target = if let Some ( target_module ) = target_module {
99
+ let ( in_file, target) = next_space_for_fn_in_module ( ctx. sema . db , target_module ) ?;
98
100
file = in_file;
99
101
target
100
102
} else {
101
103
next_space_for_fn_after_call_site ( & call) ?
102
104
} ;
103
105
let fn_name = fn_name ( & path) ?;
104
106
let ( type_params, params) = fn_args ( ctx, & call) ?;
105
- Some ( Self { target, fn_name, type_params, params, file } )
107
+ Some ( Self { target, fn_name, type_params, params, file, needs_pub } )
106
108
}
109
+
107
110
fn render ( self ) -> Option < FunctionTemplate > {
108
111
let placeholder_expr = ast:: make:: expr_todo ( ) ;
109
112
let fn_body = ast:: make:: block_expr ( vec ! [ ] , Some ( placeholder_expr) ) ;
110
- let fn_def = ast:: make:: fn_def ( self . fn_name , self . type_params , self . params , fn_body) ;
113
+ let mut fn_def = ast:: make:: fn_def ( self . fn_name , self . type_params , self . params , fn_body) ;
114
+ if self . needs_pub {
115
+ fn_def = ast:: make:: add_pub_crate_modifier ( fn_def) ;
116
+ }
111
117
112
118
let ( fn_def, insert_offset) = match self . target {
113
119
GeneratedFunctionTarget :: BehindItem ( it) => {
@@ -116,15 +122,14 @@ impl FunctionBuilder {
116
122
( indented, it. text_range ( ) . end ( ) )
117
123
}
118
124
GeneratedFunctionTarget :: InEmptyItemList ( it) => {
119
- let with_leading_newline = ast:: make:: add_leading_newlines ( 1 , fn_def) ;
120
- let indent = IndentLevel :: from_node ( it. syntax ( ) ) . indented ( ) ;
121
- let mut indented = indent. increase_indent ( with_leading_newline) ;
122
- if !item_list_has_whitespace ( & it) {
123
- // In this case we want to make sure there's a newline between the closing
124
- // function brace and the closing module brace (so it doesn't end in `}}`).
125
- indented = ast:: make:: add_trailing_newlines ( 1 , indented) ;
126
- }
127
- ( indented, it. syntax ( ) . text_range ( ) . start ( ) + TextUnit :: from_usize ( 1 ) )
125
+ let indent_once = IndentLevel ( 1 ) ;
126
+ let indent = IndentLevel :: from_node ( it. syntax ( ) ) ;
127
+
128
+ let fn_def = ast:: make:: add_leading_newlines ( 1 , fn_def) ;
129
+ let fn_def = indent_once. increase_indent ( fn_def) ;
130
+ let fn_def = ast:: make:: add_trailing_newlines ( 1 , fn_def) ;
131
+ let fn_def = indent. increase_indent ( fn_def) ;
132
+ ( fn_def, it. syntax ( ) . text_range ( ) . start ( ) + TextUnit :: from_usize ( 1 ) )
128
133
}
129
134
} ;
130
135
@@ -140,11 +145,6 @@ impl FunctionBuilder {
140
145
}
141
146
}
142
147
143
- /// Returns true if the given ItemList contains whitespace.
144
- fn item_list_has_whitespace ( it : & ast:: ItemList ) -> bool {
145
- it. syntax ( ) . descendants_with_tokens ( ) . find ( |it| it. kind ( ) == SyntaxKind :: WHITESPACE ) . is_some ( )
146
- }
147
-
148
148
enum GeneratedFunctionTarget {
149
149
BehindItem ( SyntaxNode ) ,
150
150
InEmptyItemList ( ast:: ItemList ) ,
@@ -803,29 +803,7 @@ fn foo() {
803
803
" ,
804
804
r"
805
805
mod bar {
806
- fn my_fn() {
807
- <|>todo!()
808
- }
809
- }
810
-
811
- fn foo() {
812
- bar::my_fn()
813
- }
814
- " ,
815
- ) ;
816
- check_assist (
817
- add_function,
818
- r"
819
- mod bar {
820
- }
821
-
822
- fn foo() {
823
- bar::my_fn<|>()
824
- }
825
- " ,
826
- r"
827
- mod bar {
828
- fn my_fn() {
806
+ pub(crate) fn my_fn() {
829
807
<|>todo!()
830
808
}
831
809
}
@@ -854,7 +832,7 @@ fn foo() {
854
832
mod bar {
855
833
fn something_else() {}
856
834
857
- fn my_fn() {
835
+ pub(crate) fn my_fn() {
858
836
<|>todo!()
859
837
}
860
838
}
@@ -872,8 +850,7 @@ fn foo() {
872
850
add_function,
873
851
r"
874
852
mod bar {
875
- mod baz {
876
- }
853
+ mod baz {}
877
854
}
878
855
879
856
fn foo() {
@@ -883,7 +860,7 @@ fn foo() {
883
860
r"
884
861
mod bar {
885
862
mod baz {
886
- fn my_fn() {
863
+ pub(crate) fn my_fn() {
887
864
<|>todo!()
888
865
}
889
866
}
0 commit comments