Skip to content

Commit 9157761

Browse files
committed
refactor: move editing for ast using SyntaxEditor to a separate file
Signed-off-by: Tarek <tareknaser360@gmail.com>
1 parent 2fb563f commit 9157761

File tree

4 files changed

+74
-74
lines changed

4 files changed

+74
-74
lines changed

crates/ide-assists/src/handlers/introduce_named_generic.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ pub(crate) fn introduce_named_generic(acc: &mut Assists, ctx: &AssistContext<'_>
5151

5252
editor.replace(impl_trait_type.syntax(), new_ty.syntax());
5353
let generic_param = syntax::ast::GenericParam::from(type_param);
54-
fn_.syntax_editor_add_generic_param(&mut editor, generic_param.clone());
54+
editor.syntax_editor_add_generic_param(fn_, generic_param.clone());
5555

5656
if let Some(cap) = ctx.config.snippet_cap {
5757
editor.add_annotation(generic_param.syntax(), builder.make_tabstop_before(cap));

crates/syntax/src/ast/edit_in_place.rs

Lines changed: 0 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ use parser::{SyntaxKind, T};
77
use crate::{
88
algo::{self, neighbor},
99
ast::{self, edit::IndentLevel, make, HasGenericArgs, HasGenericParams},
10-
syntax_editor::SyntaxEditor,
1110
ted::{self, Position},
1211
AstNode, AstToken, Direction, SyntaxElement,
1312
SyntaxKind::{ATTR, COMMENT, WHITESPACE},
@@ -55,78 +54,6 @@ impl GenericParamsOwnerEdit for ast::Fn {
5554
}
5655
}
5756

58-
impl ast::Fn {
59-
/// Adds a new generic param to the function using `SyntaxEditor`
60-
pub fn syntax_editor_add_generic_param(
61-
&self,
62-
editor: &mut SyntaxEditor,
63-
new_param: GenericParam,
64-
) {
65-
match self.generic_param_list() {
66-
Some(generic_param_list) => match generic_param_list.generic_params().last() {
67-
Some(last_param) => {
68-
// There exists a generic param list and it's not empty
69-
let position = generic_param_list.r_angle_token().map_or_else(
70-
|| crate::syntax_editor::Position::last_child_of(self.syntax()),
71-
crate::syntax_editor::Position::before,
72-
);
73-
74-
if last_param
75-
.syntax()
76-
.next_sibling_or_token()
77-
.map_or(false, |it| it.kind() == SyntaxKind::COMMA)
78-
{
79-
editor.insert(
80-
crate::syntax_editor::Position::after(last_param.syntax()),
81-
new_param.syntax().clone(),
82-
);
83-
editor.insert(
84-
crate::syntax_editor::Position::after(last_param.syntax()),
85-
make::token(SyntaxKind::WHITESPACE),
86-
);
87-
editor.insert(
88-
crate::syntax_editor::Position::after(last_param.syntax()),
89-
make::token(SyntaxKind::COMMA),
90-
);
91-
} else {
92-
let elements = vec![
93-
make::token(SyntaxKind::COMMA).into(),
94-
make::token(SyntaxKind::WHITESPACE).into(),
95-
new_param.syntax().clone().into(),
96-
];
97-
editor.insert_all(position, elements);
98-
}
99-
}
100-
None => {
101-
// There exists a generic param list but it's empty
102-
let position = crate::syntax_editor::Position::after(
103-
generic_param_list.l_angle_token().unwrap(),
104-
);
105-
editor.insert(position, new_param.syntax());
106-
}
107-
},
108-
None => {
109-
// There was no generic param list
110-
let position = if let Some(name) = self.name() {
111-
crate::syntax_editor::Position::after(name.syntax)
112-
} else if let Some(fn_token) = self.fn_token() {
113-
crate::syntax_editor::Position::after(fn_token)
114-
} else if let Some(param_list) = self.param_list() {
115-
crate::syntax_editor::Position::before(param_list.syntax)
116-
} else {
117-
crate::syntax_editor::Position::last_child_of(self.syntax())
118-
};
119-
let elements = vec![
120-
make::token(SyntaxKind::L_ANGLE).into(),
121-
new_param.syntax().clone().into(),
122-
make::token(SyntaxKind::R_ANGLE).into(),
123-
];
124-
editor.insert_all(position, elements);
125-
}
126-
}
127-
}
128-
}
129-
13057
impl GenericParamsOwnerEdit for ast::Impl {
13158
fn get_or_create_generic_param_list(&self) -> ast::GenericParamList {
13259
match self.generic_param_list() {

crates/syntax/src/syntax_editor.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ use rustc_hash::FxHashMap;
1616
use crate::{SyntaxElement, SyntaxNode, SyntaxToken};
1717

1818
mod edit_algo;
19+
mod edits;
1920
mod mapping;
2021

2122
pub use mapping::{SyntaxMapping, SyntaxMappingBuilder};
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
//! Structural editing for ast using `SyntaxEditor`
2+
3+
use crate::{
4+
ast::make, ast::AstNode, ast::Fn, ast::GenericParam, ast::HasGenericParams, ast::HasName,
5+
syntax_editor::Position, syntax_editor::SyntaxEditor, SyntaxKind,
6+
};
7+
8+
impl SyntaxEditor {
9+
/// Adds a new generic param to the function using `SyntaxEditor`
10+
pub fn syntax_editor_add_generic_param(&mut self, function: Fn, new_param: GenericParam) {
11+
match function.generic_param_list() {
12+
Some(generic_param_list) => match generic_param_list.generic_params().last() {
13+
Some(last_param) => {
14+
// There exists a generic param list and it's not empty
15+
let position = generic_param_list.r_angle_token().map_or_else(
16+
|| Position::last_child_of(function.syntax()),
17+
Position::before,
18+
);
19+
20+
if last_param
21+
.syntax()
22+
.next_sibling_or_token()
23+
.map_or(false, |it| it.kind() == SyntaxKind::COMMA)
24+
{
25+
self.insert(
26+
Position::after(last_param.syntax()),
27+
new_param.syntax().clone(),
28+
);
29+
self.insert(
30+
Position::after(last_param.syntax()),
31+
make::token(SyntaxKind::WHITESPACE),
32+
);
33+
self.insert(
34+
Position::after(last_param.syntax()),
35+
make::token(SyntaxKind::COMMA),
36+
);
37+
} else {
38+
let elements = vec![
39+
make::token(SyntaxKind::COMMA).into(),
40+
make::token(SyntaxKind::WHITESPACE).into(),
41+
new_param.syntax().clone().into(),
42+
];
43+
self.insert_all(position, elements);
44+
}
45+
}
46+
None => {
47+
// There exists a generic param list but it's empty
48+
let position = Position::after(generic_param_list.l_angle_token().unwrap());
49+
self.insert(position, new_param.syntax());
50+
}
51+
},
52+
None => {
53+
// There was no generic param list
54+
let position = if let Some(name) = function.name() {
55+
Position::after(name.syntax)
56+
} else if let Some(fn_token) = function.fn_token() {
57+
Position::after(fn_token)
58+
} else if let Some(param_list) = function.param_list() {
59+
Position::before(param_list.syntax)
60+
} else {
61+
Position::last_child_of(function.syntax())
62+
};
63+
let elements = vec![
64+
make::token(SyntaxKind::L_ANGLE).into(),
65+
new_param.syntax().clone().into(),
66+
make::token(SyntaxKind::R_ANGLE).into(),
67+
];
68+
self.insert_all(position, elements);
69+
}
70+
}
71+
}
72+
}

0 commit comments

Comments
 (0)