Add ide-assist: add_reference

Add reference for function parameter.

```rust
fn foo(arg0: i32, $0arg1: [i32; 32]) {}
fn bar() {
    foo(5, [8; 32])
}
```
->
```
fn foo(arg0: i32, arg1: &[i32; 32]) {}
fn bar() {
    foo(5, &[8; 32])
}
```

---

```rust
fn foo(arg0: i32, mut $0arg1: [i32; 32]) {}
fn bar() {
    foo(5, [8; 32])
}
```
->
```
fn foo(arg0: i32, arg1: &mut [i32; 32]) {}
fn bar() {
    foo(5, &mut [8; 32])
}
```
This commit is contained in:
A4-Tacks 2025-08-26 08:00:07 +08:00
parent db7b3b1b91
commit 002a1a8bca
No known key found for this signature in database
GPG key ID: 86AC1F526BA06668
3 changed files with 294 additions and 0 deletions

View file

@ -0,0 +1,254 @@
use std::ops::ControlFlow;
use ide_db::{defs::Definition, syntax_helpers::node_ext::walk_pat};
use syntax::{
AstNode, SourceFile, SyntaxKind, T, TextRange,
algo::find_node_at_range,
ast::{self, HasArgList, prec::ExprPrecedence, syntax_factory::SyntaxFactory},
syntax_editor::{Element, Position, SyntaxEditor},
};
use crate::{AssistContext, AssistId, Assists, assist_context::SourceChangeBuilder};
// Assist: add_reference
//
// Add reference for function parameter.
//
// ```
// fn foo(arg0: i32, $0arg1: [i32; 32]) {}
// fn bar() {
// foo(5, [8; 32])
// }
// ```
// ->
// ```
// fn foo(arg0: i32, arg1: &[i32; 32]) {}
// fn bar() {
// foo(5, &[8; 32])
// }
// ```
//
// ---
//
// ```
// fn foo(arg0: i32, mut $0arg1: [i32; 32]) {}
// fn bar() {
// foo(5, [8; 32])
// }
// ```
// ->
// ```
// fn foo(arg0: i32, arg1: &mut [i32; 32]) {}
// fn bar() {
// foo(5, &mut [8; 32])
// }
// ```
pub(crate) fn add_reference(acc: &mut Assists, ctx: &AssistContext<'_>) -> Option<()> {
let param = ctx.find_node_at_offset::<ast::Param>()?;
let pat = param.pat()?;
let ty = param.ty()?;
let param_list = ast::ParamList::cast(param.syntax().parent()?)?;
let fn_ = ast::Fn::cast(param_list.syntax().parent()?)?;
let param_nth = param_list.params().position(|it| it == param)?;
let ControlFlow::Break(name) = walk_pat(&pat, &mut |pat| {
if let ast::Pat::IdentPat(it) = pat {
return ControlFlow::Break(it);
}
ControlFlow::Continue(())
}) else {
return None;
};
let mutable = name.mut_token().is_some();
if ctx.offset() > pat.syntax().text_range().end()
|| matches!(&ty, ast::Type::RefType(ty) if (ty.mut_token().is_some(), mutable) != (false, true))
{
return None;
}
let sema = &ctx.sema;
let fn_def = Definition::Function(sema.to_def(&fn_)?);
acc.add(
AssistId::refactor("add_reference"),
format!("Add {}reference for parameter", if mutable { "mutable " } else { "" }),
pat.syntax().text_range(),
|builder| {
let mut edit = builder.make_editor(ty.syntax());
let make = SyntaxFactory::with_mappings();
if mutable {
insert_mut(&ty, &mut edit, &make);
}
edit.insert(Position::before(ty.syntax()), make.token(T![&]));
if let Some(mut_token) = name.mut_token() {
delete_mut(&mut edit, mut_token);
}
for (file_id, refs) in fn_def.usages(sema).all() {
let source_file = sema.parse(file_id);
let file_id = file_id.file_id(sema.db);
builder.edit_file(file_id);
for reference in refs {
let _ = process_usage(
&source_file,
param_nth,
mutable,
file_id,
reference.range,
builder,
);
}
}
edit.add_mappings(make.finish_with_mappings());
builder.add_file_edits(ctx.vfs_file_id(), edit);
},
)
}
fn process_usage(
source_file: &SourceFile,
param_nth: usize,
mutable: bool,
file_id: ide_db::FileId,
reference_range: TextRange,
builder: &mut SourceChangeBuilder,
) -> Option<()> {
let call_expr = find_node_at_range::<ast::CallExpr>(source_file.syntax(), reference_range)?;
let mut edit = builder.make_editor(call_expr.syntax());
let make = SyntaxFactory::with_mappings();
let arg = call_expr.arg_list()?.args().nth(param_nth)?;
if mutable {
insert_mut(&arg, &mut edit, &make);
}
edit.insert(Position::before(arg.syntax()), make.token(T![&]));
if arg.precedence().needs_parentheses_in(ExprPrecedence::Prefix) {
let paren_expr = make.expr_paren(arg.clone());
edit.replace(arg.syntax(), paren_expr.syntax());
}
edit.add_mappings(make.finish_with_mappings());
builder.add_file_edits(file_id, edit);
Some(())
}
fn insert_mut(node: &impl AstNode, edit: &mut SyntaxEditor, make: &SyntaxFactory) {
edit.insert_all(
Position::before(node.syntax()),
vec![make.token(T![mut]).into(), make.whitespace(" ").into()],
);
}
fn delete_mut(edit: &mut SyntaxEditor, token: impl Element) {
let element = token.syntax_element();
if let Some(next_token) = element.next_sibling_or_token()
&& next_token.kind() == SyntaxKind::WHITESPACE
{
edit.delete(next_token);
}
edit.delete(element);
}
#[cfg(test)]
mod tests {
use crate::tests::{check_assist, check_assist_not_applicable};
use super::*;
#[test]
fn test_add_reference() {
check_assist(
add_reference,
"
fn foo(arg0: i32, $0arg1: [i32; 32]) {}
fn bar() {
foo(5, [8; 32])
}
",
"
fn foo(arg0: i32, arg1: &[i32; 32]) {}
fn bar() {
foo(5, &[8; 32])
}
",
);
}
#[test]
fn test_add_mutable_reference() {
check_assist(
add_reference,
"
fn foo(arg0: i32, $0mut arg1: [i32; 32]) {}
fn bar() {
foo(5, [8; 32])
}
",
"
fn foo(arg0: i32, arg1: &mut [i32; 32]) {}
fn bar() {
foo(5, &mut [8; 32])
}
",
);
}
#[test]
fn test_add_mutable_reference_reference() {
check_assist(
add_reference,
"
fn foo(arg0: i32, $0mut arg1: &[i32; 32]) {}
fn bar() {
foo(5, &[8; 32])
}
",
"
fn foo(arg0: i32, arg1: &mut &[i32; 32]) {}
fn bar() {
foo(5, &mut &[8; 32])
}
",
);
}
#[test]
fn test_add_reference_not_applicable_ref_type() {
check_assist_not_applicable(
add_reference,
"
fn foo(arg0: i32, $0arg1: &[i32; 32]) {}
fn bar() {
foo(5, &[8; 32])
}
",
);
check_assist_not_applicable(
add_reference,
"
fn foo(arg0: i32, $0arg1: &mut [i32; 32]) {}
fn bar() {
foo(5, &mut [8; 32])
}
",
);
check_assist_not_applicable(
add_reference,
"
fn foo(arg0: i32, mut $0arg1: &mut [i32; 32]) {}
fn bar() {
foo(5, &mut [8; 32])
}
",
);
}
}

View file

@ -111,6 +111,7 @@ mod handlers {
mod add_lifetime_to_type;
mod add_missing_impl_members;
mod add_missing_match_arms;
mod add_reference;
mod add_return_type;
mod add_turbo_fish;
mod apply_demorgan;
@ -244,6 +245,7 @@ mod handlers {
add_lifetime_to_type::add_lifetime_to_type,
add_missing_match_arms::add_missing_match_arms,
add_return_type::add_return_type,
add_reference::add_reference,
add_turbo_fish::add_turbo_fish,
apply_demorgan::apply_demorgan_iterator,
apply_demorgan::apply_demorgan,

View file

@ -218,6 +218,44 @@ fn handle(action: Action) {
)
}
#[test]
fn doctest_add_reference() {
check_doc_test(
"add_reference",
r#####"
fn foo(arg0: i32, $0arg1: [i32; 32]) {}
fn bar() {
foo(5, [8; 32])
}
"#####,
r#####"
fn foo(arg0: i32, arg1: &[i32; 32]) {}
fn bar() {
foo(5, &[8; 32])
}
"#####,
)
}
#[test]
fn doctest_add_reference_1() {
check_doc_test(
"add_reference",
r#####"
fn foo(arg0: i32, mut $0arg1: [i32; 32]) {}
fn bar() {
foo(5, [8; 32])
}
"#####,
r#####"
fn foo(arg0: i32, arg1: &mut [i32; 32]) {}
fn bar() {
foo(5, &mut [8; 32])
}
"#####,
)
}
#[test]
fn doctest_add_return_type() {
check_doc_test(