pub(super) fn block_for(
        &mut self,
        element: &SyntaxElement,
        position: BlockPosition,
    ) -> &mut SpaceBlock {
        use BlockPosition::{After, Before};

        assert!(element.kind() != TOKEN_WHITESPACE);

        // Special case, for root node, leading and trailing ws are children of
        // the root. For all other node types, they are siblings
        if element.kind() == NODE_ROOT {
            let root_node = element.as_node().unwrap();
            let original_space = match position {
                BlockPosition::Before => root_node.first_child_or_token(),
                BlockPosition::After => root_node.last_child_or_token(),
            };
            return match &original_space {
                Some(NodeOrToken::Token(token)) if token.kind() == TOKEN_WHITESPACE => {
                    if let Some(&existing) = match position {
                        Before => self.by_end_offset.get(&token.text_range().end()),
                        After => self.by_start_offset.get(&token.text_range().start()),
                    } {
                        &mut self.blocks[existing]
                    } else {
                        self.push_block(SpaceBlock::new(OriginalSpace::Some(token.clone())))
                    }
                }
                _ => {
                    let offset = match position {
                        Before => root_node.text_range().start(),
                        After => root_node.text_range().end(),
                    };

                    if let Some(&existing) = match position {
                        Before => self.by_end_offset.get(&offset),
                        After => self.by_start_offset.get(&offset),
                    } {
                        &mut self.blocks[existing]
                    } else {
                        self.push_block(SpaceBlock::new(OriginalSpace::None { offset }))
                    }
                }
            };
        }

        let offset = match position {
            Before => element.text_range().start(),
            After => element.text_range().end(),
        };

        if let Some(&existing) = match position {
            Before => self.by_end_offset.get(&offset),
            After => self.by_start_offset.get(&offset),
        } {
            return &mut self.blocks[existing];
        }

        let original_token = match position {
            Before => element.prev_sibling_or_token(),
            After => element.next_sibling_or_token(),
        };

        let original_space = match &original_token {
            Some(NodeOrToken::Token(token)) if token.kind() == TOKEN_WHITESPACE => {
                OriginalSpace::Some(token.clone())
            }
            Some(_) => OriginalSpace::None { offset },
            _ => match element.parent() {
                Option::Some(parent) => return self.block_for(&parent.into(), position),
                None => OriginalSpace::None { offset },
            },
        };

        self.push_block(SpaceBlock::new(original_space))
    }
/// This method gets a `SpaceBlock` before or after element. It's pretty
/// complicated, because it needs to handle these different cases:
/// * We could have already created the block. In this case, we should
///   return the existing block instead of creating a new one.
/// * There may, or may not be, backing original whitespace token for the
///   block.
/// * The necessary whitespace token is not necessary a sibling of
///   `element`, it might be a sibling of `element`'s ancestor.
/// * Finally, root node is special, as it doesn't have siblings and instead
///   leading and trailing whitespace appear as children.
This method gets a `SpaceBlock` before or after element. It's pretty
complicated, because it needs to handle these different cases:
We could have already created the block. In this case, we should
return the existing block instead of creating a new one.
There may, or may not be, backing original whitespace token for the
block.
The necessary whitespace token is not necessary a sibling of
`element`, it might be a sibling of `element`'s ancestor.
Finally, root node is special, as it doesn't have siblings and instead
leading and trailing whitespace appear as children.
This method gets a ` SpaceBlock ` before or after element . It ' s pretty complicated because it needs to handle these different cases : We could have already created the block . In this case we should return the existing block instead of creating a new one . There may or may not be backing original whitespace token for the block . The necessary whitespace token is not necessary a sibling of ` element ` it might be a sibling of ` element ` ' s ancestor . Finally root node is special as it doesn ' t have siblings and instead leading and trailing whitespace appear as children .
pub(super) fn block_for(
        &mut self,
        element: &SyntaxElement,
        position: BlockPosition,
    ) -> &mut SpaceBlock {
        use BlockPosition::{After, Before};
        assert!(element.kind() != TOKEN_WHITESPACE);
        // Special case, for root node, leading and trailing ws are children of
        // the root. For all other node types, they are siblings
        if element.kind() == NODE_ROOT {
            let root_node = element.as_node().unwrap();
            let original_space = match position {
                BlockPosition::Before => root_node.first_child_or_token(),
                BlockPosition::After => root_node.last_child_or_token(),
            };
            return match &original_space {
                Some(NodeOrToken::Token(token)) if token.kind() == TOKEN_WHITESPACE => {
                    if let Some(&existing) = match position {
                        Before => self.by_end_offset.get(&token.text_range().end()),
                        After => self.by_start_offset.get(&token.text_range().start()),
                    } {
                        &mut self.blocks[existing]
                    } else {
                        self.push_block(SpaceBlock::new(OriginalSpace::Some(token.clone())))
                    }
                }
                _ => {
                    let offset = match position {
                        Before => root_node.text_range().start(),
                        After => root_node.text_range().end(),
                    };
                    if let Some(&existing) = match position {
                        Before => self.by_end_offset.get(&offset),
                        After => self.by_start_offset.get(&offset),
                    } {
                        &mut self.blocks[existing]
                    } else {
                        self.push_block(SpaceBlock::new(OriginalSpace::None { offset }))
                    }
                }
            };
        }
        let offset = match position {
            Before => element.text_range().start(),
            After => element.text_range().end(),
        };
        if let Some(&existing) = match position {
            Before => self.by_end_offset.get(&offset),
            After => self.by_start_offset.get(&offset),
        } {
            return &mut self.blocks[existing];
        }
        let original_token = match position {
            Before => element.prev_sibling_or_token(),
            After => element.next_sibling_or_token(),
        };
        let original_space = match &original_token {
            Some(NodeOrToken::Token(token)) if token.kind() == TOKEN_WHITESPACE => {
                OriginalSpace::Some(token.clone())
            }
            Some(_) => OriginalSpace::None { offset },
            _ => match element.parent() {
                Option::Some(parent) => return self.block_for(&parent.into(), position),
                None => OriginalSpace::None { offset },
            },
        };
        self.push_block(SpaceBlock::new(original_space))
    }
