Add incremental parsing unit tests
This commit is contained in:
parent
4a98f0b87e
commit
9a8cf39277
2 changed files with 127 additions and 68 deletions
|
|
@ -1,4 +1,6 @@
|
|||
use super::random::Rand;
|
||||
use std::ops::Range;
|
||||
use std::str;
|
||||
use tree_sitter::{InputEdit, Point, Tree};
|
||||
|
||||
pub struct Edit {
|
||||
|
|
@ -7,6 +9,53 @@ pub struct Edit {
|
|||
pub inserted_text: Vec<u8>,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct ReadRecorder<'a> {
|
||||
content: &'a Vec<u8>,
|
||||
indices_read: Vec<usize>,
|
||||
}
|
||||
|
||||
impl<'a> ReadRecorder<'a> {
|
||||
pub fn new(content: &'a Vec<u8>) -> Self {
|
||||
Self {
|
||||
content,
|
||||
indices_read: Vec::new(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn read(&mut self, offset: usize) -> &'a [u8] {
|
||||
if offset < self.content.len() {
|
||||
if let Err(i) = self.indices_read.binary_search(&offset) {
|
||||
self.indices_read.insert(i, offset);
|
||||
}
|
||||
&self.content[offset..(offset + 1)]
|
||||
} else {
|
||||
&[]
|
||||
}
|
||||
}
|
||||
|
||||
pub fn strings_read(&self) -> Vec<&'a str> {
|
||||
let mut result = Vec::new();
|
||||
let mut last_range: Option<Range<usize>> = None;
|
||||
for index in self.indices_read.iter() {
|
||||
if let Some(ref mut range) = &mut last_range {
|
||||
if range.end == *index {
|
||||
range.end += 1;
|
||||
} else {
|
||||
result.push(str::from_utf8(&self.content[range.clone()]).unwrap());
|
||||
last_range = None;
|
||||
}
|
||||
} else {
|
||||
last_range = Some(*index..(*index + 1));
|
||||
}
|
||||
}
|
||||
if let Some(range) = last_range {
|
||||
result.push(str::from_utf8(&self.content[range.clone()]).unwrap());
|
||||
}
|
||||
result
|
||||
}
|
||||
}
|
||||
|
||||
pub fn perform_edit(tree: &mut Tree, input: &mut Vec<u8>, edit: &Edit) {
|
||||
let start_byte = edit.position;
|
||||
let old_end_byte = edit.position + edit.deleted_length;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue