implement feedback from review

This commit is contained in:
Yoshua Wuyts 2021-08-10 12:21:48 +02:00
parent d6b788a9ee
commit 326890753c
5 changed files with 19 additions and 14 deletions

View File

@ -5,8 +5,8 @@ use syntax::ast::{self, make, AstNode};
use crate::{
assist_context::{AssistContext, Assists},
utils::{
add_trait_assoc_items_to_impl, filter_assoc_items, gen_trait_body, render_snippet, Cursor,
DefaultMethods,
add_trait_assoc_items_to_impl, filter_assoc_items, gen_trait_fn_body, render_snippet,
Cursor, DefaultMethods,
},
AssistId, AssistKind,
};
@ -156,10 +156,10 @@ fn try_gen_trait_body(
trait_: &hir::Trait,
impl_def: &ast::Impl,
) -> Option<()> {
let trait_path = make::path_from_text(&trait_.name(ctx.db()).to_string());
let trait_path = make::ext::ident_path(&trait_.name(ctx.db()).to_string());
let hir_ty = ctx.sema.resolve_type(&impl_def.self_ty()?)?;
let adt = hir_ty.as_adt()?.source(ctx.db())?;
gen_trait_body(func, &trait_path, &adt.value)
gen_trait_fn_body(func, &trait_path, &adt.value)
}
#[cfg(test)]

View File

@ -7,12 +7,11 @@ use syntax::{
SyntaxKind::{IDENT, WHITESPACE},
};
use crate::utils::gen_trait_body;
use crate::{
assist_context::{AssistBuilder, AssistContext, Assists},
utils::{
add_trait_assoc_items_to_impl, filter_assoc_items, generate_trait_impl_text,
render_snippet, Cursor, DefaultMethods,
add_trait_assoc_items_to_impl, filter_assoc_items, gen_trait_fn_body,
generate_trait_impl_text, render_snippet, Cursor, DefaultMethods,
},
AssistId, AssistKind,
};
@ -168,7 +167,7 @@ fn impl_def_from_trait(
// Generate a default `impl` function body for the derived trait.
if let ast::AssocItem::Fn(ref func) = first_assoc_item {
let _ = gen_trait_body(func, trait_path, adt);
let _ = gen_trait_fn_body(func, trait_path, adt);
};
Some((impl_def, first_assoc_item))

View File

@ -1,7 +1,7 @@
//! Assorted functions shared by several assists.
pub(crate) mod suggest_name;
mod gen_trait_body;
mod gen_trait_fn_body;
use std::ops;
@ -26,7 +26,7 @@ use syntax::{
use crate::assist_context::{AssistBuilder, AssistContext};
pub(crate) use gen_trait_body::gen_trait_body;
pub(crate) use gen_trait_fn_body::gen_trait_fn_body;
pub(crate) fn unwrap_trivial_block(block: ast::BlockExpr) -> ast::Expr {
extract_trivial_expression(&block)

View File

@ -1,14 +1,20 @@
//! This module contains functions to generate default trait impl function bodies where possible.
use syntax::ast::{self, edit::AstNodeEdit, make, AstNode, NameOwner};
use syntax::ted;
use syntax::{
ast::{self, edit::AstNodeEdit, make, AstNode, NameOwner},
ted,
};
/// Generate custom trait bodies where possible.
///
/// Returns `Option` so that we can use `?` rather than `if let Some`. Returning
/// `None` means that generating a custom trait body failed, and the body will remain
/// as `todo!` instead.
pub(crate) fn gen_trait_body(func: &ast::Fn, trait_path: &ast::Path, adt: &ast::Adt) -> Option<()> {
pub(crate) fn gen_trait_fn_body(
func: &ast::Fn,
trait_path: &ast::Path,
adt: &ast::Adt,
) -> Option<()> {
match trait_path.segment()?.name_ref()?.text().as_str() {
"Debug" => gen_debug_impl(adt, func),
"Default" => gen_default_impl(adt, func),

View File

@ -280,7 +280,7 @@ fn check_todo(path: &Path, text: &str) {
"ast/make.rs",
// The documentation in string literals may contain anything for its own purposes
"ide_db/src/helpers/generated_lints.rs",
"ide_assists/src/utils/gen_trait_body.rs",
"ide_assists/src/utils/gen_trait_fn_body.rs",
"ide_assists/src/tests/generated.rs",
];
if need_todo.iter().any(|p| path.ends_with(p)) {