<commit_before>use ra_syntax::{
    ast::{self, BlockExpr, Expr, LoopBodyOwner},
    AstNode, SyntaxNode,
};

use crate::{AssistContext, AssistId, AssistKind, Assists};
use test_utils::mark;

// Assist: change_return_type_to_result
//
// Change the function's return type to Result.
//
// ```
// fn foo() -> i32<|> { 42i32 }
// ```
// ->
// ```
// fn foo() -> Result<i32, ${0:_}> { Ok(42i32) }
// ```
pub(crate) fn change_return_type_to_result(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
    let ret_type = ctx.find_node_at_offset::<ast::RetType>()?;
    // FIXME: extend to lambdas as well
    let fn_def = ret_type.syntax().parent().and_then(ast::FnDef::cast)?;

    let type_ref = &ret_type.type_ref()?;
    let ret_type_str = type_ref.syntax().text().to_string();
    let first_part_ret_type = ret_type_str.splitn(2, '<').next();
    if let Some(ret_type_first_part) = first_part_ret_type {
        if ret_type_first_part.ends_with("Result") {
            mark::hit!(change_return_type_to_result_simple_return_type_already_result);
            return None;
        }
    }

    let block_expr = &fn_def.body()?;

    acc.add(
        AssistId("change_return_type_to_result", AssistKind::RefactorRewrite),
        "Change return type to Result",
        type_ref.syntax().text_range(),
        |builder| {
            let mut tail_return_expr_collector = TailReturnCollector::new();
            tail_return_expr_collector.collect_jump_exprs(block_expr, false);
            tail_return_expr_collector.collect_tail_exprs(block_expr);

            for ret_expr_arg in tail_return_expr_collector.exprs_to_wrap {
                builder.replace_node_and_indent(&ret_expr_arg, format!("Ok({})", ret_expr_arg));
            }

            match ctx.config.snippet_cap {<commit_msg>assists: change_return_type_to_result: clarify assist description<commit_after>use ra_syntax::{
    ast::{self, BlockExpr, Expr, LoopBodyOwner},
    AstNode, SyntaxNode,
};

use crate::{AssistContext, AssistId, AssistKind, Assists};
use test_utils::mark;

// Assist: change_return_type_to_result
//
// Change the function's return type to Result.
//
// ```
// fn foo() -> i32<|> { 42i32 }
// ```
// ->
// ```
// fn foo() -> Result<i32, ${0:_}> { Ok(42i32) }
// ```
pub(crate) fn change_return_type_to_result(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
    let ret_type = ctx.find_node_at_offset::<ast::RetType>()?;
    // FIXME: extend to lambdas as well
    let fn_def = ret_type.syntax().parent().and_then(ast::FnDef::cast)?;

    let type_ref = &ret_type.type_ref()?;
    let ret_type_str = type_ref.syntax().text().to_string();
    let first_part_ret_type = ret_type_str.splitn(2, '<').next();
    if let Some(ret_type_first_part) = first_part_ret_type {
        if ret_type_first_part.ends_with("Result") {
            mark::hit!(change_return_type_to_result_simple_return_type_already_result);
            return None;
        }
    }

    let block_expr = &fn_def.body()?;

    acc.add(
        AssistId("change_return_type_to_result", AssistKind::RefactorRewrite),
        "Wrap return type in Result",
        type_ref.syntax().text_range(),
        |builder| {
            let mut tail_return_expr_collector = TailReturnCollector::new();
            tail_return_expr_collector.collect_jump_exprs(block_expr, false);
            tail_return_expr_collector.collect_tail_exprs(block_expr);

            for ret_expr_arg in tail_return_expr_collector.exprs_to_wrap {
                builder.replace_node_and_indent(&ret_expr_arg, format!("Ok({})", ret_expr_arg));
            }

            match ctx.config.snippet_cap {
{'fraction_non_alphanumeric': 0.14112458654906285, 'fraction_numerical': 0.007717750826901874, 'mean_word_length': 3.431013431013431, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 19, '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': '6270997', 'n_tokens_mistral': 1263, 'n_tokens_neox': 1241, 'n_words': 206}