list segments: cache length of segment-list in cur_len
This commit is contained in:
parent
0ad1fb52e8
commit
f88f4971af
1 changed files with 63 additions and 25 deletions
|
@ -26,12 +26,11 @@ pub enum ListSegment {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* todo: switch to ListView instead of SequenceView
|
|
||||||
*/
|
|
||||||
pub struct ListSegments {
|
pub struct ListSegments {
|
||||||
data: Arc<dyn ListView<EditTree>>,
|
data: Arc<dyn ListView<EditTree>>,
|
||||||
cursor: Arc<dyn SingletonView<Item = ListCursor>>,
|
cursor: Arc<dyn SingletonView<Item = ListCursor>>,
|
||||||
|
|
||||||
|
cur_len: usize,
|
||||||
cur_cursor: ListCursor,
|
cur_cursor: ListCursor,
|
||||||
|
|
||||||
port: ViewPort<dyn ListView<ListSegment>>,
|
port: ViewPort<dyn ListView<ListSegment>>,
|
||||||
|
@ -45,13 +44,7 @@ impl View for ListSegments {
|
||||||
|
|
||||||
impl ListView<ListSegment> for ListSegments {
|
impl ListView<ListSegment> for ListSegments {
|
||||||
fn len(&self) -> Option<usize> {
|
fn len(&self) -> Option<usize> {
|
||||||
let l = self.data.len()?;
|
Some(self.cur_len)
|
||||||
match self.cur_cursor.mode {
|
|
||||||
ListCursorMode::Insert => {
|
|
||||||
Some(l + if self.cur_cursor.idx.is_some() { 1 } else { 0 })
|
|
||||||
}
|
|
||||||
_ => Some(l),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get(&self, idx: &usize) -> Option<ListSegment> {
|
fn get(&self, idx: &usize) -> Option<ListSegment> {
|
||||||
|
@ -96,23 +89,61 @@ impl ListSegments {
|
||||||
) -> Arc<RwLock<Self>> {
|
) -> Arc<RwLock<Self>> {
|
||||||
let out_port = ViewPort::new();
|
let out_port = ViewPort::new();
|
||||||
let mut proj_helper = ProjectionHelper::new(out_port.update_hooks.clone());
|
let mut proj_helper = ProjectionHelper::new(out_port.update_hooks.clone());
|
||||||
let proj = Arc::new(RwLock::new(ListSegments {
|
|
||||||
cur_cursor: cursor_port.get_view().get(),
|
|
||||||
port: out_port.clone(),
|
|
||||||
|
|
||||||
|
let cur_cursor = cursor_port.get_view().get();
|
||||||
|
let mut cur_len = data_port.get_view().unwrap().len().unwrap();
|
||||||
|
|
||||||
|
if cur_cursor.mode == ListCursorMode::Insert {
|
||||||
|
cur_len += 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
let proj = Arc::new(RwLock::new(ListSegments {
|
||||||
|
cur_cursor,
|
||||||
|
cur_len,
|
||||||
|
|
||||||
|
port: out_port.clone(),
|
||||||
cursor: proj_helper.new_singleton_arg(0, cursor_port, |s: &mut Self, _msg| {
|
cursor: proj_helper.new_singleton_arg(0, cursor_port, |s: &mut Self, _msg| {
|
||||||
let old_cursor = s.cur_cursor;
|
let old_cursor = s.cur_cursor;
|
||||||
let new_cursor = s.cursor.get();
|
let new_cursor = s.cursor.get();
|
||||||
s.cur_cursor = new_cursor;
|
s.cur_cursor = new_cursor;
|
||||||
|
|
||||||
if old_cursor.mode == ListCursorMode::Insert {
|
if let Some(cur_idx) = old_cursor.idx {
|
||||||
if let Some(idx) = old_cursor.idx {
|
match old_cursor.mode {
|
||||||
s.cast.notify(&ListDiff::Remove(idx as usize));
|
ListCursorMode::Insert => {
|
||||||
|
eprintln!("segments: notify cursor cur len = {}", s.cur_len);
|
||||||
|
s.cur_len -= 1;
|
||||||
|
s.cast.notify(&ListDiff::Remove(cur_idx as usize));
|
||||||
}
|
}
|
||||||
}
|
ListCursorMode::Select => {
|
||||||
if new_cursor.mode == ListCursorMode::Insert {
|
s.cast.notify(&ListDiff::Update{
|
||||||
if let Some(idx) = new_cursor.idx {
|
idx: cur_idx as usize,
|
||||||
s.cast.notify(&ListDiff::Insert { idx: idx as usize, val: ListSegment::InsertCursor });
|
val: ListSegment::Item {
|
||||||
|
editor: s.data.get(&(cur_idx as usize)).unwrap(),
|
||||||
|
cur_dist: 0,
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
if let Some(cur_idx) = new_cursor.idx {
|
||||||
|
match new_cursor.mode {
|
||||||
|
ListCursorMode::Insert => {
|
||||||
|
s.cur_len += 1;
|
||||||
|
s.cast.notify(&ListDiff::Insert {
|
||||||
|
idx: cur_idx as usize,
|
||||||
|
val: ListSegment::InsertCursor
|
||||||
|
});
|
||||||
|
}
|
||||||
|
ListCursorMode::Select => {
|
||||||
|
s.cast.notify(&ListDiff::Update{
|
||||||
|
idx: cur_idx as usize,
|
||||||
|
val: ListSegment::Item {
|
||||||
|
editor: s.data.get(&(cur_idx as usize)).unwrap(),
|
||||||
|
cur_dist: 0,
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}),
|
}),
|
||||||
|
@ -121,8 +152,8 @@ impl ListSegments {
|
||||||
let mut target_idx = match diff {
|
let mut target_idx = match diff {
|
||||||
ListDiff::Clear => 0,
|
ListDiff::Clear => 0,
|
||||||
ListDiff::Remove(idx) => *idx,
|
ListDiff::Remove(idx) => *idx,
|
||||||
ListDiff::Insert { idx, val } => *idx,
|
ListDiff::Insert { idx, val:_ } => *idx,
|
||||||
ListDiff::Update { idx, val } => *idx
|
ListDiff::Update { idx, val:_ } => *idx
|
||||||
};
|
};
|
||||||
let mut cur_dist = 0;
|
let mut cur_dist = 0;
|
||||||
|
|
||||||
|
@ -138,16 +169,23 @@ impl ListSegments {
|
||||||
|
|
||||||
match diff {
|
match diff {
|
||||||
ListDiff::Clear => {
|
ListDiff::Clear => {
|
||||||
|
s.cur_len = 0;
|
||||||
s.cast.notify(&ListDiff::Clear);
|
s.cast.notify(&ListDiff::Clear);
|
||||||
|
|
||||||
if s.cur_cursor.mode == ListCursorMode::Insert {
|
if s.cur_cursor.mode == ListCursorMode::Insert
|
||||||
|
&& s.cur_cursor.idx.is_some()
|
||||||
|
{
|
||||||
|
s.cur_cursor.idx = Some(0);
|
||||||
|
s.cur_len += 1;
|
||||||
s.cast.notify(&ListDiff::Insert { idx: 0, val: ListSegment::InsertCursor });
|
s.cast.notify(&ListDiff::Insert { idx: 0, val: ListSegment::InsertCursor });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
ListDiff::Remove(idx) => {
|
ListDiff::Remove(_) => {
|
||||||
|
s.cur_len -= 1;
|
||||||
s.cast.notify(&ListDiff::Remove(target_idx));
|
s.cast.notify(&ListDiff::Remove(target_idx));
|
||||||
}
|
}
|
||||||
ListDiff::Insert { idx, val } => {
|
ListDiff::Insert { idx:_, val } => {
|
||||||
|
s.cur_len += 1;
|
||||||
s.cast.notify(&ListDiff::Insert {
|
s.cast.notify(&ListDiff::Insert {
|
||||||
idx: target_idx,
|
idx: target_idx,
|
||||||
val: ListSegment::Item {
|
val: ListSegment::Item {
|
||||||
|
@ -156,7 +194,7 @@ impl ListSegments {
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
ListDiff::Update { idx, val } => {
|
ListDiff::Update { idx:_, val } => {
|
||||||
s.cast.notify(&ListDiff::Update {
|
s.cast.notify(&ListDiff::Update {
|
||||||
idx: target_idx,
|
idx: target_idx,
|
||||||
val: ListSegment::Item {
|
val: ListSegment::Item {
|
||||||
|
|
Loading…
Reference in a new issue