From 1adb19464f002926e1042027b41acef4c81585f6 Mon Sep 17 00:00:00 2001 From: Pascal Kuthe Date: Tue, 11 Jul 2023 14:26:11 +0200 Subject: search buffer contents during global search (#5652) --- helix-core/src/lib.rs | 2 ++ helix-core/src/rope_reader.rs | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+) create mode 100644 helix-core/src/rope_reader.rs (limited to 'helix-core/src') diff --git a/helix-core/src/lib.rs b/helix-core/src/lib.rs index 03fbdedf..e1b5a1a1 100644 --- a/helix-core/src/lib.rs +++ b/helix-core/src/lib.rs @@ -41,7 +41,9 @@ pub use helix_loader::find_workspace; pub fn find_first_non_whitespace_char(line: RopeSlice) -> Option { line.chars().position(|ch| !ch.is_whitespace()) } +mod rope_reader; +pub use rope_reader::RopeReader; pub use ropey::{self, str_utils, Rope, RopeBuilder, RopeSlice}; // pub use tendril::StrTendril as Tendril; diff --git a/helix-core/src/rope_reader.rs b/helix-core/src/rope_reader.rs new file mode 100644 index 00000000..20ed7bac --- /dev/null +++ b/helix-core/src/rope_reader.rs @@ -0,0 +1,37 @@ +use std::io; + +use ropey::iter::Chunks; +use ropey::RopeSlice; + +pub struct RopeReader<'a> { + current_chunk: &'a [u8], + chunks: Chunks<'a>, +} + +impl<'a> RopeReader<'a> { + pub fn new(rope: RopeSlice<'a>) -> RopeReader<'a> { + RopeReader { + current_chunk: &[], + chunks: rope.chunks(), + } + } +} + +impl io::Read for RopeReader<'_> { + fn read(&mut self, mut buf: &mut [u8]) -> io::Result { + let buf_len = buf.len(); + loop { + let read_bytes = self.current_chunk.read(buf)?; + buf = &mut buf[read_bytes..]; + if buf.is_empty() { + return Ok(buf_len); + } + + if let Some(next_chunk) = self.chunks.next() { + self.current_chunk = next_chunk.as_bytes(); + } else { + return Ok(buf_len - buf.len()); + } + } + } +} -- cgit v1.2.3-70-g09d2