pub ( super ) fn block_for ( & mut self , element : & SyntaxElement , position : BlockPosition , ) -> & mut SpaceBlock { use BlockPosition :: { After , Before } ; assert ! ( element kind ( ) TOKEN_WHITESPACE ) ; // Special case, for root node, leading and trailing ws are children of // the root. For all other node types, they are siblings if element . kind ( ) == NODE_ROOT { let root_node = element . as_node ( ) . unwrap ( ) ; let original_space = match position { BlockPosition :: Before => root_node . first_child_or_token ( ) , BlockPosition :: After => root_node . last_child_or_token ( ) , } ; return match & original_space { Some ( NodeOrToken :: Token ( token ) ) if token . kind ( ) == TOKEN_WHITESPACE => { if let Some ( & existing ) = match position { Before => self . by_end_offset . get ( & token . text_range ( ) . end ( ) ) , After => self . by_start_offset . get ( & token . text_range ( ) . start ( ) ) , } { & mut self . blocks [ existing ] } else { self . push_block ( SpaceBlock :: new ( OriginalSpace :: Some ( token . clone ( ) ) ) ) } } _ => { let offset = match position { Before => root_node . text_range ( ) . start ( ) , After => root_node . text_range ( ) . end ( ) , } ; if let Some ( & existing ) = match position { Before => self . by_end_offset . get ( & offset ) , After => self . by_start_offset . get ( & offset ) , } { & mut self . blocks [ existing ] } else { self . push_block ( SpaceBlock :: new ( OriginalSpace :: None { offset } ) ) } } } ; } let offset = match position { Before => element . text_range ( ) . start ( ) , After => element . text_range ( ) . end ( ) , } ; if let Some ( & existing ) = match position { Before => self . by_end_offset . get ( & offset ) , After => self . by_start_offset . get ( & offset ) , } { return & mut self . blocks [ existing ] ; } let original_token = match position { Before => element . prev_sibling_or_token ( ) , After => element . next_sibling_or_token ( ) , } ; let original_space = match & original_token { Some ( NodeOrToken :: Token ( token ) ) if token . kind ( ) == TOKEN_WHITESPACE => { OriginalSpace :: Some ( token . clone ( ) ) } Some ( _ ) => OriginalSpace :: None { offset } , _ => match element . parent ( ) { Option :: Some ( parent ) => return self . block_for ( & parent . into ( ) , position ) , None => OriginalSpace :: None { offset } , } , } ; self . push_block ( SpaceBlock :: new ( original_space ) ) }
This method gets a `SpaceBlock` before or after element.
This method gets a ` SpaceBlock ` before or after element .
&mut self
element
&SyntaxElement
position
BlockPosition