@@ -8,8 +8,8 @@ use rustc_hash::FxHashMap;
88use text_edit:: TextEditBuilder ;
99
1010use crate :: {
11- AstNode , Direction , NodeOrToken , SyntaxElement , SyntaxKind , SyntaxNode , SyntaxNodePtr ,
12- SyntaxToken , TextRange , TextSize ,
11+ AstNode , Direction , NodeOrToken , SyntaxElement , SyntaxKind , SyntaxNode , SyntaxToken , TextRange ,
12+ TextSize ,
1313} ;
1414
1515/// Returns ancestors of the node at the offset, sorted by length. This should
@@ -92,14 +92,6 @@ pub fn has_errors(node: &SyntaxNode) -> bool {
9292 node. children ( ) . any ( |it| it. kind ( ) == SyntaxKind :: ERROR )
9393}
9494
95- #[ derive( Debug , PartialEq , Eq , Clone , Copy ) ]
96- pub enum InsertPosition < T > {
97- First ,
98- Last ,
99- Before ( T ) ,
100- After ( T ) ,
101- }
102-
10395type FxIndexMap < K , V > = IndexMap < K , V , BuildHasherDefault < rustc_hash:: FxHasher > > ;
10496
10597#[ derive( Debug , Hash , PartialEq , Eq ) ]
@@ -250,87 +242,6 @@ pub fn diff(from: &SyntaxNode, to: &SyntaxNode) -> TreeDiff {
250242 }
251243}
252244
253- /// Adds specified children (tokens or nodes) to the current node at the
254- /// specific position.
255- ///
256- /// This is a type-unsafe low-level editing API, if you need to use it,
257- /// prefer to create a type-safe abstraction on top of it instead.
258- pub fn insert_children (
259- parent : & SyntaxNode ,
260- position : InsertPosition < SyntaxElement > ,
261- to_insert : impl IntoIterator < Item = SyntaxElement > ,
262- ) -> SyntaxNode {
263- let mut to_insert = to_insert. into_iter ( ) ;
264- _insert_children ( parent, position, & mut to_insert)
265- }
266-
267- fn _insert_children (
268- parent : & SyntaxNode ,
269- position : InsertPosition < SyntaxElement > ,
270- to_insert : & mut dyn Iterator < Item = SyntaxElement > ,
271- ) -> SyntaxNode {
272- let mut delta = TextSize :: default ( ) ;
273- let to_insert = to_insert. map ( |element| {
274- delta += element. text_range ( ) . len ( ) ;
275- to_green_element ( element)
276- } ) ;
277-
278- let parent_green = parent. green ( ) ;
279- let mut old_children = parent_green. children ( ) . map ( |it| match it {
280- NodeOrToken :: Token ( it) => NodeOrToken :: Token ( it. to_owned ( ) ) ,
281- NodeOrToken :: Node ( it) => NodeOrToken :: Node ( it. to_owned ( ) ) ,
282- } ) ;
283-
284- let new_children = match & position {
285- InsertPosition :: First => to_insert. chain ( old_children) . collect :: < Vec < _ > > ( ) ,
286- InsertPosition :: Last => old_children. chain ( to_insert) . collect :: < Vec < _ > > ( ) ,
287- InsertPosition :: Before ( anchor) | InsertPosition :: After ( anchor) => {
288- let take_anchor = if let InsertPosition :: After ( _) = position { 1 } else { 0 } ;
289- let split_at = position_of_child ( parent, anchor. clone ( ) ) + take_anchor;
290- let before = old_children. by_ref ( ) . take ( split_at) . collect :: < Vec < _ > > ( ) ;
291- before. into_iter ( ) . chain ( to_insert) . chain ( old_children) . collect :: < Vec < _ > > ( )
292- }
293- } ;
294-
295- with_children ( parent, new_children)
296- }
297-
298- fn with_children (
299- parent : & SyntaxNode ,
300- new_children : Vec < NodeOrToken < rowan:: GreenNode , rowan:: GreenToken > > ,
301- ) -> SyntaxNode {
302- let _p = profile:: span ( "with_children" ) ;
303-
304- let new_green = rowan:: GreenNode :: new ( rowan:: SyntaxKind ( parent. kind ( ) as u16 ) , new_children) ;
305- with_green ( parent, new_green)
306- }
307-
308- fn with_green ( syntax_node : & SyntaxNode , green : rowan:: GreenNode ) -> SyntaxNode {
309- let len = green. children ( ) . map ( |it| it. text_len ( ) ) . sum :: < TextSize > ( ) ;
310- let new_root_node = syntax_node. replace_with ( green) ;
311- let new_root_node = SyntaxNode :: new_root ( new_root_node) ;
312-
313- // FIXME: use a more elegant way to re-fetch the node (#1185), make
314- // `range` private afterwards
315- let mut ptr = SyntaxNodePtr :: new ( syntax_node) ;
316- ptr. range = TextRange :: at ( ptr. range . start ( ) , len) ;
317- ptr. to_node ( & new_root_node)
318- }
319-
320- fn position_of_child ( parent : & SyntaxNode , child : SyntaxElement ) -> usize {
321- parent
322- . children_with_tokens ( )
323- . position ( |it| it == child)
324- . expect ( "element is not a child of current element" )
325- }
326-
327- fn to_green_element ( element : SyntaxElement ) -> NodeOrToken < rowan:: GreenNode , rowan:: GreenToken > {
328- match element {
329- NodeOrToken :: Node ( it) => it. green ( ) . into ( ) ,
330- NodeOrToken :: Token ( it) => it. green ( ) . to_owned ( ) . into ( ) ,
331- }
332- }
333-
334245#[ cfg( test) ]
335246mod tests {
336247 use expect_test:: { expect, Expect } ;
0 commit comments