Forward compat

This commit is contained in:
Aleksey Kladov
2020-04-11 00:27:00 +02:00
parent ca9a5dd165
commit c1244c853c
8 changed files with 25 additions and 26 deletions

View File

@@ -10,7 +10,7 @@ use crate::{AstNode, SyntaxKind, SyntaxNode, TextRange};
/// A pointer to a syntax node inside a file. It can be used to remember a
/// specific node across reparses of the same file.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct SyntaxNodePtr {
pub(crate) range: TextRange,
kind: SyntaxKind,
@@ -21,7 +21,7 @@ impl SyntaxNodePtr {
SyntaxNodePtr { range: node.text_range(), kind: node.kind() }
}
pub fn to_node(self, root: &SyntaxNode) -> SyntaxNode {
pub fn to_node(&self, root: &SyntaxNode) -> SyntaxNode {
assert!(root.parent().is_none());
successors(Some(root.clone()), |node| {
node.children().find(|it| self.range.is_subrange(&it.text_range()))
@@ -30,11 +30,11 @@ impl SyntaxNodePtr {
.unwrap_or_else(|| panic!("can't resolve local ptr to SyntaxNode: {:?}", self))
}
pub fn range(self) -> TextRange {
pub fn range(&self) -> TextRange {
self.range
}
pub fn kind(self) -> SyntaxKind {
pub fn kind(&self) -> SyntaxKind {
self.kind
}
@@ -53,10 +53,9 @@ pub struct AstPtr<N: AstNode> {
_ty: PhantomData<fn() -> N>,
}
impl<N: AstNode> Copy for AstPtr<N> {}
impl<N: AstNode> Clone for AstPtr<N> {
fn clone(&self) -> AstPtr<N> {
*self
AstPtr { raw: self.raw.clone(), _ty: PhantomData }
}
}
@@ -79,13 +78,13 @@ impl<N: AstNode> AstPtr<N> {
AstPtr { raw: SyntaxNodePtr::new(node.syntax()), _ty: PhantomData }
}
pub fn to_node(self, root: &SyntaxNode) -> N {
pub fn to_node(&self, root: &SyntaxNode) -> N {
let syntax_node = self.raw.to_node(root);
N::cast(syntax_node).unwrap()
}
pub fn syntax_node_ptr(self) -> SyntaxNodePtr {
self.raw
pub fn syntax_node_ptr(&self) -> SyntaxNodePtr {
self.raw.clone()
}
pub fn cast<U: AstNode>(self) -> Option<AstPtr<U>> {