Auto merge of #16082 - DropDemBits:structured-snippet-migrate-5, r=Veykril
internal: Migrate assists to the structured snippet API, part 5 Continuing from #15874 Migrates the following assists: - `extract_variable` - `generate_function` - `replace_is_some_with_if_let_some` - `replace_is_ok_with_if_let_ok`
This commit is contained in:
commit
792c94621d
4 changed files with 369 additions and 244 deletions
|
@ -1,12 +1,8 @@
|
||||||
use hir::TypeInfo;
|
use hir::TypeInfo;
|
||||||
use stdx::format_to;
|
|
||||||
use syntax::{
|
use syntax::{
|
||||||
ast::{self, AstNode},
|
ast::{self, edit::IndentLevel, edit_in_place::Indent, make, AstNode, HasName},
|
||||||
NodeOrToken,
|
ted, NodeOrToken,
|
||||||
SyntaxKind::{
|
SyntaxKind::{BLOCK_EXPR, BREAK_EXPR, COMMENT, LOOP_EXPR, MATCH_GUARD, PATH_EXPR, RETURN_EXPR},
|
||||||
BLOCK_EXPR, BREAK_EXPR, CLOSURE_EXPR, COMMENT, LOOP_EXPR, MATCH_ARM, MATCH_GUARD,
|
|
||||||
PATH_EXPR, RETURN_EXPR,
|
|
||||||
},
|
|
||||||
SyntaxNode,
|
SyntaxNode,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -66,98 +62,140 @@ pub(crate) fn extract_variable(acc: &mut Assists, ctx: &AssistContext<'_>) -> Op
|
||||||
.as_ref()
|
.as_ref()
|
||||||
.map_or(false, |it| matches!(it, ast::Expr::FieldExpr(_) | ast::Expr::MethodCallExpr(_)));
|
.map_or(false, |it| matches!(it, ast::Expr::FieldExpr(_) | ast::Expr::MethodCallExpr(_)));
|
||||||
|
|
||||||
let reference_modifier = match ty.filter(|_| needs_adjust) {
|
|
||||||
Some(receiver_type) if receiver_type.is_mutable_reference() => "&mut ",
|
|
||||||
Some(receiver_type) if receiver_type.is_reference() => "&",
|
|
||||||
_ => "",
|
|
||||||
};
|
|
||||||
|
|
||||||
let var_modifier = match parent {
|
|
||||||
Some(ast::Expr::RefExpr(expr)) if expr.mut_token().is_some() => "mut ",
|
|
||||||
_ => "",
|
|
||||||
};
|
|
||||||
|
|
||||||
let anchor = Anchor::from(&to_extract)?;
|
let anchor = Anchor::from(&to_extract)?;
|
||||||
let indent = anchor.syntax().prev_sibling_or_token()?.as_token()?.clone();
|
|
||||||
let target = to_extract.syntax().text_range();
|
let target = to_extract.syntax().text_range();
|
||||||
acc.add(
|
acc.add(
|
||||||
AssistId("extract_variable", AssistKind::RefactorExtract),
|
AssistId("extract_variable", AssistKind::RefactorExtract),
|
||||||
"Extract into variable",
|
"Extract into variable",
|
||||||
target,
|
target,
|
||||||
move |edit| {
|
move |edit| {
|
||||||
let field_shorthand =
|
let field_shorthand = to_extract
|
||||||
match to_extract.syntax().parent().and_then(ast::RecordExprField::cast) {
|
.syntax()
|
||||||
Some(field) => field.name_ref(),
|
.parent()
|
||||||
None => None,
|
.and_then(ast::RecordExprField::cast)
|
||||||
};
|
.filter(|field| field.name_ref().is_some());
|
||||||
|
|
||||||
let mut buf = String::new();
|
let (var_name, expr_replace) = match field_shorthand {
|
||||||
|
Some(field) => (field.to_string(), field.syntax().clone()),
|
||||||
|
None => (
|
||||||
|
suggest_name::for_variable(&to_extract, &ctx.sema),
|
||||||
|
to_extract.syntax().clone(),
|
||||||
|
),
|
||||||
|
};
|
||||||
|
|
||||||
let var_name = match &field_shorthand {
|
let ident_pat = match parent {
|
||||||
Some(it) => it.to_string(),
|
Some(ast::Expr::RefExpr(expr)) if expr.mut_token().is_some() => {
|
||||||
None => suggest_name::for_variable(&to_extract, &ctx.sema),
|
make::ident_pat(false, true, make::name(&var_name))
|
||||||
|
}
|
||||||
|
_ => make::ident_pat(false, false, make::name(&var_name)),
|
||||||
};
|
};
|
||||||
let expr_range = match &field_shorthand {
|
|
||||||
Some(it) => it.syntax().text_range().cover(to_extract.syntax().text_range()),
|
let to_extract = match ty.as_ref().filter(|_| needs_adjust) {
|
||||||
None => to_extract.syntax().text_range(),
|
Some(receiver_type) if receiver_type.is_mutable_reference() => {
|
||||||
|
make::expr_ref(to_extract, true)
|
||||||
|
}
|
||||||
|
Some(receiver_type) if receiver_type.is_reference() => {
|
||||||
|
make::expr_ref(to_extract, false)
|
||||||
|
}
|
||||||
|
_ => to_extract,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
let expr_replace = edit.make_syntax_mut(expr_replace);
|
||||||
|
let let_stmt =
|
||||||
|
make::let_stmt(ident_pat.into(), None, Some(to_extract)).clone_for_update();
|
||||||
|
let name_expr = make::expr_path(make::ext::ident_path(&var_name)).clone_for_update();
|
||||||
|
|
||||||
match anchor {
|
match anchor {
|
||||||
Anchor::Before(_) | Anchor::Replace(_) => {
|
Anchor::Before(place) => {
|
||||||
format_to!(buf, "let {var_modifier}{var_name} = {reference_modifier}")
|
let prev_ws = place.prev_sibling_or_token().and_then(|it| it.into_token());
|
||||||
}
|
let indent_to = IndentLevel::from_node(&place);
|
||||||
Anchor::WrapInBlock(_) => {
|
let insert_place = edit.make_syntax_mut(place);
|
||||||
format_to!(buf, "{{ let {var_name} = {reference_modifier}")
|
|
||||||
}
|
|
||||||
};
|
|
||||||
format_to!(buf, "{to_extract}");
|
|
||||||
|
|
||||||
if let Anchor::Replace(stmt) = anchor {
|
// Adjust ws to insert depending on if this is all inline or on separate lines
|
||||||
cov_mark::hit!(test_extract_var_expr_stmt);
|
let trailing_ws = if prev_ws.is_some_and(|it| it.text().starts_with("\n")) {
|
||||||
if stmt.semicolon_token().is_none() {
|
format!("\n{indent_to}")
|
||||||
buf.push(';');
|
} else {
|
||||||
}
|
format!(" ")
|
||||||
match ctx.config.snippet_cap {
|
};
|
||||||
Some(cap) => {
|
|
||||||
let snip = buf.replace(
|
|
||||||
&format!("let {var_modifier}{var_name}"),
|
|
||||||
&format!("let {var_modifier}$0{var_name}"),
|
|
||||||
);
|
|
||||||
edit.replace_snippet(cap, expr_range, snip)
|
|
||||||
}
|
|
||||||
None => edit.replace(expr_range, buf),
|
|
||||||
}
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
buf.push(';');
|
ted::insert_all_raw(
|
||||||
|
ted::Position::before(insert_place),
|
||||||
// We want to maintain the indent level,
|
vec![
|
||||||
// but we do not want to duplicate possible
|
let_stmt.syntax().clone().into(),
|
||||||
// extra newlines in the indent block
|
make::tokens::whitespace(&trailing_ws).into(),
|
||||||
let text = indent.text();
|
],
|
||||||
if text.starts_with('\n') {
|
|
||||||
buf.push('\n');
|
|
||||||
buf.push_str(text.trim_start_matches('\n'));
|
|
||||||
} else {
|
|
||||||
buf.push_str(text);
|
|
||||||
}
|
|
||||||
|
|
||||||
edit.replace(expr_range, var_name.clone());
|
|
||||||
let offset = anchor.syntax().text_range().start();
|
|
||||||
match ctx.config.snippet_cap {
|
|
||||||
Some(cap) => {
|
|
||||||
let snip = buf.replace(
|
|
||||||
&format!("let {var_modifier}{var_name}"),
|
|
||||||
&format!("let {var_modifier}$0{var_name}"),
|
|
||||||
);
|
);
|
||||||
edit.insert_snippet(cap, offset, snip)
|
|
||||||
}
|
|
||||||
None => edit.insert(offset, buf),
|
|
||||||
}
|
|
||||||
|
|
||||||
if let Anchor::WrapInBlock(_) = anchor {
|
ted::replace(expr_replace, name_expr.syntax());
|
||||||
edit.insert(anchor.syntax().text_range().end(), " }");
|
|
||||||
|
if let Some(cap) = ctx.config.snippet_cap {
|
||||||
|
if let Some(ast::Pat::IdentPat(ident_pat)) = let_stmt.pat() {
|
||||||
|
if let Some(name) = ident_pat.name() {
|
||||||
|
edit.add_tabstop_before(cap, name);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Anchor::Replace(stmt) => {
|
||||||
|
cov_mark::hit!(test_extract_var_expr_stmt);
|
||||||
|
|
||||||
|
let stmt_replace = edit.make_mut(stmt);
|
||||||
|
ted::replace(stmt_replace.syntax(), let_stmt.syntax());
|
||||||
|
|
||||||
|
if let Some(cap) = ctx.config.snippet_cap {
|
||||||
|
if let Some(ast::Pat::IdentPat(ident_pat)) = let_stmt.pat() {
|
||||||
|
if let Some(name) = ident_pat.name() {
|
||||||
|
edit.add_tabstop_before(cap, name);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Anchor::WrapInBlock(to_wrap) => {
|
||||||
|
let indent_to = to_wrap.indent_level();
|
||||||
|
|
||||||
|
let block = if to_wrap.syntax() == &expr_replace {
|
||||||
|
// Since `expr_replace` is the same that needs to be wrapped in a block,
|
||||||
|
// we can just directly replace it with a block
|
||||||
|
let block =
|
||||||
|
make::block_expr([let_stmt.into()], Some(name_expr)).clone_for_update();
|
||||||
|
ted::replace(expr_replace, block.syntax());
|
||||||
|
|
||||||
|
block
|
||||||
|
} else {
|
||||||
|
// `expr_replace` is a descendant of `to_wrap`, so both steps need to be
|
||||||
|
// handled seperately, otherwise we wrap the wrong expression
|
||||||
|
let to_wrap = edit.make_mut(to_wrap);
|
||||||
|
|
||||||
|
// Replace the target expr first so that we don't need to find where
|
||||||
|
// `expr_replace` is in the wrapped `to_wrap`
|
||||||
|
ted::replace(expr_replace, name_expr.syntax());
|
||||||
|
|
||||||
|
// Wrap `to_wrap` in a block
|
||||||
|
let block = make::block_expr([let_stmt.into()], Some(to_wrap.clone()))
|
||||||
|
.clone_for_update();
|
||||||
|
ted::replace(to_wrap.syntax(), block.syntax());
|
||||||
|
|
||||||
|
block
|
||||||
|
};
|
||||||
|
|
||||||
|
if let Some(cap) = ctx.config.snippet_cap {
|
||||||
|
// Adding a tabstop to `name` requires finding the let stmt again, since
|
||||||
|
// the existing `let_stmt` is not actually added to the tree
|
||||||
|
let pat = block.statements().find_map(|stmt| {
|
||||||
|
let ast::Stmt::LetStmt(let_stmt) = stmt else { return None };
|
||||||
|
let_stmt.pat()
|
||||||
|
});
|
||||||
|
|
||||||
|
if let Some(ast::Pat::IdentPat(ident_pat)) = pat {
|
||||||
|
if let Some(name) = ident_pat.name() {
|
||||||
|
edit.add_tabstop_before(cap, name);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// fixup indentation of block
|
||||||
|
block.indent(indent_to);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
@ -181,7 +219,7 @@ fn valid_target_expr(node: SyntaxNode) -> Option<ast::Expr> {
|
||||||
enum Anchor {
|
enum Anchor {
|
||||||
Before(SyntaxNode),
|
Before(SyntaxNode),
|
||||||
Replace(ast::ExprStmt),
|
Replace(ast::ExprStmt),
|
||||||
WrapInBlock(SyntaxNode),
|
WrapInBlock(ast::Expr),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Anchor {
|
impl Anchor {
|
||||||
|
@ -204,16 +242,16 @@ impl Anchor {
|
||||||
}
|
}
|
||||||
|
|
||||||
if let Some(parent) = node.parent() {
|
if let Some(parent) = node.parent() {
|
||||||
if parent.kind() == CLOSURE_EXPR {
|
if let Some(parent) = ast::ClosureExpr::cast(parent.clone()) {
|
||||||
cov_mark::hit!(test_extract_var_in_closure_no_block);
|
cov_mark::hit!(test_extract_var_in_closure_no_block);
|
||||||
return Some(Anchor::WrapInBlock(node));
|
return parent.body().map(Anchor::WrapInBlock);
|
||||||
}
|
}
|
||||||
if parent.kind() == MATCH_ARM {
|
if let Some(parent) = ast::MatchArm::cast(parent) {
|
||||||
if node.kind() == MATCH_GUARD {
|
if node.kind() == MATCH_GUARD {
|
||||||
cov_mark::hit!(test_extract_var_in_match_guard);
|
cov_mark::hit!(test_extract_var_in_match_guard);
|
||||||
} else {
|
} else {
|
||||||
cov_mark::hit!(test_extract_var_in_match_arm_no_block);
|
cov_mark::hit!(test_extract_var_in_match_arm_no_block);
|
||||||
return Some(Anchor::WrapInBlock(node));
|
return parent.expr().map(Anchor::WrapInBlock);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -229,13 +267,6 @@ impl Anchor {
|
||||||
None
|
None
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
fn syntax(&self) -> &SyntaxNode {
|
|
||||||
match self {
|
|
||||||
Anchor::Before(it) | Anchor::WrapInBlock(it) => it,
|
|
||||||
Anchor::Replace(stmt) => stmt.syntax(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
|
@ -502,7 +533,10 @@ fn main() {
|
||||||
fn main() {
|
fn main() {
|
||||||
let x = true;
|
let x = true;
|
||||||
let tuple = match x {
|
let tuple = match x {
|
||||||
true => { let $0var_name = 2 + 2; (var_name, true) }
|
true => {
|
||||||
|
let $0var_name = 2 + 2;
|
||||||
|
(var_name, true)
|
||||||
|
}
|
||||||
_ => (0, false)
|
_ => (0, false)
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
@ -579,7 +613,10 @@ fn main() {
|
||||||
"#,
|
"#,
|
||||||
r#"
|
r#"
|
||||||
fn main() {
|
fn main() {
|
||||||
let lambda = |x: u32| { let $0var_name = x * 2; var_name };
|
let lambda = |x: u32| {
|
||||||
|
let $0var_name = x * 2;
|
||||||
|
var_name
|
||||||
|
};
|
||||||
}
|
}
|
||||||
"#,
|
"#,
|
||||||
);
|
);
|
||||||
|
|
|
@ -8,20 +8,21 @@ use ide_db::{
|
||||||
famous_defs::FamousDefs,
|
famous_defs::FamousDefs,
|
||||||
helpers::is_editable_crate,
|
helpers::is_editable_crate,
|
||||||
path_transform::PathTransform,
|
path_transform::PathTransform,
|
||||||
|
source_change::SourceChangeBuilder,
|
||||||
FxHashMap, FxHashSet, RootDatabase, SnippetCap,
|
FxHashMap, FxHashSet, RootDatabase, SnippetCap,
|
||||||
};
|
};
|
||||||
|
use itertools::Itertools;
|
||||||
use stdx::to_lower_snake_case;
|
use stdx::to_lower_snake_case;
|
||||||
use syntax::{
|
use syntax::{
|
||||||
ast::{
|
ast::{
|
||||||
self,
|
self, edit::IndentLevel, edit_in_place::Indent, make, AstNode, CallExpr, HasArgList,
|
||||||
edit::{AstNodeEdit, IndentLevel},
|
HasGenericParams, HasModuleItem, HasTypeBounds,
|
||||||
make, AstNode, CallExpr, HasArgList, HasGenericParams, HasModuleItem, HasTypeBounds,
|
|
||||||
},
|
},
|
||||||
SyntaxKind, SyntaxNode, TextRange, TextSize,
|
ted, SyntaxKind, SyntaxNode, TextRange, T,
|
||||||
};
|
};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
utils::{convert_reference_type, find_struct_impl, render_snippet, Cursor},
|
utils::{convert_reference_type, find_struct_impl},
|
||||||
AssistContext, AssistId, AssistKind, Assists,
|
AssistContext, AssistId, AssistKind, Assists,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -65,7 +66,7 @@ fn gen_fn(acc: &mut Assists, ctx: &AssistContext<'_>) -> Option<()> {
|
||||||
}
|
}
|
||||||
|
|
||||||
let fn_name = &*name_ref.text();
|
let fn_name = &*name_ref.text();
|
||||||
let TargetInfo { target_module, adt_name, target, file, insert_offset } =
|
let TargetInfo { target_module, adt_name, target, file } =
|
||||||
fn_target_info(ctx, path, &call, fn_name)?;
|
fn_target_info(ctx, path, &call, fn_name)?;
|
||||||
|
|
||||||
if let Some(m) = target_module {
|
if let Some(m) = target_module {
|
||||||
|
@ -77,16 +78,7 @@ fn gen_fn(acc: &mut Assists, ctx: &AssistContext<'_>) -> Option<()> {
|
||||||
let function_builder = FunctionBuilder::from_call(ctx, &call, fn_name, target_module, target)?;
|
let function_builder = FunctionBuilder::from_call(ctx, &call, fn_name, target_module, target)?;
|
||||||
let text_range = call.syntax().text_range();
|
let text_range = call.syntax().text_range();
|
||||||
let label = format!("Generate {} function", function_builder.fn_name);
|
let label = format!("Generate {} function", function_builder.fn_name);
|
||||||
add_func_to_accumulator(
|
add_func_to_accumulator(acc, ctx, text_range, function_builder, file, adt_name, label)
|
||||||
acc,
|
|
||||||
ctx,
|
|
||||||
text_range,
|
|
||||||
function_builder,
|
|
||||||
insert_offset,
|
|
||||||
file,
|
|
||||||
adt_name,
|
|
||||||
label,
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
struct TargetInfo {
|
struct TargetInfo {
|
||||||
|
@ -94,7 +86,6 @@ struct TargetInfo {
|
||||||
adt_name: Option<hir::Name>,
|
adt_name: Option<hir::Name>,
|
||||||
target: GeneratedFunctionTarget,
|
target: GeneratedFunctionTarget,
|
||||||
file: FileId,
|
file: FileId,
|
||||||
insert_offset: TextSize,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl TargetInfo {
|
impl TargetInfo {
|
||||||
|
@ -103,9 +94,8 @@ impl TargetInfo {
|
||||||
adt_name: Option<hir::Name>,
|
adt_name: Option<hir::Name>,
|
||||||
target: GeneratedFunctionTarget,
|
target: GeneratedFunctionTarget,
|
||||||
file: FileId,
|
file: FileId,
|
||||||
insert_offset: TextSize,
|
|
||||||
) -> Self {
|
) -> Self {
|
||||||
Self { target_module, adt_name, target, file, insert_offset }
|
Self { target_module, adt_name, target, file }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -156,7 +146,7 @@ fn gen_method(acc: &mut Assists, ctx: &AssistContext<'_>) -> Option<()> {
|
||||||
}
|
}
|
||||||
|
|
||||||
let (impl_, file) = get_adt_source(ctx, &adt, fn_name.text().as_str())?;
|
let (impl_, file) = get_adt_source(ctx, &adt, fn_name.text().as_str())?;
|
||||||
let (target, insert_offset) = get_method_target(ctx, &impl_, &adt)?;
|
let target = get_method_target(ctx, &impl_, &adt)?;
|
||||||
|
|
||||||
let function_builder = FunctionBuilder::from_method_call(
|
let function_builder = FunctionBuilder::from_method_call(
|
||||||
ctx,
|
ctx,
|
||||||
|
@ -169,16 +159,7 @@ fn gen_method(acc: &mut Assists, ctx: &AssistContext<'_>) -> Option<()> {
|
||||||
let text_range = call.syntax().text_range();
|
let text_range = 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 };
|
||||||
let label = format!("Generate {} method", function_builder.fn_name);
|
let label = format!("Generate {} method", function_builder.fn_name);
|
||||||
add_func_to_accumulator(
|
add_func_to_accumulator(acc, ctx, text_range, function_builder, file, adt_name, label)
|
||||||
acc,
|
|
||||||
ctx,
|
|
||||||
text_range,
|
|
||||||
function_builder,
|
|
||||||
insert_offset,
|
|
||||||
file,
|
|
||||||
adt_name,
|
|
||||||
label,
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn add_func_to_accumulator(
|
fn add_func_to_accumulator(
|
||||||
|
@ -186,23 +167,28 @@ fn add_func_to_accumulator(
|
||||||
ctx: &AssistContext<'_>,
|
ctx: &AssistContext<'_>,
|
||||||
text_range: TextRange,
|
text_range: TextRange,
|
||||||
function_builder: FunctionBuilder,
|
function_builder: FunctionBuilder,
|
||||||
insert_offset: TextSize,
|
|
||||||
file: FileId,
|
file: FileId,
|
||||||
adt_name: Option<hir::Name>,
|
adt_name: Option<hir::Name>,
|
||||||
label: String,
|
label: String,
|
||||||
) -> Option<()> {
|
) -> Option<()> {
|
||||||
acc.add(AssistId("generate_function", AssistKind::Generate), label, text_range, |builder| {
|
acc.add(AssistId("generate_function", AssistKind::Generate), label, text_range, |edit| {
|
||||||
let indent = IndentLevel::from_node(function_builder.target.syntax());
|
edit.edit_file(file);
|
||||||
let function_template = function_builder.render(adt_name.is_some());
|
|
||||||
let mut func = function_template.to_string(ctx.config.snippet_cap);
|
let target = function_builder.target.clone();
|
||||||
|
let function_template = function_builder.render();
|
||||||
|
let func = function_template.to_ast(ctx.config.snippet_cap, edit);
|
||||||
|
|
||||||
if let Some(name) = adt_name {
|
if let Some(name) = adt_name {
|
||||||
|
let name = make::ty_path(make::ext::ident_path(&format!("{}", name.display(ctx.db()))));
|
||||||
|
|
||||||
// FIXME: adt may have generic params.
|
// FIXME: adt may have generic params.
|
||||||
func = format!("\n{indent}impl {} {{\n{func}\n{indent}}}", name.display(ctx.db()));
|
let impl_ = make::impl_(None, None, name, None, None).clone_for_update();
|
||||||
}
|
|
||||||
builder.edit_file(file);
|
func.indent(IndentLevel(1));
|
||||||
match ctx.config.snippet_cap {
|
impl_.get_or_create_assoc_item_list().add_item(func.into());
|
||||||
Some(cap) => builder.insert_snippet(cap, insert_offset, func),
|
target.insert_impl_at(edit, impl_);
|
||||||
None => builder.insert(insert_offset, func),
|
} else {
|
||||||
|
target.insert_fn_at(edit, func);
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
@ -220,36 +206,33 @@ fn get_adt_source(
|
||||||
}
|
}
|
||||||
|
|
||||||
struct FunctionTemplate {
|
struct FunctionTemplate {
|
||||||
leading_ws: String,
|
|
||||||
fn_def: ast::Fn,
|
fn_def: ast::Fn,
|
||||||
ret_type: Option<ast::RetType>,
|
ret_type: Option<ast::RetType>,
|
||||||
should_focus_return_type: bool,
|
should_focus_return_type: bool,
|
||||||
trailing_ws: String,
|
|
||||||
tail_expr: ast::Expr,
|
tail_expr: ast::Expr,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl FunctionTemplate {
|
impl FunctionTemplate {
|
||||||
fn to_string(&self, cap: Option<SnippetCap>) -> String {
|
fn to_ast(&self, cap: Option<SnippetCap>, edit: &mut SourceChangeBuilder) -> ast::Fn {
|
||||||
let Self { leading_ws, fn_def, ret_type, should_focus_return_type, trailing_ws, tail_expr } =
|
let Self { fn_def, ret_type, should_focus_return_type, tail_expr } = self;
|
||||||
self;
|
|
||||||
|
|
||||||
let f = match cap {
|
if let Some(cap) = cap {
|
||||||
Some(cap) => {
|
if *should_focus_return_type {
|
||||||
let cursor = if *should_focus_return_type {
|
// Focus the return type if there is one
|
||||||
// Focus the return type if there is one
|
match ret_type {
|
||||||
match ret_type {
|
Some(ret_type) => {
|
||||||
Some(ret_type) => ret_type.syntax(),
|
edit.add_placeholder_snippet(cap, ret_type.clone());
|
||||||
None => tail_expr.syntax(),
|
|
||||||
}
|
}
|
||||||
} else {
|
None => {
|
||||||
tail_expr.syntax()
|
edit.add_placeholder_snippet(cap, tail_expr.clone());
|
||||||
};
|
}
|
||||||
render_snippet(cap, fn_def.syntax(), Cursor::Replace(cursor))
|
}
|
||||||
|
} else {
|
||||||
|
edit.add_placeholder_snippet(cap, tail_expr.clone());
|
||||||
}
|
}
|
||||||
None => fn_def.to_string(),
|
}
|
||||||
};
|
|
||||||
|
|
||||||
format!("{leading_ws}{f}{trailing_ws}")
|
fn_def.clone()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -356,7 +339,7 @@ impl FunctionBuilder {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
fn render(self, is_method: bool) -> FunctionTemplate {
|
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 = match self.visibility {
|
let visibility = match self.visibility {
|
||||||
|
@ -364,7 +347,7 @@ impl FunctionBuilder {
|
||||||
Visibility::Crate => Some(make::visibility_pub_crate()),
|
Visibility::Crate => Some(make::visibility_pub_crate()),
|
||||||
Visibility::Pub => Some(make::visibility_pub()),
|
Visibility::Pub => Some(make::visibility_pub()),
|
||||||
};
|
};
|
||||||
let mut fn_def = make::fn_(
|
let fn_def = make::fn_(
|
||||||
visibility,
|
visibility,
|
||||||
self.fn_name,
|
self.fn_name,
|
||||||
self.generic_param_list,
|
self.generic_param_list,
|
||||||
|
@ -375,34 +358,10 @@ impl FunctionBuilder {
|
||||||
self.is_async,
|
self.is_async,
|
||||||
false, // FIXME : const and unsafe are not handled yet.
|
false, // FIXME : const and unsafe are not handled yet.
|
||||||
false,
|
false,
|
||||||
);
|
)
|
||||||
let leading_ws;
|
.clone_for_update();
|
||||||
let trailing_ws;
|
|
||||||
|
|
||||||
match self.target {
|
|
||||||
GeneratedFunctionTarget::BehindItem(it) => {
|
|
||||||
let mut indent = IndentLevel::from_node(&it);
|
|
||||||
if is_method {
|
|
||||||
indent = indent + 1;
|
|
||||||
leading_ws = format!("{indent}");
|
|
||||||
} else {
|
|
||||||
leading_ws = format!("\n\n{indent}");
|
|
||||||
}
|
|
||||||
|
|
||||||
fn_def = fn_def.indent(indent);
|
|
||||||
trailing_ws = String::new();
|
|
||||||
}
|
|
||||||
GeneratedFunctionTarget::InEmptyItemList(it) => {
|
|
||||||
let indent = IndentLevel::from_node(&it);
|
|
||||||
let leading_indent = indent + 1;
|
|
||||||
leading_ws = format!("\n{leading_indent}");
|
|
||||||
fn_def = fn_def.indent(leading_indent);
|
|
||||||
trailing_ws = format!("\n{indent}");
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
FunctionTemplate {
|
FunctionTemplate {
|
||||||
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
|
tail_expr: fn_def
|
||||||
|
@ -412,7 +371,6 @@ impl FunctionBuilder {
|
||||||
.expect("function body should have a tail expression"),
|
.expect("function body should have a tail expression"),
|
||||||
should_focus_return_type: self.should_focus_return_type,
|
should_focus_return_type: self.should_focus_return_type,
|
||||||
fn_def,
|
fn_def,
|
||||||
trailing_ws,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -456,40 +414,37 @@ fn get_fn_target_info(
|
||||||
target_module: Option<Module>,
|
target_module: Option<Module>,
|
||||||
call: CallExpr,
|
call: CallExpr,
|
||||||
) -> Option<TargetInfo> {
|
) -> Option<TargetInfo> {
|
||||||
let (target, file, insert_offset) = get_fn_target(ctx, target_module, call)?;
|
let (target, file) = get_fn_target(ctx, target_module, call)?;
|
||||||
Some(TargetInfo::new(target_module, None, target, file, insert_offset))
|
Some(TargetInfo::new(target_module, None, target, file))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_fn_target(
|
fn get_fn_target(
|
||||||
ctx: &AssistContext<'_>,
|
ctx: &AssistContext<'_>,
|
||||||
target_module: Option<Module>,
|
target_module: Option<Module>,
|
||||||
call: CallExpr,
|
call: CallExpr,
|
||||||
) -> Option<(GeneratedFunctionTarget, FileId, TextSize)> {
|
) -> Option<(GeneratedFunctionTarget, FileId)> {
|
||||||
let mut file = ctx.file_id();
|
let mut file = ctx.file_id();
|
||||||
let target = match target_module {
|
let target = match target_module {
|
||||||
Some(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.db(), target_module);
|
||||||
let (in_file, target) = next_space_for_fn_in_module(ctx.sema.db, &module_source)?;
|
|
||||||
file = in_file;
|
file = in_file;
|
||||||
target
|
target
|
||||||
}
|
}
|
||||||
None => next_space_for_fn_after_call_site(ast::CallableExpr::Call(call))?,
|
None => next_space_for_fn_after_call_site(ast::CallableExpr::Call(call))?,
|
||||||
};
|
};
|
||||||
Some((target.clone(), file, get_insert_offset(&target)))
|
Some((target.clone(), file))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_method_target(
|
fn get_method_target(
|
||||||
ctx: &AssistContext<'_>,
|
ctx: &AssistContext<'_>,
|
||||||
impl_: &Option<ast::Impl>,
|
impl_: &Option<ast::Impl>,
|
||||||
adt: &Adt,
|
adt: &Adt,
|
||||||
) -> Option<(GeneratedFunctionTarget, TextSize)> {
|
) -> Option<GeneratedFunctionTarget> {
|
||||||
let target = match impl_ {
|
let target = match impl_ {
|
||||||
Some(impl_) => next_space_for_fn_in_impl(impl_)?,
|
Some(impl_) => GeneratedFunctionTarget::InImpl(impl_.clone()),
|
||||||
None => {
|
None => GeneratedFunctionTarget::AfterItem(adt.source(ctx.sema.db)?.syntax().value.clone()),
|
||||||
GeneratedFunctionTarget::BehindItem(adt.source(ctx.sema.db)?.syntax().value.clone())
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
Some((target.clone(), get_insert_offset(&target)))
|
Some(target)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn assoc_fn_target_info(
|
fn assoc_fn_target_info(
|
||||||
|
@ -505,36 +460,120 @@ fn assoc_fn_target_info(
|
||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
let (impl_, file) = get_adt_source(ctx, &adt, fn_name)?;
|
let (impl_, file) = get_adt_source(ctx, &adt, fn_name)?;
|
||||||
let (target, insert_offset) = get_method_target(ctx, &impl_, &adt)?;
|
let target = get_method_target(ctx, &impl_, &adt)?;
|
||||||
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 };
|
||||||
Some(TargetInfo::new(target_module, adt_name, target, file, insert_offset))
|
Some(TargetInfo::new(target_module, adt_name, target, file))
|
||||||
}
|
|
||||||
|
|
||||||
fn get_insert_offset(target: &GeneratedFunctionTarget) -> TextSize {
|
|
||||||
match target {
|
|
||||||
GeneratedFunctionTarget::BehindItem(it) => it.text_range().end(),
|
|
||||||
GeneratedFunctionTarget::InEmptyItemList(it) => it.text_range().start() + TextSize::of('{'),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
enum GeneratedFunctionTarget {
|
enum GeneratedFunctionTarget {
|
||||||
BehindItem(SyntaxNode),
|
AfterItem(SyntaxNode),
|
||||||
InEmptyItemList(SyntaxNode),
|
InEmptyItemList(SyntaxNode),
|
||||||
|
InImpl(ast::Impl),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl GeneratedFunctionTarget {
|
impl GeneratedFunctionTarget {
|
||||||
fn syntax(&self) -> &SyntaxNode {
|
fn syntax(&self) -> &SyntaxNode {
|
||||||
match self {
|
match self {
|
||||||
GeneratedFunctionTarget::BehindItem(it) => it,
|
GeneratedFunctionTarget::AfterItem(it) => it,
|
||||||
GeneratedFunctionTarget::InEmptyItemList(it) => it,
|
GeneratedFunctionTarget::InEmptyItemList(it) => it,
|
||||||
|
GeneratedFunctionTarget::InImpl(it) => it.syntax(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn parent(&self) -> SyntaxNode {
|
fn parent(&self) -> SyntaxNode {
|
||||||
match self {
|
match self {
|
||||||
GeneratedFunctionTarget::BehindItem(it) => it.parent().expect("item without parent"),
|
GeneratedFunctionTarget::AfterItem(it) => it.parent().expect("item without parent"),
|
||||||
GeneratedFunctionTarget::InEmptyItemList(it) => it.clone(),
|
GeneratedFunctionTarget::InEmptyItemList(it) => it.clone(),
|
||||||
|
GeneratedFunctionTarget::InImpl(it) => it.syntax().clone(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn insert_impl_at(&self, edit: &mut SourceChangeBuilder, impl_: ast::Impl) {
|
||||||
|
match self {
|
||||||
|
GeneratedFunctionTarget::AfterItem(item) => {
|
||||||
|
let item = edit.make_syntax_mut(item.clone());
|
||||||
|
let position = if item.parent().is_some() {
|
||||||
|
ted::Position::after(&item)
|
||||||
|
} else {
|
||||||
|
ted::Position::first_child_of(&item)
|
||||||
|
};
|
||||||
|
|
||||||
|
let indent = IndentLevel::from_node(&item);
|
||||||
|
let leading_ws = make::tokens::whitespace(&format!("\n{indent}"));
|
||||||
|
impl_.indent(indent);
|
||||||
|
|
||||||
|
ted::insert_all(position, vec![leading_ws.into(), impl_.syntax().clone().into()]);
|
||||||
|
}
|
||||||
|
GeneratedFunctionTarget::InEmptyItemList(item_list) => {
|
||||||
|
let item_list = edit.make_syntax_mut(item_list.clone());
|
||||||
|
let insert_after =
|
||||||
|
item_list.children_with_tokens().find_or_first(|child| child.kind() == T!['{']);
|
||||||
|
let position = match insert_after {
|
||||||
|
Some(child) => ted::Position::after(child),
|
||||||
|
None => ted::Position::first_child_of(&item_list),
|
||||||
|
};
|
||||||
|
|
||||||
|
let indent = IndentLevel::from_node(&item_list);
|
||||||
|
let leading_indent = indent + 1;
|
||||||
|
let leading_ws = make::tokens::whitespace(&format!("\n{leading_indent}"));
|
||||||
|
impl_.indent(indent);
|
||||||
|
|
||||||
|
ted::insert_all(position, vec![leading_ws.into(), impl_.syntax().clone().into()]);
|
||||||
|
}
|
||||||
|
GeneratedFunctionTarget::InImpl(_) => {
|
||||||
|
unreachable!("can't insert an impl inside an impl")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn insert_fn_at(&self, edit: &mut SourceChangeBuilder, func: ast::Fn) {
|
||||||
|
match self {
|
||||||
|
GeneratedFunctionTarget::AfterItem(item) => {
|
||||||
|
let item = edit.make_syntax_mut(item.clone());
|
||||||
|
let position = if item.parent().is_some() {
|
||||||
|
ted::Position::after(&item)
|
||||||
|
} else {
|
||||||
|
ted::Position::first_child_of(&item)
|
||||||
|
};
|
||||||
|
|
||||||
|
let indent = IndentLevel::from_node(&item);
|
||||||
|
let leading_ws = make::tokens::whitespace(&format!("\n\n{indent}"));
|
||||||
|
func.indent(indent);
|
||||||
|
|
||||||
|
ted::insert_all_raw(
|
||||||
|
position,
|
||||||
|
vec![leading_ws.into(), func.syntax().clone().into()],
|
||||||
|
);
|
||||||
|
}
|
||||||
|
GeneratedFunctionTarget::InEmptyItemList(item_list) => {
|
||||||
|
let item_list = edit.make_syntax_mut(item_list.clone());
|
||||||
|
let insert_after =
|
||||||
|
item_list.children_with_tokens().find_or_first(|child| child.kind() == T!['{']);
|
||||||
|
let position = match insert_after {
|
||||||
|
Some(child) => ted::Position::after(child),
|
||||||
|
None => ted::Position::first_child_of(&item_list),
|
||||||
|
};
|
||||||
|
|
||||||
|
let indent = IndentLevel::from_node(&item_list);
|
||||||
|
let leading_indent = indent + 1;
|
||||||
|
let leading_ws = make::tokens::whitespace(&format!("\n{leading_indent}"));
|
||||||
|
let trailing_ws = make::tokens::whitespace(&format!("\n{indent}"));
|
||||||
|
func.indent(leading_indent);
|
||||||
|
|
||||||
|
ted::insert_all(
|
||||||
|
position,
|
||||||
|
vec![leading_ws.into(), func.syntax().clone().into(), trailing_ws.into()],
|
||||||
|
);
|
||||||
|
}
|
||||||
|
GeneratedFunctionTarget::InImpl(impl_) => {
|
||||||
|
let impl_ = edit.make_mut(impl_.clone());
|
||||||
|
|
||||||
|
let leading_indent = impl_.indent_level() + 1;
|
||||||
|
func.indent(leading_indent);
|
||||||
|
|
||||||
|
impl_.get_or_create_assoc_item_list().add_item(func.into());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1026,43 +1065,40 @@ fn next_space_for_fn_after_call_site(expr: ast::CallableExpr) -> Option<Generate
|
||||||
}
|
}
|
||||||
last_ancestor = Some(next_ancestor);
|
last_ancestor = Some(next_ancestor);
|
||||||
}
|
}
|
||||||
last_ancestor.map(GeneratedFunctionTarget::BehindItem)
|
last_ancestor.map(GeneratedFunctionTarget::AfterItem)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn next_space_for_fn_in_module(
|
fn next_space_for_fn_in_module(
|
||||||
db: &dyn hir::db::ExpandDatabase,
|
db: &dyn hir::db::HirDatabase,
|
||||||
module_source: &hir::InFile<hir::ModuleSource>,
|
target_module: hir::Module,
|
||||||
) -> Option<(FileId, GeneratedFunctionTarget)> {
|
) -> (FileId, GeneratedFunctionTarget) {
|
||||||
let file = module_source.file_id.original_file(db);
|
let module_source = target_module.definition_source(db);
|
||||||
|
let file = module_source.file_id.original_file(db.upcast());
|
||||||
let assist_item = match &module_source.value {
|
let assist_item = match &module_source.value {
|
||||||
hir::ModuleSource::SourceFile(it) => match it.items().last() {
|
hir::ModuleSource::SourceFile(it) => match it.items().last() {
|
||||||
Some(last_item) => GeneratedFunctionTarget::BehindItem(last_item.syntax().clone()),
|
Some(last_item) => GeneratedFunctionTarget::AfterItem(last_item.syntax().clone()),
|
||||||
None => GeneratedFunctionTarget::BehindItem(it.syntax().clone()),
|
None => GeneratedFunctionTarget::AfterItem(it.syntax().clone()),
|
||||||
},
|
},
|
||||||
hir::ModuleSource::Module(it) => match it.item_list().and_then(|it| it.items().last()) {
|
hir::ModuleSource::Module(it) => match it.item_list().and_then(|it| it.items().last()) {
|
||||||
Some(last_item) => GeneratedFunctionTarget::BehindItem(last_item.syntax().clone()),
|
Some(last_item) => GeneratedFunctionTarget::AfterItem(last_item.syntax().clone()),
|
||||||
None => GeneratedFunctionTarget::InEmptyItemList(it.item_list()?.syntax().clone()),
|
None => {
|
||||||
|
let item_list =
|
||||||
|
it.item_list().expect("module definition source should have an item list");
|
||||||
|
GeneratedFunctionTarget::InEmptyItemList(item_list.syntax().clone())
|
||||||
|
}
|
||||||
},
|
},
|
||||||
hir::ModuleSource::BlockExpr(it) => {
|
hir::ModuleSource::BlockExpr(it) => {
|
||||||
if let Some(last_item) =
|
if let Some(last_item) =
|
||||||
it.statements().take_while(|stmt| matches!(stmt, ast::Stmt::Item(_))).last()
|
it.statements().take_while(|stmt| matches!(stmt, ast::Stmt::Item(_))).last()
|
||||||
{
|
{
|
||||||
GeneratedFunctionTarget::BehindItem(last_item.syntax().clone())
|
GeneratedFunctionTarget::AfterItem(last_item.syntax().clone())
|
||||||
} else {
|
} else {
|
||||||
GeneratedFunctionTarget::InEmptyItemList(it.syntax().clone())
|
GeneratedFunctionTarget::InEmptyItemList(it.syntax().clone())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
Some((file, assist_item))
|
|
||||||
}
|
|
||||||
|
|
||||||
fn next_space_for_fn_in_impl(impl_: &ast::Impl) -> Option<GeneratedFunctionTarget> {
|
(file, assist_item)
|
||||||
let assoc_item_list = impl_.assoc_item_list()?;
|
|
||||||
if let Some(last_item) = assoc_item_list.assoc_items().last() {
|
|
||||||
Some(GeneratedFunctionTarget::BehindItem(last_item.syntax().clone()))
|
|
||||||
} else {
|
|
||||||
Some(GeneratedFunctionTarget::InEmptyItemList(assoc_item_list.syntax().clone()))
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, Copy)]
|
#[derive(Clone, Copy)]
|
||||||
|
|
|
@ -1,4 +1,7 @@
|
||||||
use syntax::ast::{self, AstNode};
|
use syntax::{
|
||||||
|
ast::{self, make, AstNode},
|
||||||
|
ted,
|
||||||
|
};
|
||||||
|
|
||||||
use crate::{utils::suggest_name, AssistContext, AssistId, AssistKind, Assists};
|
use crate::{utils::suggest_name, AssistContext, AssistId, AssistKind, Assists};
|
||||||
|
|
||||||
|
@ -42,19 +45,34 @@ pub(crate) fn replace_is_method_with_if_let_method(
|
||||||
suggest_name::for_variable(&receiver, &ctx.sema)
|
suggest_name::for_variable(&receiver, &ctx.sema)
|
||||||
};
|
};
|
||||||
|
|
||||||
let target = call_expr.syntax().text_range();
|
|
||||||
|
|
||||||
let (assist_id, message, text) = if name_ref.text() == "is_some" {
|
let (assist_id, message, text) = if name_ref.text() == "is_some" {
|
||||||
("replace_is_some_with_if_let_some", "Replace `is_some` with `if let Some`", "Some")
|
("replace_is_some_with_if_let_some", "Replace `is_some` with `if let Some`", "Some")
|
||||||
} else {
|
} else {
|
||||||
("replace_is_ok_with_if_let_ok", "Replace `is_ok` with `if let Ok`", "Ok")
|
("replace_is_ok_with_if_let_ok", "Replace `is_ok` with `if let Ok`", "Ok")
|
||||||
};
|
};
|
||||||
|
|
||||||
acc.add(AssistId(assist_id, AssistKind::RefactorRewrite), message, target, |edit| {
|
acc.add(
|
||||||
let var_name = format!("${{0:{}}}", var_name);
|
AssistId(assist_id, AssistKind::RefactorRewrite),
|
||||||
let replacement = format!("let {}({}) = {}", text, var_name, receiver);
|
message,
|
||||||
edit.replace(target, replacement);
|
call_expr.syntax().text_range(),
|
||||||
})
|
|edit| {
|
||||||
|
let call_expr = edit.make_mut(call_expr);
|
||||||
|
|
||||||
|
let var_pat = make::ident_pat(false, false, make::name(&var_name));
|
||||||
|
let pat = make::tuple_struct_pat(make::ext::ident_path(text), [var_pat.into()]);
|
||||||
|
let let_expr = make::expr_let(pat.into(), receiver).clone_for_update();
|
||||||
|
|
||||||
|
if let Some(cap) = ctx.config.snippet_cap {
|
||||||
|
if let Some(ast::Pat::TupleStructPat(pat)) = let_expr.pat() {
|
||||||
|
if let Some(first_var) = pat.fields().next() {
|
||||||
|
edit.add_placeholder_snippet(cap, first_var);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
ted::replace(call_expr.syntax(), let_expr.syntax());
|
||||||
|
},
|
||||||
|
)
|
||||||
}
|
}
|
||||||
_ => return None,
|
_ => return None,
|
||||||
}
|
}
|
||||||
|
|
|
@ -505,16 +505,33 @@ pub fn test_some_range(a: int) -> bool {
|
||||||
TextEdit {
|
TextEdit {
|
||||||
indels: [
|
indels: [
|
||||||
Indel {
|
Indel {
|
||||||
insert: "let $0var_name = 5;\n ",
|
insert: "let",
|
||||||
delete: 45..45,
|
delete: 45..47,
|
||||||
},
|
},
|
||||||
Indel {
|
Indel {
|
||||||
insert: "var_name",
|
insert: "var_name",
|
||||||
delete: 59..60,
|
delete: 48..60,
|
||||||
|
},
|
||||||
|
Indel {
|
||||||
|
insert: "=",
|
||||||
|
delete: 61..81,
|
||||||
|
},
|
||||||
|
Indel {
|
||||||
|
insert: "5;\n if let 2..6 = var_name {\n true\n } else {\n false\n }",
|
||||||
|
delete: 82..108,
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
None,
|
Some(
|
||||||
|
SnippetEdit(
|
||||||
|
[
|
||||||
|
(
|
||||||
|
0,
|
||||||
|
49..49,
|
||||||
|
),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
),
|
||||||
),
|
),
|
||||||
},
|
},
|
||||||
file_system_edits: [],
|
file_system_edits: [],
|
||||||
|
@ -567,16 +584,33 @@ pub fn test_some_range(a: int) -> bool {
|
||||||
TextEdit {
|
TextEdit {
|
||||||
indels: [
|
indels: [
|
||||||
Indel {
|
Indel {
|
||||||
insert: "let $0var_name = 5;\n ",
|
insert: "let",
|
||||||
delete: 45..45,
|
delete: 45..47,
|
||||||
},
|
},
|
||||||
Indel {
|
Indel {
|
||||||
insert: "var_name",
|
insert: "var_name",
|
||||||
delete: 59..60,
|
delete: 48..60,
|
||||||
|
},
|
||||||
|
Indel {
|
||||||
|
insert: "=",
|
||||||
|
delete: 61..81,
|
||||||
|
},
|
||||||
|
Indel {
|
||||||
|
insert: "5;\n if let 2..6 = var_name {\n true\n } else {\n false\n }",
|
||||||
|
delete: 82..108,
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
None,
|
Some(
|
||||||
|
SnippetEdit(
|
||||||
|
[
|
||||||
|
(
|
||||||
|
0,
|
||||||
|
49..49,
|
||||||
|
),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
),
|
||||||
),
|
),
|
||||||
},
|
},
|
||||||
file_system_edits: [],
|
file_system_edits: [],
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue