mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-10-22 00:01:53 +00:00
Merge 3548396edf
into 6c3f962efd
This commit is contained in:
commit
e80d615895
1 changed files with 53 additions and 1 deletions
|
@ -7,6 +7,7 @@ use ide_db::{
|
||||||
};
|
};
|
||||||
use itertools::Itertools;
|
use itertools::Itertools;
|
||||||
use syntax::{
|
use syntax::{
|
||||||
|
T,
|
||||||
ast::{self, AstNode, FieldExpr, HasName, IdentPat, make},
|
ast::{self, AstNode, FieldExpr, HasName, IdentPat, make},
|
||||||
ted,
|
ted,
|
||||||
};
|
};
|
||||||
|
@ -179,6 +180,11 @@ fn edit_tuple_assignment(
|
||||||
.map(|name| ast::Pat::from(make::ident_pat(is_ref, is_mut, make::name(name))));
|
.map(|name| ast::Pat::from(make::ident_pat(is_ref, is_mut, make::name(name))));
|
||||||
make::tuple_pat(fields).clone_for_update()
|
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 {
|
if let Some(cap) = ctx.config.snippet_cap {
|
||||||
// place cursor on first tuple name
|
// place cursor on first tuple name
|
||||||
|
@ -190,12 +196,13 @@ 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 {
|
struct AssignmentEdit {
|
||||||
ident_pat: ast::IdentPat,
|
ident_pat: ast::IdentPat,
|
||||||
tuple_pat: ast::TuplePat,
|
tuple_pat: ast::TuplePat,
|
||||||
in_sub_pattern: bool,
|
in_sub_pattern: bool,
|
||||||
|
is_shorthand_field: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl AssignmentEdit {
|
impl AssignmentEdit {
|
||||||
|
@ -203,6 +210,9 @@ impl AssignmentEdit {
|
||||||
// with sub_pattern: keep original tuple and add subpattern: `tup @ (_0, _1)`
|
// with sub_pattern: keep original tuple and add subpattern: `tup @ (_0, _1)`
|
||||||
if self.in_sub_pattern {
|
if self.in_sub_pattern {
|
||||||
self.ident_pat.set_pat(Some(self.tuple_pat.into()))
|
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 {
|
} else {
|
||||||
ted::replace(self.ident_pat.syntax(), self.tuple_pat.syntax())
|
ted::replace(self.ident_pat.syntax(), self.tuple_pat.syntax())
|
||||||
}
|
}
|
||||||
|
@ -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]
|
#[test]
|
||||||
fn in_nested_tuple() {
|
fn in_nested_tuple() {
|
||||||
check_assist(
|
check_assist(
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue