Unnamed repository; edit this file 'description' to name the repository.
Diffstat (limited to 'helix-syntax/src/ropey.rs')
| -rw-r--r-- | helix-syntax/src/ropey.rs | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/helix-syntax/src/ropey.rs b/helix-syntax/src/ropey.rs new file mode 100644 index 00000000..650fcfb9 --- /dev/null +++ b/helix-syntax/src/ropey.rs @@ -0,0 +1,29 @@ +// glue code for using TS with ropey, this should be put behind a feature flag +// in the future (and potentially be partially removed) + +use ropey::RopeSlice; +use tree_sitter::{Node, TextProvider}; + +// Adapter to convert rope chunks to bytes +pub struct ChunksBytes<'a> { + chunks: ropey::iter::Chunks<'a>, +} +impl<'a> Iterator for ChunksBytes<'a> { + type Item = &'a [u8]; + fn next(&mut self) -> Option<Self::Item> { + self.chunks.next().map(str::as_bytes) + } +} + +pub struct RopeProvider<'a>(pub RopeSlice<'a>); + +impl<'a> TextProvider<&'a [u8]> for RopeProvider<'a> { + type I = ChunksBytes<'a>; + + fn text(&mut self, node: Node) -> Self::I { + let fragment = self.0.byte_slice(node.start_byte()..node.end_byte()); + ChunksBytes { + chunks: fragment.chunks(), + } + } +} |