1 //! Assorted functions shared by several assists.
2
3 use std::ops;
4
5 pub(crate) use gen_trait_fn_body::gen_trait_fn_body;
6 use hir::{db::HirDatabase, HirDisplay, InFile, Semantics};
7 use ide_db::{
8 famous_defs::FamousDefs, path_transform::PathTransform,
9 syntax_helpers::insert_whitespace_into_node::insert_ws_into, RootDatabase, SnippetCap,
10 };
11 use stdx::format_to;
12 use syntax::{
13 ast::{
14 self,
15 edit::{AstNodeEdit, IndentLevel},
16 edit_in_place::{AttrsOwnerEdit, Indent, Removable},
17 make, HasArgList, HasAttrs, HasGenericParams, HasName, HasTypeBounds, Whitespace,
18 },
19 ted, AstNode, AstToken, Direction, SourceFile,
20 SyntaxKind::*,
21 SyntaxNode, TextRange, TextSize, T,
22 };
23
24 use crate::assist_context::{AssistContext, SourceChangeBuilder};
25
26 pub(crate) mod suggest_name;
27 mod gen_trait_fn_body;
28
unwrap_trivial_block(block_expr: ast::BlockExpr) -> ast::Expr29 pub(crate) fn unwrap_trivial_block(block_expr: ast::BlockExpr) -> ast::Expr {
30 extract_trivial_expression(&block_expr)
31 .filter(|expr| !expr.syntax().text().contains_char('\n'))
32 .unwrap_or_else(|| block_expr.into())
33 }
34
extract_trivial_expression(block_expr: &ast::BlockExpr) -> Option<ast::Expr>35 pub fn extract_trivial_expression(block_expr: &ast::BlockExpr) -> Option<ast::Expr> {
36 if block_expr.modifier().is_some() {
37 return None;
38 }
39 let stmt_list = block_expr.stmt_list()?;
40 let has_anything_else = |thing: &SyntaxNode| -> bool {
41 let mut non_trivial_children =
42 stmt_list.syntax().children_with_tokens().filter(|it| match it.kind() {
43 WHITESPACE | T!['{'] | T!['}'] => false,
44 _ => it.as_node() != Some(thing),
45 });
46 non_trivial_children.next().is_some()
47 };
48
49 if let Some(expr) = stmt_list.tail_expr() {
50 if has_anything_else(expr.syntax()) {
51 return None;
52 }
53 return Some(expr);
54 }
55 // Unwrap `{ continue; }`
56 let stmt = stmt_list.statements().next()?;
57 if let ast::Stmt::ExprStmt(expr_stmt) = stmt {
58 if has_anything_else(expr_stmt.syntax()) {
59 return None;
60 }
61 let expr = expr_stmt.expr()?;
62 if matches!(expr.syntax().kind(), CONTINUE_EXPR | BREAK_EXPR | RETURN_EXPR) {
63 return Some(expr);
64 }
65 }
66 None
67 }
68
69 /// This is a method with a heuristics to support test methods annotated with custom test annotations, such as
70 /// `#[test_case(...)]`, `#[tokio::test]` and similar.
71 /// Also a regular `#[test]` annotation is supported.
72 ///
73 /// It may produce false positives, for example, `#[wasm_bindgen_test]` requires a different command to run the test,
74 /// but it's better than not to have the runnables for the tests at all.
test_related_attribute(fn_def: &ast::Fn) -> Option<ast::Attr>75 pub fn test_related_attribute(fn_def: &ast::Fn) -> Option<ast::Attr> {
76 fn_def.attrs().find_map(|attr| {
77 let path = attr.path()?;
78 let text = path.syntax().text().to_string();
79 if text.starts_with("test") || text.ends_with("test") {
80 Some(attr)
81 } else {
82 None
83 }
84 })
85 }
86
87 #[derive(Copy, Clone, PartialEq)]
88 pub enum DefaultMethods {
89 Only,
90 No,
91 }
92
filter_assoc_items( sema: &Semantics<'_, RootDatabase>, items: &[hir::AssocItem], default_methods: DefaultMethods, ) -> Vec<InFile<ast::AssocItem>>93 pub fn filter_assoc_items(
94 sema: &Semantics<'_, RootDatabase>,
95 items: &[hir::AssocItem],
96 default_methods: DefaultMethods,
97 ) -> Vec<InFile<ast::AssocItem>> {
98 return items
99 .iter()
100 // Note: This throws away items with no source.
101 .copied()
102 .filter_map(|assoc_item| {
103 let item = match assoc_item {
104 hir::AssocItem::Function(it) => sema.source(it)?.map(ast::AssocItem::Fn),
105 hir::AssocItem::TypeAlias(it) => sema.source(it)?.map(ast::AssocItem::TypeAlias),
106 hir::AssocItem::Const(it) => sema.source(it)?.map(ast::AssocItem::Const),
107 };
108 Some(item)
109 })
110 .filter(has_def_name)
111 .filter(|it| match &it.value {
112 ast::AssocItem::Fn(def) => matches!(
113 (default_methods, def.body()),
114 (DefaultMethods::Only, Some(_)) | (DefaultMethods::No, None)
115 ),
116 ast::AssocItem::Const(def) => matches!(
117 (default_methods, def.body()),
118 (DefaultMethods::Only, Some(_)) | (DefaultMethods::No, None)
119 ),
120 _ => default_methods == DefaultMethods::No,
121 })
122 .collect();
123
124 fn has_def_name(item: &InFile<ast::AssocItem>) -> bool {
125 match &item.value {
126 ast::AssocItem::Fn(def) => def.name(),
127 ast::AssocItem::TypeAlias(def) => def.name(),
128 ast::AssocItem::Const(def) => def.name(),
129 ast::AssocItem::MacroCall(_) => None,
130 }
131 .is_some()
132 }
133 }
134
135 /// Given `original_items` retrieved from the trait definition (usually by
136 /// [`filter_assoc_items()`]), clones each item for update and applies path transformation to it,
137 /// then inserts into `impl_`. Returns the modified `impl_` and the first associated item that got
138 /// inserted.
add_trait_assoc_items_to_impl( sema: &Semantics<'_, RootDatabase>, original_items: &[InFile<ast::AssocItem>], trait_: hir::Trait, impl_: &ast::Impl, target_scope: hir::SemanticsScope<'_>, ) -> ast::AssocItem139 pub fn add_trait_assoc_items_to_impl(
140 sema: &Semantics<'_, RootDatabase>,
141 original_items: &[InFile<ast::AssocItem>],
142 trait_: hir::Trait,
143 impl_: &ast::Impl,
144 target_scope: hir::SemanticsScope<'_>,
145 ) -> ast::AssocItem {
146 let new_indent_level = IndentLevel::from_node(impl_.syntax()) + 1;
147 let items = original_items.into_iter().map(|InFile { file_id, value: original_item }| {
148 let cloned_item = {
149 if file_id.is_macro() {
150 if let Some(formatted) =
151 ast::AssocItem::cast(insert_ws_into(original_item.syntax().clone()))
152 {
153 return formatted;
154 } else {
155 stdx::never!("formatted `AssocItem` could not be cast back to `AssocItem`");
156 }
157 }
158 original_item.clone_for_update()
159 };
160
161 if let Some(source_scope) = sema.scope(original_item.syntax()) {
162 // FIXME: Paths in nested macros are not handled well. See
163 // `add_missing_impl_members::paths_in_nested_macro_should_get_transformed` test.
164 let transform =
165 PathTransform::trait_impl(&target_scope, &source_scope, trait_, impl_.clone());
166 transform.apply(cloned_item.syntax());
167 }
168 cloned_item.remove_attrs_and_docs();
169 cloned_item.reindent_to(new_indent_level);
170 cloned_item
171 });
172
173 let assoc_item_list = impl_.get_or_create_assoc_item_list();
174 let mut first_item = None;
175 for item in items {
176 first_item.get_or_insert_with(|| item.clone());
177 match &item {
178 ast::AssocItem::Fn(fn_) if fn_.body().is_none() => {
179 let body = AstNodeEdit::indent(
180 &make::block_expr(None, Some(make::ext::expr_todo())),
181 new_indent_level,
182 );
183 ted::replace(fn_.get_or_create_body().syntax(), body.clone_for_update().syntax())
184 }
185 ast::AssocItem::TypeAlias(type_alias) => {
186 if let Some(type_bound_list) = type_alias.type_bound_list() {
187 type_bound_list.remove()
188 }
189 }
190 _ => {}
191 }
192
193 assoc_item_list.add_item(item)
194 }
195
196 first_item.unwrap()
197 }
198
199 #[derive(Clone, Copy, Debug)]
200 pub(crate) enum Cursor<'a> {
201 Replace(&'a SyntaxNode),
202 Before(&'a SyntaxNode),
203 }
204
205 impl<'a> Cursor<'a> {
node(self) -> &'a SyntaxNode206 fn node(self) -> &'a SyntaxNode {
207 match self {
208 Cursor::Replace(node) | Cursor::Before(node) => node,
209 }
210 }
211 }
212
render_snippet(_cap: SnippetCap, node: &SyntaxNode, cursor: Cursor<'_>) -> String213 pub(crate) fn render_snippet(_cap: SnippetCap, node: &SyntaxNode, cursor: Cursor<'_>) -> String {
214 assert!(cursor.node().ancestors().any(|it| it == *node));
215 let range = cursor.node().text_range() - node.text_range().start();
216 let range: ops::Range<usize> = range.into();
217
218 let mut placeholder = cursor.node().to_string();
219 escape(&mut placeholder);
220 let tab_stop = match cursor {
221 Cursor::Replace(placeholder) => format!("${{0:{placeholder}}}"),
222 Cursor::Before(placeholder) => format!("$0{placeholder}"),
223 };
224
225 let mut buf = node.to_string();
226 buf.replace_range(range, &tab_stop);
227 return buf;
228
229 fn escape(buf: &mut String) {
230 stdx::replace(buf, '{', r"\{");
231 stdx::replace(buf, '}', r"\}");
232 stdx::replace(buf, '$', r"\$");
233 }
234 }
235
vis_offset(node: &SyntaxNode) -> TextSize236 pub(crate) fn vis_offset(node: &SyntaxNode) -> TextSize {
237 node.children_with_tokens()
238 .find(|it| !matches!(it.kind(), WHITESPACE | COMMENT | ATTR))
239 .map(|it| it.text_range().start())
240 .unwrap_or_else(|| node.text_range().start())
241 }
242
invert_boolean_expression(expr: ast::Expr) -> ast::Expr243 pub(crate) fn invert_boolean_expression(expr: ast::Expr) -> ast::Expr {
244 invert_special_case(&expr).unwrap_or_else(|| make::expr_prefix(T![!], expr))
245 }
246
invert_special_case(expr: &ast::Expr) -> Option<ast::Expr>247 fn invert_special_case(expr: &ast::Expr) -> Option<ast::Expr> {
248 match expr {
249 ast::Expr::BinExpr(bin) => {
250 let bin = bin.clone_for_update();
251 let op_token = bin.op_token()?;
252 let rev_token = match op_token.kind() {
253 T![==] => T![!=],
254 T![!=] => T![==],
255 T![<] => T![>=],
256 T![<=] => T![>],
257 T![>] => T![<=],
258 T![>=] => T![<],
259 // Parenthesize other expressions before prefixing `!`
260 _ => return Some(make::expr_prefix(T![!], make::expr_paren(expr.clone()))),
261 };
262 ted::replace(op_token, make::token(rev_token));
263 Some(bin.into())
264 }
265 ast::Expr::MethodCallExpr(mce) => {
266 let receiver = mce.receiver()?;
267 let method = mce.name_ref()?;
268 let arg_list = mce.arg_list()?;
269
270 let method = match method.text().as_str() {
271 "is_some" => "is_none",
272 "is_none" => "is_some",
273 "is_ok" => "is_err",
274 "is_err" => "is_ok",
275 _ => return None,
276 };
277 Some(make::expr_method_call(receiver, make::name_ref(method), arg_list))
278 }
279 ast::Expr::PrefixExpr(pe) if pe.op_kind()? == ast::UnaryOp::Not => match pe.expr()? {
280 ast::Expr::ParenExpr(parexpr) => parexpr.expr(),
281 _ => pe.expr(),
282 },
283 ast::Expr::Literal(lit) => match lit.kind() {
284 ast::LiteralKind::Bool(b) => match b {
285 true => Some(ast::Expr::Literal(make::expr_literal("false"))),
286 false => Some(ast::Expr::Literal(make::expr_literal("true"))),
287 },
288 _ => None,
289 },
290 _ => None,
291 }
292 }
293
next_prev() -> impl Iterator<Item = Direction>294 pub(crate) fn next_prev() -> impl Iterator<Item = Direction> {
295 [Direction::Next, Direction::Prev].into_iter()
296 }
297
does_pat_match_variant(pat: &ast::Pat, var: &ast::Pat) -> bool298 pub(crate) fn does_pat_match_variant(pat: &ast::Pat, var: &ast::Pat) -> bool {
299 let first_node_text = |pat: &ast::Pat| pat.syntax().first_child().map(|node| node.text());
300
301 let pat_head = match pat {
302 ast::Pat::IdentPat(bind_pat) => match bind_pat.pat() {
303 Some(p) => first_node_text(&p),
304 None => return pat.syntax().text() == var.syntax().text(),
305 },
306 pat => first_node_text(pat),
307 };
308
309 let var_head = first_node_text(var);
310
311 pat_head == var_head
312 }
313
does_nested_pattern(pat: &ast::Pat) -> bool314 pub(crate) fn does_nested_pattern(pat: &ast::Pat) -> bool {
315 let depth = calc_depth(pat, 0);
316
317 if 1 < depth {
318 return true;
319 }
320 false
321 }
322
calc_depth(pat: &ast::Pat, depth: usize) -> usize323 fn calc_depth(pat: &ast::Pat, depth: usize) -> usize {
324 match pat {
325 ast::Pat::IdentPat(_)
326 | ast::Pat::BoxPat(_)
327 | ast::Pat::RestPat(_)
328 | ast::Pat::LiteralPat(_)
329 | ast::Pat::MacroPat(_)
330 | ast::Pat::OrPat(_)
331 | ast::Pat::ParenPat(_)
332 | ast::Pat::PathPat(_)
333 | ast::Pat::WildcardPat(_)
334 | ast::Pat::RangePat(_)
335 | ast::Pat::RecordPat(_)
336 | ast::Pat::RefPat(_)
337 | ast::Pat::SlicePat(_)
338 | ast::Pat::TuplePat(_)
339 | ast::Pat::ConstBlockPat(_) => depth,
340
341 // FIXME: Other patterns may also be nested. Currently it simply supports only `TupleStructPat`
342 ast::Pat::TupleStructPat(pat) => {
343 let mut max_depth = depth;
344 for p in pat.fields() {
345 let d = calc_depth(&p, depth + 1);
346 if d > max_depth {
347 max_depth = d
348 }
349 }
350 max_depth
351 }
352 }
353 }
354
355 // Uses a syntax-driven approach to find any impl blocks for the struct that
356 // exist within the module/file
357 //
358 // Returns `None` if we've found an existing fn
359 //
360 // FIXME: change the new fn checking to a more semantic approach when that's more
361 // viable (e.g. we process proc macros, etc)
362 // FIXME: this partially overlaps with `find_impl_block_*`
363
364 /// `find_struct_impl` looks for impl of a struct, but this also has additional feature
365 /// where it takes a list of function names and check if they exist inside impl_, if
366 /// even one match is found, it returns None.
367 ///
368 /// That means this function can have 3 potential return values:
369 /// - `None`: an impl exists, but one of the function names within the impl matches one of the provided names.
370 /// - `Some(None)`: no impl exists.
371 /// - `Some(Some(_))`: an impl exists, with no matching function names.
find_struct_impl( ctx: &AssistContext<'_>, adt: &ast::Adt, names: &[String], ) -> Option<Option<ast::Impl>>372 pub(crate) fn find_struct_impl(
373 ctx: &AssistContext<'_>,
374 adt: &ast::Adt,
375 names: &[String],
376 ) -> Option<Option<ast::Impl>> {
377 let db = ctx.db();
378 let module = adt.syntax().parent()?;
379
380 let struct_def = ctx.sema.to_def(adt)?;
381
382 let block = module.descendants().filter_map(ast::Impl::cast).find_map(|impl_blk| {
383 let blk = ctx.sema.to_def(&impl_blk)?;
384
385 // FIXME: handle e.g. `struct S<T>; impl<U> S<U> {}`
386 // (we currently use the wrong type parameter)
387 // also we wouldn't want to use e.g. `impl S<u32>`
388
389 let same_ty = match blk.self_ty(db).as_adt() {
390 Some(def) => def == struct_def,
391 None => false,
392 };
393 let not_trait_impl = blk.trait_(db).is_none();
394
395 if !(same_ty && not_trait_impl) {
396 None
397 } else {
398 Some(impl_blk)
399 }
400 });
401
402 if let Some(ref impl_blk) = block {
403 if has_any_fn(impl_blk, names) {
404 return None;
405 }
406 }
407
408 Some(block)
409 }
410
has_any_fn(imp: &ast::Impl, names: &[String]) -> bool411 fn has_any_fn(imp: &ast::Impl, names: &[String]) -> bool {
412 if let Some(il) = imp.assoc_item_list() {
413 for item in il.assoc_items() {
414 if let ast::AssocItem::Fn(f) = item {
415 if let Some(name) = f.name() {
416 if names.iter().any(|n| n.eq_ignore_ascii_case(&name.text())) {
417 return true;
418 }
419 }
420 }
421 }
422 }
423
424 false
425 }
426
427 /// Find the start of the `impl` block for the given `ast::Impl`.
428 //
429 // FIXME: this partially overlaps with `find_struct_impl`
find_impl_block_start(impl_def: ast::Impl, buf: &mut String) -> Option<TextSize>430 pub(crate) fn find_impl_block_start(impl_def: ast::Impl, buf: &mut String) -> Option<TextSize> {
431 buf.push('\n');
432 let start = impl_def.assoc_item_list().and_then(|it| it.l_curly_token())?.text_range().end();
433 Some(start)
434 }
435
436 /// Find the end of the `impl` block for the given `ast::Impl`.
437 //
438 // FIXME: this partially overlaps with `find_struct_impl`
find_impl_block_end(impl_def: ast::Impl, buf: &mut String) -> Option<TextSize>439 pub(crate) fn find_impl_block_end(impl_def: ast::Impl, buf: &mut String) -> Option<TextSize> {
440 buf.push('\n');
441 let end = impl_def
442 .assoc_item_list()
443 .and_then(|it| it.r_curly_token())?
444 .prev_sibling_or_token()?
445 .text_range()
446 .end();
447 Some(end)
448 }
449
450 /// Generates the surrounding `impl Type { <code> }` including type and lifetime
451 /// parameters.
generate_impl_text(adt: &ast::Adt, code: &str) -> String452 pub(crate) fn generate_impl_text(adt: &ast::Adt, code: &str) -> String {
453 generate_impl_text_inner(adt, None, true, code)
454 }
455
456 /// Generates the surrounding `impl <trait> for Type { <code> }` including type
457 /// and lifetime parameters, with `<trait>` appended to `impl`'s generic parameters' bounds.
458 ///
459 /// This is useful for traits like `PartialEq`, since `impl<T> PartialEq for U<T>` often requires `T: PartialEq`.
generate_trait_impl_text(adt: &ast::Adt, trait_text: &str, code: &str) -> String460 pub(crate) fn generate_trait_impl_text(adt: &ast::Adt, trait_text: &str, code: &str) -> String {
461 generate_impl_text_inner(adt, Some(trait_text), true, code)
462 }
463
464 /// Generates the surrounding `impl <trait> for Type { <code> }` including type
465 /// and lifetime parameters, with `impl`'s generic parameters' bounds kept as-is.
466 ///
467 /// This is useful for traits like `From<T>`, since `impl<T> From<T> for U<T>` doesn't require `T: From<T>`.
generate_trait_impl_text_intransitive( adt: &ast::Adt, trait_text: &str, code: &str, ) -> String468 pub(crate) fn generate_trait_impl_text_intransitive(
469 adt: &ast::Adt,
470 trait_text: &str,
471 code: &str,
472 ) -> String {
473 generate_impl_text_inner(adt, Some(trait_text), false, code)
474 }
475
generate_impl_text_inner( adt: &ast::Adt, trait_text: Option<&str>, trait_is_transitive: bool, code: &str, ) -> String476 fn generate_impl_text_inner(
477 adt: &ast::Adt,
478 trait_text: Option<&str>,
479 trait_is_transitive: bool,
480 code: &str,
481 ) -> String {
482 // Ensure lifetime params are before type & const params
483 let generic_params = adt.generic_param_list().map(|generic_params| {
484 let lifetime_params =
485 generic_params.lifetime_params().map(ast::GenericParam::LifetimeParam);
486 let ty_or_const_params = generic_params.type_or_const_params().map(|param| {
487 match param {
488 ast::TypeOrConstParam::Type(param) => {
489 let param = param.clone_for_update();
490 // remove defaults since they can't be specified in impls
491 param.remove_default();
492 let mut bounds =
493 param.type_bound_list().map_or_else(Vec::new, |it| it.bounds().collect());
494 if let Some(trait_) = trait_text {
495 // Add the current trait to `bounds` if the trait is transitive,
496 // meaning `impl<T> Trait for U<T>` requires `T: Trait`.
497 if trait_is_transitive {
498 bounds.push(make::type_bound(trait_));
499 }
500 };
501 // `{ty_param}: {bounds}`
502 let param =
503 make::type_param(param.name().unwrap(), make::type_bound_list(bounds));
504 ast::GenericParam::TypeParam(param)
505 }
506 ast::TypeOrConstParam::Const(param) => {
507 let param = param.clone_for_update();
508 // remove defaults since they can't be specified in impls
509 param.remove_default();
510 ast::GenericParam::ConstParam(param)
511 }
512 }
513 });
514
515 make::generic_param_list(itertools::chain(lifetime_params, ty_or_const_params))
516 });
517
518 // FIXME: use syntax::make & mutable AST apis instead
519 // `trait_text` and `code` can't be opaque blobs of text
520 let mut buf = String::with_capacity(code.len());
521
522 // Copy any cfg attrs from the original adt
523 buf.push_str("\n\n");
524 let cfg_attrs = adt
525 .attrs()
526 .filter(|attr| attr.as_simple_call().map(|(name, _arg)| name == "cfg").unwrap_or(false));
527 cfg_attrs.for_each(|attr| buf.push_str(&format!("{attr}\n")));
528
529 // `impl{generic_params} {trait_text} for {name}{generic_params.to_generic_args()}`
530 buf.push_str("impl");
531 if let Some(generic_params) = &generic_params {
532 format_to!(buf, "{generic_params}");
533 }
534 buf.push(' ');
535 if let Some(trait_text) = trait_text {
536 buf.push_str(trait_text);
537 buf.push_str(" for ");
538 }
539 buf.push_str(&adt.name().unwrap().text());
540 if let Some(generic_params) = generic_params {
541 format_to!(buf, "{}", generic_params.to_generic_args());
542 }
543
544 match adt.where_clause() {
545 Some(where_clause) => {
546 format_to!(buf, "\n{where_clause}\n{{\n{code}\n}}");
547 }
548 None => {
549 format_to!(buf, " {{\n{code}\n}}");
550 }
551 }
552
553 buf
554 }
555
add_method_to_adt( builder: &mut SourceChangeBuilder, adt: &ast::Adt, impl_def: Option<ast::Impl>, method: &str, )556 pub(crate) fn add_method_to_adt(
557 builder: &mut SourceChangeBuilder,
558 adt: &ast::Adt,
559 impl_def: Option<ast::Impl>,
560 method: &str,
561 ) {
562 let mut buf = String::with_capacity(method.len() + 2);
563 if impl_def.is_some() {
564 buf.push('\n');
565 }
566 buf.push_str(method);
567
568 let start_offset = impl_def
569 .and_then(|impl_def| find_impl_block_end(impl_def, &mut buf))
570 .unwrap_or_else(|| {
571 buf = generate_impl_text(adt, &buf);
572 adt.syntax().text_range().end()
573 });
574
575 builder.insert(start_offset, buf);
576 }
577
578 #[derive(Debug)]
579 pub(crate) struct ReferenceConversion {
580 conversion: ReferenceConversionType,
581 ty: hir::Type,
582 }
583
584 #[derive(Debug)]
585 enum ReferenceConversionType {
586 // reference can be stripped if the type is Copy
587 Copy,
588 // &String -> &str
589 AsRefStr,
590 // &Vec<T> -> &[T]
591 AsRefSlice,
592 // &Box<T> -> &T
593 Dereferenced,
594 // &Option<T> -> Option<&T>
595 Option,
596 // &Result<T, E> -> Result<&T, &E>
597 Result,
598 }
599
600 impl ReferenceConversion {
convert_type(&self, db: &dyn HirDatabase) -> String601 pub(crate) fn convert_type(&self, db: &dyn HirDatabase) -> String {
602 match self.conversion {
603 ReferenceConversionType::Copy => self.ty.display(db).to_string(),
604 ReferenceConversionType::AsRefStr => "&str".to_string(),
605 ReferenceConversionType::AsRefSlice => {
606 let type_argument_name =
607 self.ty.type_arguments().next().unwrap().display(db).to_string();
608 format!("&[{type_argument_name}]")
609 }
610 ReferenceConversionType::Dereferenced => {
611 let type_argument_name =
612 self.ty.type_arguments().next().unwrap().display(db).to_string();
613 format!("&{type_argument_name}")
614 }
615 ReferenceConversionType::Option => {
616 let type_argument_name =
617 self.ty.type_arguments().next().unwrap().display(db).to_string();
618 format!("Option<&{type_argument_name}>")
619 }
620 ReferenceConversionType::Result => {
621 let mut type_arguments = self.ty.type_arguments();
622 let first_type_argument_name =
623 type_arguments.next().unwrap().display(db).to_string();
624 let second_type_argument_name =
625 type_arguments.next().unwrap().display(db).to_string();
626 format!("Result<&{first_type_argument_name}, &{second_type_argument_name}>")
627 }
628 }
629 }
630
getter(&self, field_name: String) -> String631 pub(crate) fn getter(&self, field_name: String) -> String {
632 match self.conversion {
633 ReferenceConversionType::Copy => format!("self.{field_name}"),
634 ReferenceConversionType::AsRefStr
635 | ReferenceConversionType::AsRefSlice
636 | ReferenceConversionType::Dereferenced
637 | ReferenceConversionType::Option
638 | ReferenceConversionType::Result => format!("self.{field_name}.as_ref()"),
639 }
640 }
641 }
642
643 // FIXME: It should return a new hir::Type, but currently constructing new types is too cumbersome
644 // and all users of this function operate on string type names, so they can do the conversion
645 // itself themselves.
convert_reference_type( ty: hir::Type, db: &RootDatabase, famous_defs: &FamousDefs<'_, '_>, ) -> Option<ReferenceConversion>646 pub(crate) fn convert_reference_type(
647 ty: hir::Type,
648 db: &RootDatabase,
649 famous_defs: &FamousDefs<'_, '_>,
650 ) -> Option<ReferenceConversion> {
651 handle_copy(&ty, db)
652 .or_else(|| handle_as_ref_str(&ty, db, famous_defs))
653 .or_else(|| handle_as_ref_slice(&ty, db, famous_defs))
654 .or_else(|| handle_dereferenced(&ty, db, famous_defs))
655 .or_else(|| handle_option_as_ref(&ty, db, famous_defs))
656 .or_else(|| handle_result_as_ref(&ty, db, famous_defs))
657 .map(|conversion| ReferenceConversion { ty, conversion })
658 }
659
handle_copy(ty: &hir::Type, db: &dyn HirDatabase) -> Option<ReferenceConversionType>660 fn handle_copy(ty: &hir::Type, db: &dyn HirDatabase) -> Option<ReferenceConversionType> {
661 ty.is_copy(db).then_some(ReferenceConversionType::Copy)
662 }
663
handle_as_ref_str( ty: &hir::Type, db: &dyn HirDatabase, famous_defs: &FamousDefs<'_, '_>, ) -> Option<ReferenceConversionType>664 fn handle_as_ref_str(
665 ty: &hir::Type,
666 db: &dyn HirDatabase,
667 famous_defs: &FamousDefs<'_, '_>,
668 ) -> Option<ReferenceConversionType> {
669 let str_type = hir::BuiltinType::str().ty(db);
670
671 ty.impls_trait(db, famous_defs.core_convert_AsRef()?, &[str_type])
672 .then_some(ReferenceConversionType::AsRefStr)
673 }
674
handle_as_ref_slice( ty: &hir::Type, db: &dyn HirDatabase, famous_defs: &FamousDefs<'_, '_>, ) -> Option<ReferenceConversionType>675 fn handle_as_ref_slice(
676 ty: &hir::Type,
677 db: &dyn HirDatabase,
678 famous_defs: &FamousDefs<'_, '_>,
679 ) -> Option<ReferenceConversionType> {
680 let type_argument = ty.type_arguments().next()?;
681 let slice_type = hir::Type::new_slice(type_argument);
682
683 ty.impls_trait(db, famous_defs.core_convert_AsRef()?, &[slice_type])
684 .then_some(ReferenceConversionType::AsRefSlice)
685 }
686
handle_dereferenced( ty: &hir::Type, db: &dyn HirDatabase, famous_defs: &FamousDefs<'_, '_>, ) -> Option<ReferenceConversionType>687 fn handle_dereferenced(
688 ty: &hir::Type,
689 db: &dyn HirDatabase,
690 famous_defs: &FamousDefs<'_, '_>,
691 ) -> Option<ReferenceConversionType> {
692 let type_argument = ty.type_arguments().next()?;
693
694 ty.impls_trait(db, famous_defs.core_convert_AsRef()?, &[type_argument])
695 .then_some(ReferenceConversionType::Dereferenced)
696 }
697
handle_option_as_ref( ty: &hir::Type, db: &dyn HirDatabase, famous_defs: &FamousDefs<'_, '_>, ) -> Option<ReferenceConversionType>698 fn handle_option_as_ref(
699 ty: &hir::Type,
700 db: &dyn HirDatabase,
701 famous_defs: &FamousDefs<'_, '_>,
702 ) -> Option<ReferenceConversionType> {
703 if ty.as_adt() == famous_defs.core_option_Option()?.ty(db).as_adt() {
704 Some(ReferenceConversionType::Option)
705 } else {
706 None
707 }
708 }
709
handle_result_as_ref( ty: &hir::Type, db: &dyn HirDatabase, famous_defs: &FamousDefs<'_, '_>, ) -> Option<ReferenceConversionType>710 fn handle_result_as_ref(
711 ty: &hir::Type,
712 db: &dyn HirDatabase,
713 famous_defs: &FamousDefs<'_, '_>,
714 ) -> Option<ReferenceConversionType> {
715 if ty.as_adt() == famous_defs.core_result_Result()?.ty(db).as_adt() {
716 Some(ReferenceConversionType::Result)
717 } else {
718 None
719 }
720 }
721
get_methods(items: &ast::AssocItemList) -> Vec<ast::Fn>722 pub(crate) fn get_methods(items: &ast::AssocItemList) -> Vec<ast::Fn> {
723 items
724 .assoc_items()
725 .flat_map(|i| match i {
726 ast::AssocItem::Fn(f) => Some(f),
727 _ => None,
728 })
729 .filter(|f| f.name().is_some())
730 .collect()
731 }
732
733 /// Trim(remove leading and trailing whitespace) `initial_range` in `source_file`, return the trimmed range.
trimmed_text_range(source_file: &SourceFile, initial_range: TextRange) -> TextRange734 pub(crate) fn trimmed_text_range(source_file: &SourceFile, initial_range: TextRange) -> TextRange {
735 let mut trimmed_range = initial_range;
736 while source_file
737 .syntax()
738 .token_at_offset(trimmed_range.start())
739 .find_map(Whitespace::cast)
740 .is_some()
741 && trimmed_range.start() < trimmed_range.end()
742 {
743 let start = trimmed_range.start() + TextSize::from(1);
744 trimmed_range = TextRange::new(start, trimmed_range.end());
745 }
746 while source_file
747 .syntax()
748 .token_at_offset(trimmed_range.end())
749 .find_map(Whitespace::cast)
750 .is_some()
751 && trimmed_range.start() < trimmed_range.end()
752 {
753 let end = trimmed_range.end() - TextSize::from(1);
754 trimmed_range = TextRange::new(trimmed_range.start(), end);
755 }
756 trimmed_range
757 }
758
759 /// Convert a list of function params to a list of arguments that can be passed
760 /// into a function call.
convert_param_list_to_arg_list(list: ast::ParamList) -> ast::ArgList761 pub(crate) fn convert_param_list_to_arg_list(list: ast::ParamList) -> ast::ArgList {
762 let mut args = vec![];
763 for param in list.params() {
764 if let Some(ast::Pat::IdentPat(pat)) = param.pat() {
765 if let Some(name) = pat.name() {
766 let name = name.to_string();
767 let expr = make::expr_path(make::ext::ident_path(&name));
768 args.push(expr);
769 }
770 }
771 }
772 make::arg_list(args)
773 }
774
775 /// Calculate the number of hashes required for a raw string containing `s`
required_hashes(s: &str) -> usize776 pub(crate) fn required_hashes(s: &str) -> usize {
777 let mut res = 0usize;
778 for idx in s.match_indices('"').map(|(i, _)| i) {
779 let (_, sub) = s.split_at(idx + 1);
780 let n_hashes = sub.chars().take_while(|c| *c == '#').count();
781 res = res.max(n_hashes + 1)
782 }
783 res
784 }
785 #[test]
test_required_hashes()786 fn test_required_hashes() {
787 assert_eq!(0, required_hashes("abc"));
788 assert_eq!(0, required_hashes("###"));
789 assert_eq!(1, required_hashes("\""));
790 assert_eq!(2, required_hashes("\"#abc"));
791 assert_eq!(0, required_hashes("#abc"));
792 assert_eq!(3, required_hashes("#ab\"##c"));
793 assert_eq!(5, required_hashes("#ab\"##\"####c"));
794 }
795