mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-28 21:05:02 +00:00
Merge #11887
11887: fix: Add missing fields diagnostic fix for patterns r=Veykril a=Veykril bors r+ Co-authored-by: Lukas Wirth <lukastw97@gmail.com>
This commit is contained in:
commit
65eaabc200
4 changed files with 184 additions and 62 deletions
|
@ -563,6 +563,49 @@ impl ast::RecordExprField {
|
|||
}
|
||||
}
|
||||
|
||||
impl ast::RecordPatFieldList {
|
||||
pub fn add_field(&self, field: ast::RecordPatField) {
|
||||
let is_multiline = self.syntax().text().contains_char('\n');
|
||||
let whitespace = if is_multiline {
|
||||
let indent = IndentLevel::from_node(self.syntax()) + 1;
|
||||
make::tokens::whitespace(&format!("\n{}", indent))
|
||||
} else {
|
||||
make::tokens::single_space()
|
||||
};
|
||||
|
||||
if is_multiline {
|
||||
normalize_ws_between_braces(self.syntax());
|
||||
}
|
||||
|
||||
let position = match self.fields().last() {
|
||||
Some(last_field) => {
|
||||
let comma = match last_field
|
||||
.syntax()
|
||||
.siblings_with_tokens(Direction::Next)
|
||||
.filter_map(|it| it.into_token())
|
||||
.find(|it| it.kind() == T![,])
|
||||
{
|
||||
Some(it) => it,
|
||||
None => {
|
||||
let comma = ast::make::token(T![,]);
|
||||
ted::insert(Position::after(last_field.syntax()), &comma);
|
||||
comma
|
||||
}
|
||||
};
|
||||
Position::after(comma)
|
||||
}
|
||||
None => match self.l_curly_token() {
|
||||
Some(it) => Position::after(it),
|
||||
None => Position::last_child_of(self.syntax()),
|
||||
},
|
||||
};
|
||||
|
||||
ted::insert_all(position, vec![whitespace.into(), field.syntax().clone().into()]);
|
||||
if is_multiline {
|
||||
ted::insert(Position::after(field.syntax()), ast::make::token(T![,]));
|
||||
}
|
||||
}
|
||||
}
|
||||
impl ast::StmtList {
|
||||
pub fn push_front(&self, statement: ast::Stmt) {
|
||||
ted::insert(Position::after(self.l_curly_token().unwrap()), statement.syntax());
|
||||
|
|
|
@ -555,6 +555,10 @@ pub fn record_pat_field(name_ref: ast::NameRef, pat: ast::Pat) -> ast::RecordPat
|
|||
ast_from_text(&format!("fn f(S {{ {}: {} }}: ()))", name_ref, pat))
|
||||
}
|
||||
|
||||
pub fn record_pat_field_shorthand(name_ref: ast::NameRef) -> ast::RecordPatField {
|
||||
ast_from_text(&format!("fn f(S {{ {} }}: ()))", name_ref))
|
||||
}
|
||||
|
||||
/// Returns a `BindPat` if the path has just one segment, a `PathPat` otherwise.
|
||||
pub fn path_pat(path: ast::Path) -> ast::Pat {
|
||||
return from_text(&path.to_string());
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue