mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-30 22:01:37 +00:00
cleaned up record field builder
This commit is contained in:
parent
69689625ce
commit
25fca04753
2 changed files with 4 additions and 7 deletions
|
@ -1,6 +1,5 @@
|
|||
use itertools::Itertools;
|
||||
|
||||
use hir::Name;
|
||||
use ra_syntax::{ast, AstNode, SourceFile};
|
||||
|
||||
pub struct AstBuilder<N: AstNode> {
|
||||
|
@ -8,15 +7,11 @@ pub struct AstBuilder<N: AstNode> {
|
|||
}
|
||||
|
||||
impl AstBuilder<ast::RecordField> {
|
||||
pub fn from_name(name: &Name) -> ast::RecordField {
|
||||
ast_node_from_file_text(&format!("fn f() {{ S {{ {}: (), }} }}", name))
|
||||
}
|
||||
|
||||
fn from_text(text: &str) -> ast::RecordField {
|
||||
ast_node_from_file_text(&format!("fn f() {{ S {{ {}, }} }}", text))
|
||||
}
|
||||
|
||||
pub fn from_pieces(name: &ast::NameRef, expr: Option<&ast::Expr>) -> ast::RecordField {
|
||||
pub fn from_pieces(name: ast::NameRef, expr: Option<ast::Expr>) -> ast::RecordField {
|
||||
match expr {
|
||||
Some(expr) => Self::from_text(&format!("{}: {}", name.syntax(), expr.syntax())),
|
||||
None => Self::from_text(&name.syntax().to_string()),
|
||||
|
|
|
@ -59,7 +59,9 @@ pub(crate) fn diagnostics(db: &RootDatabase, file_id: FileId) -> Vec<Diagnostic>
|
|||
let node = d.ast(db);
|
||||
let mut ast_editor = AstEditor::new(node);
|
||||
for f in d.missed_fields.iter() {
|
||||
ast_editor.append_field(&AstBuilder::<RecordField>::from_name(f));
|
||||
let name_ref = AstBuilder::<ast::NameRef>::new(&f.to_string());
|
||||
let unit = AstBuilder::<ast::Expr>::unit();
|
||||
ast_editor.append_field(&AstBuilder::<RecordField>::from_pieces(name_ref, Some(unit)));
|
||||
}
|
||||
|
||||
let mut builder = TextEditBuilder::default();
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue