Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
110 changes: 106 additions & 4 deletions crates/ide-assists/src/handlers/extract_variable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,16 @@ pub(crate) fn extract_variable(acc: &mut Assists, ctx: &AssistContext<'_, '_>) -
if let Some(t) = ctx.token_at_offset().find(|it| it.kind() == T![;]) {
t.parent().and_then(ast::ExprStmt::cast)?.syntax().clone()
} else {
let expr = ancestors_at_offset(ctx.source_file().syntax(), ctx.offset())
.next()
.and_then(ast::Expr::cast)?;
expr.syntax().ancestors().find_map(valid_target_expr(ctx))?.syntax().clone()
// Offer the assist only if the nearest syntax node is an expression, or a record
// field, or a record field’s name. This prevents the assist from appearing when
// it is unlikely to be relevant, such as when the cursor is in a pattern.
// (If we did not want to restrict it this way, we could just apply
// `valid_target_expr()` to all ancestors.)

@A4-Tacks A4-Tacks Aug 26, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Redundant comments, we have testing

View changes since the review

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment is not for specifying the desired functionality, but for someone reading the code (like me) to understand what the goal of this piece of the code is. Not having this part explained was a significant obstacle; I had to dig into git-blame to understand what the code was supposed to achieve.

let expr_or_field = ancestors_at_offset(ctx.source_file().syntax(), ctx.offset())
.find(|it| !ast::NameRef::can_cast(it.kind()))
.and_then(either::Either::<ast::Expr, ast::RecordExprField>::cast)?;

expr_or_field.syntax().ancestors().find_map(valid_target_expr(ctx))?.syntax().clone()
}
} else {
match ctx.covering_element() {
Expand Down Expand Up @@ -367,6 +373,11 @@ fn valid_target_expr(ctx: &AssistContext<'_, '_>) -> impl Fn(SyntaxNode) -> Opti
let path_resolution = ctx.sema.resolve_path(&path_expr.path()?)?;
like_const_value(ctx, path_resolution).then_some(path_expr.into())
}
SyntaxKind::RECORD_EXPR_FIELD => {
// If we are on `k` in `Struct { k: v }`, then extract `v`.
let record_field = ast::RecordExprField::cast(node)?;
record_field.expr()
}
_ => ast::Expr::cast(node),
}
}
Expand Down Expand Up @@ -952,6 +963,16 @@ fn foo() {
check_assist_not_applicable(extract_variable, r#"fn main() { 1 + /* $0comment$0 */ 1; }"#);
}

#[test]
fn dont_extract_in_pattern_with_selection() {
check_assist_not_applicable(extract_variable, r#"fn foo() { [].map(|$0bar$0| bar + 1) } "#);
}

#[test]
fn dont_extract_in_pattern_without_selection() {
check_assist_not_applicable(extract_variable, r#"fn foo() { [].map(|b$0ar| bar + 1) } "#);
}

#[test]
fn extract_var_expr_stmt() {
cov_mark::check!(test_extract_var_expr_stmt);
Expand Down Expand Up @@ -1586,6 +1607,87 @@ struct S {
foo: i32
}

fn main() {
let $0foo = 1 + 1;
S { foo }
}
"#,
"Extract into variable",
)
}

#[test]
fn extract_var_from_record_field() {
check_assist_by_label(
extract_variable,
r#"
struct S {
foo: i32
}

fn main() {
S { $0foo: 1 + 1,$0 }
}
"#,
r#"
struct S {
foo: i32
}

fn main() {
let $0foo = 1 + 1;
S { foo, }
}
"#,
"Extract into variable",
)
}

#[test]
fn extract_var_from_record_field_name() {
check_assist_by_label(
extract_variable,
r#"
struct S {
foo: i32
}

fn main() {
S { f$0oo: 1 + 1 }
}
"#,
r#"
struct S {
foo: i32
}

fn main() {
let $0foo = 1 + 1;
S { foo }
}
"#,
"Extract into variable",
)
}

#[test]
fn extract_var_from_record_field_colon() {
check_assist_by_label(
extract_variable,
r#"
struct S {
foo: i32
}

fn main() {
S { foo $0: 1 + 1 }
}
"#,
r#"
struct S {
foo: i32
}

fn main() {
let $0foo = 1 + 1;
S { foo }
Expand Down