mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-11-18 03:13:31 +00:00
Merge pull request #20211 from Hmikihiro/migrate-convert_named_struct
Migrate `convert_named_struct_to_tuple_struct` assist to use 'SyntaxEditor'
This commit is contained in:
commit
05ca3f16b3
1 changed files with 9 additions and 6 deletions
|
|
@ -4,7 +4,8 @@ use itertools::Itertools;
|
||||||
use syntax::{
|
use syntax::{
|
||||||
SyntaxKind,
|
SyntaxKind,
|
||||||
ast::{self, AstNode, HasAttrs, HasGenericParams, HasVisibility},
|
ast::{self, AstNode, HasAttrs, HasGenericParams, HasVisibility},
|
||||||
match_ast, ted,
|
match_ast,
|
||||||
|
syntax_editor::{Position, SyntaxEditor},
|
||||||
};
|
};
|
||||||
|
|
||||||
use crate::{AssistContext, AssistId, Assists, assist_context::SourceChangeBuilder};
|
use crate::{AssistContext, AssistId, Assists, assist_context::SourceChangeBuilder};
|
||||||
|
|
@ -97,11 +98,14 @@ fn edit_struct_def(
|
||||||
// Note that we don't need to consider macro files in this function because this is
|
// Note that we don't need to consider macro files in this function because this is
|
||||||
// currently not triggered for struct definitions inside macro calls.
|
// currently not triggered for struct definitions inside macro calls.
|
||||||
let tuple_fields = record_fields.fields().filter_map(|f| {
|
let tuple_fields = record_fields.fields().filter_map(|f| {
|
||||||
let field = ast::make::tuple_field(f.visibility(), f.ty()?).clone_for_update();
|
let field = ast::make::tuple_field(f.visibility(), f.ty()?);
|
||||||
ted::insert_all(
|
let mut editor = SyntaxEditor::new(field.syntax().clone());
|
||||||
ted::Position::first_child_of(field.syntax()),
|
editor.insert_all(
|
||||||
|
Position::first_child_of(field.syntax()),
|
||||||
f.attrs().map(|attr| attr.syntax().clone_subtree().clone_for_update().into()).collect(),
|
f.attrs().map(|attr| attr.syntax().clone_subtree().clone_for_update().into()).collect(),
|
||||||
);
|
);
|
||||||
|
let field_syntax = editor.finish().new_root().clone();
|
||||||
|
let field = ast::TupleField::cast(field_syntax)?;
|
||||||
Some(field)
|
Some(field)
|
||||||
});
|
});
|
||||||
let tuple_fields = ast::make::tuple_field_list(tuple_fields);
|
let tuple_fields = ast::make::tuple_field_list(tuple_fields);
|
||||||
|
|
@ -1086,8 +1090,7 @@ pub struct $0Foo {
|
||||||
}
|
}
|
||||||
"#,
|
"#,
|
||||||
r#"
|
r#"
|
||||||
pub struct Foo(#[my_custom_attr]
|
pub struct Foo(#[my_custom_attr]u32);
|
||||||
u32);
|
|
||||||
"#,
|
"#,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue