func gen: seperate generation form position(2)

This commit is contained in:
mahdi-frms 2021-08-21 17:31:37 +04:30
parent 87439b1d6a
commit 1ac9400100

View file

@ -85,9 +85,10 @@ fn gen_fn(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
None => None, None => None,
}; };
let function_builder = FunctionBuilder::from_call(ctx, &call, &path, target_module)?; let (function_builder, inserting_offset, file) =
FunctionBuilder::from_call(ctx, &call, &path, target_module)?;
let target = call.syntax().text_range(); let target = call.syntax().text_range();
add_func_to_accumulator(acc, ctx, target, function_builder, None) add_func_to_accumulator(acc, ctx, target, function_builder, inserting_offset, file, None)
} }
fn gen_method(acc: &mut Assists, ctx: &AssistContext) -> Option<()> { fn gen_method(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
@ -108,7 +109,7 @@ fn gen_method(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
ctx.sema.find_node_at_offset_with_macros(file.syntax(), range.range.start())?; ctx.sema.find_node_at_offset_with_macros(file.syntax(), range.range.start())?;
let impl_ = find_struct_impl(ctx, &adt_source, fn_name.text().as_str())?; let impl_ = find_struct_impl(ctx, &adt_source, fn_name.text().as_str())?;
let function_builder = FunctionBuilder::from_method_call( let (function_builder, inserting_offset, file) = FunctionBuilder::from_method_call(
ctx, ctx,
&call, &call,
&fn_name, &fn_name,
@ -119,7 +120,7 @@ fn gen_method(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
)?; )?;
let target = call.syntax().text_range(); let target = call.syntax().text_range();
let adt_name = if impl_.is_none() { Some(adt.name(ctx.sema.db)) } else { None }; let adt_name = if impl_.is_none() { Some(adt.name(ctx.sema.db)) } else { None };
add_func_to_accumulator(acc, ctx, target, function_builder, adt_name) add_func_to_accumulator(acc, ctx, target, function_builder, inserting_offset, file, adt_name)
} }
fn add_func_to_accumulator( fn add_func_to_accumulator(
@ -127,6 +128,8 @@ fn add_func_to_accumulator(
ctx: &AssistContext, ctx: &AssistContext,
target: TextRange, target: TextRange,
function_builder: FunctionBuilder, function_builder: FunctionBuilder,
insert_offset: TextSize,
file: FileId,
adt_name: Option<hir::Name>, adt_name: Option<hir::Name>,
) -> Option<()> { ) -> Option<()> {
acc.add( acc.add(
@ -134,7 +137,7 @@ fn add_func_to_accumulator(
format!("Generate `{}` method", function_builder.fn_name), format!("Generate `{}` method", function_builder.fn_name),
target, target,
|builder| { |builder| {
let (function_template, insert_offset, file) = function_builder.render(); let function_template = function_builder.render();
let mut func = function_template.to_string(ctx.config.snippet_cap); let mut func = function_template.to_string(ctx.config.snippet_cap);
if let Some(name) = adt_name { if let Some(name) = adt_name {
func = format!("\nimpl {} {{\n{}\n}}", name, func); func = format!("\nimpl {} {{\n{}\n}}", name, func);
@ -204,7 +207,7 @@ impl FunctionBuilder {
call: &ast::CallExpr, call: &ast::CallExpr,
path: &ast::Path, path: &ast::Path,
target_module: Option<hir::Module>, target_module: Option<hir::Module>,
) -> Option<Self> { ) -> Option<(Self, TextSize, FileId)> {
let mut file = ctx.frange.file_id; let mut file = ctx.frange.file_id;
let target = match &target_module { let target = match &target_module {
Some(target_module) => { Some(target_module) => {
@ -226,17 +229,28 @@ impl FunctionBuilder {
let (ret_type, should_focus_return_type) = let (ret_type, should_focus_return_type) =
make_return_type(ctx, &ast::Expr::CallExpr(call.clone()), target_module); make_return_type(ctx, &ast::Expr::CallExpr(call.clone()), target_module);
Some(Self { let insert_offset = match &target {
target, GeneratedFunctionTarget::BehindItem(it) => it.text_range().end(),
fn_name, GeneratedFunctionTarget::InEmptyItemList(it) => {
type_params, it.text_range().start() + TextSize::of('{')
params, }
ret_type, };
should_focus_return_type,
Some((
Self {
target,
fn_name,
type_params,
params,
ret_type,
should_focus_return_type,
file,
needs_pub,
is_async,
},
insert_offset,
file, file,
needs_pub, ))
is_async,
})
} }
fn from_method_call( fn from_method_call(
@ -247,7 +261,7 @@ impl FunctionBuilder {
file: FileId, file: FileId,
target_module: Module, target_module: Module,
current_module: Module, current_module: Module,
) -> Option<Self> { ) -> Option<(Self, TextSize, FileId)> {
let target = match impl_ { let target = match impl_ {
Some(impl_) => next_space_for_fn_in_impl(&impl_)?, Some(impl_) => next_space_for_fn_in_impl(&impl_)?,
None => { None => {
@ -269,20 +283,31 @@ impl FunctionBuilder {
let (ret_type, should_focus_return_type) = let (ret_type, should_focus_return_type) =
make_return_type(ctx, &ast::Expr::MethodCallExpr(call.clone()), target_module); make_return_type(ctx, &ast::Expr::MethodCallExpr(call.clone()), target_module);
Some(Self { let insert_offset = match &target {
target, GeneratedFunctionTarget::BehindItem(it) => it.text_range().end(),
fn_name, GeneratedFunctionTarget::InEmptyItemList(it) => {
type_params, it.text_range().start() + TextSize::of('{')
params, }
ret_type, };
should_focus_return_type,
Some((
Self {
target,
fn_name,
type_params,
params,
ret_type,
should_focus_return_type,
file,
needs_pub,
is_async,
},
insert_offset,
file, file,
needs_pub, ))
is_async,
})
} }
fn render(self) -> (FunctionTemplate, TextSize, FileId) { fn render(self) -> FunctionTemplate {
let placeholder_expr = make::ext::expr_todo(); let placeholder_expr = make::ext::expr_todo();
let fn_body = make::block_expr(vec![], Some(placeholder_expr)); let fn_body = make::block_expr(vec![], Some(placeholder_expr));
let visibility = if self.needs_pub { Some(make::visibility_pub_crate()) } else { None }; let visibility = if self.needs_pub { Some(make::visibility_pub_crate()) } else { None };
@ -298,36 +323,30 @@ impl FunctionBuilder {
let leading_ws; let leading_ws;
let trailing_ws; let trailing_ws;
let insert_offset = match self.target { match self.target {
GeneratedFunctionTarget::BehindItem(it) => { GeneratedFunctionTarget::BehindItem(it) => {
let indent = IndentLevel::from_node(&it); let indent = IndentLevel::from_node(&it);
leading_ws = format!("\n\n{}", indent); leading_ws = format!("\n\n{}", indent);
fn_def = fn_def.indent(indent); fn_def = fn_def.indent(indent);
trailing_ws = String::new(); trailing_ws = String::new();
it.text_range().end()
} }
GeneratedFunctionTarget::InEmptyItemList(it) => { GeneratedFunctionTarget::InEmptyItemList(it) => {
let indent = IndentLevel::from_node(&it); let indent = IndentLevel::from_node(&it);
leading_ws = format!("\n{}", indent + 1); leading_ws = format!("\n{}", indent + 1);
fn_def = fn_def.indent(indent + 1); fn_def = fn_def.indent(indent + 1);
trailing_ws = format!("\n{}", indent); trailing_ws = format!("\n{}", indent);
it.text_range().start() + TextSize::of('{')
} }
}; };
( FunctionTemplate {
FunctionTemplate { leading_ws,
leading_ws, ret_type: fn_def.ret_type(),
ret_type: fn_def.ret_type(), // PANIC: we guarantee we always create a function body with a tail expr
// PANIC: we guarantee we always create a function body with a tail expr tail_expr: fn_def.body().unwrap().tail_expr().unwrap(),
tail_expr: fn_def.body().unwrap().tail_expr().unwrap(), should_focus_return_type: self.should_focus_return_type,
should_focus_return_type: self.should_focus_return_type, fn_def,
fn_def, trailing_ws,
trailing_ws, }
},
insert_offset,
self.file,
)
} }
} }