mirror of
https://github.com/rust-lang/rust-analyzer
synced 2024-11-15 01:17:27 +00:00
func gen: seperate generation form position(2)
This commit is contained in:
parent
87439b1d6a
commit
1ac9400100
1 changed files with 63 additions and 44 deletions
|
@ -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,7 +229,15 @@ 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 {
|
||||||
|
GeneratedFunctionTarget::BehindItem(it) => it.text_range().end(),
|
||||||
|
GeneratedFunctionTarget::InEmptyItemList(it) => {
|
||||||
|
it.text_range().start() + TextSize::of('{')
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
Some((
|
||||||
|
Self {
|
||||||
target,
|
target,
|
||||||
fn_name,
|
fn_name,
|
||||||
type_params,
|
type_params,
|
||||||
|
@ -236,7 +247,10 @@ impl FunctionBuilder {
|
||||||
file,
|
file,
|
||||||
needs_pub,
|
needs_pub,
|
||||||
is_async,
|
is_async,
|
||||||
})
|
},
|
||||||
|
insert_offset,
|
||||||
|
file,
|
||||||
|
))
|
||||||
}
|
}
|
||||||
|
|
||||||
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,7 +283,15 @@ 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 {
|
||||||
|
GeneratedFunctionTarget::BehindItem(it) => it.text_range().end(),
|
||||||
|
GeneratedFunctionTarget::InEmptyItemList(it) => {
|
||||||
|
it.text_range().start() + TextSize::of('{')
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
Some((
|
||||||
|
Self {
|
||||||
target,
|
target,
|
||||||
fn_name,
|
fn_name,
|
||||||
type_params,
|
type_params,
|
||||||
|
@ -279,10 +301,13 @@ impl FunctionBuilder {
|
||||||
file,
|
file,
|
||||||
needs_pub,
|
needs_pub,
|
||||||
is_async,
|
is_async,
|
||||||
})
|
},
|
||||||
|
insert_offset,
|
||||||
|
file,
|
||||||
|
))
|
||||||
}
|
}
|
||||||
|
|
||||||
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,24 +323,21 @@ 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(),
|
||||||
|
@ -324,10 +346,7 @@ impl FunctionBuilder {
|
||||||
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,
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue