Skip to content

Commit dbfbe8f

Browse files
authored
Merge pull request #20712 from A4-Tacks/destruct-tuple-shorthand
Fix shorthand field pat for destructure_tuple_binding
2 parents 10066b3 + 404f749 commit dbfbe8f

File tree

1 file changed

+53
-1
lines changed

1 file changed

+53
-1
lines changed

src/tools/rust-analyzer/crates/ide-assists/src/handlers/destructure_tuple_binding.rs

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use ide_db::{
77
};
88
use itertools::Itertools;
99
use syntax::{
10+
T,
1011
ast::{self, AstNode, FieldExpr, HasName, IdentPat, make},
1112
ted,
1213
};
@@ -179,6 +180,11 @@ fn edit_tuple_assignment(
179180
.map(|name| ast::Pat::from(make::ident_pat(is_ref, is_mut, make::name(name))));
180181
make::tuple_pat(fields).clone_for_update()
181182
};
183+
let is_shorthand_field = ident_pat
184+
.name()
185+
.as_ref()
186+
.and_then(ast::RecordPatField::for_field_name)
187+
.is_some_and(|field| field.colon_token().is_none());
182188

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

193-
AssignmentEdit { ident_pat, tuple_pat, in_sub_pattern }
199+
AssignmentEdit { ident_pat, tuple_pat, in_sub_pattern, is_shorthand_field }
194200
}
195201
struct AssignmentEdit {
196202
ident_pat: ast::IdentPat,
197203
tuple_pat: ast::TuplePat,
198204
in_sub_pattern: bool,
205+
is_shorthand_field: bool,
199206
}
200207

201208
impl AssignmentEdit {
202209
fn apply(self) {
203210
// with sub_pattern: keep original tuple and add subpattern: `tup @ (_0, _1)`
204211
if self.in_sub_pattern {
205212
self.ident_pat.set_pat(Some(self.tuple_pat.into()))
213+
} else if self.is_shorthand_field {
214+
ted::insert(ted::Position::after(self.ident_pat.syntax()), self.tuple_pat.syntax());
215+
ted::insert_raw(ted::Position::after(self.ident_pat.syntax()), make::token(T![:]));
206216
} else {
207217
ted::replace(self.ident_pat.syntax(), self.tuple_pat.syntax())
208218
}
@@ -799,6 +809,48 @@ fn main() {
799809
)
800810
}
801811

812+
#[test]
813+
fn in_record_shorthand_field() {
814+
check_assist(
815+
assist,
816+
r#"
817+
struct S { field: (i32, i32) }
818+
fn main() {
819+
let S { $0field } = S { field: (2, 3) };
820+
let v = field.0 + field.1;
821+
}
822+
"#,
823+
r#"
824+
struct S { field: (i32, i32) }
825+
fn main() {
826+
let S { field: ($0_0, _1) } = S { field: (2, 3) };
827+
let v = _0 + _1;
828+
}
829+
"#,
830+
)
831+
}
832+
833+
#[test]
834+
fn in_record_field() {
835+
check_assist(
836+
assist,
837+
r#"
838+
struct S { field: (i32, i32) }
839+
fn main() {
840+
let S { field: $0t } = S { field: (2, 3) };
841+
let v = t.0 + t.1;
842+
}
843+
"#,
844+
r#"
845+
struct S { field: (i32, i32) }
846+
fn main() {
847+
let S { field: ($0_0, _1) } = S { field: (2, 3) };
848+
let v = _0 + _1;
849+
}
850+
"#,
851+
)
852+
}
853+
802854
#[test]
803855
fn in_nested_tuple() {
804856
check_assist(

0 commit comments

Comments
 (0)