]> git.lizzy.rs Git - rust.git/blob - crates/ide_assists/src/utils.rs
Feat: inline generics in const and func trait completions
[rust.git] / crates / ide_assists / src / utils.rs
1 //! Assorted functions shared by several assists.
2
3 pub(crate) mod suggest_name;
4
5 use std::ops;
6
7 use ast::TypeBoundsOwner;
8 use hir::{Adt, HasSource, Semantics};
9 use ide_db::{
10     helpers::{FamousDefs, SnippetCap},
11     path_transform::PathTransform,
12     RootDatabase,
13 };
14 use itertools::Itertools;
15 use stdx::format_to;
16 use syntax::{
17     ast::edit::AstNodeEdit,
18     ast::AttrsOwner,
19     ast::NameOwner,
20     ast::{self, edit, make, ArgListOwner, GenericParamsOwner},
21     ted, AstNode, Direction, SmolStr,
22     SyntaxKind::*,
23     SyntaxNode, TextSize, T,
24 };
25
26 use crate::assist_context::{AssistBuilder, AssistContext};
27
28 pub(crate) fn unwrap_trivial_block(block: ast::BlockExpr) -> ast::Expr {
29     extract_trivial_expression(&block)
30         .filter(|expr| !expr.syntax().text().contains_char('\n'))
31         .unwrap_or_else(|| block.into())
32 }
33
34 pub fn extract_trivial_expression(block: &ast::BlockExpr) -> Option<ast::Expr> {
35     let has_anything_else = |thing: &SyntaxNode| -> bool {
36         let mut non_trivial_children =
37             block.syntax().children_with_tokens().filter(|it| match it.kind() {
38                 WHITESPACE | T!['{'] | T!['}'] => false,
39                 _ => it.as_node() != Some(thing),
40             });
41         non_trivial_children.next().is_some()
42     };
43
44     if let Some(expr) = block.tail_expr() {
45         if has_anything_else(expr.syntax()) {
46             return None;
47         }
48         return Some(expr);
49     }
50     // Unwrap `{ continue; }`
51     let (stmt,) = block.statements().next_tuple()?;
52     if let ast::Stmt::ExprStmt(expr_stmt) = stmt {
53         if has_anything_else(expr_stmt.syntax()) {
54             return None;
55         }
56         let expr = expr_stmt.expr()?;
57         match expr.syntax().kind() {
58             CONTINUE_EXPR | BREAK_EXPR | RETURN_EXPR => return Some(expr),
59             _ => (),
60         }
61     }
62     None
63 }
64
65 /// This is a method with a heuristics to support test methods annotated with custom test annotations, such as
66 /// `#[test_case(...)]`, `#[tokio::test]` and similar.
67 /// Also a regular `#[test]` annotation is supported.
68 ///
69 /// It may produce false positives, for example, `#[wasm_bindgen_test]` requires a different command to run the test,
70 /// but it's better than not to have the runnables for the tests at all.
71 pub fn test_related_attribute(fn_def: &ast::Fn) -> Option<ast::Attr> {
72     fn_def.attrs().find_map(|attr| {
73         let path = attr.path()?;
74         if path.syntax().text().to_string().contains("test") {
75             Some(attr)
76         } else {
77             None
78         }
79     })
80 }
81
82 #[derive(Copy, Clone, PartialEq)]
83 pub enum DefaultMethods {
84     Only,
85     No,
86 }
87
88 pub fn filter_assoc_items(
89     db: &RootDatabase,
90     items: &[hir::AssocItem],
91     default_methods: DefaultMethods,
92 ) -> Vec<ast::AssocItem> {
93     fn has_def_name(item: &ast::AssocItem) -> bool {
94         match item {
95             ast::AssocItem::Fn(def) => def.name(),
96             ast::AssocItem::TypeAlias(def) => def.name(),
97             ast::AssocItem::Const(def) => def.name(),
98             ast::AssocItem::MacroCall(_) => None,
99         }
100         .is_some()
101     }
102
103     items
104         .iter()
105         // Note: This throws away items with no source.
106         .filter_map(|i| {
107             let item = match i {
108                 hir::AssocItem::Function(i) => ast::AssocItem::Fn(i.source(db)?.value),
109                 hir::AssocItem::TypeAlias(i) => ast::AssocItem::TypeAlias(i.source(db)?.value),
110                 hir::AssocItem::Const(i) => ast::AssocItem::Const(i.source(db)?.value),
111             };
112             Some(item)
113         })
114         .filter(has_def_name)
115         .filter(|it| match it {
116             ast::AssocItem::Fn(def) => matches!(
117                 (default_methods, def.body()),
118                 (DefaultMethods::Only, Some(_)) | (DefaultMethods::No, None)
119             ),
120             _ => default_methods == DefaultMethods::No,
121         })
122         .collect::<Vec<_>>()
123 }
124
125 pub fn add_trait_assoc_items_to_impl(
126     sema: &hir::Semantics<ide_db::RootDatabase>,
127     items: Vec<ast::AssocItem>,
128     trait_: hir::Trait,
129     impl_: ast::Impl,
130     target_scope: hir::SemanticsScope,
131 ) -> (ast::Impl, ast::AssocItem) {
132     let source_scope = sema.scope_for_def(trait_);
133
134     let transform = PathTransform {
135         subst: (trait_, impl_.clone()),
136         source_scope: &source_scope,
137         target_scope: &target_scope,
138     };
139
140     let items = items.into_iter().map(|assoc_item| {
141         let assoc_item = assoc_item.clone_for_update();
142         transform.apply(assoc_item.clone());
143         edit::remove_attrs_and_docs(&assoc_item).clone_subtree().clone_for_update()
144     });
145
146     let res = impl_.clone_for_update();
147
148     let assoc_item_list = res.get_or_create_assoc_item_list();
149     let mut first_item = None;
150     for item in items {
151         first_item.get_or_insert_with(|| item.clone());
152         match &item {
153             ast::AssocItem::Fn(fn_) if fn_.body().is_none() => {
154                 let body = make::block_expr(None, Some(make::ext::expr_todo()))
155                     .indent(edit::IndentLevel(1));
156                 ted::replace(fn_.get_or_create_body().syntax(), body.clone_for_update().syntax())
157             }
158             ast::AssocItem::TypeAlias(type_alias) => {
159                 if let Some(type_bound_list) = type_alias.type_bound_list() {
160                     type_bound_list.remove()
161                 }
162             }
163             _ => {}
164         }
165
166         assoc_item_list.add_item(item)
167     }
168
169     (res, first_item.unwrap())
170 }
171
172 #[derive(Clone, Copy, Debug)]
173 pub(crate) enum Cursor<'a> {
174     Replace(&'a SyntaxNode),
175     Before(&'a SyntaxNode),
176 }
177
178 impl<'a> Cursor<'a> {
179     fn node(self) -> &'a SyntaxNode {
180         match self {
181             Cursor::Replace(node) | Cursor::Before(node) => node,
182         }
183     }
184 }
185
186 pub(crate) fn render_snippet(_cap: SnippetCap, node: &SyntaxNode, cursor: Cursor) -> String {
187     assert!(cursor.node().ancestors().any(|it| it == *node));
188     let range = cursor.node().text_range() - node.text_range().start();
189     let range: ops::Range<usize> = range.into();
190
191     let mut placeholder = cursor.node().to_string();
192     escape(&mut placeholder);
193     let tab_stop = match cursor {
194         Cursor::Replace(placeholder) => format!("${{0:{}}}", placeholder),
195         Cursor::Before(placeholder) => format!("$0{}", placeholder),
196     };
197
198     let mut buf = node.to_string();
199     buf.replace_range(range, &tab_stop);
200     return buf;
201
202     fn escape(buf: &mut String) {
203         stdx::replace(buf, '{', r"\{");
204         stdx::replace(buf, '}', r"\}");
205         stdx::replace(buf, '$', r"\$");
206     }
207 }
208
209 pub(crate) fn vis_offset(node: &SyntaxNode) -> TextSize {
210     node.children_with_tokens()
211         .find(|it| !matches!(it.kind(), WHITESPACE | COMMENT | ATTR))
212         .map(|it| it.text_range().start())
213         .unwrap_or_else(|| node.text_range().start())
214 }
215
216 pub(crate) fn invert_boolean_expression(
217     sema: &Semantics<RootDatabase>,
218     expr: ast::Expr,
219 ) -> ast::Expr {
220     if let Some(expr) = invert_special_case(sema, &expr) {
221         return expr;
222     }
223     make::expr_prefix(T![!], expr)
224 }
225
226 fn invert_special_case(sema: &Semantics<RootDatabase>, expr: &ast::Expr) -> Option<ast::Expr> {
227     match expr {
228         ast::Expr::BinExpr(bin) => match bin.op_kind()? {
229             ast::BinOp::NegatedEqualityTest => bin.replace_op(T![==]).map(|it| it.into()),
230             ast::BinOp::EqualityTest => bin.replace_op(T![!=]).map(|it| it.into()),
231             // Swap `<` with `>=`, `<=` with `>`, ... if operands `impl Ord`
232             ast::BinOp::LesserTest if bin_impls_ord(sema, bin) => {
233                 bin.replace_op(T![>=]).map(|it| it.into())
234             }
235             ast::BinOp::LesserEqualTest if bin_impls_ord(sema, bin) => {
236                 bin.replace_op(T![>]).map(|it| it.into())
237             }
238             ast::BinOp::GreaterTest if bin_impls_ord(sema, bin) => {
239                 bin.replace_op(T![<=]).map(|it| it.into())
240             }
241             ast::BinOp::GreaterEqualTest if bin_impls_ord(sema, bin) => {
242                 bin.replace_op(T![<]).map(|it| it.into())
243             }
244             // Parenthesize other expressions before prefixing `!`
245             _ => Some(make::expr_prefix(T![!], make::expr_paren(expr.clone()))),
246         },
247         ast::Expr::MethodCallExpr(mce) => {
248             let receiver = mce.receiver()?;
249             let method = mce.name_ref()?;
250             let arg_list = mce.arg_list()?;
251
252             let method = match method.text().as_str() {
253                 "is_some" => "is_none",
254                 "is_none" => "is_some",
255                 "is_ok" => "is_err",
256                 "is_err" => "is_ok",
257                 _ => return None,
258             };
259             Some(make::expr_method_call(receiver, method, arg_list))
260         }
261         ast::Expr::PrefixExpr(pe) if pe.op_kind()? == ast::PrefixOp::Not => {
262             if let ast::Expr::ParenExpr(parexpr) = pe.expr()? {
263                 parexpr.expr()
264             } else {
265                 pe.expr()
266             }
267         }
268         // FIXME:
269         // ast::Expr::Literal(true | false )
270         _ => None,
271     }
272 }
273
274 fn bin_impls_ord(sema: &Semantics<RootDatabase>, bin: &ast::BinExpr) -> bool {
275     match (
276         bin.lhs().and_then(|lhs| sema.type_of_expr(&lhs)),
277         bin.rhs().and_then(|rhs| sema.type_of_expr(&rhs)),
278     ) {
279         (Some(lhs_ty), Some(rhs_ty)) if lhs_ty == rhs_ty => {
280             let krate = sema.scope(bin.syntax()).module().map(|it| it.krate());
281             let ord_trait = FamousDefs(sema, krate).core_cmp_Ord();
282             ord_trait.map_or(false, |ord_trait| {
283                 lhs_ty.autoderef(sema.db).any(|ty| ty.impls_trait(sema.db, ord_trait, &[]))
284             })
285         }
286         _ => false,
287     }
288 }
289
290 pub(crate) fn next_prev() -> impl Iterator<Item = Direction> {
291     [Direction::Next, Direction::Prev].iter().copied()
292 }
293
294 pub(crate) fn does_pat_match_variant(pat: &ast::Pat, var: &ast::Pat) -> bool {
295     let first_node_text = |pat: &ast::Pat| pat.syntax().first_child().map(|node| node.text());
296
297     let pat_head = match pat {
298         ast::Pat::IdentPat(bind_pat) => {
299             if let Some(p) = bind_pat.pat() {
300                 first_node_text(&p)
301             } else {
302                 return pat.syntax().text() == var.syntax().text();
303             }
304         }
305         pat => first_node_text(pat),
306     };
307
308     let var_head = first_node_text(var);
309
310     pat_head == var_head
311 }
312
313 // Uses a syntax-driven approach to find any impl blocks for the struct that
314 // exist within the module/file
315 //
316 // Returns `None` if we've found an existing fn
317 //
318 // FIXME: change the new fn checking to a more semantic approach when that's more
319 // viable (e.g. we process proc macros, etc)
320 // FIXME: this partially overlaps with `find_impl_block_*`
321 pub(crate) fn find_struct_impl(
322     ctx: &AssistContext,
323     strukt: &ast::Adt,
324     name: &str,
325 ) -> Option<Option<ast::Impl>> {
326     let db = ctx.db();
327     let module = strukt.syntax().ancestors().find(|node| {
328         ast::Module::can_cast(node.kind()) || ast::SourceFile::can_cast(node.kind())
329     })?;
330
331     let struct_def = match strukt {
332         ast::Adt::Enum(e) => Adt::Enum(ctx.sema.to_def(e)?),
333         ast::Adt::Struct(s) => Adt::Struct(ctx.sema.to_def(s)?),
334         ast::Adt::Union(u) => Adt::Union(ctx.sema.to_def(u)?),
335     };
336
337     let block = module.descendants().filter_map(ast::Impl::cast).find_map(|impl_blk| {
338         let blk = ctx.sema.to_def(&impl_blk)?;
339
340         // FIXME: handle e.g. `struct S<T>; impl<U> S<U> {}`
341         // (we currently use the wrong type parameter)
342         // also we wouldn't want to use e.g. `impl S<u32>`
343
344         let same_ty = match blk.self_ty(db).as_adt() {
345             Some(def) => def == struct_def,
346             None => false,
347         };
348         let not_trait_impl = blk.trait_(db).is_none();
349
350         if !(same_ty && not_trait_impl) {
351             None
352         } else {
353             Some(impl_blk)
354         }
355     });
356
357     if let Some(ref impl_blk) = block {
358         if has_fn(impl_blk, name) {
359             return None;
360         }
361     }
362
363     Some(block)
364 }
365
366 fn has_fn(imp: &ast::Impl, rhs_name: &str) -> bool {
367     if let Some(il) = imp.assoc_item_list() {
368         for item in il.assoc_items() {
369             if let ast::AssocItem::Fn(f) = item {
370                 if let Some(name) = f.name() {
371                     if name.text().eq_ignore_ascii_case(rhs_name) {
372                         return true;
373                     }
374                 }
375             }
376         }
377     }
378
379     false
380 }
381
382 /// Find the start of the `impl` block for the given `ast::Impl`.
383 //
384 // FIXME: this partially overlaps with `find_struct_impl`
385 pub(crate) fn find_impl_block_start(impl_def: ast::Impl, buf: &mut String) -> Option<TextSize> {
386     buf.push('\n');
387     let start = impl_def.assoc_item_list().and_then(|it| it.l_curly_token())?.text_range().end();
388     Some(start)
389 }
390
391 /// Find the end of the `impl` block for the given `ast::Impl`.
392 //
393 // FIXME: this partially overlaps with `find_struct_impl`
394 pub(crate) fn find_impl_block_end(impl_def: ast::Impl, buf: &mut String) -> Option<TextSize> {
395     buf.push('\n');
396     let end = impl_def
397         .assoc_item_list()
398         .and_then(|it| it.r_curly_token())?
399         .prev_sibling_or_token()?
400         .text_range()
401         .end();
402     Some(end)
403 }
404
405 // Generates the surrounding `impl Type { <code> }` including type and lifetime
406 // parameters
407 pub(crate) fn generate_impl_text(adt: &ast::Adt, code: &str) -> String {
408     generate_impl_text_inner(adt, None, code)
409 }
410
411 // Generates the surrounding `impl <trait> for Type { <code> }` including type
412 // and lifetime parameters
413 pub(crate) fn generate_trait_impl_text(adt: &ast::Adt, trait_text: &str, code: &str) -> String {
414     generate_impl_text_inner(adt, Some(trait_text), code)
415 }
416
417 fn generate_impl_text_inner(adt: &ast::Adt, trait_text: Option<&str>, code: &str) -> String {
418     let generic_params = adt.generic_param_list();
419     let mut buf = String::with_capacity(code.len());
420     buf.push_str("\n\n");
421     adt.attrs()
422         .filter(|attr| attr.as_simple_call().map(|(name, _arg)| name == "cfg").unwrap_or(false))
423         .for_each(|attr| buf.push_str(format!("{}\n", attr.to_string()).as_str()));
424     buf.push_str("impl");
425     if let Some(generic_params) = &generic_params {
426         let lifetimes = generic_params.lifetime_params().map(|lt| format!("{}", lt.syntax()));
427         let type_params = generic_params.type_params().map(|type_param| {
428             let mut buf = String::new();
429             if let Some(it) = type_param.name() {
430                 format_to!(buf, "{}", it.syntax());
431             }
432             if let Some(it) = type_param.colon_token() {
433                 format_to!(buf, "{} ", it);
434             }
435             if let Some(it) = type_param.type_bound_list() {
436                 format_to!(buf, "{}", it.syntax());
437             }
438             buf
439         });
440         let const_params = generic_params.const_params().map(|t| t.syntax().to_string());
441         let generics = lifetimes.chain(type_params).chain(const_params).format(", ");
442         format_to!(buf, "<{}>", generics);
443     }
444     buf.push(' ');
445     if let Some(trait_text) = trait_text {
446         buf.push_str(trait_text);
447         buf.push_str(" for ");
448     }
449     buf.push_str(&adt.name().unwrap().text());
450     if let Some(generic_params) = generic_params {
451         let lifetime_params = generic_params
452             .lifetime_params()
453             .filter_map(|it| it.lifetime())
454             .map(|it| SmolStr::from(it.text()));
455         let type_params = generic_params
456             .type_params()
457             .filter_map(|it| it.name())
458             .map(|it| SmolStr::from(it.text()));
459         let const_params = generic_params
460             .const_params()
461             .filter_map(|it| it.name())
462             .map(|it| SmolStr::from(it.text()));
463         format_to!(buf, "<{}>", lifetime_params.chain(type_params).chain(const_params).format(", "))
464     }
465
466     match adt.where_clause() {
467         Some(where_clause) => {
468             format_to!(buf, "\n{}\n{{\n{}\n}}", where_clause, code);
469         }
470         None => {
471             format_to!(buf, " {{\n{}\n}}", code);
472         }
473     }
474
475     buf
476 }
477
478 pub(crate) fn add_method_to_adt(
479     builder: &mut AssistBuilder,
480     adt: &ast::Adt,
481     impl_def: Option<ast::Impl>,
482     method: &str,
483 ) {
484     let mut buf = String::with_capacity(method.len() + 2);
485     if impl_def.is_some() {
486         buf.push('\n');
487     }
488     buf.push_str(method);
489
490     let start_offset = impl_def
491         .and_then(|impl_def| find_impl_block_end(impl_def, &mut buf))
492         .unwrap_or_else(|| {
493             buf = generate_impl_text(adt, &buf);
494             adt.syntax().text_range().end()
495         });
496
497     builder.insert(start_offset, buf);
498 }