diff options
| author | Laurenz <laurmaedje@gmail.com> | 2023-01-27 11:54:30 +0100 |
|---|---|---|
| committer | Laurenz <laurmaedje@gmail.com> | 2023-01-27 11:54:30 +0100 |
| commit | a8fd64f9289b92614b9e6c16e909ec0c45429027 (patch) | |
| tree | 76ce8797a6fe9c8b8c0bb1783910ba4b9e22da8b /src/syntax/node.rs | |
| parent | 33585d9a3fbab8a76d3fd8e9c2560f929202a518 (diff) | |
Hashtags everywhere!
Diffstat (limited to 'src/syntax/node.rs')
| -rw-r--r-- | src/syntax/node.rs | 29 |
1 files changed, 26 insertions, 3 deletions
diff --git a/src/syntax/node.rs b/src/syntax/node.rs index 038d13bc..d133fc5d 100644 --- a/src/syntax/node.rs +++ b/src/syntax/node.rs @@ -95,6 +95,11 @@ impl SyntaxNode { } } + /// Whether the node can be cast to the given AST node. + pub fn is<T: AstNode>(&self) -> bool { + self.cast::<T>().is_some() + } + /// Try to convert the node to a typed AST node. pub fn cast<T: AstNode>(&self) -> Option<T> { T::from_untyped(self) @@ -144,6 +149,16 @@ impl SyntaxNode { } } + /// Convert the child to another kind. + pub(super) fn convert_to_kind(&mut self, kind: SyntaxKind) { + debug_assert!(!kind.is_error()); + match &mut self.0 { + Repr::Leaf(leaf) => leaf.kind = kind, + Repr::Inner(inner) => Arc::make_mut(inner).kind = kind, + Repr::Error(_) => panic!("cannot convert error"), + } + } + /// Convert the child to an error. pub(super) fn convert_to_error(&mut self, message: impl Into<EcoString>) { let len = self.len(); @@ -695,6 +710,14 @@ impl<'a> LinkedNode<'a> { Some(next) } } + + /// Whether an error follows directly after the node. + pub fn before_error(&self) -> bool { + let Some(parent) = self.parent() else { return false }; + let Some(index) = self.index.checked_add(1) else { return false }; + let Some(node) = parent.node.children().nth(index) else { return false }; + node.kind().is_error() + } } /// Access to leafs. @@ -865,8 +888,8 @@ mod tests { // Go back to "#set". Skips the space. let prev = node.prev_sibling().unwrap(); - assert_eq!(prev.offset(), 0); - assert_eq!(prev.text(), "#set"); + assert_eq!(prev.offset(), 1); + assert_eq!(prev.text(), "set"); } #[test] @@ -875,7 +898,7 @@ mod tests { let leaf = LinkedNode::new(source.root()).leaf_at(6).unwrap(); let prev = leaf.prev_leaf().unwrap(); assert_eq!(leaf.text(), "fun"); - assert_eq!(prev.text(), "#set"); + assert_eq!(prev.text(), "set"); let source = Source::detached("#let x = 10"); let leaf = LinkedNode::new(source.root()).leaf_at(9).unwrap(); |
