1 //! Assorted functions shared by several assists.
3 pub(crate) mod suggest_name;
7 use ast::TypeBoundsOwner;
8 use hir::{Adt, HasSource, Semantics};
10 helpers::{FamousDefs, SnippetCap},
11 path_transform::PathTransform,
14 use itertools::Itertools;
17 ast::edit::AstNodeEdit,
20 ast::{self, edit, make, ArgListOwner, GenericParamsOwner},
21 ted, AstNode, Direction, SmolStr,
23 SyntaxNode, TextSize, T,
26 use crate::assist_context::{AssistBuilder, AssistContext};
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())
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),
41 non_trivial_children.next().is_some()
44 if let Some(expr) = block.tail_expr() {
45 if has_anything_else(expr.syntax()) {
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()) {
56 let expr = expr_stmt.expr()?;
57 match expr.syntax().kind() {
58 CONTINUE_EXPR | BREAK_EXPR | RETURN_EXPR => return Some(expr),
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.
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") {
82 #[derive(Copy, Clone, PartialEq)]
83 pub enum DefaultMethods {
88 pub fn filter_assoc_items(
90 items: &[hir::AssocItem],
91 default_methods: DefaultMethods,
92 ) -> Vec<ast::AssocItem> {
93 fn has_def_name(item: &ast::AssocItem) -> bool {
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,
105 // Note: This throws away items with no source.
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),
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)
120 _ => default_methods == DefaultMethods::No,
125 pub fn add_trait_assoc_items_to_impl(
126 sema: &hir::Semantics<ide_db::RootDatabase>,
127 items: Vec<ast::AssocItem>,
130 target_scope: hir::SemanticsScope,
131 ) -> (ast::Impl, ast::AssocItem) {
132 let source_scope = sema.scope_for_def(trait_);
134 let transform = PathTransform {
135 subst: (trait_, impl_.clone()),
136 source_scope: &source_scope,
137 target_scope: &target_scope,
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()
146 let res = impl_.clone_for_update();
148 let assoc_item_list = res.get_or_create_assoc_item_list();
149 let mut first_item = None;
151 first_item.get_or_insert_with(|| item.clone());
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())
158 ast::AssocItem::TypeAlias(type_alias) => {
159 if let Some(type_bound_list) = type_alias.type_bound_list() {
160 type_bound_list.remove()
166 assoc_item_list.add_item(item)
169 (res, first_item.unwrap())
172 #[derive(Clone, Copy, Debug)]
173 pub(crate) enum Cursor<'a> {
174 Replace(&'a SyntaxNode),
175 Before(&'a SyntaxNode),
178 impl<'a> Cursor<'a> {
179 fn node(self) -> &'a SyntaxNode {
181 Cursor::Replace(node) | Cursor::Before(node) => node,
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();
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),
198 let mut buf = node.to_string();
199 buf.replace_range(range, &tab_stop);
202 fn escape(buf: &mut String) {
203 stdx::replace(buf, '{', r"\{");
204 stdx::replace(buf, '}', r"\}");
205 stdx::replace(buf, '$', r"\$");
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())
216 pub(crate) fn invert_boolean_expression(
217 sema: &Semantics<RootDatabase>,
220 if let Some(expr) = invert_special_case(sema, &expr) {
223 make::expr_prefix(T![!], expr)
226 fn invert_special_case(sema: &Semantics<RootDatabase>, expr: &ast::Expr) -> Option<ast::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())
235 ast::BinOp::LesserEqualTest if bin_impls_ord(sema, bin) => {
236 bin.replace_op(T![>]).map(|it| it.into())
238 ast::BinOp::GreaterTest if bin_impls_ord(sema, bin) => {
239 bin.replace_op(T![<=]).map(|it| it.into())
241 ast::BinOp::GreaterEqualTest if bin_impls_ord(sema, bin) => {
242 bin.replace_op(T![<]).map(|it| it.into())
244 // Parenthesize other expressions before prefixing `!`
245 _ => Some(make::expr_prefix(T![!], make::expr_paren(expr.clone()))),
247 ast::Expr::MethodCallExpr(mce) => {
248 let receiver = mce.receiver()?;
249 let method = mce.name_ref()?;
250 let arg_list = mce.arg_list()?;
252 let method = match method.text().as_str() {
253 "is_some" => "is_none",
254 "is_none" => "is_some",
259 Some(make::expr_method_call(receiver, method, arg_list))
261 ast::Expr::PrefixExpr(pe) if pe.op_kind()? == ast::PrefixOp::Not => {
262 if let ast::Expr::ParenExpr(parexpr) = pe.expr()? {
269 // ast::Expr::Literal(true | false )
274 fn bin_impls_ord(sema: &Semantics<RootDatabase>, bin: &ast::BinExpr) -> bool {
276 bin.lhs().and_then(|lhs| sema.type_of_expr(&lhs)),
277 bin.rhs().and_then(|rhs| sema.type_of_expr(&rhs)),
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, &[]))
290 pub(crate) fn next_prev() -> impl Iterator<Item = Direction> {
291 [Direction::Next, Direction::Prev].iter().copied()
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());
297 let pat_head = match pat {
298 ast::Pat::IdentPat(bind_pat) => {
299 if let Some(p) = bind_pat.pat() {
302 return pat.syntax().text() == var.syntax().text();
305 pat => first_node_text(pat),
308 let var_head = first_node_text(var);
313 // Uses a syntax-driven approach to find any impl blocks for the struct that
314 // exist within the module/file
316 // Returns `None` if we've found an existing fn
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(
325 ) -> Option<Option<ast::Impl>> {
327 let module = strukt.syntax().ancestors().find(|node| {
328 ast::Module::can_cast(node.kind()) || ast::SourceFile::can_cast(node.kind())
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)?),
337 let block = module.descendants().filter_map(ast::Impl::cast).find_map(|impl_blk| {
338 let blk = ctx.sema.to_def(&impl_blk)?;
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>`
344 let same_ty = match blk.self_ty(db).as_adt() {
345 Some(def) => def == struct_def,
348 let not_trait_impl = blk.trait_(db).is_none();
350 if !(same_ty && not_trait_impl) {
357 if let Some(ref impl_blk) = block {
358 if has_fn(impl_blk, name) {
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) {
382 /// Find the start of the `impl` block for the given `ast::Impl`.
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> {
387 let start = impl_def.assoc_item_list().and_then(|it| it.l_curly_token())?.text_range().end();
391 /// Find the end of the `impl` block for the given `ast::Impl`.
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> {
398 .and_then(|it| it.r_curly_token())?
399 .prev_sibling_or_token()?
405 // Generates the surrounding `impl Type { <code> }` including type and lifetime
407 pub(crate) fn generate_impl_text(adt: &ast::Adt, code: &str) -> String {
408 generate_impl_text_inner(adt, None, code)
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)
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");
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());
432 if let Some(it) = type_param.colon_token() {
433 format_to!(buf, "{} ", it);
435 if let Some(it) = type_param.type_bound_list() {
436 format_to!(buf, "{}", it.syntax());
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);
445 if let Some(trait_text) = trait_text {
446 buf.push_str(trait_text);
447 buf.push_str(" for ");
449 buf.push_str(&adt.name().unwrap().text());
450 if let Some(generic_params) = generic_params {
451 let lifetime_params = generic_params
453 .filter_map(|it| it.lifetime())
454 .map(|it| SmolStr::from(it.text()));
455 let type_params = generic_params
457 .filter_map(|it| it.name())
458 .map(|it| SmolStr::from(it.text()));
459 let const_params = generic_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(", "))
466 match adt.where_clause() {
467 Some(where_clause) => {
468 format_to!(buf, "\n{}\n{{\n{}\n}}", where_clause, code);
471 format_to!(buf, " {{\n{}\n}}", code);
478 pub(crate) fn add_method_to_adt(
479 builder: &mut AssistBuilder,
481 impl_def: Option<ast::Impl>,
484 let mut buf = String::with_capacity(method.len() + 2);
485 if impl_def.is_some() {
488 buf.push_str(method);
490 let start_offset = impl_def
491 .and_then(|impl_def| find_impl_block_end(impl_def, &mut buf))
493 buf = generate_impl_text(adt, &buf);
494 adt.syntax().text_range().end()
497 builder.insert(start_offset, buf);