mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-08-31 07:37:30 +00:00
Cleanup editing API
This commit is contained in:
parent
b53ff214aa
commit
5f8b37563e
5 changed files with 36 additions and 12 deletions
|
@ -142,6 +142,15 @@ pub fn diff(from: &SyntaxNode, to: &SyntaxNode) -> TreeDiff {
|
|||
/// This is a type-unsafe low-level editing API, if you need to use it,
|
||||
/// prefer to create a type-safe abstraction on top of it instead.
|
||||
pub fn insert_children(
|
||||
parent: &SyntaxNode,
|
||||
position: InsertPosition<SyntaxElement>,
|
||||
to_insert: impl IntoIterator<Item = SyntaxElement>,
|
||||
) -> SyntaxNode {
|
||||
let mut to_insert = to_insert.into_iter();
|
||||
_insert_children(parent, position, &mut to_insert)
|
||||
}
|
||||
|
||||
fn _insert_children(
|
||||
parent: &SyntaxNode,
|
||||
position: InsertPosition<SyntaxElement>,
|
||||
to_insert: &mut dyn Iterator<Item = SyntaxElement>,
|
||||
|
@ -176,6 +185,15 @@ pub fn insert_children(
|
|||
/// This is a type-unsafe low-level editing API, if you need to use it,
|
||||
/// prefer to create a type-safe abstraction on top of it instead.
|
||||
pub fn replace_children(
|
||||
parent: &SyntaxNode,
|
||||
to_delete: RangeInclusive<SyntaxElement>,
|
||||
to_insert: impl IntoIterator<Item = SyntaxElement>,
|
||||
) -> SyntaxNode {
|
||||
let mut to_insert = to_insert.into_iter();
|
||||
_replace_children(parent, to_delete, &mut to_insert)
|
||||
}
|
||||
|
||||
fn _replace_children(
|
||||
parent: &SyntaxNode,
|
||||
to_delete: RangeInclusive<SyntaxElement>,
|
||||
to_insert: &mut dyn Iterator<Item = SyntaxElement>,
|
||||
|
@ -202,14 +220,21 @@ pub fn replace_children(
|
|||
/// to create a type-safe abstraction on top of it instead.
|
||||
pub fn replace_descendants(
|
||||
parent: &SyntaxNode,
|
||||
map: &impl Fn(&SyntaxElement) -> Option<SyntaxElement>,
|
||||
map: impl Fn(&SyntaxElement) -> Option<SyntaxElement>,
|
||||
) -> SyntaxNode {
|
||||
_replace_descendants(parent, &map)
|
||||
}
|
||||
|
||||
fn _replace_descendants(
|
||||
parent: &SyntaxNode,
|
||||
map: &dyn Fn(&SyntaxElement) -> Option<SyntaxElement>,
|
||||
) -> SyntaxNode {
|
||||
// FIXME: this could be made much faster.
|
||||
let new_children = parent.children_with_tokens().map(|it| go(map, it)).collect::<Vec<_>>();
|
||||
return with_children(parent, new_children);
|
||||
|
||||
fn go(
|
||||
map: &impl Fn(&SyntaxElement) -> Option<SyntaxElement>,
|
||||
map: &dyn Fn(&SyntaxElement) -> Option<SyntaxElement>,
|
||||
element: SyntaxElement,
|
||||
) -> NodeOrToken<rowan::GreenNode, rowan::GreenToken> {
|
||||
if let Some(replacement) = map(&element) {
|
||||
|
@ -221,7 +246,7 @@ pub fn replace_descendants(
|
|||
match element {
|
||||
NodeOrToken::Token(it) => NodeOrToken::Token(it.green().clone()),
|
||||
NodeOrToken::Node(it) => {
|
||||
NodeOrToken::Node(replace_descendants(&it, map).green().clone())
|
||||
NodeOrToken::Node(_replace_descendants(&it, map).green().clone())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue