From f3a10a0645468c622f19770074ce72e3dc286b50 Mon Sep 17 00:00:00 2001 From: myypo Date: Sat, 24 Aug 2024 00:11:44 +0300 Subject: [PATCH] feat(marks): newly placed marks are now inactive instead of hidden --- README.md | 5 +- compass/src/config/tracker.rs | 4 +- compass/src/state/record.rs | 32 ++-- compass/src/state/track_list.rs | 275 +++++++++++++++----------------- compass/src/state/tracker.rs | 8 +- compass/src/state/worker.rs | 4 +- 6 files changed, 164 insertions(+), 164 deletions(-) diff --git a/README.md b/README.md index 8865055..4533916 100644 --- a/README.md +++ b/README.md @@ -127,7 +127,10 @@ Default configuration: debounce_milliseconds = { run = 200, -- Change checking interval maintenance = 500, -- Consistency enforcing interval - show = 3000, -- How long to wait before visualizing a freshly placed mark + -- How long to wait before activating a freshly placed mark + -- Inactive marks are not visualized and can't be jumped back to, + -- but still can be jump forward to and by using goto or follow commands + activate = 3000, }, -- Files matching the following glob patterns will never be tracked ignored_patterns = { diff --git a/compass/src/config/tracker.rs b/compass/src/config/tracker.rs index 97baac1..3a5613a 100644 --- a/compass/src/config/tracker.rs +++ b/compass/src/config/tracker.rs @@ -41,7 +41,7 @@ pub struct Debounce { #[serde(deserialize_with = "duration_from_millis")] pub maintenance: Duration, #[serde(deserialize_with = "duration_from_millis")] - pub show: Duration, + pub activate: Duration, } impl Default for Debounce { @@ -49,7 +49,7 @@ impl Default for Debounce { Self { run: Duration::from_millis(200), maintenance: Duration::from_millis(500), - show: Duration::from_millis(3000), + activate: Duration::from_millis(3000), } } } diff --git a/compass/src/state/record.rs b/compass/src/state/record.rs index 8df8b9e..a0b3781 100644 --- a/compass/src/state/record.rs +++ b/compass/src/state/record.rs @@ -11,7 +11,7 @@ use bitcode::{Decode, Encode}; use nvim_oxi::api::{command, set_current_buf, Buffer, Window}; use serde::Deserialize; -use super::track_list::Mark; +use super::track_list::{Active, Mark}; #[derive(Clone, Debug, PartialEq, Eq, Hash)] pub struct Record { @@ -25,7 +25,7 @@ pub struct Record { pub enum LazyExtmark { Loaded(Extmark), Unloaded((CursorPosition, RecordMarkTime)), - Hidden((CursorPosition, RecordMarkTime, Instant)), + Inactive((CursorPosition, RecordMarkTime, Instant)), } impl LazyExtmark { @@ -33,7 +33,7 @@ impl LazyExtmark { match self { Self::Loaded(e) => e.pos(buf), Self::Unloaded((p, _)) => p.clone(), - Self::Hidden((p, _, _)) => p.clone(), + Self::Inactive((p, _, _)) => p.clone(), } } @@ -48,7 +48,7 @@ impl LazyExtmark { match self { Self::Loaded(e) => e.delete(buf), Self::Unloaded(_) => Ok(()), - Self::Hidden(_) => Ok(()), + Self::Inactive(_) => Ok(()), } } } @@ -103,11 +103,11 @@ impl Record { }) } - pub fn try_new_hidden(buf: Buffer, typ: TypeRecord, pos: CursorPosition) -> Result { + pub fn try_new_inactive(buf: Buffer, typ: TypeRecord, pos: CursorPosition) -> Result { Ok(Self { buf, typ, - lazy_extmark: LazyExtmark::Hidden((pos, RecordMarkTime::PastClose, Instant::now())), + lazy_extmark: LazyExtmark::Inactive((pos, RecordMarkTime::PastClose, Instant::now())), frecency: Frecency::new(), }) } @@ -122,7 +122,7 @@ impl Record { extmark } - LazyExtmark::Hidden((p, t, _)) => { + LazyExtmark::Inactive((p, t, _)) => { let extmark = create_record_mark(self.buf.clone(), &Into::::into(p), *t)?; self.lazy_extmark = LazyExtmark::Loaded(extmark.clone()); @@ -148,7 +148,7 @@ impl Record { create_record_mark(self.buf.clone(), &Into::::into(&pos), time)?; self.lazy_extmark = LazyExtmark::Loaded(extmark.clone()); } - LazyExtmark::Hidden(_) => { + LazyExtmark::Inactive(_) => { let extmark = create_record_mark(self.buf.clone(), &Into::::into(&pos), time)?; self.lazy_extmark = LazyExtmark::Loaded(extmark.clone()); @@ -161,7 +161,7 @@ impl Record { Ok(()) } - pub fn hide_update( + pub fn deact_update( &mut self, buf: Buffer, typ: TypeRecord, @@ -172,7 +172,7 @@ impl Record { e.delete(buf)?; } - self.lazy_extmark = LazyExtmark::Hidden((pos, time, Instant::now())); + self.lazy_extmark = LazyExtmark::Inactive((pos, time, Instant::now())); self.typ = typ; self.frecency.add_record(FrecencyType::Update); @@ -213,8 +213,8 @@ impl Record { LazyExtmark::Unloaded((p, _)) => { self.lazy_extmark = LazyExtmark::Unloaded((p.clone(), time)); } - LazyExtmark::Hidden((p, _, i)) => { - self.lazy_extmark = LazyExtmark::Hidden((p.clone(), time, *i)); + LazyExtmark::Inactive((p, _, i)) => { + self.lazy_extmark = LazyExtmark::Inactive((p.clone(), time, *i)); } } } @@ -251,7 +251,7 @@ impl Mark for Record { Ok(()) } - LazyExtmark::Hidden((p, t, _)) => { + LazyExtmark::Inactive((p, t, _)) => { let extmark = create_record_mark(self.buf.clone(), &Into::::into(p), *t)?; self.lazy_extmark = LazyExtmark::Loaded(extmark.clone()); @@ -269,6 +269,12 @@ impl Mark for Record { } } +impl Active for Record { + fn is_active(&self) -> bool { + !matches!(self.lazy_extmark, LazyExtmark::Inactive(_)) + } +} + mod tests { use core::panic; diff --git a/compass/src/state/track_list.rs b/compass/src/state/track_list.rs index df5bef4..572ab14 100644 --- a/compass/src/state/track_list.rs +++ b/compass/src/state/track_list.rs @@ -22,125 +22,8 @@ pub trait Mark { fn open_buf(&self) -> Result<()>; } -impl TrackList -where - T: Mark + IndicateCloseness, -{ - pub fn close_past_mut(&mut self) -> Option<&mut T> { - match self.pos { - Some(p) => self.get_mut(p + 1), - None => self.get_mut(0), - } - } - - pub fn make_close_past(&mut self, idx: usize) -> Option<()> { - if let Some(p) = self.pos { - if p + 1 == idx { - return Some(()); - } - } - - let val = self.ring.get_mut(idx)?; - val.as_close_past(); - - let len = self.len(); - if len == 1 { - self.pos = None; - return Some(()); - } - - match self.pos { - Some(p) => { - match idx.ge(&p) { - // past -> close past - true => { - if let Some(old_close) = self.close_past_mut() { - old_close.as_past(); - }; - - self.ring.swap(idx, p); - - if p + 1 < idx { - self.ring.make_contiguous()[p..=idx].rotate_right(1); - } else { - self.pos = p.checked_sub(1); - } - - Some(()) - } - - // future -> close past - false => { - match p.checked_sub(1) { - Some(new_pos) => { - if p == idx { - if let Some(close_new) = self.ring.get_mut(new_pos) { - close_new.as_close_future(); - }; - } - - self.pos = Some(new_pos); - self.ring.swap(idx, p); - self.ring.make_contiguous()[idx..=new_pos].rotate_right(1); - } - None => { - self.pos = None; - } - } - - Some(()) - } - } - } - None => { - if idx != 0 { - self.ring.front_mut()?.as_past(); - } - - self.ring.make_contiguous()[0..=idx].rotate_right(1); - - Some(()) - } - } - } - - pub fn remove(&mut self, i: usize) { - match self.pos { - Some(p) => match i { - _ if i + 1 == p => { - if let Some(next_past) = self.ring.get_mut(i + 1) { - next_past.as_close_past(); - }; - } - - _ if i == p => { - let nfi = p.checked_sub(1); - self.pos = nfi; - - if let Some(nfi) = p.checked_sub(1) { - if let Some(next_fut) = self.ring.get_mut(nfi) { - next_fut.as_close_future(); - }; - } - } - - _ if i < p => { - let nfi = p.checked_sub(1); - self.pos = nfi; - } - - _ => {} - }, - None if i == 0 => { - if let Some(next_past) = self.ring.get_mut(i + 1) { - next_past.as_close_past(); - }; - } - _ => {} - } - - let _ = self.ring.remove(i); - } +pub trait Active { + fn is_active(&self) -> bool; } impl TrackList { @@ -167,18 +50,6 @@ impl TrackList { self.ring.iter_mut() } - fn past_exists(&self) -> bool { - if self.ring.is_empty() { - return false; - } - - let Some(p) = self.pos else { - return true; - }; - - p + 1 < self.ring.len() - } - pub fn get_mut(&mut self, i: usize) -> Option<&mut T> { self.ring.get_mut(i) } @@ -191,8 +62,19 @@ impl TrackList { impl TrackList where - T: IndicateCloseness + Mark, + T: IndicateCloseness + Mark + Active, { + fn active_close_past_idx(&self) -> Option { + self.ring + .iter() + .enumerate() + .skip(self.pos.map(|p| p + 1).unwrap_or(0)) + .find_map(|(i, v)| match v.is_active() { + true => Some(i), + false => None, + }) + } + pub fn push(&mut self, val: T) { match self.pos { Some(p) => { @@ -213,11 +95,7 @@ where } pub fn step_past(&mut self) -> Option<&mut T> { - if !self.past_exists() { - return None; - }; - - let pos = self.pos.map(|p| p + 1).unwrap_or(0); + let pos = self.active_close_past_idx()?; self.pos = Some(pos); { @@ -268,11 +146,7 @@ where } pub fn pop_past(&mut self) -> Option { - if !self.past_exists() { - return None; - }; - - let pos = self.pos.map(|p| p + 1).unwrap_or(0); + let pos = self.active_close_past_idx()?; if let Some(new_close) = self.ring.get_mut(pos + 1) { new_close.as_close_past(); @@ -295,6 +169,118 @@ where self.ring.remove(pos) } + + pub fn remove(&mut self, i: usize) { + match self.pos { + Some(p) => match i { + _ if i + 1 == p => { + if let Some(next_past) = self.ring.get_mut(i + 1) { + next_past.as_close_past(); + }; + } + + _ if i == p => { + let nfi = p.checked_sub(1); + self.pos = nfi; + + if let Some(nfi) = p.checked_sub(1) { + if let Some(next_fut) = self.ring.get_mut(nfi) { + next_fut.as_close_future(); + }; + } + } + + _ if i < p => { + let nfi = p.checked_sub(1); + self.pos = nfi; + } + + _ => {} + }, + None if i == 0 => { + if let Some(next_past) = self.ring.get_mut(i + 1) { + next_past.as_close_past(); + }; + } + _ => {} + } + + let _ = self.ring.remove(i); + } + + pub fn make_close_past(&mut self, idx: usize) -> Option<()> { + if let Some(p) = self.pos { + if p + 1 == idx { + return Some(()); + } + } + + let val = self.ring.get_mut(idx)?; + val.as_close_past(); + + let len = self.len(); + if len == 1 { + self.pos = None; + return Some(()); + } + + match self.pos { + Some(p) => { + match idx.ge(&p) { + // past -> close past + true => { + if let Some(old_close) = match self.pos { + Some(p) => self.get_mut(p + 1), + None => self.get_mut(0), + } { + old_close.as_past(); + }; + + self.ring.swap(idx, p); + + if p + 1 < idx { + self.ring.make_contiguous()[p..=idx].rotate_right(1); + } else { + self.pos = p.checked_sub(1); + } + + Some(()) + } + + // future -> close past + false => { + match p.checked_sub(1) { + Some(new_pos) => { + if p == idx { + if let Some(close_new) = self.ring.get_mut(new_pos) { + close_new.as_close_future(); + }; + } + + self.pos = Some(new_pos); + self.ring.swap(idx, p); + self.ring.make_contiguous()[idx..=new_pos].rotate_right(1); + } + None => { + self.pos = None; + } + } + + Some(()) + } + } + } + None => { + if idx != 0 { + self.ring.front_mut()?.as_past(); + } + + self.ring.make_contiguous()[0..=idx].rotate_right(1); + + Some(()) + } + } + } } impl TrackList @@ -341,6 +327,11 @@ mod tests { Ok(()) } } + impl Active for Num { + fn is_active(&self) -> bool { + true + } + } #[test] fn can_go_to_oldest() { diff --git a/compass/src/state/tracker.rs b/compass/src/state/tracker.rs index fa2d9a5..6930d10 100644 --- a/compass/src/state/tracker.rs +++ b/compass/src/state/tracker.rs @@ -129,13 +129,13 @@ impl Tracker { buf_new == *buf && { lazy_extmark.pos(buf_new.clone()).is_nearby(&pos_new) } }, ) { - nearby_record.hide_update(buf_new, tick_new, pos_new, RecordMarkTime::PastClose)?; + nearby_record.deact_update(buf_new, tick_new, pos_new, RecordMarkTime::PastClose)?; self.list.make_close_past(i); return Ok(()); }; - let record_new = Record::try_new_hidden(buf_new, tick_new, pos_new)?; + let record_new = Record::try_new_inactive(buf_new, tick_new, pos_new)?; self.list.push(record_new); @@ -250,7 +250,7 @@ impl SyncTracker { Ok(()) } - pub fn show(&mut self) -> Result<()> { + pub fn activate(&mut self) -> Result<()> { let conf = get_config(); let curr_bufs: Vec = list_wins().filter_map(|w| w.get_buf().ok()).collect(); @@ -258,7 +258,7 @@ impl SyncTracker { let list = &mut self.lock()?.list; for r in list.iter_mut_from_future().filter(|r| { curr_bufs.iter().any(|b| b == &r.buf) && - !matches!(r.lazy_extmark, LazyExtmark::Hidden((_, _, i)) if i.elapsed() < conf.tracker.debounce_milliseconds.show) + !matches!(r.lazy_extmark, LazyExtmark::Inactive((_, _, i)) if i.elapsed() < conf.tracker.debounce_milliseconds.activate) }) { r.load_extmark()?; } diff --git a/compass/src/state/worker.rs b/compass/src/state/worker.rs index 3d9422c..5b25dcc 100644 --- a/compass/src/state/worker.rs +++ b/compass/src/state/worker.rs @@ -22,7 +22,7 @@ impl Worker { pub fn run_jobs(mut self) { std::thread::spawn(move || { let debounce = &get_config().tracker.debounce_milliseconds; - let min_deb = min!(debounce.run, debounce.maintenance, debounce.show); + let min_deb = min!(debounce.run, debounce.maintenance, debounce.activate); let mut run_inst = Instant::now(); let mut maint_inst = Instant::now(); @@ -45,7 +45,7 @@ impl Worker { } if let Some(tracker) = &mut self.tracker { - tracker.show()?; + tracker.activate()?; }; Ok(())