Skip to content

Commit

Permalink
fix: warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
zxch3n committed Nov 3, 2023
1 parent 5cac1ed commit 200a6dd
Show file tree
Hide file tree
Showing 4 changed files with 4 additions and 123 deletions.
95 changes: 2 additions & 93 deletions crates/loro-internal/src/container/richtext/richtext_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use serde::{ser::SerializeStruct, Serialize};
use std::fmt::{Display, Formatter};
use std::{
ops::{Add, AddAssign, Range, Sub},
str::{from_utf8_unchecked, Utf8Error},
str::Utf8Error,
sync::Arc,
};

Expand All @@ -32,7 +32,7 @@ use self::{

use super::{
query_by_len::{IndexQuery, QueryByLen},
style_range_map::{StyleRangeMap, Styles, EMPTY_STYLES},
style_range_map::{StyleRangeMap, Styles},
AnchorType, RichtextSpan, StyleOp,
};

Expand Down Expand Up @@ -1449,97 +1449,6 @@ impl RichtextState {
.annotate(range.start..range.end + 2, style);
}

// FIXME: tests (unstable)
/// iter item is (event_length, styles)
pub fn iter_styles_in_event_index_range(
&self,
target_event_range: Range<usize>,
) -> impl Iterator<Item = (usize, &Styles)> + '_ {
let start = self
.tree
.query::<EventIndexQuery>(&target_event_range.start);
let start_entity_index = match start {
Some(start) => self.get_entity_index_from_path(start.cursor),
None => 0,
};

let mut event_index = target_event_range.start;
let mut entity_index = start_entity_index;
let mut style_range_iter = self.style_ranges.iter_from(start_entity_index);
let mut cur_style_range = style_range_iter
.next()
.unwrap_or_else(|| (start_entity_index..usize::MAX, &EMPTY_STYLES));
let mut text_iter = self.tree.iter_range(
start.map(|x| x.cursor).unwrap_or_else(|| Cursor {
leaf: self.tree.first_leaf().unwrap_leaf().into(),
offset: 0,
})..,
);
let mut last_emit_event_index = target_event_range.start;
std::iter::from_fn(move || loop {
if entity_index >= cur_style_range.0.end {
let ans = cur_style_range.1;
cur_style_range = style_range_iter
.next()
.unwrap_or_else(|| (entity_index..usize::MAX, &EMPTY_STYLES));
let len = event_index - last_emit_event_index;
last_emit_event_index = event_index;
return Some((len, ans));
}

if event_index >= target_event_range.end {
if last_emit_event_index < target_event_range.end {
let ans = cur_style_range.1;
let len = target_event_range.end - last_emit_event_index;
last_emit_event_index = target_event_range.end;
return Some((len, ans));
} else {
return None;
}
}

let Some(slice) = text_iter.next() else {
if event_index > last_emit_event_index {
let ans = cur_style_range.1;
let len = event_index - last_emit_event_index;
last_emit_event_index = event_index;
return Some((len, ans));
} else {
return None;
}
};

let start_offset = slice.start.unwrap_or(0);
let elem = slice.elem;
match elem {
RichtextStateChunk::Text { unicode_len, text } => {
event_index += if cfg!(feature = "wasm") {
if start_offset == 0 {
count_utf16_chars(text)
} else {
let offset = unicode_to_utf8_index(
// SAFETY: we know that the text is valid utf8
unsafe { from_utf8_unchecked(text) },
start_offset,
)
.unwrap();
count_utf16_chars(&text[offset..])
}
} else if start_offset == 0 {
*unicode_len as usize
} else {
*unicode_len as usize - start_offset
};

entity_index += *unicode_len as usize;
}
RichtextStateChunk::Style { .. } => {
entity_index += 1;
}
}
})
}

pub fn iter(&self) -> impl Iterator<Item = RichtextSpan> + '_ {
let mut entity_index = 0;
let mut style_range_iter = self.style_ranges.iter();
Expand Down
29 changes: 0 additions & 29 deletions crates/loro-internal/src/container/richtext/style_range_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,30 +211,6 @@ impl StyleRangeMap {
})
}

pub fn iter_from(
&self,
start_entity_index: usize,
) -> impl Iterator<Item = (Range<usize>, &Styles)> + '_ {
let start = self
.tree
.query::<LengthFinder>(&start_entity_index)
.unwrap();
let mut index = start_entity_index - start.offset();
self.tree
.iter_range(start.cursor()..)
.filter_map(move |elem| {
let len = elem.elem.len;
let value = &elem.elem.styles;
let range = index.max(start_entity_index)..index + len;
index += len;
if elem.elem.styles.is_empty() {
return None;
}

Some((range, value))
})
}

pub fn delete(&mut self, range: Range<usize>) {
if !self.has_style {
return;
Expand All @@ -254,11 +230,6 @@ impl StyleRangeMap {
self.tree.drain(start..end);
}

#[allow(unused)]
pub fn len(&self) -> usize {
*self.tree.root_cache() as usize
}

pub(crate) fn has_style(&self) -> bool {
self.has_style
}
Expand Down
2 changes: 1 addition & 1 deletion crates/loro-internal/src/fuzz.rs
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ pub fn change_pos_to_char_boundary(pos: &mut usize, len: usize) {
*pos %= len + 1;
}

fn check_synced(sites: &mut [LoroDoc], texts: &[Arc<Mutex<Delta<StringSlice, StyleMeta>>>]) {
fn check_synced(sites: &mut [LoroDoc], _: &[Arc<Mutex<Delta<StringSlice, StyleMeta>>>]) {
for i in 0..sites.len() - 1 {
for j in i + 1..sites.len() {
debug_log::group!("checking {} with {}", i, j);
Expand Down
1 change: 1 addition & 0 deletions crates/loro-wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ mod convert;

#[wasm_bindgen(js_name = setPanicHook)]
pub fn set_panic_hook() {
#[cfg(feature = "console_error_panic_hook")]
console_error_panic_hook::set_once();
}

Expand Down

0 comments on commit 200a6dd

Please sign in to comment.