aboutsummaryrefslogtreecommitdiff
path: root/crates
diff options
context:
space:
mode:
Diffstat (limited to 'crates')
-rw-r--r--crates/hir/src/code_model.rs6
-rw-r--r--crates/hir_def/src/body.rs4
-rw-r--r--crates/hir_def/src/child_by_source.rs4
-rw-r--r--crates/hir_def/src/resolver.rs24
-rw-r--r--crates/ide_assists/src/handlers/generate_default_from_new.rs374
-rw-r--r--crates/ide_assists/src/handlers/generate_function.rs56
-rw-r--r--crates/ide_assists/src/handlers/replace_string_with_char.rs136
-rw-r--r--crates/ide_assists/src/lib.rs2
-rw-r--r--crates/ide_assists/src/tests/generated.rs31
9 files changed, 573 insertions, 64 deletions
diff --git a/crates/hir/src/code_model.rs b/crates/hir/src/code_model.rs
index 7656db974..9ee4b3059 100644
--- a/crates/hir/src/code_model.rs
+++ b/crates/hir/src/code_model.rs
@@ -365,14 +365,14 @@ impl Module {
365 let _p = profile::span("Module::diagnostics").detail(|| { 365 let _p = profile::span("Module::diagnostics").detail(|| {
366 format!("{:?}", self.name(db).map_or("<unknown>".into(), |name| name.to_string())) 366 format!("{:?}", self.name(db).map_or("<unknown>".into(), |name| name.to_string()))
367 }); 367 });
368 let crate_def_map = self.id.def_map(db.upcast()); 368 let def_map = self.id.def_map(db.upcast());
369 crate_def_map.add_diagnostics(db.upcast(), self.id.local_id, sink); 369 def_map.add_diagnostics(db.upcast(), self.id.local_id, sink);
370 for decl in self.declarations(db) { 370 for decl in self.declarations(db) {
371 match decl { 371 match decl {
372 crate::ModuleDef::Function(f) => f.diagnostics(db, sink), 372 crate::ModuleDef::Function(f) => f.diagnostics(db, sink),
373 crate::ModuleDef::Module(m) => { 373 crate::ModuleDef::Module(m) => {
374 // Only add diagnostics from inline modules 374 // Only add diagnostics from inline modules
375 if crate_def_map[m.id.local_id].origin.is_inline() { 375 if def_map[m.id.local_id].origin.is_inline() {
376 m.diagnostics(db, sink) 376 m.diagnostics(db, sink)
377 } 377 }
378 } 378 }
diff --git a/crates/hir_def/src/body.rs b/crates/hir_def/src/body.rs
index ff4b4a0cf..16e1bac40 100644
--- a/crates/hir_def/src/body.rs
+++ b/crates/hir_def/src/body.rs
@@ -87,11 +87,11 @@ impl Expander {
87 module: ModuleId, 87 module: ModuleId,
88 ) -> Expander { 88 ) -> Expander {
89 let cfg_expander = CfgExpander::new(db, current_file_id, module.krate); 89 let cfg_expander = CfgExpander::new(db, current_file_id, module.krate);
90 let crate_def_map = module.def_map(db); 90 let def_map = module.def_map(db);
91 let ast_id_map = db.ast_id_map(current_file_id); 91 let ast_id_map = db.ast_id_map(current_file_id);
92 Expander { 92 Expander {
93 cfg_expander, 93 cfg_expander,
94 def_map: crate_def_map, 94 def_map,
95 current_file_id, 95 current_file_id,
96 ast_id_map, 96 ast_id_map,
97 module: module.local_id, 97 module: module.local_id,
diff --git a/crates/hir_def/src/child_by_source.rs b/crates/hir_def/src/child_by_source.rs
index 65d85c86a..75c2d756b 100644
--- a/crates/hir_def/src/child_by_source.rs
+++ b/crates/hir_def/src/child_by_source.rs
@@ -74,8 +74,8 @@ impl ChildBySource for ImplId {
74 74
75impl ChildBySource for ModuleId { 75impl ChildBySource for ModuleId {
76 fn child_by_source(&self, db: &dyn DefDatabase) -> DynMap { 76 fn child_by_source(&self, db: &dyn DefDatabase) -> DynMap {
77 let crate_def_map = self.def_map(db); 77 let def_map = self.def_map(db);
78 let module_data = &crate_def_map[self.local_id]; 78 let module_data = &def_map[self.local_id];
79 module_data.scope.child_by_source(db) 79 module_data.scope.child_by_source(db)
80 } 80 }
81} 81}
diff --git a/crates/hir_def/src/resolver.rs b/crates/hir_def/src/resolver.rs
index e85f85e49..77ff21739 100644
--- a/crates/hir_def/src/resolver.rs
+++ b/crates/hir_def/src/resolver.rs
@@ -34,7 +34,7 @@ pub struct Resolver {
34// FIXME how to store these best 34// FIXME how to store these best
35#[derive(Debug, Clone)] 35#[derive(Debug, Clone)]
36struct ModuleItemMap { 36struct ModuleItemMap {
37 crate_def_map: Arc<DefMap>, 37 def_map: Arc<DefMap>,
38 module_id: LocalModuleId, 38 module_id: LocalModuleId,
39} 39}
40 40
@@ -337,11 +337,11 @@ impl Resolver {
337 let mut traits = FxHashSet::default(); 337 let mut traits = FxHashSet::default();
338 for scope in &self.scopes { 338 for scope in &self.scopes {
339 if let Scope::ModuleScope(m) = scope { 339 if let Scope::ModuleScope(m) = scope {
340 if let Some(prelude) = m.crate_def_map.prelude() { 340 if let Some(prelude) = m.def_map.prelude() {
341 let prelude_def_map = prelude.def_map(db); 341 let prelude_def_map = prelude.def_map(db);
342 traits.extend(prelude_def_map[prelude.local_id].scope.traits()); 342 traits.extend(prelude_def_map[prelude.local_id].scope.traits());
343 } 343 }
344 traits.extend(m.crate_def_map[m.module_id].scope.traits()); 344 traits.extend(m.def_map[m.module_id].scope.traits());
345 } 345 }
346 } 346 }
347 traits 347 traits
@@ -349,7 +349,7 @@ impl Resolver {
349 349
350 fn module_scope(&self) -> Option<(&DefMap, LocalModuleId)> { 350 fn module_scope(&self) -> Option<(&DefMap, LocalModuleId)> {
351 self.scopes.iter().rev().find_map(|scope| match scope { 351 self.scopes.iter().rev().find_map(|scope| match scope {
352 Scope::ModuleScope(m) => Some((&*m.crate_def_map, m.module_id)), 352 Scope::ModuleScope(m) => Some((&*m.def_map, m.module_id)),
353 353
354 _ => None, 354 _ => None,
355 }) 355 })
@@ -413,21 +413,21 @@ impl Scope {
413 // def: m.module.into(), 413 // def: m.module.into(),
414 // }), 414 // }),
415 // ); 415 // );
416 m.crate_def_map[m.module_id].scope.entries().for_each(|(name, def)| { 416 m.def_map[m.module_id].scope.entries().for_each(|(name, def)| {
417 f(name.clone(), ScopeDef::PerNs(def)); 417 f(name.clone(), ScopeDef::PerNs(def));
418 }); 418 });
419 m.crate_def_map[m.module_id].scope.legacy_macros().for_each(|(name, macro_)| { 419 m.def_map[m.module_id].scope.legacy_macros().for_each(|(name, macro_)| {
420 let scope = PerNs::macros(macro_, Visibility::Public); 420 let scope = PerNs::macros(macro_, Visibility::Public);
421 seen.insert((name.clone(), scope)); 421 seen.insert((name.clone(), scope));
422 f(name.clone(), ScopeDef::PerNs(scope)); 422 f(name.clone(), ScopeDef::PerNs(scope));
423 }); 423 });
424 m.crate_def_map.extern_prelude().for_each(|(name, &def)| { 424 m.def_map.extern_prelude().for_each(|(name, &def)| {
425 f(name.clone(), ScopeDef::PerNs(PerNs::types(def, Visibility::Public))); 425 f(name.clone(), ScopeDef::PerNs(PerNs::types(def, Visibility::Public)));
426 }); 426 });
427 BUILTIN_SCOPE.iter().for_each(|(name, &def)| { 427 BUILTIN_SCOPE.iter().for_each(|(name, &def)| {
428 f(name.clone(), ScopeDef::PerNs(def)); 428 f(name.clone(), ScopeDef::PerNs(def));
429 }); 429 });
430 if let Some(prelude) = m.crate_def_map.prelude() { 430 if let Some(prelude) = m.def_map.prelude() {
431 let prelude_def_map = prelude.def_map(db); 431 let prelude_def_map = prelude.def_map(db);
432 prelude_def_map[prelude.local_id].scope.entries().for_each(|(name, def)| { 432 prelude_def_map[prelude.local_id].scope.entries().for_each(|(name, def)| {
433 let seen_tuple = (name.clone(), def); 433 let seen_tuple = (name.clone(), def);
@@ -513,8 +513,8 @@ impl Resolver {
513 self.push_scope(Scope::ImplDefScope(impl_def)) 513 self.push_scope(Scope::ImplDefScope(impl_def))
514 } 514 }
515 515
516 fn push_module_scope(self, crate_def_map: Arc<DefMap>, module_id: LocalModuleId) -> Resolver { 516 fn push_module_scope(self, def_map: Arc<DefMap>, module_id: LocalModuleId) -> Resolver {
517 self.push_scope(Scope::ModuleScope(ModuleItemMap { crate_def_map, module_id })) 517 self.push_scope(Scope::ModuleScope(ModuleItemMap { def_map, module_id }))
518 } 518 }
519 519
520 fn push_expr_scope( 520 fn push_expr_scope(
@@ -534,7 +534,7 @@ impl ModuleItemMap {
534 path: &ModPath, 534 path: &ModPath,
535 ) -> Option<ResolveValueResult> { 535 ) -> Option<ResolveValueResult> {
536 let (module_def, idx) = 536 let (module_def, idx) =
537 self.crate_def_map.resolve_path(db, self.module_id, &path, BuiltinShadowMode::Other); 537 self.def_map.resolve_path(db, self.module_id, &path, BuiltinShadowMode::Other);
538 match idx { 538 match idx {
539 None => { 539 None => {
540 let value = to_value_ns(module_def)?; 540 let value = to_value_ns(module_def)?;
@@ -564,7 +564,7 @@ impl ModuleItemMap {
564 path: &ModPath, 564 path: &ModPath,
565 ) -> Option<(TypeNs, Option<usize>)> { 565 ) -> Option<(TypeNs, Option<usize>)> {
566 let (module_def, idx) = 566 let (module_def, idx) =
567 self.crate_def_map.resolve_path(db, self.module_id, &path, BuiltinShadowMode::Other); 567 self.def_map.resolve_path(db, self.module_id, &path, BuiltinShadowMode::Other);
568 let res = to_type_ns(module_def)?; 568 let res = to_type_ns(module_def)?;
569 Some((res, idx)) 569 Some((res, idx))
570 } 570 }
diff --git a/crates/ide_assists/src/handlers/generate_default_from_new.rs b/crates/ide_assists/src/handlers/generate_default_from_new.rs
new file mode 100644
index 000000000..fa1254579
--- /dev/null
+++ b/crates/ide_assists/src/handlers/generate_default_from_new.rs
@@ -0,0 +1,374 @@
1use crate::{
2 assist_context::{AssistContext, Assists},
3 AssistId,
4};
5use ide_db::helpers::FamousDefs;
6use syntax::{
7 ast::{self, Impl, NameOwner},
8 AstNode,
9};
10use test_utils::mark;
11
12// Assist: generate_default_from_new
13//
14// Generates default implementation from new method.
15//
16// ```
17// struct Example { _inner: () }
18//
19// impl Example {
20// pub fn n$0ew() -> Self {
21// Self { _inner: () }
22// }
23// }
24// ```
25// ->
26// ```
27// struct Example { _inner: () }
28//
29// impl Example {
30// pub fn new() -> Self {
31// Self { _inner: () }
32// }
33// }
34//
35// impl Default for Example {
36// fn default() -> Self {
37// Self::new()
38// }
39// }
40// ```
41pub(crate) fn generate_default_from_new(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
42 let fn_node = ctx.find_node_at_offset::<ast::Fn>()?;
43 let fn_name = fn_node.name()?;
44
45 if fn_name.text() != "new" {
46 mark::hit!(other_function_than_new);
47 return None;
48 }
49
50 if fn_node.param_list()?.params().next().is_some() {
51 mark::hit!(new_function_with_parameters);
52 return None;
53 }
54
55 let impl_ = fn_node.syntax().ancestors().into_iter().find_map(ast::Impl::cast)?;
56 if is_default_implemented(ctx, &impl_) {
57 mark::hit!(default_block_is_already_present);
58 mark::hit!(struct_in_module_with_default);
59 return None;
60 }
61
62 let insert_location = impl_.syntax().text_range();
63
64 acc.add(
65 AssistId("generate_default_from_new", crate::AssistKind::Generate),
66 "Generate a Default impl from a new fn",
67 insert_location,
68 move |builder| {
69 let code = default_fn_node_for_new(impl_);
70 builder.insert(insert_location.end(), code);
71 },
72 )
73}
74
75fn default_fn_node_for_new(impl_: Impl) -> String {
76 format!(
77 "
78
79impl Default for {} {{
80 fn default() -> Self {{
81 Self::new()
82 }}
83}}",
84 impl_.self_ty().unwrap().syntax().text()
85 )
86}
87
88fn is_default_implemented(ctx: &AssistContext, impl_: &Impl) -> bool {
89 let db = ctx.sema.db;
90 let impl_ = ctx.sema.to_def(impl_);
91 let impl_def = match impl_ {
92 Some(value) => value,
93 None => return false,
94 };
95
96 let ty = impl_def.target_ty(db);
97 let krate = impl_def.module(db).krate();
98 let default = FamousDefs(&ctx.sema, Some(krate)).core_default_Default();
99 let default_trait = match default {
100 Some(value) => value,
101 None => return false,
102 };
103
104 ty.impls_trait(db, default_trait, &[])
105}
106
107#[cfg(test)]
108mod tests {
109 use ide_db::helpers::FamousDefs;
110
111 use crate::tests::{check_assist, check_assist_not_applicable};
112
113 use super::*;
114
115 #[test]
116 fn generate_default() {
117 check_pass(
118 r#"
119struct Example { _inner: () }
120
121impl Example {
122 pub fn ne$0w() -> Self {
123 Self { _inner: () }
124 }
125}
126
127fn main() {}
128"#,
129 r#"
130struct Example { _inner: () }
131
132impl Example {
133 pub fn new() -> Self {
134 Self { _inner: () }
135 }
136}
137
138impl Default for Example {
139 fn default() -> Self {
140 Self::new()
141 }
142}
143
144fn main() {}
145"#,
146 );
147 }
148
149 #[test]
150 fn generate_default2() {
151 check_pass(
152 r#"
153struct Test { value: u32 }
154
155impl Test {
156 pub fn ne$0w() -> Self {
157 Self { value: 0 }
158 }
159}
160"#,
161 r#"
162struct Test { value: u32 }
163
164impl Test {
165 pub fn new() -> Self {
166 Self { value: 0 }
167 }
168}
169
170impl Default for Test {
171 fn default() -> Self {
172 Self::new()
173 }
174}
175"#,
176 );
177 }
178
179 #[test]
180 fn new_function_with_parameters() {
181 mark::check!(new_function_with_parameters);
182 check_not_applicable(
183 r#"
184struct Example { _inner: () }
185
186impl Example {
187 pub fn $0new(value: ()) -> Self {
188 Self { _inner: value }
189 }
190}
191"#,
192 );
193 }
194
195 #[test]
196 fn other_function_than_new() {
197 mark::check!(other_function_than_new);
198 check_not_applicable(
199 r#"
200struct Example { _inner: () }
201
202impl Example {
203 pub fn a$0dd() -> Self {
204 Self { _inner: () }
205 }
206}
207
208"#,
209 );
210 }
211
212 #[test]
213 fn default_block_is_already_present() {
214 mark::check!(default_block_is_already_present);
215 check_not_applicable(
216 r#"
217struct Example { _inner: () }
218
219impl Example {
220 pub fn n$0ew() -> Self {
221 Self { _inner: () }
222 }
223}
224
225impl Default for Example {
226 fn default() -> Self {
227 Self::new()
228 }
229}
230"#,
231 );
232 }
233
234 #[test]
235 fn standalone_new_function() {
236 check_not_applicable(
237 r#"
238fn n$0ew() -> u32 {
239 0
240}
241"#,
242 );
243 }
244
245 #[test]
246 fn multiple_struct_blocks() {
247 check_pass(
248 r#"
249struct Example { _inner: () }
250struct Test { value: u32 }
251
252impl Example {
253 pub fn new$0() -> Self {
254 Self { _inner: () }
255 }
256}
257"#,
258 r#"
259struct Example { _inner: () }
260struct Test { value: u32 }
261
262impl Example {
263 pub fn new() -> Self {
264 Self { _inner: () }
265 }
266}
267
268impl Default for Example {
269 fn default() -> Self {
270 Self::new()
271 }
272}
273"#,
274 );
275 }
276
277 #[test]
278 fn when_struct_is_after_impl() {
279 check_pass(
280 r#"
281impl Example {
282 pub fn $0new() -> Self {
283 Self { _inner: () }
284 }
285}
286
287struct Example { _inner: () }
288"#,
289 r#"
290impl Example {
291 pub fn new() -> Self {
292 Self { _inner: () }
293 }
294}
295
296impl Default for Example {
297 fn default() -> Self {
298 Self::new()
299 }
300}
301
302struct Example { _inner: () }
303"#,
304 );
305 }
306
307 #[test]
308 fn struct_in_module() {
309 check_pass(
310 r#"
311mod test {
312 struct Example { _inner: () }
313
314 impl Example {
315 pub fn n$0ew() -> Self {
316 Self { _inner: () }
317 }
318 }
319}
320"#,
321 r#"
322mod test {
323 struct Example { _inner: () }
324
325 impl Example {
326 pub fn new() -> Self {
327 Self { _inner: () }
328 }
329 }
330
331impl Default for Example {
332 fn default() -> Self {
333 Self::new()
334 }
335}
336}
337"#,
338 );
339 }
340
341 #[test]
342 fn struct_in_module_with_default() {
343 mark::check!(struct_in_module_with_default);
344 check_not_applicable(
345 r#"
346mod test {
347 struct Example { _inner: () }
348
349 impl Example {
350 pub fn n$0ew() -> Self {
351 Self { _inner: () }
352 }
353 }
354
355 impl Default for Example {
356 fn default() -> Self {
357 Self::new()
358 }
359 }
360}
361"#,
362 );
363 }
364
365 fn check_pass(before: &str, after: &str) {
366 let before = &format!("//- /main.rs crate:main deps:core{}{}", before, FamousDefs::FIXTURE);
367 check_assist(generate_default_from_new, before, after);
368 }
369
370 fn check_not_applicable(before: &str) {
371 let before = &format!("//- /main.rs crate:main deps:core{}{}", before, FamousDefs::FIXTURE);
372 check_assist_not_applicable(generate_default_from_new, before);
373 }
374}
diff --git a/crates/ide_assists/src/handlers/generate_function.rs b/crates/ide_assists/src/handlers/generate_function.rs
index 959824981..3870b7e75 100644
--- a/crates/ide_assists/src/handlers/generate_function.rs
+++ b/crates/ide_assists/src/handlers/generate_function.rs
@@ -1,6 +1,7 @@
1use hir::HirDisplay; 1use hir::HirDisplay;
2use ide_db::{base_db::FileId, helpers::SnippetCap}; 2use ide_db::{base_db::FileId, helpers::SnippetCap};
3use rustc_hash::{FxHashMap, FxHashSet}; 3use rustc_hash::{FxHashMap, FxHashSet};
4use stdx::to_lower_snake_case;
4use syntax::{ 5use syntax::{
5 ast::{ 6 ast::{
6 self, 7 self,
@@ -257,14 +258,15 @@ fn deduplicate_arg_names(arg_names: &mut Vec<String>) {
257fn fn_arg_name(fn_arg: &ast::Expr) -> Option<String> { 258fn fn_arg_name(fn_arg: &ast::Expr) -> Option<String> {
258 match fn_arg { 259 match fn_arg {
259 ast::Expr::CastExpr(cast_expr) => fn_arg_name(&cast_expr.expr()?), 260 ast::Expr::CastExpr(cast_expr) => fn_arg_name(&cast_expr.expr()?),
260 _ => Some( 261 _ => {
261 fn_arg 262 let s = fn_arg
262 .syntax() 263 .syntax()
263 .descendants() 264 .descendants()
264 .filter(|d| ast::NameRef::can_cast(d.kind())) 265 .filter(|d| ast::NameRef::can_cast(d.kind()))
265 .last()? 266 .last()?
266 .to_string(), 267 .to_string();
267 ), 268 Some(to_lower_snake_case(&s))
269 }
268 } 270 }
269} 271}
270 272
@@ -448,6 +450,52 @@ mod baz {
448 } 450 }
449 451
450 #[test] 452 #[test]
453 fn add_function_with_upper_camel_case_arg() {
454 check_assist(
455 generate_function,
456 r"
457struct BazBaz;
458fn foo() {
459 bar$0(BazBaz);
460}
461",
462 r"
463struct BazBaz;
464fn foo() {
465 bar(BazBaz);
466}
467
468fn bar(baz_baz: BazBaz) ${0:-> ()} {
469 todo!()
470}
471",
472 );
473 }
474
475 #[test]
476 fn add_function_with_upper_camel_case_arg_as_cast() {
477 check_assist(
478 generate_function,
479 r"
480struct BazBaz;
481fn foo() {
482 bar$0(&BazBaz as *const BazBaz);
483}
484",
485 r"
486struct BazBaz;
487fn foo() {
488 bar(&BazBaz as *const BazBaz);
489}
490
491fn bar(baz_baz: *const BazBaz) ${0:-> ()} {
492 todo!()
493}
494",
495 );
496 }
497
498 #[test]
451 fn add_function_with_function_call_arg() { 499 fn add_function_with_function_call_arg() {
452 check_assist( 500 check_assist(
453 generate_function, 501 generate_function,
diff --git a/crates/ide_assists/src/handlers/replace_string_with_char.rs b/crates/ide_assists/src/handlers/replace_string_with_char.rs
index 317318c24..634b9c0b7 100644
--- a/crates/ide_assists/src/handlers/replace_string_with_char.rs
+++ b/crates/ide_assists/src/handlers/replace_string_with_char.rs
@@ -25,13 +25,16 @@ pub(crate) fn replace_string_with_char(acc: &mut Assists, ctx: &AssistContext) -
25 if value.chars().take(2).count() != 1 { 25 if value.chars().take(2).count() != 1 {
26 return None; 26 return None;
27 } 27 }
28 let quote_offets = token.quote_offsets()?;
28 29
29 acc.add( 30 acc.add(
30 AssistId("replace_string_with_char", AssistKind::RefactorRewrite), 31 AssistId("replace_string_with_char", AssistKind::RefactorRewrite),
31 "Replace string with char", 32 "Replace string with char",
32 target, 33 target,
33 |edit| { 34 |edit| {
34 edit.replace(token.syntax().text_range(), format!("'{}'", value)); 35 let (left, right) = quote_offets.quotes;
36 edit.replace(left, String::from('\''));
37 edit.replace(right, String::from('\''));
35 }, 38 },
36 ) 39 )
37} 40}
@@ -47,10 +50,10 @@ mod tests {
47 check_assist_target( 50 check_assist_target(
48 replace_string_with_char, 51 replace_string_with_char,
49 r#" 52 r#"
50 fn f() { 53fn f() {
51 let s = "$0c"; 54 let s = "$0c";
52 } 55}
53 "#, 56"#,
54 r#""c""#, 57 r#""c""#,
55 ); 58 );
56 } 59 }
@@ -60,15 +63,15 @@ mod tests {
60 check_assist( 63 check_assist(
61 replace_string_with_char, 64 replace_string_with_char,
62 r#" 65 r#"
63 fn f() { 66fn f() {
64 let s = "$0c"; 67 let s = "$0c";
65 } 68}
66 "#, 69"#,
67 r##" 70 r##"
68 fn f() { 71fn f() {
69 let s = 'c'; 72 let s = 'c';
70 } 73}
71 "##, 74"##,
72 ) 75 )
73 } 76 }
74 77
@@ -77,15 +80,15 @@ mod tests {
77 check_assist( 80 check_assist(
78 replace_string_with_char, 81 replace_string_with_char,
79 r#" 82 r#"
80 fn f() { 83fn f() {
81 let s = "$0😀"; 84 let s = "$0😀";
82 } 85}
83 "#, 86"#,
84 r##" 87 r##"
85 fn f() { 88fn f() {
86 let s = '😀'; 89 let s = '😀';
87 } 90}
88 "##, 91"##,
89 ) 92 )
90 } 93 }
91 94
@@ -94,10 +97,10 @@ mod tests {
94 check_assist_not_applicable( 97 check_assist_not_applicable(
95 replace_string_with_char, 98 replace_string_with_char,
96 r#" 99 r#"
97 fn f() { 100fn f() {
98 let s = "$0test"; 101 let s = "$0test";
99 } 102}
100 "#, 103"#,
101 ) 104 )
102 } 105 }
103 106
@@ -106,15 +109,15 @@ mod tests {
106 check_assist( 109 check_assist(
107 replace_string_with_char, 110 replace_string_with_char,
108 r#" 111 r#"
109 fn f() { 112fn f() {
110 format!($0"x", 92) 113 format!($0"x", 92)
111 } 114}
112 "#, 115"#,
113 r##" 116 r##"
114 fn f() { 117fn f() {
115 format!('x', 92) 118 format!('x', 92)
116 } 119}
117 "##, 120"##,
118 ) 121 )
119 } 122 }
120 123
@@ -123,15 +126,66 @@ mod tests {
123 check_assist( 126 check_assist(
124 replace_string_with_char, 127 replace_string_with_char,
125 r#" 128 r#"
126 fn f() { 129fn f() {
127 find($0"x"); 130 find($0"x");
128 } 131}
129 "#, 132"#,
130 r##" 133 r##"
131 fn f() { 134fn f() {
132 find('x'); 135 find('x');
133 } 136}
134 "##, 137"##,
138 )
139 }
140
141 #[test]
142 fn replace_string_with_char_newline() {
143 check_assist(
144 replace_string_with_char,
145 r#"
146fn f() {
147 find($0"\n");
148}
149"#,
150 r##"
151fn f() {
152 find('\n');
153}
154"##,
155 )
156 }
157
158 #[test]
159 fn replace_string_with_char_unicode_escape() {
160 check_assist(
161 replace_string_with_char,
162 r#"
163fn f() {
164 find($0"\u{7FFF}");
165}
166"#,
167 r##"
168fn f() {
169 find('\u{7FFF}');
170}
171"##,
172 )
173 }
174
175 #[test]
176 fn replace_raw_string_with_char() {
177 check_assist(
178 replace_string_with_char,
179 r##"
180fn f() {
181 $0r#"X"#
182}
183"##,
184 r##"
185fn f() {
186 'X'
187}
188"##,
135 ) 189 )
136 } 190 }
137} 191}
diff --git a/crates/ide_assists/src/lib.rs b/crates/ide_assists/src/lib.rs
index 9c8148462..ea62d5f5d 100644
--- a/crates/ide_assists/src/lib.rs
+++ b/crates/ide_assists/src/lib.rs
@@ -127,6 +127,7 @@ mod handlers {
127 mod flip_comma; 127 mod flip_comma;
128 mod flip_trait_bound; 128 mod flip_trait_bound;
129 mod generate_default_from_enum_variant; 129 mod generate_default_from_enum_variant;
130 mod generate_default_from_new;
130 mod generate_derive; 131 mod generate_derive;
131 mod generate_enum_is_method; 132 mod generate_enum_is_method;
132 mod generate_enum_projection_method; 133 mod generate_enum_projection_method;
@@ -189,6 +190,7 @@ mod handlers {
189 flip_comma::flip_comma, 190 flip_comma::flip_comma,
190 flip_trait_bound::flip_trait_bound, 191 flip_trait_bound::flip_trait_bound,
191 generate_default_from_enum_variant::generate_default_from_enum_variant, 192 generate_default_from_enum_variant::generate_default_from_enum_variant,
193 generate_default_from_new::generate_default_from_new,
192 generate_derive::generate_derive, 194 generate_derive::generate_derive,
193 generate_enum_is_method::generate_enum_is_method, 195 generate_enum_is_method::generate_enum_is_method,
194 generate_enum_projection_method::generate_enum_as_method, 196 generate_enum_projection_method::generate_enum_as_method,
diff --git a/crates/ide_assists/src/tests/generated.rs b/crates/ide_assists/src/tests/generated.rs
index 4f007aa48..304b5798f 100644
--- a/crates/ide_assists/src/tests/generated.rs
+++ b/crates/ide_assists/src/tests/generated.rs
@@ -440,6 +440,37 @@ impl Default for Version {
440} 440}
441 441
442#[test] 442#[test]
443fn doctest_generate_default_from_new() {
444 check_doc_test(
445 "generate_default_from_new",
446 r#####"
447struct Example { _inner: () }
448
449impl Example {
450 pub fn n$0ew() -> Self {
451 Self { _inner: () }
452 }
453}
454"#####,
455 r#####"
456struct Example { _inner: () }
457
458impl Example {
459 pub fn new() -> Self {
460 Self { _inner: () }
461 }
462}
463
464impl Default for Example {
465 fn default() -> Self {
466 Self::new()
467 }
468}
469"#####,
470 )
471}
472
473#[test]
443fn doctest_generate_derive() { 474fn doctest_generate_derive() {
444 check_doc_test( 475 check_doc_test(
445 "generate_derive", 476 "generate_derive",