<commit_before>use hir::{HasSource, HirDisplay, InFile, Module, TypeInfo};
use ide_db::{base_db::FileId, helpers::SnippetCap};
use rustc_hash::{FxHashMap, FxHashSet};
use stdx::to_lower_snake_case;
use syntax::{
    ast::{
        self,
        edit::{AstNodeEdit, IndentLevel},
        make, ArgList, ArgListOwner, AstNode, ModuleItemOwner,
    },
    SyntaxKind, SyntaxNode, TextSize,
};

use crate::{
    utils::useless_type_special_case,
    utils::{find_struct_impl, render_snippet, Cursor},
    AssistContext, AssistId, AssistKind, Assists,
};

enum FuncExpr<'a> {
    Func(&'a ast::CallExpr),
    Method(&'a ast::MethodCallExpr),
}

impl<'a> FuncExpr<'a> {
    fn arg_list(&self) -> Option<ArgList> {
        match *self {
            FuncExpr::Func(fn_call) => fn_call.arg_list(),
            FuncExpr::Method(m_call) => m_call.arg_list(),
        }
    }

    fn syntax(&self) -> &SyntaxNode {
        match *self {
            FuncExpr::Func(fn_call) => fn_call.syntax(),
            FuncExpr::Method(m_call) => m_call.syntax(),
        }
    }
}

// Assist: generate_function
//
// Adds a stub function with a signature matching the function under the cursor.
//
// ```
// struct Baz;
// fn baz() -> Baz { Baz }
// fn foo() {
//     bar$0("", baz());
// }
//
// ```
// ->
// ```
// struct Baz;
// fn baz() -> Baz { Baz }
// fn foo() {
//     bar("", baz());
// }
//
// fn bar(arg: &str, baz: Baz) ${0:-> ()} {
//     todo!()
// }
//
// ```
pub(crate) fn generate_function(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
    let path_expr: ast::PathExpr = ctx.find_node_at_offset()?;
    let call = path_expr.syntax().parent().and_then(ast::CallExpr::cast)?;

    let path = path_expr.path()?;
    if ctx.sema.resolve_path(&path).is_some() {
        // The function call already resolves, no need to add a function
        return None;
    }

    let target_module = match path.qualifier() {
        Some(qualifier) => match ctx.sema.resolve_path(&qualifier) {
            Some(hir::PathResolution::Def(hir::ModuleDef::Module(module))) => Some(module),
            _ => return None,
        },
        None => None,
    };

    let function_builder = FunctionBuilder::from_call(ctx, &call, &path, target_module)?;
    let target = call.syntax().text_range();

    acc.add(
        AssistId("generate_function", AssistKind::Generate),
        format!("Generate `{}` function", function_builder.fn_name),
        target,
        |builder| {
            let function_template = function_builder.render();
            builder.edit_file(function_template.file);
            let new_fn = function_template.to_string(ctx.config.snippet_cap);
            match ctx.config.snippet_cap {
                Some(cap) => builder.insert_snippet(cap, function_template.insert_offset, new_fn),
                None => builder.insert(function_template.insert_offset, new_fn),
            }
        },
    )
}

pub(crate) fn generate_method(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
    let fn_name: ast::NameRef = ctx.find_node_at_offset()?;
    let call: ast::MethodCallExpr = ctx.find_node_at_offset()?;
    let ty = ctx.sema.type_of_expr(&call.receiver()?)?.original().strip_references().as_adt()?;

    let (impl_, file) = match ty {
        hir::Adt::Struct(strukt) => get_impl(strukt.source(ctx.sema.db)?.syntax(), &fn_name, ctx),
        hir::Adt::Enum(en) => get_impl(en.source(ctx.sema.db)?.syntax(), &fn_name, ctx),
        hir::Adt::Union(union) => get_impl(union.source(ctx.sema.db)?.syntax(), &fn_name, ctx),
    }?;

    let function_builder = FunctionBuilder::from_method_call(
        ctx,
        &call,
        &fn_name,
        &impl_,
        file,
        ty.module(ctx.sema.db),
    )?;
    let target = call.syntax().text_range();

    acc.add(
        AssistId("generate_method", AssistKind::Generate),
        format!("Generate `{}` method", function_builder.fn_name),
        target,
        |builder| {
            let function_template = function_builder.render();
            builder.edit_file(function_template.file);
            let mut new_fn = function_template.to_string(ctx.config.snippet_cap);
            if impl_.is_none() {
                new_fn = format!("\nimpl {} {{\n   {}\n}}", ty.name(ctx.sema.db), new_fn,);
            }
            match ctx.config.snippet_cap {
                Some(cap) => builder.insert_snippet(cap, function_template.insert_offset, new_fn),
                None => builder.insert(function_template.insert_offset, new_fn),
            }
        },
    )
}

fn get_impl(
    adt: InFile<&SyntaxNode>,
    fn_name: &ast::NameRef,
    ctx: &AssistContext,
) -> Option<(Option<ast::Impl>, FileId)> {
    let file = adt.file_id.original_file(ctx.sema.db);
    let adt = adt.value;
    let adt = ast::Adt::cast(adt.clone())?;
    let r = find_struct_impl(ctx, &adt, fn_name.text().as_str())?;
    Some((r, file))
}

struct FunctionTemplate {
    insert_offset: TextSize,
    leading_ws: String,
    fn_def: ast::Fn,
    ret_type: ast::RetType,
    should_render_snippet: bool,
    trailing_ws: String,
    file: FileId,
}

impl FunctionTemplate {
    fn to_string(&self, cap: Option<SnippetCap>) -> String {
        let f = match (cap, self.should_render_snippet) {
            (Some(cap), true) => {
                render_snippet(cap, self.fn_def.syntax(), Cursor::Replace(self.ret_type.syntax()))
            }
            _ => self.fn_def.to_string(),
        };
        format!("{}{}{}", self.leading_ws, f, self.trailing_ws)
    }
}

struct FunctionBuilder {
    target: GeneratedFunctionTarget,
    fn_name: ast::Name,
    type_params: Option<ast::GenericParamList>,
    params: ast::ParamList,
    ret_type: ast::RetType,
    should_render_snippet: bool,
    file: FileId,
    needs_pub: bool,
    is_async: bool,
}

impl FunctionBuilder {
    /// Prepares a generated function that matches `call`.
    /// The function is generated in `target_module` or next to `call`
    fn from_call(
        ctx: &AssistContext,
        call: &ast::CallExpr,
        path: &ast::Path,
        target_module: Option<hir::Module>,
    ) -> Option<Self> {
        let mut file = ctx.frange.file_id;
        let target = match &target_module {
            Some(target_module) => {
                let module_source = target_module.definition_source(ctx.db());
                let (in_file, target) = next_space_for_fn_in_module(ctx.sema.db, &module_source)?;
                file = in_file;
                target
            }
            None => next_space_for_fn_after_call_site(FuncExpr::Func(call))?,
        };
        let needs_pub = target_module.is_some();
        let target_module = target_module.or_else(|| ctx.sema.scope(target.syntax()).module())?;
        let fn_name = fn_name(path)?;
        let (type_params, params) = fn_args(ctx, target_module, FuncExpr::Func(call))?;

        let await_expr = call.syntax().parent().and_then(ast::AwaitExpr::cast);
        let is_async = await_expr.is_some();

        // should_render_snippet intends to express a rough level of confidence about
        // the correctness of the return type.
        //
        // If we are able to infer some return type, and that return type is not unit, we
        // don't want to render the snippet. The assumption here is in this situation the
        // return type is just as likely to be correct as any other part of the generated
        // function.
        //
        // In the case where the return type is inferred as unit it is likely that the
        // user does in fact intend for this generated function to return some non unit
        // type, but that the current state of their code doesn't allow that return type
        // to be accurately inferred.
        let (ret_ty, should_render_snippet) = {
            match ctx.sema.type_of_expr(&ast::Expr::CallExpr(call.clone())).map(TypeInfo::original)
            {
                Some(ty) if ty.is_unknown() || ty.is_unit() => (make::ty_unit(), true),
                Some(ty) => {
                    let rendered = ty.display_source_code(ctx.db(), target_module.into());
                    match rendered {
                        Ok(rendered) => (make::ty(&rendered), false),
                        Err(_) => (make::ty_unit(), true),
                    }
                }
                None => (make::ty_unit(), true),
            }
        };
        let ret_type = make::ret_type(ret_ty);

        Some(Self {
            target,
            fn_name,
            type_params,
            params,
            ret_type,
            should_render_snippet,
            file,
            needs_pub,
            is_async,
        })
    }

    fn from_method_call(
        ctx: &AssistContext,
        call: &ast::MethodCallExpr,
        name: &ast::NameRef,
        impl_: &Option<ast::Impl>,
        file: FileId,
        target_module: Module,
    ) -> Option<Self> {
        // let mut file = ctx.frange.file_id;
        // let target_module = ctx.sema.scope(call.syntax()).module()?;
        let target = match impl_ {
            Some(impl_) => next_space_for_fn_in_impl(&impl_)?,
            None => {
                next_space_for_fn_in_module(
                    ctx.sema.db,
                    &target_module.definition_source(ctx.sema.db),
                )?
                .1
            }
        };

        let needs_pub = false;
        let fn_name = make::name(&name.text());
        let (type_params, params) = fn_args(ctx, target_module, FuncExpr::Method(call))?;

        let await_expr = call.syntax().parent().and_then(ast::AwaitExpr::cast);
        let is_async = await_expr.is_some();

        // should_render_snippet intends to express a rough level of confidence about
        // the correctness of the return type.
        //
        // If we are able to infer some return type, and that return type is not unit, we
        // don't want to render the snippet. The assumption here is in this situation the
        // return type is just as likely to be correct as any other part of the generated
        // function.
        //
        // In the case where the return type is inferred as unit it is likely that the
        // user does in fact intend for this generated function to return some non unit
        // type, but that the current state of their code doesn't allow that return type
        // to be accurately inferred.
        let (ret_ty, should_render_snippet) = {
            match ctx
                .sema
                .type_of_expr(&ast::Expr::MethodCallExpr(call.clone()))
                .map(TypeInfo::original)
            {
                Some(ty) if ty.is_unknown() || ty.is_unit() => (make::ty_unit(), true),
                Some(ty) => {
                    let rendered = ty.display_source_code(ctx.db(), target_module.into());
                    match rendered {
                        Ok(rendered) => (make::ty(&rendered), false),
                        Err(_) => (make::ty_unit(), true),
                    }
                }
                None => (make::ty_unit(), true),
            }
        };
        let ret_type = make::ret_type(ret_ty);

        Some(Self {
            target,
            fn_name,
            type_params,
            params,
            ret_type,
            should_render_snippet,
            file,
            needs_pub,
            is_async,
        })
    }

    fn render(self) -> FunctionTemplate {
        let placeholder_expr = make::ext::expr_todo();
        let fn_body = make::block_expr(vec![], Some(placeholder_expr));
        let visibility = if self.needs_pub { Some(make::visibility_pub_crate()) } else { None };
        let mut fn_def = make::fn_(
            visibility,
            self.fn_name,
            self.type_params,
            self.params,
            fn_body,
            Some(self.ret_type),
            self.is_async,
        );
        let leading_ws;
        let trailing_ws;

        let insert_offset = match self.target {
            GeneratedFunctionTarget::BehindItem(it) => {
                let indent = IndentLevel::from_node(&it);
                leading_ws = format!("\n\n{}", indent);
                fn_def = fn_def.indent(indent);
                trailing_ws = String::new();
                it.text_range().end()
            }
            GeneratedFunctionTarget::InEmptyItemList(it) => {
                let indent = IndentLevel::from_node(&it);
                leading_ws = format!("\n{}", indent + 1);
                fn_def = fn_def.indent(indent + 1);
                trailing_ws = format!("\n{}", indent);
                it.text_range().start() + TextSize::of('{')
            }
        };

        FunctionTemplate {
            insert_offset,
            leading_ws,
            ret_type: fn_def.ret_type().unwrap(),
            should_render_snippet: self.should_render_snippet,
            fn_def,
            trailing_ws,
            file: self.file,
        }
    }
}

enum GeneratedFunctionTarget {
    BehindItem(SyntaxNode),
    InEmptyItemList(SyntaxNode),
}

impl GeneratedFunctionTarget {
    fn syntax(&self) -> &SyntaxNode {
        match self {
            GeneratedFunctionTarget::BehindItem(it) => it,
            GeneratedFunctionTarget::InEmptyItemList(it) => it,
        }
    }
}

fn fn_name(call: &ast::Path) -> Option<ast::Name> {
    let name = call.segment()?.syntax().to_string();
    Some(make::name(&name))
}

/// Computes the type variables and arguments required for the generated function
fn fn_args(
    ctx: &AssistContext,
    target_module: hir::Module,
    call: FuncExpr,
) -> Option<(Option<ast::GenericParamList>, ast::ParamList)> {
    let mut arg_names = Vec::new();
    let mut arg_types = Vec::new();
    for arg in call.arg_list()?.args() {
        arg_names.push(match fn_arg_name(&arg) {
            Some(name) => name,
            None => String::from("arg"),
        });
        arg_types.push(match fn_arg_type(ctx, target_module, &arg) {
            Some(ty) => {
                if ty.len() > 0 && ty.starts_with('&') {
                    if let Some((new_ty, _)) = useless_type_special_case("", &ty[1..].to_owned()) {
                        new_ty
                    } else {
                        ty
                    }
                } else {
                    ty
                }
            }
            None => String::from("()"),
        });
    }
    deduplicate_arg_names(&mut arg_names);
    let params = arg_names.into_iter().zip(arg_types).map(|(name, ty)| {
        make::param(make::ext::simple_ident_pat(make::name(&name)).into(), make::ty(&ty))
    });

    Some((
        None,
        make::param_list(
            match call {
                FuncExpr::Func(_) => None,
                FuncExpr::Method(_) => Some(make::self_param()),
            },
            params,
        ),
    ))
}

/// Makes duplicate argument names unique by appending incrementing numbers.
///
/// ```
/// let mut names: Vec<String> =
///     vec!["foo".into(), "foo".into(), "bar".into(), "baz".into(), "bar".into()];
/// deduplicate_arg_names(&mut names);
/// let expected: Vec<String> =
///     vec!["foo_1".into(), "foo_2".into(), "bar_1".into(), "baz".into(), "bar_2".into()];
/// assert_eq!(names, expected);
/// ```
fn deduplicate_arg_names(arg_names: &mut Vec<String>) {
    let arg_name_counts = arg_names.iter().fold(FxHashMap::default(), |mut m, name| {
        *m.entry(name).or_insert(0) += 1;
        m
    });
    let duplicate_arg_names: FxHashSet<String> = arg_name_counts
        .into_iter()
        .filter(|(_, count)| *count >= 2)
        .map(|(name, _)| name.clone())
        .collect();

    let mut counter_per_name = FxHashMap::default();
    for arg_name in arg_names.iter_mut() {
        if duplicate_arg_names.contains(arg_name) {
            let counter = counter_per_name.entry(arg_name.clone()).or_insert(1);
            arg_name.push('_');
            arg_name.push_str(&counter.to_string());
            *counter += 1;
        }
    }
}

fn fn_arg_name(fn_arg: &ast::Expr) -> Option<String> {
    match fn_arg {
        ast::Expr::CastExpr(cast_expr) => fn_arg_name(&cast_expr.expr()?),
        _ => {
            let s = fn_arg
                .syntax()
                .descendants()
                .filter(|d| ast::NameRef::can_cast(d.kind()))
                .last()?
                .to_string();
            Some(to_lower_snake_case(&s))
        }
    }
}

fn fn_arg_type(
    ctx: &AssistContext,
    target_module: hir::Module,
    fn_arg: &ast::Expr,
) -> Option<String> {
    let ty = ctx.sema.type_of_expr(fn_arg)?.adjusted();
    if ty.is_unknown() {
        return None;
    }

    if let Ok(rendered) = ty.display_source_code(ctx.db(), target_module.into()) {
        Some(rendered)
    } else {
        None
    }
}

/// Returns the position inside the current mod or file
/// directly after the current block
/// We want to write the generated function directly after
/// fns, impls or macro calls, but inside mods
fn next_space_for_fn_after_call_site(expr: FuncExpr) -> Option<GeneratedFunctionTarget> {
    let mut ancestors = expr.syntax().ancestors().peekable();
    let mut last_ancestor: Option<SyntaxNode> = None;
    while let Some(next_ancestor) = ancestors.next() {
        match next_ancestor.kind() {
            SyntaxKind::SOURCE_FILE => {
                break;
            }
            SyntaxKind::ITEM_LIST => {
                if ancestors.peek().map(|a| a.kind()) == Some(SyntaxKind::MODULE) {
                    break;
                }
            }
            _ => {}
        }
        last_ancestor = Some(next_ancestor);
    }
    last_ancestor.map(GeneratedFunctionTarget::BehindItem)
}

fn next_space_for_fn_in_module(
    db: &dyn hir::db::AstDatabase,
    module_source: &hir::InFile<hir::ModuleSource>,
) -> Option<(FileId, GeneratedFunctionTarget)> {
    let file = module_source.file_id.original_file(db);
    let assist_item = match &module_source.value {
        hir::ModuleSource::SourceFile(it) => {
            if let Some(last_item) = it.items().last() {
                GeneratedFunctionTarget::BehindItem(last_item.syntax().clone())
            } else {
                GeneratedFunctionTarget::BehindItem(it.syntax().clone())
            }
        }
        hir::ModuleSource::Module(it) => {
            if let Some(last_item) = it.item_list().and_then(|it| it.items().last()) {
                GeneratedFunctionTarget::BehindItem(last_item.syntax().clone())
            } else {
                GeneratedFunctionTarget::InEmptyItemList(it.item_list()?.syntax().clone())
            }
        }
        hir::ModuleSource::BlockExpr(it) => {
            if let Some(last_item) =
                it.statements().take_while(|stmt| matches!(stmt, ast::Stmt::Item(_))).last()
            {
                GeneratedFunctionTarget::BehindItem(last_item.syntax().clone())
            } else {
                GeneratedFunctionTarget::InEmptyItemList(it.syntax().clone())
            }
        }
    };
    Some((file, assist_item))
}

fn next_space_for_fn_in_impl(impl_: &ast::Impl) -> Option<GeneratedFunctionTarget> {
    if let Some(last_item) = impl_.assoc_item_list().and_then(|it| it.assoc_items().last()) {
        Some(GeneratedFunctionTarget::BehindItem(last_item.syntax().clone()))
    } else {
        Some(GeneratedFunctionTarget::InEmptyItemList(impl_.assoc_item_list()?.syntax().clone()))
    }
}

#[cfg(test)]
mod tests {
    use crate::tests::{check_assist, check_assist_not_applicable};

    use super::*;
<commit_msg>generate method adds pub keyword<commit_after>use hir::{HasSource, HirDisplay, InFile, Module, TypeInfo};
use ide_db::{base_db::FileId, helpers::SnippetCap};
use rustc_hash::{FxHashMap, FxHashSet};
use stdx::to_lower_snake_case;
use syntax::{
    ast::{
        self,
        edit::{AstNodeEdit, IndentLevel},
        make, ArgList, ArgListOwner, AstNode, ModuleItemOwner,
    },
    SyntaxKind, SyntaxNode, TextSize,
};

use crate::{
    utils::useless_type_special_case,
    utils::{find_struct_impl, render_snippet, Cursor},
    AssistContext, AssistId, AssistKind, Assists,
};

enum FuncExpr<'a> {
    Func(&'a ast::CallExpr),
    Method(&'a ast::MethodCallExpr),
}

impl<'a> FuncExpr<'a> {
    fn arg_list(&self) -> Option<ArgList> {
        match *self {
            FuncExpr::Func(fn_call) => fn_call.arg_list(),
            FuncExpr::Method(m_call) => m_call.arg_list(),
        }
    }

    fn syntax(&self) -> &SyntaxNode {
        match *self {
            FuncExpr::Func(fn_call) => fn_call.syntax(),
            FuncExpr::Method(m_call) => m_call.syntax(),
        }
    }
}

// Assist: generate_function
//
// Adds a stub function with a signature matching the function under the cursor.
//
// ```
// struct Baz;
// fn baz() -> Baz { Baz }
// fn foo() {
//     bar$0("", baz());
// }
//
// ```
// ->
// ```
// struct Baz;
// fn baz() -> Baz { Baz }
// fn foo() {
//     bar("", baz());
// }
//
// fn bar(arg: &str, baz: Baz) ${0:-> ()} {
//     todo!()
// }
//
// ```
pub(crate) fn generate_function(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
    let path_expr: ast::PathExpr = ctx.find_node_at_offset()?;
    let call = path_expr.syntax().parent().and_then(ast::CallExpr::cast)?;

    let path = path_expr.path()?;
    if ctx.sema.resolve_path(&path).is_some() {
        // The function call already resolves, no need to add a function
        return None;
    }

    let target_module = match path.qualifier() {
        Some(qualifier) => match ctx.sema.resolve_path(&qualifier) {
            Some(hir::PathResolution::Def(hir::ModuleDef::Module(module))) => Some(module),
            _ => return None,
        },
        None => None,
    };

    let function_builder = FunctionBuilder::from_call(ctx, &call, &path, target_module)?;
    let target = call.syntax().text_range();

    acc.add(
        AssistId("generate_function", AssistKind::Generate),
        format!("Generate `{}` function", function_builder.fn_name),
        target,
        |builder| {
            let function_template = function_builder.render();
            builder.edit_file(function_template.file);
            let new_fn = function_template.to_string(ctx.config.snippet_cap);
            match ctx.config.snippet_cap {
                Some(cap) => builder.insert_snippet(cap, function_template.insert_offset, new_fn),
                None => builder.insert(function_template.insert_offset, new_fn),
            }
        },
    )
}

pub(crate) fn generate_method(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
    let fn_name: ast::NameRef = ctx.find_node_at_offset()?;
    let call: ast::MethodCallExpr = ctx.find_node_at_offset()?;
    let ty = ctx.sema.type_of_expr(&call.receiver()?)?.original().strip_references().as_adt()?;

    let current_module =
        ctx.sema.scope(ctx.find_node_at_offset::<ast::MethodCallExpr>()?.syntax()).module()?;
    let target_module = ty.module(ctx.sema.db);

    if current_module.krate() != target_module.krate() {
        return None;
    }

    let (impl_, file) = match ty {
        hir::Adt::Struct(strukt) => get_impl(strukt.source(ctx.sema.db)?.syntax(), &fn_name, ctx),
        hir::Adt::Enum(en) => get_impl(en.source(ctx.sema.db)?.syntax(), &fn_name, ctx),
        hir::Adt::Union(union) => get_impl(union.source(ctx.sema.db)?.syntax(), &fn_name, ctx),
    }?;

    let function_builder = FunctionBuilder::from_method_call(
        ctx,
        &call,
        &fn_name,
        &impl_,
        file,
        target_module,
        current_module,
    )?;
    let target = call.syntax().text_range();

    acc.add(
        AssistId("generate_method", AssistKind::Generate),
        format!("Generate `{}` method", function_builder.fn_name),
        target,
        |builder| {
            let function_template = function_builder.render();
            builder.edit_file(function_template.file);
            let mut new_fn = function_template.to_string(ctx.config.snippet_cap);
            if impl_.is_none() {
                new_fn = format!("\nimpl {} {{\n   {}\n}}", ty.name(ctx.sema.db), new_fn,);
            }
            match ctx.config.snippet_cap {
                Some(cap) => builder.insert_snippet(cap, function_template.insert_offset, new_fn),
                None => builder.insert(function_template.insert_offset, new_fn),
            }
        },
    )
}

fn get_impl(
    adt: InFile<&SyntaxNode>,
    fn_name: &ast::NameRef,
    ctx: &AssistContext,
) -> Option<(Option<ast::Impl>, FileId)> {
    let file = adt.file_id.original_file(ctx.sema.db);
    let adt = adt.value;
    let adt = ast::Adt::cast(adt.clone())?;
    let r = find_struct_impl(ctx, &adt, fn_name.text().as_str())?;
    Some((r, file))
}

struct FunctionTemplate {
    insert_offset: TextSize,
    leading_ws: String,
    fn_def: ast::Fn,
    ret_type: ast::RetType,
    should_render_snippet: bool,
    trailing_ws: String,
    file: FileId,
}

impl FunctionTemplate {
    fn to_string(&self, cap: Option<SnippetCap>) -> String {
        let f = match (cap, self.should_render_snippet) {
            (Some(cap), true) => {
                render_snippet(cap, self.fn_def.syntax(), Cursor::Replace(self.ret_type.syntax()))
            }
            _ => self.fn_def.to_string(),
        };
        format!("{}{}{}", self.leading_ws, f, self.trailing_ws)
    }
}

struct FunctionBuilder {
    target: GeneratedFunctionTarget,
    fn_name: ast::Name,
    type_params: Option<ast::GenericParamList>,
    params: ast::ParamList,
    ret_type: ast::RetType,
    should_render_snippet: bool,
    file: FileId,
    needs_pub: bool,
    is_async: bool,
}

impl FunctionBuilder {
    /// Prepares a generated function that matches `call`.
    /// The function is generated in `target_module` or next to `call`
    fn from_call(
        ctx: &AssistContext,
        call: &ast::CallExpr,
        path: &ast::Path,
        target_module: Option<hir::Module>,
    ) -> Option<Self> {
        let mut file = ctx.frange.file_id;
        let target = match &target_module {
            Some(target_module) => {
                let module_source = target_module.definition_source(ctx.db());
                let (in_file, target) = next_space_for_fn_in_module(ctx.sema.db, &module_source)?;
                file = in_file;
                target
            }
            None => next_space_for_fn_after_call_site(FuncExpr::Func(call))?,
        };
        let needs_pub = target_module.is_some();
        let target_module = target_module.or_else(|| ctx.sema.scope(target.syntax()).module())?;
        let fn_name = fn_name(path)?;
        let (type_params, params) = fn_args(ctx, target_module, FuncExpr::Func(call))?;

        let await_expr = call.syntax().parent().and_then(ast::AwaitExpr::cast);
        let is_async = await_expr.is_some();

        // should_render_snippet intends to express a rough level of confidence about
        // the correctness of the return type.
        //
        // If we are able to infer some return type, and that return type is not unit, we
        // don't want to render the snippet. The assumption here is in this situation the
        // return type is just as likely to be correct as any other part of the generated
        // function.
        //
        // In the case where the return type is inferred as unit it is likely that the
        // user does in fact intend for this generated function to return some non unit
        // type, but that the current state of their code doesn't allow that return type
        // to be accurately inferred.
        let (ret_ty, should_render_snippet) = {
            match ctx.sema.type_of_expr(&ast::Expr::CallExpr(call.clone())).map(TypeInfo::original)
            {
                Some(ty) if ty.is_unknown() || ty.is_unit() => (make::ty_unit(), true),
                Some(ty) => {
                    let rendered = ty.display_source_code(ctx.db(), target_module.into());
                    match rendered {
                        Ok(rendered) => (make::ty(&rendered), false),
                        Err(_) => (make::ty_unit(), true),
                    }
                }
                None => (make::ty_unit(), true),
            }
        };
        let ret_type = make::ret_type(ret_ty);

        Some(Self {
            target,
            fn_name,
            type_params,
            params,
            ret_type,
            should_render_snippet,
            file,
            needs_pub,
            is_async,
        })
    }

    fn from_method_call(
        ctx: &AssistContext,
        call: &ast::MethodCallExpr,
        name: &ast::NameRef,
        impl_: &Option<ast::Impl>,
        file: FileId,
        target_module: Module,
        current_module: Module,
    ) -> Option<Self> {
        // let mut file = ctx.frange.file_id;
        // let target_module = ctx.sema.scope(call.syntax()).module()?;
        let target = match impl_ {
            Some(impl_) => next_space_for_fn_in_impl(&impl_)?,
            None => {
                next_space_for_fn_in_module(
                    ctx.sema.db,
                    &target_module.definition_source(ctx.sema.db),
                )?
                .1
            }
        };
        let needs_pub = !module_is_descendant(&current_module, &target_module, ctx);

        let fn_name = make::name(&name.text());
        let (type_params, params) = fn_args(ctx, target_module, FuncExpr::Method(call))?;

        let await_expr = call.syntax().parent().and_then(ast::AwaitExpr::cast);
        let is_async = await_expr.is_some();

        // should_render_snippet intends to express a rough level of confidence about
        // the correctness of the return type.
        //
        // If we are able to infer some return type, and that return type is not unit, we
        // don't want to render the snippet. The assumption here is in this situation the
        // return type is just as likely to be correct as any other part of the generated
        // function.
        //
        // In the case where the return type is inferred as unit it is likely that the
        // user does in fact intend for this generated function to return some non unit
        // type, but that the current state of their code doesn't allow that return type
        // to be accurately inferred.
        let (ret_ty, should_render_snippet) = {
            match ctx
                .sema
                .type_of_expr(&ast::Expr::MethodCallExpr(call.clone()))
                .map(TypeInfo::original)
            {
                Some(ty) if ty.is_unknown() || ty.is_unit() => (make::ty_unit(), true),
                Some(ty) => {
                    let rendered = ty.display_source_code(ctx.db(), target_module.into());
                    match rendered {
                        Ok(rendered) => (make::ty(&rendered), false),
                        Err(_) => (make::ty_unit(), true),
                    }
                }
                None => (make::ty_unit(), true),
            }
        };
        let ret_type = make::ret_type(ret_ty);

        Some(Self {
            target,
            fn_name,
            type_params,
            params,
            ret_type,
            should_render_snippet,
            file,
            needs_pub,
            is_async,
        })
    }

    fn render(self) -> FunctionTemplate {
        let placeholder_expr = make::ext::expr_todo();
        let fn_body = make::block_expr(vec![], Some(placeholder_expr));
        let visibility = if self.needs_pub { Some(make::visibility_pub_crate()) } else { None };
        let mut fn_def = make::fn_(
            visibility,
            self.fn_name,
            self.type_params,
            self.params,
            fn_body,
            Some(self.ret_type),
            self.is_async,
        );
        let leading_ws;
        let trailing_ws;

        let insert_offset = match self.target {
            GeneratedFunctionTarget::BehindItem(it) => {
                let indent = IndentLevel::from_node(&it);
                leading_ws = format!("\n\n{}", indent);
                fn_def = fn_def.indent(indent);
                trailing_ws = String::new();
                it.text_range().end()
            }
            GeneratedFunctionTarget::InEmptyItemList(it) => {
                let indent = IndentLevel::from_node(&it);
                leading_ws = format!("\n{}", indent + 1);
                fn_def = fn_def.indent(indent + 1);
                trailing_ws = format!("\n{}", indent);
                it.text_range().start() + TextSize::of('{')
            }
        };

        FunctionTemplate {
            insert_offset,
            leading_ws,
            ret_type: fn_def.ret_type().unwrap(),
            should_render_snippet: self.should_render_snippet,
            fn_def,
            trailing_ws,
            file: self.file,
        }
    }
}

enum GeneratedFunctionTarget {
    BehindItem(SyntaxNode),
    InEmptyItemList(SyntaxNode),
}

impl GeneratedFunctionTarget {
    fn syntax(&self) -> &SyntaxNode {
        match self {
            GeneratedFunctionTarget::BehindItem(it) => it,
            GeneratedFunctionTarget::InEmptyItemList(it) => it,
        }
    }
}

fn fn_name(call: &ast::Path) -> Option<ast::Name> {
    let name = call.segment()?.syntax().to_string();
    Some(make::name(&name))
}

/// Computes the type variables and arguments required for the generated function
fn fn_args(
    ctx: &AssistContext,
    target_module: hir::Module,
    call: FuncExpr,
) -> Option<(Option<ast::GenericParamList>, ast::ParamList)> {
    let mut arg_names = Vec::new();
    let mut arg_types = Vec::new();
    for arg in call.arg_list()?.args() {
        arg_names.push(match fn_arg_name(&arg) {
            Some(name) => name,
            None => String::from("arg"),
        });
        arg_types.push(match fn_arg_type(ctx, target_module, &arg) {
            Some(ty) => {
                if ty.len() > 0 && ty.starts_with('&') {
                    if let Some((new_ty, _)) = useless_type_special_case("", &ty[1..].to_owned()) {
                        new_ty
                    } else {
                        ty
                    }
                } else {
                    ty
                }
            }
            None => String::from("()"),
        });
    }
    deduplicate_arg_names(&mut arg_names);
    let params = arg_names.into_iter().zip(arg_types).map(|(name, ty)| {
        make::param(make::ext::simple_ident_pat(make::name(&name)).into(), make::ty(&ty))
    });

    Some((
        None,
        make::param_list(
            match call {
                FuncExpr::Func(_) => None,
                FuncExpr::Method(_) => Some(make::self_param()),
            },
            params,
        ),
    ))
}

/// Makes duplicate argument names unique by appending incrementing numbers.
///
/// ```
/// let mut names: Vec<String> =
///     vec!["foo".into(), "foo".into(), "bar".into(), "baz".into(), "bar".into()];
/// deduplicate_arg_names(&mut names);
/// let expected: Vec<String> =
///     vec!["foo_1".into(), "foo_2".into(), "bar_1".into(), "baz".into(), "bar_2".into()];
/// assert_eq!(names, expected);
/// ```
fn deduplicate_arg_names(arg_names: &mut Vec<String>) {
    let arg_name_counts = arg_names.iter().fold(FxHashMap::default(), |mut m, name| {
        *m.entry(name).or_insert(0) += 1;
        m
    });
    let duplicate_arg_names: FxHashSet<String> = arg_name_counts
        .into_iter()
        .filter(|(_, count)| *count >= 2)
        .map(|(name, _)| name.clone())
        .collect();

    let mut counter_per_name = FxHashMap::default();
    for arg_name in arg_names.iter_mut() {
        if duplicate_arg_names.contains(arg_name) {
            let counter = counter_per_name.entry(arg_name.clone()).or_insert(1);
            arg_name.push('_');
            arg_name.push_str(&counter.to_string());
            *counter += 1;
        }
    }
}

fn fn_arg_name(fn_arg: &ast::Expr) -> Option<String> {
    match fn_arg {
        ast::Expr::CastExpr(cast_expr) => fn_arg_name(&cast_expr.expr()?),
        _ => {
            let s = fn_arg
                .syntax()
                .descendants()
                .filter(|d| ast::NameRef::can_cast(d.kind()))
                .last()?
                .to_string();
            Some(to_lower_snake_case(&s))
        }
    }
}

fn fn_arg_type(
    ctx: &AssistContext,
    target_module: hir::Module,
    fn_arg: &ast::Expr,
) -> Option<String> {
    let ty = ctx.sema.type_of_expr(fn_arg)?.adjusted();
    if ty.is_unknown() {
        return None;
    }

    if let Ok(rendered) = ty.display_source_code(ctx.db(), target_module.into()) {
        Some(rendered)
    } else {
        None
    }
}

/// Returns the position inside the current mod or file
/// directly after the current block
/// We want to write the generated function directly after
/// fns, impls or macro calls, but inside mods
fn next_space_for_fn_after_call_site(expr: FuncExpr) -> Option<GeneratedFunctionTarget> {
    let mut ancestors = expr.syntax().ancestors().peekable();
    let mut last_ancestor: Option<SyntaxNode> = None;
    while let Some(next_ancestor) = ancestors.next() {
        match next_ancestor.kind() {
            SyntaxKind::SOURCE_FILE => {
                break;
            }
            SyntaxKind::ITEM_LIST => {
                if ancestors.peek().map(|a| a.kind()) == Some(SyntaxKind::MODULE) {
                    break;
                }
            }
            _ => {}
        }
        last_ancestor = Some(next_ancestor);
    }
    last_ancestor.map(GeneratedFunctionTarget::BehindItem)
}

fn next_space_for_fn_in_module(
    db: &dyn hir::db::AstDatabase,
    module_source: &hir::InFile<hir::ModuleSource>,
) -> Option<(FileId, GeneratedFunctionTarget)> {
    let file = module_source.file_id.original_file(db);
    let assist_item = match &module_source.value {
        hir::ModuleSource::SourceFile(it) => {
            if let Some(last_item) = it.items().last() {
                GeneratedFunctionTarget::BehindItem(last_item.syntax().clone())
            } else {
                GeneratedFunctionTarget::BehindItem(it.syntax().clone())
            }
        }
        hir::ModuleSource::Module(it) => {
            if let Some(last_item) = it.item_list().and_then(|it| it.items().last()) {
                GeneratedFunctionTarget::BehindItem(last_item.syntax().clone())
            } else {
                GeneratedFunctionTarget::InEmptyItemList(it.item_list()?.syntax().clone())
            }
        }
        hir::ModuleSource::BlockExpr(it) => {
            if let Some(last_item) =
                it.statements().take_while(|stmt| matches!(stmt, ast::Stmt::Item(_))).last()
            {
                GeneratedFunctionTarget::BehindItem(last_item.syntax().clone())
            } else {
                GeneratedFunctionTarget::InEmptyItemList(it.syntax().clone())
            }
        }
    };
    Some((file, assist_item))
}

fn next_space_for_fn_in_impl(impl_: &ast::Impl) -> Option<GeneratedFunctionTarget> {
    if let Some(last_item) = impl_.assoc_item_list().and_then(|it| it.assoc_items().last()) {
        Some(GeneratedFunctionTarget::BehindItem(last_item.syntax().clone()))
    } else {
        Some(GeneratedFunctionTarget::InEmptyItemList(impl_.assoc_item_list()?.syntax().clone()))
    }
}

fn module_is_descendant(module: &hir::Module, ans: &hir::Module, ctx: &AssistContext) -> bool {
    if module == ans {
        return true;
    }
    for c in ans.children(ctx.sema.db) {
        if module_is_descendant(module, &c, ctx) {
            return true;
        }
    }
    false
}

#[cfg(test)]
mod tests {
    use crate::tests::{check_assist, check_assist_not_applicable};

    use super::*;

{'fraction_non_alphanumeric': 0.1395453974632676, 'fraction_numerical': 0.0008037171920130604, 'mean_word_length': 2.1316658801321378, 'pattern_counts': {'":': 0, '<': 62, '<?xml version=': 0, '>': 207, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2008348', 'n_tokens_mistral': 12329, 'n_tokens_neox': 12041, 'n_words': 2440}