From 7cc845e88d870173e1baa39ce4d3885a5b1f7043 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sun, 21 Apr 2019 17:47:55 +0300 Subject: start structured editing API --- crates/ra_syntax/src/lib.rs | 2 +- crates/ra_syntax/src/ptr.rs | 2 +- crates/ra_syntax/src/syntax_node.rs | 81 ++++++++++++++++++++++++++++++++++++- 3 files changed, 82 insertions(+), 3 deletions(-) (limited to 'crates/ra_syntax') 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::{ ast::AstNode, syntax_error::{SyntaxError, SyntaxErrorKind, Location}, syntax_text::SyntaxText, - syntax_node::{Direction, SyntaxNode, WalkEvent, TreeArc, SyntaxTreeBuilder, SyntaxElement, SyntaxToken}, + syntax_node::{Direction, SyntaxNode, WalkEvent, TreeArc, SyntaxTreeBuilder, SyntaxElement, SyntaxToken, InsertPosition}, ptr::{SyntaxNodePtr, AstPtr}, parsing::{tokenize, classify_literal, Token}, }; 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::{ /// specific node across reparses of the same file. #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] pub struct SyntaxNodePtr { - range: TextRange, + pub(crate) range: TextRange, kind: SyntaxKind, } 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; use rowan::{TransparentNewType, GreenNodeBuilder}; use crate::{ - SmolStr, SyntaxKind, TextUnit, TextRange, SyntaxText, SourceFile, AstNode, + SmolStr, SyntaxKind, TextUnit, TextRange, SyntaxText, SourceFile, AstNode, SyntaxNodePtr, syntax_error::{SyntaxError, SyntaxErrorKind}, }; pub use rowan::WalkEvent; pub(crate) use rowan::{GreenNode, GreenToken}; +pub enum InsertPosition { + First, + Last, + Before(T), + After(T), +} + /// Marker trait for CST and AST nodes pub trait SyntaxNodeWrapper: TransparentNewType {} impl> SyntaxNodeWrapper for T {} @@ -309,6 +316,71 @@ impl SyntaxNode { pub(crate) fn replace_with(&self, replacement: GreenNode) -> GreenNode { self.0.replace_with(replacement) } + + /// Adds specified children (tokens or nodes) to the current node at the + /// specific position. + /// + /// 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<'a>( + &self, + position: InsertPosition>, + to_insert: impl Iterator>, + ) -> TreeArc { + let mut delta = TextUnit::default(); + let to_insert = to_insert.map(|element| { + delta += element.text_len(); + to_green_element(element) + }); + + let old_children = self.0.green().children(); + + let get_anchor_pos = |anchor: SyntaxElement| -> usize { + self.children_with_tokens() + .position(|it| it == anchor) + .expect("anchor is not a child of current element") + }; + + let new_children = match position { + InsertPosition::First => { + to_insert.chain(old_children.iter().cloned()).collect::>() + } + InsertPosition::Last => { + old_children.iter().cloned().chain(to_insert).collect::>() + } + InsertPosition::Before(anchor) | InsertPosition::After(anchor) => { + let take_anchor = if let InsertPosition::After(_) = position { 1 } else { 0 }; + let (before, after) = old_children.split_at(get_anchor_pos(anchor) + take_anchor); + before + .iter() + .cloned() + .chain(to_insert) + .chain(after.iter().cloned()) + .collect::>() + } + }; + + let new_node = GreenNode::new(rowan::SyntaxKind(self.kind() as u16), new_children); + let new_file_node = self.replace_with(new_node); + let file = SourceFile::new(new_file_node, Vec::new()); + + // FIXME: use a more elegant way to re-fetch the node (#1185), make + // `range` private afterwards + let mut ptr = SyntaxNodePtr::new(self); + ptr.range = TextRange::from_to(ptr.range().start(), ptr.range().end() + delta); + return ptr.to_node(&file).to_owned(); + + fn to_green_element(element: SyntaxElement) -> rowan::GreenElement { + match element { + SyntaxElement::Node(node) => node.0.green().clone().into(), + SyntaxElement::Token(tok) => { + GreenToken::new(rowan::SyntaxKind(tok.kind() as u16), tok.text().clone()).into() + } + } + } + } } #[derive(Clone, Copy, PartialEq, Eq, Hash)] @@ -451,6 +523,13 @@ impl<'a> SyntaxElement<'a> { } .ancestors() } + + fn text_len(&self) -> TextUnit { + match self { + SyntaxElement::Node(node) => node.0.green().text_len(), + SyntaxElement::Token(token) => TextUnit::of_str(token.0.text()), + } + } } impl<'a> From> for SyntaxElement<'a> { -- cgit v1.2.3 From 97f41d7343a4801f296cd6a7a45ece9249c15886 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Mon, 22 Apr 2019 00:52:21 +0300 Subject: test short structs --- crates/ra_syntax/src/syntax_node.rs | 1 + 1 file changed, 1 insertion(+) (limited to 'crates/ra_syntax') diff --git a/crates/ra_syntax/src/syntax_node.rs b/crates/ra_syntax/src/syntax_node.rs index d9591781d..628cabc29 100644 --- a/crates/ra_syntax/src/syntax_node.rs +++ b/crates/ra_syntax/src/syntax_node.rs @@ -24,6 +24,7 @@ use crate::{ pub use rowan::WalkEvent; pub(crate) use rowan::{GreenNode, GreenToken}; +#[derive(Debug, PartialEq, Eq, Clone, Copy)] pub enum InsertPosition { First, Last, -- cgit v1.2.3 From 268e739c94d2e9edbb45374dfcc252b1648d3181 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Mon, 22 Apr 2019 13:01:33 +0300 Subject: move add_missing_members to structured editing API Currently, this is more code, and we also loose auto-indenting of bodies, but, long-term, this is the right approach --- crates/ra_syntax/src/ast/extensions.rs | 9 +++++ crates/ra_syntax/src/syntax_node.rs | 61 ++++++++++++++++++++++++---------- 2 files changed, 53 insertions(+), 17 deletions(-) (limited to 'crates/ra_syntax') diff --git a/crates/ra_syntax/src/ast/extensions.rs b/crates/ra_syntax/src/ast/extensions.rs index 5c4c0ffc1..9cbd2c6b8 100644 --- a/crates/ra_syntax/src/ast/extensions.rs +++ b/crates/ra_syntax/src/ast/extensions.rs @@ -210,6 +210,15 @@ impl ast::EnumVariant { } } +impl ast::FnDef { + pub fn semicolon_token(&self) -> Option> { + self.syntax() + .last_child_or_token() + .and_then(|it| it.as_token()) + .filter(|it| it.kind() == SEMI) + } +} + impl ast::LetStmt { pub fn has_semi(&self) -> bool { match self.syntax().last_child_or_token() { diff --git a/crates/ra_syntax/src/syntax_node.rs b/crates/ra_syntax/src/syntax_node.rs index 628cabc29..92c15234e 100644 --- a/crates/ra_syntax/src/syntax_node.rs +++ b/crates/ra_syntax/src/syntax_node.rs @@ -7,6 +7,7 @@ //! modules just wraps its API. use std::{ + ops::RangeInclusive, fmt::{self, Write}, any::Any, borrow::Borrow, @@ -323,8 +324,6 @@ impl SyntaxNode { /// /// 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<'a>( &self, position: InsertPosition>, @@ -338,12 +337,6 @@ impl SyntaxNode { let old_children = self.0.green().children(); - let get_anchor_pos = |anchor: SyntaxElement| -> usize { - self.children_with_tokens() - .position(|it| it == anchor) - .expect("anchor is not a child of current element") - }; - let new_children = match position { InsertPosition::First => { to_insert.chain(old_children.iter().cloned()).collect::>() @@ -353,7 +346,8 @@ impl SyntaxNode { } InsertPosition::Before(anchor) | InsertPosition::After(anchor) => { let take_anchor = if let InsertPosition::After(_) = position { 1 } else { 0 }; - let (before, after) = old_children.split_at(get_anchor_pos(anchor) + take_anchor); + let split_at = self.position_of_child(anchor) + take_anchor; + let (before, after) = old_children.split_at(split_at); before .iter() .cloned() @@ -363,6 +357,33 @@ impl SyntaxNode { } }; + self.with_children(new_children) + } + + /// Replaces all nodes in `to_delete` with nodes from `to_insert` + /// + /// 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<'a>( + &self, + to_delete: RangeInclusive>, + to_insert: impl Iterator>, + ) -> TreeArc { + let start = self.position_of_child(*to_delete.start()); + let end = self.position_of_child(*to_delete.end()); + let old_children = self.0.green().children(); + + let new_children = old_children[..start] + .iter() + .cloned() + .chain(to_insert.map(to_green_element)) + .chain(old_children[end + 1..].iter().cloned()) + .collect::>(); + self.with_children(new_children) + } + + fn with_children(&self, new_children: Box<[rowan::GreenElement]>) -> TreeArc { + let len = new_children.iter().map(|it| it.text_len()).sum::(); let new_node = GreenNode::new(rowan::SyntaxKind(self.kind() as u16), new_children); let new_file_node = self.replace_with(new_node); let file = SourceFile::new(new_file_node, Vec::new()); @@ -370,16 +391,22 @@ impl SyntaxNode { // FIXME: use a more elegant way to re-fetch the node (#1185), make // `range` private afterwards let mut ptr = SyntaxNodePtr::new(self); - ptr.range = TextRange::from_to(ptr.range().start(), ptr.range().end() + delta); + ptr.range = TextRange::offset_len(ptr.range().start(), len); return ptr.to_node(&file).to_owned(); + } - fn to_green_element(element: SyntaxElement) -> rowan::GreenElement { - match element { - SyntaxElement::Node(node) => node.0.green().clone().into(), - SyntaxElement::Token(tok) => { - GreenToken::new(rowan::SyntaxKind(tok.kind() as u16), tok.text().clone()).into() - } - } + fn position_of_child(&self, child: SyntaxElement) -> usize { + self.children_with_tokens() + .position(|it| it == child) + .expect("elemetn is not a child of current element") + } +} + +fn to_green_element(element: SyntaxElement) -> rowan::GreenElement { + match element { + SyntaxElement::Node(node) => node.0.green().clone().into(), + SyntaxElement::Token(tok) => { + GreenToken::new(rowan::SyntaxKind(tok.kind() as u16), tok.text().clone()).into() } } } -- cgit v1.2.3