mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-10-01 14:21:44 +00:00
Merge #3375
3375: Cleanup editing API a bit r=matklad a=matklad
bors r+
🤖
Co-authored-by: Aleksey Kladov <aleksey.kladov@gmail.com>
This commit is contained in:
commit
0ec7f760fc
8 changed files with 39 additions and 15 deletions
|
@ -157,7 +157,7 @@ impl<'a> QualifyPaths<'a> {
|
||||||
|
|
||||||
pub fn apply<'a, N: AstNode>(transformer: &dyn AstTransform<'a>, node: N) -> N {
|
pub fn apply<'a, N: AstNode>(transformer: &dyn AstTransform<'a>, node: N) -> N {
|
||||||
let syntax = node.syntax();
|
let syntax = node.syntax();
|
||||||
let result = ra_syntax::algo::replace_descendants(syntax, &|element| match element {
|
let result = ra_syntax::algo::replace_descendants(syntax, |element| match element {
|
||||||
ra_syntax::SyntaxElement::Node(n) => {
|
ra_syntax::SyntaxElement::Node(n) => {
|
||||||
let replacement = transformer.get_substitution(&n)?;
|
let replacement = transformer.get_substitution(&n)?;
|
||||||
Some(replacement.into())
|
Some(replacement.into())
|
||||||
|
|
|
@ -120,7 +120,7 @@ pub(crate) fn convert_to_guarded_return(ctx: AssistCtx) -> Option<Assist> {
|
||||||
let expr = {
|
let expr = {
|
||||||
let name_ref = make::name_ref("it");
|
let name_ref = make::name_ref("it");
|
||||||
let segment = make::path_segment(name_ref);
|
let segment = make::path_segment(name_ref);
|
||||||
let path = make::path_unqalified(segment);
|
let path = make::path_unqualified(segment);
|
||||||
make::expr_path(path)
|
make::expr_path(path)
|
||||||
};
|
};
|
||||||
make::match_arm(once(pat.into()), expr)
|
make::match_arm(once(pat.into()), expr)
|
||||||
|
|
|
@ -75,7 +75,7 @@ fn build_predicate(param: ast::TypeParam) -> Option<ast::WherePred> {
|
||||||
let path = {
|
let path = {
|
||||||
let name_ref = make::name_ref(¶m.name()?.syntax().to_string());
|
let name_ref = make::name_ref(¶m.name()?.syntax().to_string());
|
||||||
let segment = make::path_segment(name_ref);
|
let segment = make::path_segment(name_ref);
|
||||||
make::path_unqalified(segment)
|
make::path_unqualified(segment)
|
||||||
};
|
};
|
||||||
let predicate = make::where_pred(path, param.type_bound_list()?.bounds());
|
let predicate = make::where_pred(path, param.type_bound_list()?.bounds());
|
||||||
Some(predicate)
|
Some(predicate)
|
||||||
|
|
|
@ -52,7 +52,7 @@ fn expand_macro_recur(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Some(replace_descendants(&expanded, &|n| replaces.get(n).cloned()))
|
Some(replace_descendants(&expanded, |n| replaces.get(n).cloned()))
|
||||||
}
|
}
|
||||||
|
|
||||||
// FIXME: It would also be cool to share logic here and in the mbe tests,
|
// FIXME: It would also be cool to share logic here and in the mbe tests,
|
||||||
|
|
|
@ -570,7 +570,7 @@ mod tests {
|
||||||
let token_tree = insert_children(
|
let token_tree = insert_children(
|
||||||
&rbrace.parent().unwrap(),
|
&rbrace.parent().unwrap(),
|
||||||
InsertPosition::Last,
|
InsertPosition::Last,
|
||||||
&mut std::iter::once(space),
|
std::iter::once(space),
|
||||||
);
|
);
|
||||||
|
|
||||||
// Token Tree now is :
|
// Token Tree now is :
|
||||||
|
|
|
@ -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,
|
/// 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.
|
/// prefer to create a type-safe abstraction on top of it instead.
|
||||||
pub fn insert_children(
|
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,
|
parent: &SyntaxNode,
|
||||||
position: InsertPosition<SyntaxElement>,
|
position: InsertPosition<SyntaxElement>,
|
||||||
to_insert: &mut dyn Iterator<Item = 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,
|
/// 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.
|
/// prefer to create a type-safe abstraction on top of it instead.
|
||||||
pub fn replace_children(
|
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,
|
parent: &SyntaxNode,
|
||||||
to_delete: RangeInclusive<SyntaxElement>,
|
to_delete: RangeInclusive<SyntaxElement>,
|
||||||
to_insert: &mut dyn Iterator<Item = 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.
|
/// to create a type-safe abstraction on top of it instead.
|
||||||
pub fn replace_descendants(
|
pub fn replace_descendants(
|
||||||
parent: &SyntaxNode,
|
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 {
|
) -> SyntaxNode {
|
||||||
// FIXME: this could be made much faster.
|
// FIXME: this could be made much faster.
|
||||||
let new_children = parent.children_with_tokens().map(|it| go(map, it)).collect::<Vec<_>>();
|
let new_children = parent.children_with_tokens().map(|it| go(map, it)).collect::<Vec<_>>();
|
||||||
return with_children(parent, new_children);
|
return with_children(parent, new_children);
|
||||||
|
|
||||||
fn go(
|
fn go(
|
||||||
map: &impl Fn(&SyntaxElement) -> Option<SyntaxElement>,
|
map: &dyn Fn(&SyntaxElement) -> Option<SyntaxElement>,
|
||||||
element: SyntaxElement,
|
element: SyntaxElement,
|
||||||
) -> NodeOrToken<rowan::GreenNode, rowan::GreenToken> {
|
) -> NodeOrToken<rowan::GreenNode, rowan::GreenToken> {
|
||||||
if let Some(replacement) = map(&element) {
|
if let Some(replacement) = map(&element) {
|
||||||
|
@ -221,7 +246,7 @@ pub fn replace_descendants(
|
||||||
match element {
|
match element {
|
||||||
NodeOrToken::Token(it) => NodeOrToken::Token(it.green().clone()),
|
NodeOrToken::Token(it) => NodeOrToken::Token(it.green().clone()),
|
||||||
NodeOrToken::Node(it) => {
|
NodeOrToken::Node(it) => {
|
||||||
NodeOrToken::Node(replace_descendants(&it, map).green().clone())
|
NodeOrToken::Node(_replace_descendants(&it, map).green().clone())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -276,7 +276,7 @@ pub fn replace_descendants<N: AstNode, D: AstNode>(
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.map(|(from, to)| (from.syntax().clone().into(), to.syntax().clone().into()))
|
.map(|(from, to)| (from.syntax().clone().into(), to.syntax().clone().into()))
|
||||||
.collect::<FxHashMap<SyntaxElement, _>>();
|
.collect::<FxHashMap<SyntaxElement, _>>();
|
||||||
let new_syntax = algo::replace_descendants(parent.syntax(), &|n| map.get(n).cloned());
|
let new_syntax = algo::replace_descendants(parent.syntax(), |n| map.get(n).cloned());
|
||||||
N::cast(new_syntax).unwrap()
|
N::cast(new_syntax).unwrap()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -331,7 +331,7 @@ impl IndentLevel {
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
.collect();
|
.collect();
|
||||||
algo::replace_descendants(&node, &|n| replacements.get(n).cloned())
|
algo::replace_descendants(&node, |n| replacements.get(n).cloned())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn decrease_indent<N: AstNode>(self, node: N) -> N {
|
pub fn decrease_indent<N: AstNode>(self, node: N) -> N {
|
||||||
|
@ -359,7 +359,7 @@ impl IndentLevel {
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
.collect();
|
.collect();
|
||||||
algo::replace_descendants(&node, &|n| replacements.get(n).cloned())
|
algo::replace_descendants(&node, |n| replacements.get(n).cloned())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -389,7 +389,7 @@ fn insert_children<N: AstNode>(
|
||||||
position: InsertPosition<SyntaxElement>,
|
position: InsertPosition<SyntaxElement>,
|
||||||
to_insert: impl IntoIterator<Item = SyntaxElement>,
|
to_insert: impl IntoIterator<Item = SyntaxElement>,
|
||||||
) -> N {
|
) -> N {
|
||||||
let new_syntax = algo::insert_children(parent.syntax(), position, &mut to_insert.into_iter());
|
let new_syntax = algo::insert_children(parent.syntax(), position, to_insert);
|
||||||
N::cast(new_syntax).unwrap()
|
N::cast(new_syntax).unwrap()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -404,8 +404,7 @@ fn replace_children<N: AstNode>(
|
||||||
to_replace: RangeInclusive<SyntaxElement>,
|
to_replace: RangeInclusive<SyntaxElement>,
|
||||||
to_insert: impl IntoIterator<Item = SyntaxElement>,
|
to_insert: impl IntoIterator<Item = SyntaxElement>,
|
||||||
) -> N {
|
) -> N {
|
||||||
let new_syntax =
|
let new_syntax = algo::replace_children(parent.syntax(), to_replace, to_insert);
|
||||||
algo::replace_children(parent.syntax(), to_replace, &mut to_insert.into_iter());
|
|
||||||
N::cast(new_syntax).unwrap()
|
N::cast(new_syntax).unwrap()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -15,7 +15,7 @@ pub fn name_ref(text: &str) -> ast::NameRef {
|
||||||
pub fn path_segment(name_ref: ast::NameRef) -> ast::PathSegment {
|
pub fn path_segment(name_ref: ast::NameRef) -> ast::PathSegment {
|
||||||
ast_from_text(&format!("use {};", name_ref.syntax()))
|
ast_from_text(&format!("use {};", name_ref.syntax()))
|
||||||
}
|
}
|
||||||
pub fn path_unqalified(segment: ast::PathSegment) -> ast::Path {
|
pub fn path_unqualified(segment: ast::PathSegment) -> ast::Path {
|
||||||
path_from_text(&format!("use {}", segment.syntax()))
|
path_from_text(&format!("use {}", segment.syntax()))
|
||||||
}
|
}
|
||||||
pub fn path_qualified(qual: ast::Path, segment: ast::PathSegment) -> ast::Path {
|
pub fn path_qualified(qual: ast::Path, segment: ast::PathSegment) -> ast::Path {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue