Skip to content
Open
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
114 changes: 111 additions & 3 deletions crates/ide-assists/src/handlers/extract_variable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,22 @@ 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())
let node = 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()
.filter(|node| {
// Don’t offer the assist if we encounter a node that is not one of the kinds
// we want. This prevents the assist from appearing when, while there is an
// enclosing expression, it is unlikely to be relevant.
let kind = node.kind();
ast::Expr::can_cast(kind)
|| kind == SyntaxKind::RECORD_EXPR_FIELD
|| (kind == SyntaxKind::NAME_REF
&& node
.parent()
.is_some_and(|p| p.kind() == SyntaxKind::RECORD_EXPR_FIELD))
})?;

node.ancestors().find_map(valid_target_expr(ctx))?.syntax().clone()

@A4-Tacks A4-Tacks Aug 25, 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.

Don't be so complicated, just:

             let expr = ancestors_at_offset(ctx.source_file().syntax(), ctx.offset())
-                .next()
-                .and_then(ast::Expr::cast)?;
+                .find(|it| !ast::NameRef::can_cast(it.kind()))
+                .and_then(either::Either::<ast::Expr, ast::RecordExprField>::cast)?;
             expr.syntax().ancestors().find_map(valid_target_expr(ctx))?.syntax().clone()

View changes since the review

}
} else {
match ctx.covering_element() {
Expand Down Expand Up @@ -367,6 +379,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 +969,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 +1613,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