Skip to content

Commit

Permalink
fix: use dumb implementation for web-tree-sitter
Browse files Browse the repository at this point in the history
  • Loading branch information
HerringtonDarkholme committed May 13, 2024
1 parent d284388 commit 5cb7052
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions crates/core/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,7 @@ impl<'r, D: Doc> Node<'r, D> {
// NOTE: Need go to parent first, then move to current node by byte offset.
// This is because tree_sitter cursor is scoped to the starting node.
// See https://github.com/tree-sitter/tree-sitter/issues/567
#[cfg(not(target_arch = "wasm32"))]
pub fn next_all(&self) -> impl Iterator<Item = Node<'r, D>> + '_ {
// if root is none, use self as fallback to return a type-stable Iterator
let node = self.parent().unwrap_or_else(|| self.clone());
Expand All @@ -393,6 +394,18 @@ impl<'r, D: Doc> Node<'r, D> {
})
}

// wasm32 has wrong goto_first_child_for_byte
#[cfg(target_arch = "wasm32")]
pub fn next_all(&self) -> impl Iterator<Item = Node<'r, D>> + '_ {
let mut node = self.clone();
std::iter::from_fn(move || {
node.next().map(|n| {
node = n.clone();
n
})
})
}

#[must_use]
pub fn prev(&self) -> Option<Node<'r, D>> {
let inner = self.inner.prev_sibling()?;
Expand All @@ -402,6 +415,7 @@ impl<'r, D: Doc> Node<'r, D> {
})
}

#[cfg(not(target_arch = "wasm32"))]
pub fn prev_all(&self) -> impl Iterator<Item = Node<'r, D>> + '_ {
// if root is none, use self as fallback to return a type-stable Iterator
let node = self.parent().unwrap_or_else(|| self.clone());
Expand All @@ -416,6 +430,18 @@ impl<'r, D: Doc> Node<'r, D> {
})
}

// wasm32 has wrong goto_first_child_for_byte
#[cfg(target_arch = "wasm32")]
pub fn prev_all(&self) -> impl Iterator<Item = Node<'r, D>> + '_ {
let mut node = self.clone();
std::iter::from_fn(move || {
node.prev().map(|n| {
node = n.clone();
n
})
})
}

pub fn dfs<'s>(&'s self) -> Pre<'r, D> {
Pre::new(self)
}
Expand Down

0 comments on commit 5cb7052

Please sign in to comment.