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
54 changes: 53 additions & 1 deletion crates/ide-assists/src/handlers/destructure_tuple_binding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use ide_db::{
};
use itertools::Itertools;
use syntax::{
T,
ast::{self, AstNode, FieldExpr, HasName, IdentPat, make},
ted,
};
Expand Down Expand Up @@ -179,6 +180,11 @@ fn edit_tuple_assignment(
.map(|name| ast::Pat::from(make::ident_pat(is_ref, is_mut, make::name(name))));
make::tuple_pat(fields).clone_for_update()
};
let is_shorthand_field = ident_pat
.name()
.as_ref()
.and_then(ast::RecordPatField::for_field_name)
.is_some_and(|field| field.colon_token().is_none());

if let Some(cap) = ctx.config.snippet_cap {
// place cursor on first tuple name
Expand All @@ -190,19 +196,23 @@ fn edit_tuple_assignment(
}
}

AssignmentEdit { ident_pat, tuple_pat, in_sub_pattern }
AssignmentEdit { ident_pat, tuple_pat, in_sub_pattern, is_shorthand_field }
}
struct AssignmentEdit {
ident_pat: ast::IdentPat,
tuple_pat: ast::TuplePat,
in_sub_pattern: bool,
is_shorthand_field: bool,
}

impl AssignmentEdit {
fn apply(self) {
// with sub_pattern: keep original tuple and add subpattern: `tup @ (_0, _1)`
if self.in_sub_pattern {
self.ident_pat.set_pat(Some(self.tuple_pat.into()))
} else if self.is_shorthand_field {
ted::insert(ted::Position::after(self.ident_pat.syntax()), self.tuple_pat.syntax());
ted::insert_raw(ted::Position::after(self.ident_pat.syntax()), make::token(T![:]));
} else {
ted::replace(self.ident_pat.syntax(), self.tuple_pat.syntax())
}
Expand Down Expand Up @@ -799,6 +809,48 @@ fn main() {
)
}

#[test]
fn in_record_shorthand_field() {
check_assist(
assist,
r#"
struct S { field: (i32, i32) }
fn main() {
let S { $0field } = S { field: (2, 3) };
let v = field.0 + field.1;
}
"#,
r#"
struct S { field: (i32, i32) }
fn main() {
let S { field: ($0_0, _1) } = S { field: (2, 3) };
let v = _0 + _1;
}
"#,
)
}

#[test]
fn in_record_field() {
check_assist(
assist,
r#"
struct S { field: (i32, i32) }
fn main() {
let S { field: $0t } = S { field: (2, 3) };
let v = t.0 + t.1;
}
"#,
r#"
struct S { field: (i32, i32) }
fn main() {
let S { field: ($0_0, _1) } = S { field: (2, 3) };
let v = _0 + _1;
}
"#,
)
}

#[test]
fn in_nested_tuple() {
check_assist(
Expand Down
Loading