aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_syntax/src
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_syntax/src')
-rw-r--r--crates/ra_syntax/src/lib.rs2
-rw-r--r--crates/ra_syntax/src/ptr.rs2
-rw-r--r--crates/ra_syntax/src/syntax_node.rs81
3 files changed, 82 insertions, 3 deletions
diff --git a/crates/ra_syntax/src/lib.rs b/crates/ra_syntax/src/lib.rs
index a6ce14f06..9cb66b76b 100644
--- a/crates/ra_syntax/src/lib.rs
+++ b/crates/ra_syntax/src/lib.rs
@@ -38,7 +38,7 @@ pub use crate::{
38 ast::AstNode, 38 ast::AstNode,
39 syntax_error::{SyntaxError, SyntaxErrorKind, Location}, 39 syntax_error::{SyntaxError, SyntaxErrorKind, Location},
40 syntax_text::SyntaxText, 40 syntax_text::SyntaxText,
41 syntax_node::{Direction, SyntaxNode, WalkEvent, TreeArc, SyntaxTreeBuilder, SyntaxElement, SyntaxToken}, 41 syntax_node::{Direction, SyntaxNode, WalkEvent, TreeArc, SyntaxTreeBuilder, SyntaxElement, SyntaxToken, InsertPosition},
42 ptr::{SyntaxNodePtr, AstPtr}, 42 ptr::{SyntaxNodePtr, AstPtr},
43 parsing::{tokenize, classify_literal, Token}, 43 parsing::{tokenize, classify_literal, Token},
44}; 44};
diff --git a/crates/ra_syntax/src/ptr.rs b/crates/ra_syntax/src/ptr.rs
index 15a8b94cd..b0816b135 100644
--- a/crates/ra_syntax/src/ptr.rs
+++ b/crates/ra_syntax/src/ptr.rs
@@ -10,7 +10,7 @@ use crate::{
10/// specific node across reparses of the same file. 10/// specific node across reparses of the same file.
11#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] 11#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
12pub struct SyntaxNodePtr { 12pub struct SyntaxNodePtr {
13 range: TextRange, 13 pub(crate) range: TextRange,
14 kind: SyntaxKind, 14 kind: SyntaxKind,
15} 15}
16 16
diff --git a/crates/ra_syntax/src/syntax_node.rs b/crates/ra_syntax/src/syntax_node.rs
index dc2352c76..d9591781d 100644
--- a/crates/ra_syntax/src/syntax_node.rs
+++ b/crates/ra_syntax/src/syntax_node.rs
@@ -17,13 +17,20 @@ use ra_parser::ParseError;
17use rowan::{TransparentNewType, GreenNodeBuilder}; 17use rowan::{TransparentNewType, GreenNodeBuilder};
18 18
19use crate::{ 19use crate::{
20 SmolStr, SyntaxKind, TextUnit, TextRange, SyntaxText, SourceFile, AstNode, 20 SmolStr, SyntaxKind, TextUnit, TextRange, SyntaxText, SourceFile, AstNode, SyntaxNodePtr,
21 syntax_error::{SyntaxError, SyntaxErrorKind}, 21 syntax_error::{SyntaxError, SyntaxErrorKind},
22}; 22};
23 23
24pub use rowan::WalkEvent; 24pub use rowan::WalkEvent;
25pub(crate) use rowan::{GreenNode, GreenToken}; 25pub(crate) use rowan::{GreenNode, GreenToken};
26 26
27pub enum InsertPosition<T> {
28 First,
29 Last,
30 Before(T),
31 After(T),
32}
33
27/// Marker trait for CST and AST nodes 34/// Marker trait for CST and AST nodes
28pub trait SyntaxNodeWrapper: TransparentNewType<Repr = rowan::SyntaxNode> {} 35pub trait SyntaxNodeWrapper: TransparentNewType<Repr = rowan::SyntaxNode> {}
29impl<T: TransparentNewType<Repr = rowan::SyntaxNode>> SyntaxNodeWrapper for T {} 36impl<T: TransparentNewType<Repr = rowan::SyntaxNode>> SyntaxNodeWrapper for T {}
@@ -309,6 +316,71 @@ impl SyntaxNode {
309 pub(crate) fn replace_with(&self, replacement: GreenNode) -> GreenNode { 316 pub(crate) fn replace_with(&self, replacement: GreenNode) -> GreenNode {
310 self.0.replace_with(replacement) 317 self.0.replace_with(replacement)
311 } 318 }
319
320 /// Adds specified children (tokens or nodes) to the current node at the
321 /// specific position.
322 ///
323 /// This is a type-unsafe low-level editing API, if you need to use it,
324 /// prefer to create a type-safe abstraction on top of it instead.
325 ///
326 ///
327 pub fn insert_children<'a>(
328 &self,
329 position: InsertPosition<SyntaxElement<'_>>,
330 to_insert: impl Iterator<Item = SyntaxElement<'a>>,
331 ) -> TreeArc<SyntaxNode> {
332 let mut delta = TextUnit::default();
333 let to_insert = to_insert.map(|element| {
334 delta += element.text_len();
335 to_green_element(element)
336 });
337
338 let old_children = self.0.green().children();
339
340 let get_anchor_pos = |anchor: SyntaxElement| -> usize {
341 self.children_with_tokens()
342 .position(|it| it == anchor)
343 .expect("anchor is not a child of current element")
344 };
345
346 let new_children = match position {
347 InsertPosition::First => {
348 to_insert.chain(old_children.iter().cloned()).collect::<Box<[_]>>()
349 }
350 InsertPosition::Last => {
351 old_children.iter().cloned().chain(to_insert).collect::<Box<[_]>>()
352 }
353 InsertPosition::Before(anchor) | InsertPosition::After(anchor) => {
354 let take_anchor = if let InsertPosition::After(_) = position { 1 } else { 0 };
355 let (before, after) = old_children.split_at(get_anchor_pos(anchor) + take_anchor);
356 before
357 .iter()
358 .cloned()
359 .chain(to_insert)
360 .chain(after.iter().cloned())
361 .collect::<Box<[_]>>()
362 }
363 };
364
365 let new_node = GreenNode::new(rowan::SyntaxKind(self.kind() as u16), new_children);
366 let new_file_node = self.replace_with(new_node);
367 let file = SourceFile::new(new_file_node, Vec::new());
368
369 // FIXME: use a more elegant way to re-fetch the node (#1185), make
370 // `range` private afterwards
371 let mut ptr = SyntaxNodePtr::new(self);
372 ptr.range = TextRange::from_to(ptr.range().start(), ptr.range().end() + delta);
373 return ptr.to_node(&file).to_owned();
374
375 fn to_green_element(element: SyntaxElement) -> rowan::GreenElement {
376 match element {
377 SyntaxElement::Node(node) => node.0.green().clone().into(),
378 SyntaxElement::Token(tok) => {
379 GreenToken::new(rowan::SyntaxKind(tok.kind() as u16), tok.text().clone()).into()
380 }
381 }
382 }
383 }
312} 384}
313 385
314#[derive(Clone, Copy, PartialEq, Eq, Hash)] 386#[derive(Clone, Copy, PartialEq, Eq, Hash)]
@@ -451,6 +523,13 @@ impl<'a> SyntaxElement<'a> {
451 } 523 }
452 .ancestors() 524 .ancestors()
453 } 525 }
526
527 fn text_len(&self) -> TextUnit {
528 match self {
529 SyntaxElement::Node(node) => node.0.green().text_len(),
530 SyntaxElement::Token(token) => TextUnit::of_str(token.0.text()),
531 }
532 }
454} 533}
455 534
456impl<'a> From<rowan::SyntaxElement<'a>> for SyntaxElement<'a> { 535impl<'a> From<rowan::SyntaxElement<'a>> for SyntaxElement<'a> {