Unnamed repository; edit this file 'description' to name the repository.
add remove_attrs_and_docs editor variant to edit
bit-aloo 11 days ago
parent a6c1086 · commit 08fdbae
-rw-r--r--crates/syntax/src/ast/edit.rs24
1 files changed, 23 insertions, 1 deletions
diff --git a/crates/syntax/src/ast/edit.rs b/crates/syntax/src/ast/edit.rs
index 0ffcbd212b..a99fe5d51b 100644
--- a/crates/syntax/src/ast/edit.rs
+++ b/crates/syntax/src/ast/edit.rs
@@ -6,7 +6,7 @@ use std::{fmt, iter, ops};
use crate::{
AstToken, NodeOrToken, SyntaxElement,
- SyntaxKind::WHITESPACE,
+ SyntaxKind::{ATTR, COMMENT, WHITESPACE},
SyntaxNode, SyntaxToken,
ast::{self, AstNode, HasName, make},
syntax_editor::{Position, SyntaxEditor, SyntaxMappingBuilder},
@@ -197,6 +197,28 @@ pub trait AstNodeEdit: AstNode + Clone + Sized {
impl<N: AstNode + Clone> AstNodeEdit for N {}
+pub trait AttrsOwnerEdit: ast::HasAttrs {
+ fn remove_attrs_and_docs(&self, editor: &SyntaxEditor) {
+ let mut remove_next_ws = false;
+ for child in self.syntax().children_with_tokens() {
+ match child.kind() {
+ ATTR | COMMENT => {
+ remove_next_ws = true;
+ editor.delete(child);
+ continue;
+ }
+ WHITESPACE if remove_next_ws => {
+ editor.delete(child);
+ }
+ _ => (),
+ }
+ remove_next_ws = false;
+ }
+ }
+}
+
+impl<T: ast::HasAttrs> AttrsOwnerEdit for T {}
+
impl ast::IdentPat {
pub fn set_pat(&self, pat: Option<ast::Pat>, editor: &SyntaxEditor) -> ast::IdentPat {
let make = editor.make();