@@ -11,7 +11,12 @@ use crate::{
1111} ;
1212
1313/// Helper function to get path to `ModuleDef`
14- fn mod_item_path ( sema_scope : & SemanticsScope < ' _ > , def : & ModuleDef ) -> Option < ModPath > {
14+ fn mod_item_path (
15+ sema_scope : & SemanticsScope < ' _ > ,
16+ def : & ModuleDef ,
17+ prefer_no_std : bool ,
18+ prefer_prelude : bool ,
19+ ) -> Option < ModPath > {
1520 let db = sema_scope. db ;
1621 // Account for locals shadowing items from module
1722 let name_hit_count = def. name ( db) . map ( |def_name| {
@@ -26,25 +31,43 @@ fn mod_item_path(sema_scope: &SemanticsScope<'_>, def: &ModuleDef) -> Option<Mod
2631
2732 let m = sema_scope. module ( ) ;
2833 match name_hit_count {
29- Some ( 0 ..=1 ) | None => m. find_use_path ( db. upcast ( ) , * def, false , true ) ,
30- Some ( _) => m. find_use_path_prefixed ( db. upcast ( ) , * def, PrefixKind :: ByCrate , false , true ) ,
34+ Some ( 0 ..=1 ) | None => m. find_use_path ( db. upcast ( ) , * def, prefer_no_std, prefer_prelude) ,
35+ Some ( _) => m. find_use_path_prefixed (
36+ db. upcast ( ) ,
37+ * def,
38+ PrefixKind :: ByCrate ,
39+ prefer_no_std,
40+ prefer_prelude,
41+ ) ,
3142 }
3243}
3344
3445/// Helper function to get path to `ModuleDef` as string
35- fn mod_item_path_str ( sema_scope : & SemanticsScope < ' _ > , def : & ModuleDef ) -> String {
36- let path = mod_item_path ( sema_scope, def) ;
46+ fn mod_item_path_str (
47+ sema_scope : & SemanticsScope < ' _ > ,
48+ def : & ModuleDef ,
49+ prefer_no_std : bool ,
50+ prefer_prelude : bool ,
51+ ) -> String {
52+ let path = mod_item_path ( sema_scope, def, prefer_no_std, prefer_prelude) ;
3753 path. map ( |it| it. display ( sema_scope. db . upcast ( ) ) . to_string ( ) ) . unwrap ( )
3854}
3955
4056/// Helper function to get path to `Type`
41- fn type_path ( sema_scope : & SemanticsScope < ' _ > , ty : & Type ) -> String {
57+ fn type_path (
58+ sema_scope : & SemanticsScope < ' _ > ,
59+ ty : & Type ,
60+ prefer_no_std : bool ,
61+ prefer_prelude : bool ,
62+ ) -> String {
4263 let db = sema_scope. db ;
4364 match ty. as_adt ( ) {
4465 Some ( adt) => {
4566 let ty_name = ty. display ( db) . to_string ( ) ;
4667
47- let mut path = mod_item_path ( sema_scope, & ModuleDef :: Adt ( adt) ) . unwrap ( ) ;
68+ let mut path =
69+ mod_item_path ( sema_scope, & ModuleDef :: Adt ( adt) , prefer_no_std, prefer_prelude)
70+ . unwrap ( ) ;
4871 path. pop_segment ( ) ;
4972 let path = path. display ( db. upcast ( ) ) . to_string ( ) ;
5073 match path. is_empty ( ) {
@@ -125,17 +148,24 @@ impl Expr {
125148 & self ,
126149 sema_scope : & SemanticsScope < ' _ > ,
127150 many_formatter : & mut dyn FnMut ( & Type ) -> String ,
151+ prefer_no_std : bool ,
152+ prefer_prelude : bool ,
128153 ) -> String {
129154 let db = sema_scope. db ;
155+ let mod_item_path_str = |s, def| mod_item_path_str ( s, def, prefer_no_std, prefer_prelude) ;
130156 match self {
131157 Expr :: Const ( it) => mod_item_path_str ( sema_scope, & ModuleDef :: Const ( * it) ) ,
132158 Expr :: Static ( it) => mod_item_path_str ( sema_scope, & ModuleDef :: Static ( * it) ) ,
133159 Expr :: Local ( it) => return it. name ( db) . display ( db. upcast ( ) ) . to_string ( ) ,
134160 Expr :: ConstParam ( it) => return it. name ( db) . display ( db. upcast ( ) ) . to_string ( ) ,
135161 Expr :: FamousType { value, .. } => return value. to_string ( ) ,
136162 Expr :: Function { func, params, .. } => {
137- let args =
138- params. iter ( ) . map ( |f| f. gen_source_code ( sema_scope, many_formatter) ) . join ( ", " ) ;
163+ let args = params
164+ . iter ( )
165+ . map ( |f| {
166+ f. gen_source_code ( sema_scope, many_formatter, prefer_no_std, prefer_prelude)
167+ } )
168+ . join ( ", " ) ;
139169
140170 match func. as_assoc_item ( db) . map ( |it| it. container ( db) ) {
141171 Some ( container) => {
@@ -146,10 +176,14 @@ impl Expr {
146176 crate :: AssocItemContainer :: Impl ( imp) => {
147177 let self_ty = imp. self_ty ( db) ;
148178 // Should it be guaranteed that `mod_item_path` always exists?
149- match self_ty
150- . as_adt ( )
151- . and_then ( |adt| mod_item_path ( sema_scope, & adt. into ( ) ) )
152- {
179+ match self_ty. as_adt ( ) . and_then ( |adt| {
180+ mod_item_path (
181+ sema_scope,
182+ & adt. into ( ) ,
183+ prefer_no_std,
184+ prefer_prelude,
185+ )
186+ } ) {
153187 Some ( path) => path. display ( sema_scope. db . upcast ( ) ) . to_string ( ) ,
154188 None => self_ty. display ( db) . to_string ( ) ,
155189 }
@@ -171,9 +205,18 @@ impl Expr {
171205
172206 let func_name = func. name ( db) . display ( db. upcast ( ) ) . to_string ( ) ;
173207 let self_param = func. self_param ( db) . unwrap ( ) ;
174- let target = target. gen_source_code ( sema_scope, many_formatter) ;
175- let args =
176- params. iter ( ) . map ( |f| f. gen_source_code ( sema_scope, many_formatter) ) . join ( ", " ) ;
208+ let target = target. gen_source_code (
209+ sema_scope,
210+ many_formatter,
211+ prefer_no_std,
212+ prefer_prelude,
213+ ) ;
214+ let args = params
215+ . iter ( )
216+ . map ( |f| {
217+ f. gen_source_code ( sema_scope, many_formatter, prefer_no_std, prefer_prelude)
218+ } )
219+ . join ( ", " ) ;
177220
178221 match func. as_assoc_item ( db) . and_then ( |it| it. containing_trait_or_trait_impl ( db) ) {
179222 Some ( trait_) => {
@@ -196,16 +239,25 @@ impl Expr {
196239 let generics_str = match generics. is_empty ( ) {
197240 true => String :: new ( ) ,
198241 false => {
199- let generics =
200- generics. iter ( ) . map ( |it| type_path ( sema_scope, it) ) . join ( ", " ) ;
242+ let generics = generics
243+ . iter ( )
244+ . map ( |it| type_path ( sema_scope, it, prefer_no_std, prefer_prelude) )
245+ . join ( ", " ) ;
201246 format ! ( "::<{generics}>" )
202247 }
203248 } ;
204249 let inner = match variant. kind ( db) {
205250 StructKind :: Tuple => {
206251 let args = params
207252 . iter ( )
208- . map ( |f| f. gen_source_code ( sema_scope, many_formatter) )
253+ . map ( |f| {
254+ f. gen_source_code (
255+ sema_scope,
256+ many_formatter,
257+ prefer_no_std,
258+ prefer_prelude,
259+ )
260+ } )
209261 . join ( ", " ) ;
210262 format ! ( "{generics_str}({args})" )
211263 }
@@ -218,7 +270,12 @@ impl Expr {
218270 format ! (
219271 "{}: {}" ,
220272 f. name( db) . display( db. upcast( ) ) . to_string( ) ,
221- a. gen_source_code( sema_scope, many_formatter)
273+ a. gen_source_code(
274+ sema_scope,
275+ many_formatter,
276+ prefer_no_std,
277+ prefer_prelude
278+ )
222279 )
223280 } )
224281 . join ( ", " ) ;
@@ -236,7 +293,14 @@ impl Expr {
236293 StructKind :: Tuple => {
237294 let args = params
238295 . iter ( )
239- . map ( |a| a. gen_source_code ( sema_scope, many_formatter) )
296+ . map ( |a| {
297+ a. gen_source_code (
298+ sema_scope,
299+ many_formatter,
300+ prefer_no_std,
301+ prefer_prelude,
302+ )
303+ } )
240304 . join ( ", " ) ;
241305 format ! ( "({args})" )
242306 }
@@ -249,7 +313,12 @@ impl Expr {
249313 format ! (
250314 "{}: {}" ,
251315 f. name( db) . display( db. upcast( ) ) . to_string( ) ,
252- a. gen_source_code( sema_scope, many_formatter)
316+ a. gen_source_code(
317+ sema_scope,
318+ many_formatter,
319+ prefer_no_std,
320+ prefer_prelude
321+ )
253322 )
254323 } )
255324 . join ( ", " ) ;
@@ -258,8 +327,10 @@ impl Expr {
258327 StructKind :: Unit => match generics. is_empty ( ) {
259328 true => String :: new ( ) ,
260329 false => {
261- let generics =
262- generics. iter ( ) . map ( |it| type_path ( sema_scope, it) ) . join ( ", " ) ;
330+ let generics = generics
331+ . iter ( )
332+ . map ( |it| type_path ( sema_scope, it, prefer_no_std, prefer_prelude) )
333+ . join ( ", " ) ;
263334 format ! ( "::<{generics}>" )
264335 }
265336 } ,
@@ -273,7 +344,8 @@ impl Expr {
273344 return many_formatter ( & expr. ty ( db) ) ;
274345 }
275346
276- let strukt = expr. gen_source_code ( sema_scope, many_formatter) ;
347+ let strukt =
348+ expr. gen_source_code ( sema_scope, many_formatter, prefer_no_std, prefer_prelude) ;
277349 let field = field. name ( db) . display ( db. upcast ( ) ) . to_string ( ) ;
278350 format ! ( "{strukt}.{field}" )
279351 }
@@ -282,7 +354,8 @@ impl Expr {
282354 return many_formatter ( & expr. ty ( db) ) ;
283355 }
284356
285- let inner = expr. gen_source_code ( sema_scope, many_formatter) ;
357+ let inner =
358+ expr. gen_source_code ( sema_scope, many_formatter, prefer_no_std, prefer_prelude) ;
286359 format ! ( "&{inner}" )
287360 }
288361 Expr :: Many ( ty) => many_formatter ( ty) ,
0 commit comments