|
3 | 3 | mod format_like; |
4 | 4 |
|
5 | 5 | use base_db::SourceDatabase; |
6 | | -use hir::{ItemInNs, Semantics}; |
| 6 | +use hir::{HirDisplay, ItemInNs, Semantics}; |
7 | 7 | use ide_db::{ |
8 | 8 | RootDatabase, SnippetCap, |
9 | 9 | documentation::{Documentation, HasDocs}, |
@@ -117,6 +117,34 @@ pub(crate) fn complete_postfix( |
117 | 117 | postfix_snippet("call", "function(expr)", &format!("${{1}}({receiver_text})")) |
118 | 118 | .add_to(acc, ctx.db); |
119 | 119 |
|
| 120 | + if let Some(expected_ty) = ctx.expected_type.as_ref() { |
| 121 | + let is_valid_new = expected_ty |
| 122 | + .iterate_assoc_items(ctx.db, |item| { |
| 123 | + if let hir::AssocItem::Function(func) = item |
| 124 | + && func.name(ctx.db).as_str() == "new" |
| 125 | + && !func.has_self_param(ctx.db) |
| 126 | + { |
| 127 | + let params = func.params_without_self(ctx.db); |
| 128 | + if params.len() == 1 { |
| 129 | + return Some(()); |
| 130 | + } |
| 131 | + } |
| 132 | + None |
| 133 | + }) |
| 134 | + .is_some(); |
| 135 | + |
| 136 | + if is_valid_new { |
| 137 | + let ty_name = expected_ty.display(ctx.db, ctx.display_target).to_string(); |
| 138 | + |
| 139 | + postfix_snippet( |
| 140 | + "new", |
| 141 | + &format!("{}::new(expr)", ty_name), |
| 142 | + &format!("{}::new({}$0)", ty_name, receiver_text), |
| 143 | + ) |
| 144 | + .add_to(acc, ctx.db); |
| 145 | + } |
| 146 | + } |
| 147 | + |
120 | 148 | let try_enum = TryEnum::from_ty(&ctx.sema, receiver_ty); |
121 | 149 | let is_in_cond = is_in_condition(&dot_receiver_including_refs); |
122 | 150 | if let Some(parent) = dot_receiver_including_refs.syntax().parent() { |
@@ -1558,6 +1586,37 @@ fn foo(x: Option<i32>, y: Option<i32>) { |
1558 | 1586 | .map(|it| it+2); |
1559 | 1587 | }; |
1560 | 1588 | } |
| 1589 | +"#, |
| 1590 | + ); |
| 1591 | + } |
| 1592 | + |
| 1593 | + #[test] |
| 1594 | + fn postfix_new() { |
| 1595 | + check_edit( |
| 1596 | + "new", |
| 1597 | + r#" |
| 1598 | +struct OtherThing; |
| 1599 | +struct RefCell<T>(T); |
| 1600 | +impl<T> RefCell<T> { |
| 1601 | + fn new(t: T) -> Self { RefCell(t) } |
| 1602 | +} |
| 1603 | +
|
| 1604 | +fn main() { |
| 1605 | + let other_thing = OtherThing; |
| 1606 | + let thing: RefCell<OtherThing> = other_thing.$0; |
| 1607 | +} |
| 1608 | +"#, |
| 1609 | + r#" |
| 1610 | +struct OtherThing; |
| 1611 | +struct RefCell<T>(T); |
| 1612 | +impl<T> RefCell<T> { |
| 1613 | + fn new(t: T) -> Self { RefCell(t) } |
| 1614 | +} |
| 1615 | +
|
| 1616 | +fn main() { |
| 1617 | + let other_thing = OtherThing; |
| 1618 | + let thing: RefCell<OtherThing> = RefCell<OtherThing>::new(other_thing$0); |
| 1619 | +} |
1561 | 1620 | "#, |
1562 | 1621 | ); |
1563 | 1622 | } |
|
0 commit comments