diff options
| author | DropDemBits <r3usrlnd@gmail.com> | 2023-06-24 21:20:57 -0400 |
|---|---|---|
| committer | DropDemBits <r3usrlnd@gmail.com> | 2023-07-07 20:06:09 -0400 |
| commit | 419f641d49beaf7db70aeea385dbfadc6283e906 (patch) | |
| tree | 474327f191374ccfd1c46da06a457fbcbdbe3018 | |
| parent | c0172333c2973add4c4a3575646733d31333df06 (diff) | |
| download | rust-419f641d49beaf7db70aeea385dbfadc6283e906.tar.gz rust-419f641d49beaf7db70aeea385dbfadc6283e906.zip | |
add `AttrsOwnerEdit::add_attr`
| -rw-r--r-- | crates/syntax/src/ast/edit_in_place.rs | 22 |
1 files changed, 22 insertions, 0 deletions
diff --git a/crates/syntax/src/ast/edit_in_place.rs b/crates/syntax/src/ast/edit_in_place.rs index 5919609d91e..1f5ed206b4a 100644 --- a/crates/syntax/src/ast/edit_in_place.rs +++ b/crates/syntax/src/ast/edit_in_place.rs @@ -213,6 +213,28 @@ pub trait AttrsOwnerEdit: ast::HasAttrs { } } } + + fn add_attr(&self, attr: ast::Attr) { + add_attr(self.syntax(), attr); + + fn add_attr(node: &SyntaxNode, attr: ast::Attr) { + let indent = IndentLevel::from_node(node); + attr.reindent_to(indent); + + let after_attrs_and_comments = node + .children_with_tokens() + .find(|it| !matches!(it.kind(), WHITESPACE | COMMENT | ATTR)) + .map_or(Position::first_child_of(node), |it| Position::before(it)); + + ted::insert_all( + after_attrs_and_comments, + vec![ + attr.syntax().clone().into(), + make::tokens::whitespace(&format!("\n{indent}")).into(), + ], + ) + } + } } impl<T: ast::HasAttrs> AttrsOwnerEdit for T {} |
