aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_syntax/src
diff options
context:
space:
mode:
authorbors[bot] <26634292+bors[bot]@users.noreply.github.com>2019-09-25 21:44:20 +0100
committerGitHub <[email protected]>2019-09-25 21:44:20 +0100
commit870ce4b1a50a07e3a536ab26215804acdfc9ba8a (patch)
tree2ea0c53b6d7b09998dbc0b32d4552aeb30d210ed /crates/ra_syntax/src
parent0d277faf6c4052dcc80037fc43b4986980d0814b (diff)
parenta525e830a62272d21fbb0fb1c20bfa865791512d (diff)
Merge #1912
1912: add new editing API, suitable for modifying several nodes at once r=viorina a=matklad r? @viorina Co-authored-by: Aleksey Kladov <[email protected]>
Diffstat (limited to 'crates/ra_syntax/src')
-rw-r--r--crates/ra_syntax/src/algo.rs32
-rw-r--r--crates/ra_syntax/src/ast/extensions.rs9
2 files changed, 41 insertions, 0 deletions
diff --git a/crates/ra_syntax/src/algo.rs b/crates/ra_syntax/src/algo.rs
index 7ee5aa85b..f0ed96a17 100644
--- a/crates/ra_syntax/src/algo.rs
+++ b/crates/ra_syntax/src/algo.rs
@@ -3,6 +3,7 @@ pub mod visit;
3use std::ops::RangeInclusive; 3use std::ops::RangeInclusive;
4 4
5use itertools::Itertools; 5use itertools::Itertools;
6use rustc_hash::FxHashMap;
6 7
7use crate::{ 8use crate::{
8 AstNode, Direction, NodeOrToken, SyntaxElement, SyntaxNode, SyntaxNodePtr, TextRange, TextUnit, 9 AstNode, Direction, NodeOrToken, SyntaxElement, SyntaxNode, SyntaxNodePtr, TextRange, TextUnit,
@@ -123,6 +124,37 @@ pub fn replace_children(
123 with_children(parent, new_children) 124 with_children(parent, new_children)
124} 125}
125 126
127/// Replaces descendants in the node, according to the mapping.
128///
129/// This is a type-unsafe low-level editing API, if you need to use it, prefer
130/// to create a type-safe abstraction on top of it instead.
131pub fn replace_descendants(
132 parent: &SyntaxNode,
133 map: &FxHashMap<SyntaxElement, SyntaxElement>,
134) -> SyntaxNode {
135 // FIXME: this could be made much faster.
136 let new_children = parent.children_with_tokens().map(|it| go(map, it)).collect::<Box<[_]>>();
137 return with_children(parent, new_children);
138
139 fn go(
140 map: &FxHashMap<SyntaxElement, SyntaxElement>,
141 element: SyntaxElement,
142 ) -> NodeOrToken<rowan::GreenNode, rowan::GreenToken> {
143 if let Some(replacement) = map.get(&element) {
144 return match replacement {
145 NodeOrToken::Node(it) => NodeOrToken::Node(it.green().clone()),
146 NodeOrToken::Token(it) => NodeOrToken::Token(it.green().clone()),
147 };
148 }
149 match element {
150 NodeOrToken::Token(it) => NodeOrToken::Token(it.green().clone()),
151 NodeOrToken::Node(it) => {
152 NodeOrToken::Node(replace_descendants(&it, map).green().clone())
153 }
154 }
155 }
156}
157
126fn with_children( 158fn with_children(
127 parent: &SyntaxNode, 159 parent: &SyntaxNode,
128 new_children: Box<[NodeOrToken<rowan::GreenNode, rowan::GreenToken>]>, 160 new_children: Box<[NodeOrToken<rowan::GreenNode, rowan::GreenToken>]>,
diff --git a/crates/ra_syntax/src/ast/extensions.rs b/crates/ra_syntax/src/ast/extensions.rs
index d3a375f87..5f7e9f5b1 100644
--- a/crates/ra_syntax/src/ast/extensions.rs
+++ b/crates/ra_syntax/src/ast/extensions.rs
@@ -373,6 +373,15 @@ impl ast::LifetimeParam {
373 } 373 }
374} 374}
375 375
376impl ast::TypeParam {
377 pub fn colon_token(&self) -> Option<SyntaxToken> {
378 self.syntax()
379 .children_with_tokens()
380 .filter_map(|it| it.into_token())
381 .find(|it| it.kind() == T![:])
382 }
383}
384
376impl ast::WherePred { 385impl ast::WherePred {
377 pub fn lifetime_token(&self) -> Option<SyntaxToken> { 386 pub fn lifetime_token(&self) -> Option<SyntaxToken> {
378 self.syntax() 387 self.syntax()