Skip to content

Commit b54e4b8

Browse files
bors[bot]matklad
andauthored
Merge #8109
8109: Make ast editing more ergonomic r=matklad a=matklad bors r+ 🤖 Co-authored-by: Aleksey Kladov <[email protected]>
2 parents 2813afd + a616910 commit b54e4b8

File tree

2 files changed

+66
-40
lines changed

2 files changed

+66
-40
lines changed

crates/syntax/src/ast/edit_in_place.rs

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use parser::T;
88
use crate::{
99
ast,
1010
ted::{self, Position},
11-
AstNode, Direction, SyntaxElement,
11+
AstNode, Direction,
1212
};
1313

1414
use super::NameOwner;
@@ -21,11 +21,11 @@ impl GenericParamsOwnerEdit for ast::Fn {
2121
fn get_or_create_where_clause(&self) -> WhereClause {
2222
if self.where_clause().is_none() {
2323
let position = if let Some(ty) = self.ret_type() {
24-
Position::after(ty.syntax().clone())
24+
Position::after(ty.syntax())
2525
} else if let Some(param_list) = self.param_list() {
26-
Position::after(param_list.syntax().clone())
26+
Position::after(param_list.syntax())
2727
} else {
28-
Position::last_child_of(self.syntax().clone())
28+
Position::last_child_of(self.syntax())
2929
};
3030
create_where_clause(position)
3131
}
@@ -37,9 +37,9 @@ impl GenericParamsOwnerEdit for ast::Impl {
3737
fn get_or_create_where_clause(&self) -> WhereClause {
3838
if self.where_clause().is_none() {
3939
let position = if let Some(items) = self.assoc_item_list() {
40-
Position::before(items.syntax().clone())
40+
Position::before(items.syntax())
4141
} else {
42-
Position::last_child_of(self.syntax().clone())
42+
Position::last_child_of(self.syntax())
4343
};
4444
create_where_clause(position)
4545
}
@@ -51,9 +51,9 @@ impl GenericParamsOwnerEdit for ast::Trait {
5151
fn get_or_create_where_clause(&self) -> WhereClause {
5252
if self.where_clause().is_none() {
5353
let position = if let Some(items) = self.assoc_item_list() {
54-
Position::before(items.syntax().clone())
54+
Position::before(items.syntax())
5555
} else {
56-
Position::last_child_of(self.syntax().clone())
56+
Position::last_child_of(self.syntax())
5757
};
5858
create_where_clause(position)
5959
}
@@ -69,13 +69,13 @@ impl GenericParamsOwnerEdit for ast::Struct {
6969
ast::FieldList::TupleFieldList(it) => Some(it),
7070
});
7171
let position = if let Some(tfl) = tfl {
72-
Position::after(tfl.syntax().clone())
72+
Position::after(tfl.syntax())
7373
} else if let Some(gpl) = self.generic_param_list() {
74-
Position::after(gpl.syntax().clone())
74+
Position::after(gpl.syntax())
7575
} else if let Some(name) = self.name() {
76-
Position::after(name.syntax().clone())
76+
Position::after(name.syntax())
7777
} else {
78-
Position::last_child_of(self.syntax().clone())
78+
Position::last_child_of(self.syntax())
7979
};
8080
create_where_clause(position)
8181
}
@@ -87,11 +87,11 @@ impl GenericParamsOwnerEdit for ast::Enum {
8787
fn get_or_create_where_clause(&self) -> WhereClause {
8888
if self.where_clause().is_none() {
8989
let position = if let Some(gpl) = self.generic_param_list() {
90-
Position::after(gpl.syntax().clone())
90+
Position::after(gpl.syntax())
9191
} else if let Some(name) = self.name() {
92-
Position::after(name.syntax().clone())
92+
Position::after(name.syntax())
9393
} else {
94-
Position::last_child_of(self.syntax().clone())
94+
Position::last_child_of(self.syntax())
9595
};
9696
create_where_clause(position)
9797
}
@@ -100,19 +100,18 @@ impl GenericParamsOwnerEdit for ast::Enum {
100100
}
101101

102102
fn create_where_clause(position: Position) {
103-
let where_clause: SyntaxElement =
104-
make::where_clause(empty()).clone_for_update().syntax().clone().into();
105-
ted::insert(position, where_clause);
103+
let where_clause = make::where_clause(empty()).clone_for_update();
104+
ted::insert(position, where_clause.syntax());
106105
}
107106

108107
impl ast::WhereClause {
109108
pub fn add_predicate(&self, predicate: ast::WherePred) {
110109
if let Some(pred) = self.predicates().last() {
111110
if !pred.syntax().siblings_with_tokens(Direction::Next).any(|it| it.kind() == T![,]) {
112-
ted::append_child_raw(self.syntax().clone(), make::token(T![,]));
111+
ted::append_child_raw(self.syntax(), make::token(T![,]));
113112
}
114113
}
115-
ted::append_child(self.syntax().clone(), predicate.syntax().clone())
114+
ted::append_child(self.syntax(), predicate.syntax())
116115
}
117116
}
118117

@@ -123,7 +122,7 @@ impl ast::TypeBoundList {
123122
{
124123
ted::remove_all(colon..=self.syntax().clone().into())
125124
} else {
126-
ted::remove(self.syntax().clone())
125+
ted::remove(self.syntax())
127126
}
128127
}
129128
}

crates/syntax/src/ted.rs

Lines changed: 46 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,33 @@ use parser::T;
88

99
use crate::{ast::make, SyntaxElement, SyntaxKind, SyntaxNode, SyntaxToken};
1010

11+
/// Utility trait to allow calling `ted` functions with references or owned
12+
/// nodes. Do not use outside of this module.
13+
pub trait Element {
14+
fn syntax_element(self) -> SyntaxElement;
15+
}
16+
17+
impl<E: Element + Clone> Element for &'_ E {
18+
fn syntax_element(self) -> SyntaxElement {
19+
self.clone().syntax_element()
20+
}
21+
}
22+
impl Element for SyntaxElement {
23+
fn syntax_element(self) -> SyntaxElement {
24+
self
25+
}
26+
}
27+
impl Element for SyntaxNode {
28+
fn syntax_element(self) -> SyntaxElement {
29+
self.into()
30+
}
31+
}
32+
impl Element for SyntaxToken {
33+
fn syntax_element(self) -> SyntaxElement {
34+
self.into()
35+
}
36+
}
37+
1138
#[derive(Debug)]
1239
pub struct Position {
1340
repr: PositionRepr,
@@ -20,24 +47,24 @@ enum PositionRepr {
2047
}
2148

2249
impl Position {
23-
pub fn after(elem: impl Into<SyntaxElement>) -> Position {
24-
let repr = PositionRepr::After(elem.into());
50+
pub fn after(elem: impl Element) -> Position {
51+
let repr = PositionRepr::After(elem.syntax_element());
2552
Position { repr }
2653
}
27-
pub fn before(elem: impl Into<SyntaxElement>) -> Position {
28-
let elem = elem.into();
54+
pub fn before(elem: impl Element) -> Position {
55+
let elem = elem.syntax_element();
2956
let repr = match elem.prev_sibling_or_token() {
3057
Some(it) => PositionRepr::After(it),
3158
None => PositionRepr::FirstChild(elem.parent().unwrap()),
3259
};
3360
Position { repr }
3461
}
35-
pub fn first_child_of(node: impl Into<SyntaxNode>) -> Position {
36-
let repr = PositionRepr::FirstChild(node.into());
62+
pub fn first_child_of(node: &(impl Into<SyntaxNode> + Clone)) -> Position {
63+
let repr = PositionRepr::FirstChild(node.clone().into());
3764
Position { repr }
3865
}
39-
pub fn last_child_of(node: impl Into<SyntaxNode>) -> Position {
40-
let node = node.into();
66+
pub fn last_child_of(node: &(impl Into<SyntaxNode> + Clone)) -> Position {
67+
let node = node.clone().into();
4168
let repr = match node.last_child_or_token() {
4269
Some(it) => PositionRepr::After(it),
4370
None => PositionRepr::FirstChild(node),
@@ -46,11 +73,11 @@ impl Position {
4673
}
4774
}
4875

49-
pub fn insert(position: Position, elem: impl Into<SyntaxElement>) {
50-
insert_all(position, vec![elem.into()])
76+
pub fn insert(position: Position, elem: impl Element) {
77+
insert_all(position, vec![elem.syntax_element()])
5178
}
52-
pub fn insert_raw(position: Position, elem: impl Into<SyntaxElement>) {
53-
insert_all_raw(position, vec![elem.into()])
79+
pub fn insert_raw(position: Position, elem: impl Element) {
80+
insert_all_raw(position, vec![elem.syntax_element()])
5481
}
5582
pub fn insert_all(position: Position, mut elements: Vec<SyntaxElement>) {
5683
if let Some(first) = elements.first() {
@@ -73,17 +100,17 @@ pub fn insert_all_raw(position: Position, elements: Vec<SyntaxElement>) {
73100
parent.splice_children(index..index, elements);
74101
}
75102

76-
pub fn remove(elem: impl Into<SyntaxElement>) {
77-
let elem = elem.into();
103+
pub fn remove(elem: impl Element) {
104+
let elem = elem.syntax_element();
78105
remove_all(elem.clone()..=elem)
79106
}
80107
pub fn remove_all(range: RangeInclusive<SyntaxElement>) {
81108
replace_all(range, Vec::new())
82109
}
83110

84-
pub fn replace(old: impl Into<SyntaxElement>, new: impl Into<SyntaxElement>) {
85-
let old = old.into();
86-
replace_all(old.clone()..=old, vec![new.into()])
111+
pub fn replace(old: impl Element, new: impl Element) {
112+
let old = old.syntax_element();
113+
replace_all(old.clone()..=old, vec![new.syntax_element()])
87114
}
88115
pub fn replace_all(range: RangeInclusive<SyntaxElement>, new: Vec<SyntaxElement>) {
89116
let start = range.start().index();
@@ -92,11 +119,11 @@ pub fn replace_all(range: RangeInclusive<SyntaxElement>, new: Vec<SyntaxElement>
92119
parent.splice_children(start..end + 1, new)
93120
}
94121

95-
pub fn append_child(node: impl Into<SyntaxNode>, child: impl Into<SyntaxElement>) {
122+
pub fn append_child(node: &(impl Into<SyntaxNode> + Clone), child: impl Element) {
96123
let position = Position::last_child_of(node);
97124
insert(position, child)
98125
}
99-
pub fn append_child_raw(node: impl Into<SyntaxNode>, child: impl Into<SyntaxElement>) {
126+
pub fn append_child_raw(node: &(impl Into<SyntaxNode> + Clone), child: impl Element) {
100127
let position = Position::last_child_of(node);
101128
insert_raw(position, child)
102129
}

0 commit comments

Comments
 (0)