mirror of
https://github.com/rust-lang/rust.git
synced 2025-05-02 21:17:39 +00:00
rearrange methods
This commit is contained in:
parent
f7f99af0a6
commit
b51b71bf25
@ -96,13 +96,24 @@ unsafe impl TransparentNewType for SyntaxNode {
|
|||||||
type Repr = rowan::SyntaxNode<RaTypes>;
|
type Repr = rowan::SyntaxNode<RaTypes>;
|
||||||
}
|
}
|
||||||
|
|
||||||
impl SyntaxNode {
|
impl ToOwned for SyntaxNode {
|
||||||
pub(crate) fn new(green: GreenNode, errors: Vec<SyntaxError>) -> TreeArc<SyntaxNode> {
|
type Owned = TreeArc<SyntaxNode>;
|
||||||
let ptr = TreeArc(rowan::SyntaxNode::new(green, errors));
|
fn to_owned(&self) -> TreeArc<SyntaxNode> {
|
||||||
|
let ptr = TreeArc(self.0.to_owned());
|
||||||
TreeArc::cast(ptr)
|
TreeArc::cast(ptr)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl fmt::Debug for SyntaxNode {
|
||||||
|
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
|
||||||
|
write!(fmt, "{:?}@{:?}", self.kind(), self.range())?;
|
||||||
|
if has_short_text(self.kind()) {
|
||||||
|
write!(fmt, " \"{}\"", self.text())?;
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||||
pub enum Direction {
|
pub enum Direction {
|
||||||
Next,
|
Next,
|
||||||
@ -110,24 +121,73 @@ pub enum Direction {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl SyntaxNode {
|
impl SyntaxNode {
|
||||||
|
pub(crate) fn new(green: GreenNode, errors: Vec<SyntaxError>) -> TreeArc<SyntaxNode> {
|
||||||
|
let ptr = TreeArc(rowan::SyntaxNode::new(green, errors));
|
||||||
|
TreeArc::cast(ptr)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn kind(&self) -> SyntaxKind {
|
||||||
|
self.0.kind()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn range(&self) -> TextRange {
|
||||||
|
self.0.range()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn text(&self) -> SyntaxText {
|
||||||
|
SyntaxText::new(self)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn is_leaf(&self) -> bool {
|
||||||
|
self.0.is_leaf()
|
||||||
|
}
|
||||||
|
|
||||||
pub fn leaf_text(&self) -> Option<&SmolStr> {
|
pub fn leaf_text(&self) -> Option<&SmolStr> {
|
||||||
self.0.leaf_text()
|
self.0.leaf_text()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn parent(&self) -> Option<&SyntaxNode> {
|
||||||
|
self.0.parent().map(SyntaxNode::from_repr)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn first_child(&self) -> Option<&SyntaxNode> {
|
||||||
|
self.0.first_child().map(SyntaxNode::from_repr)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn last_child(&self) -> Option<&SyntaxNode> {
|
||||||
|
self.0.last_child().map(SyntaxNode::from_repr)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn next_sibling(&self) -> Option<&SyntaxNode> {
|
||||||
|
self.0.next_sibling().map(SyntaxNode::from_repr)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn prev_sibling(&self) -> Option<&SyntaxNode> {
|
||||||
|
self.0.prev_sibling().map(SyntaxNode::from_repr)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn children(&self) -> SyntaxNodeChildren {
|
||||||
|
SyntaxNodeChildren(self.0.children())
|
||||||
|
}
|
||||||
|
|
||||||
pub fn ancestors(&self) -> impl Iterator<Item = &SyntaxNode> {
|
pub fn ancestors(&self) -> impl Iterator<Item = &SyntaxNode> {
|
||||||
crate::algo::generate(Some(self), |&node| node.parent())
|
crate::algo::generate(Some(self), |&node| node.parent())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn descendants(&self) -> impl Iterator<Item = &SyntaxNode> {
|
pub fn descendants(&self) -> impl Iterator<Item = &SyntaxNode> {
|
||||||
self.preorder().filter_map(|event| match event {
|
self.preorder().filter_map(|event| match event {
|
||||||
WalkEvent::Enter(node) => Some(node),
|
WalkEvent::Enter(node) => Some(node),
|
||||||
WalkEvent::Leave(_) => None,
|
WalkEvent::Leave(_) => None,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn siblings(&self, direction: Direction) -> impl Iterator<Item = &SyntaxNode> {
|
pub fn siblings(&self, direction: Direction) -> impl Iterator<Item = &SyntaxNode> {
|
||||||
crate::algo::generate(Some(self), move |&node| match direction {
|
crate::algo::generate(Some(self), move |&node| match direction {
|
||||||
Direction::Next => node.next_sibling(),
|
Direction::Next => node.next_sibling(),
|
||||||
Direction::Prev => node.prev_sibling(),
|
Direction::Prev => node.prev_sibling(),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn preorder(&self) -> impl Iterator<Item = WalkEvent<&SyntaxNode>> {
|
pub fn preorder(&self) -> impl Iterator<Item = WalkEvent<&SyntaxNode>> {
|
||||||
self.0.preorder().map(|event| match event {
|
self.0.preorder().map(|event| match event {
|
||||||
WalkEvent::Enter(n) => WalkEvent::Enter(SyntaxNode::from_repr(n)),
|
WalkEvent::Enter(n) => WalkEvent::Enter(SyntaxNode::from_repr(n)),
|
||||||
@ -135,6 +195,10 @@ impl SyntaxNode {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn memory_size_of_subtree(&self) -> usize {
|
||||||
|
self.0.memory_size_of_subtree()
|
||||||
|
}
|
||||||
|
|
||||||
pub fn debug_dump(&self) -> String {
|
pub fn debug_dump(&self) -> String {
|
||||||
let mut errors: Vec<_> = match self.ancestors().find_map(SourceFile::cast) {
|
let mut errors: Vec<_> = match self.ancestors().find_map(SourceFile::cast) {
|
||||||
Some(file) => file.errors(),
|
Some(file) => file.errors(),
|
||||||
@ -178,17 +242,7 @@ impl SyntaxNode {
|
|||||||
|
|
||||||
buf
|
buf
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
impl ToOwned for SyntaxNode {
|
|
||||||
type Owned = TreeArc<SyntaxNode>;
|
|
||||||
fn to_owned(&self) -> TreeArc<SyntaxNode> {
|
|
||||||
let ptr = TreeArc(self.0.to_owned());
|
|
||||||
TreeArc::cast(ptr)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl SyntaxNode {
|
|
||||||
pub(crate) fn root_data(&self) -> &Vec<SyntaxError> {
|
pub(crate) fn root_data(&self) -> &Vec<SyntaxError> {
|
||||||
self.0.root_data()
|
self.0.root_data()
|
||||||
}
|
}
|
||||||
@ -196,60 +250,6 @@ impl SyntaxNode {
|
|||||||
pub(crate) fn replace_with(&self, replacement: GreenNode) -> GreenNode {
|
pub(crate) fn replace_with(&self, replacement: GreenNode) -> GreenNode {
|
||||||
self.0.replace_self(replacement)
|
self.0.replace_self(replacement)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn kind(&self) -> SyntaxKind {
|
|
||||||
self.0.kind()
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn range(&self) -> TextRange {
|
|
||||||
self.0.range()
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn text(&self) -> SyntaxText {
|
|
||||||
SyntaxText::new(self)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn is_leaf(&self) -> bool {
|
|
||||||
self.0.is_leaf()
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn parent(&self) -> Option<&SyntaxNode> {
|
|
||||||
self.0.parent().map(SyntaxNode::from_repr)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn first_child(&self) -> Option<&SyntaxNode> {
|
|
||||||
self.0.first_child().map(SyntaxNode::from_repr)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn last_child(&self) -> Option<&SyntaxNode> {
|
|
||||||
self.0.last_child().map(SyntaxNode::from_repr)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn next_sibling(&self) -> Option<&SyntaxNode> {
|
|
||||||
self.0.next_sibling().map(SyntaxNode::from_repr)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn prev_sibling(&self) -> Option<&SyntaxNode> {
|
|
||||||
self.0.prev_sibling().map(SyntaxNode::from_repr)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn children(&self) -> SyntaxNodeChildren {
|
|
||||||
SyntaxNodeChildren(self.0.children())
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn memory_size_of_subtree(&self) -> usize {
|
|
||||||
self.0.memory_size_of_subtree()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl fmt::Debug for SyntaxNode {
|
|
||||||
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
|
|
||||||
write!(fmt, "{:?}@{:?}", self.kind(), self.range())?;
|
|
||||||
if has_short_text(self.kind()) {
|
|
||||||
write!(fmt, " \"{}\"", self.text())?;
|
|
||||||
}
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
|
Loading…
Reference in New Issue
Block a user