project_note
Clone
https://git.tahinli.com/Tahinli/project_note.git
0 watching·0 forks·Rust·3.6 MiB·c49f5e7·Tahinli·1 month ago·2 commits
| 1 | use std::ops::Range; |
| 2 | |
| 3 | use generic_btree::rle::{CanRemove, HasLength, Mergeable, Sliceable, TryInsert}; |
| 4 | use loro_common::{CompactId, Counter, HasId, IdFull, IdSpan, Lamport, ID}; |
| 5 | use serde::{Deserialize, Serialize}; |
| 6 | |
| 7 | use super::{tracker::UNKNOWN_PEER_ID, AnchorType}; |
| 8 | |
| 9 | #[derive(Clone, PartialEq, Eq, Copy, Serialize, Deserialize)] |
| 10 | pub(crate) struct RichtextChunk { |
| 11 | start: u32, |
| 12 | end: u32, |
| 13 | } |
| 14 | |
| 15 | impl std::fmt::Debug for RichtextChunk { |
| 16 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 17 | f.debug_struct("RichtextChunk") |
| 18 | .field("value", &self.value()) |
| 19 | .finish() |
| 20 | } |
| 21 | } |
| 22 | |
| 23 | #[derive(Debug, Clone, PartialEq, Eq, Copy)] |
| 24 | pub(crate) enum RichtextChunkKind { |
| 25 | Text, |
| 26 | StyleAnchor, |
| 27 | Unknown, |
| 28 | /// Move anchor is a special anchor that is used to mark the FugueSpan created |
| 29 | /// by a move operation. |
| 30 | MoveAnchor, |
| 31 | } |
| 32 | |
| 33 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 34 | pub(crate) enum RichtextChunkValue { |
| 35 | Text(Range<u32>), |
| 36 | StyleAnchor { |
| 37 | id: u32, |
| 38 | anchor_type: AnchorType, |
| 39 | }, |
| 40 | Unknown(u32), |
| 41 | /// Move anchor is a special anchor that is used to mark the FugueSpan created |
| 42 | /// by a move operation. |
| 43 | MoveAnchor, |
| 44 | } |
| 45 | |
| 46 | impl RichtextChunk { |
| 47 | pub(crate) const UNKNOWN: u32 = u32::MAX; |
| 48 | pub(crate) const START_STYLE_ANCHOR: u32 = u32::MAX - 1; |
| 49 | pub(crate) const END_STYLE_ANCHOR: u32 = u32::MAX - 2; |
| 50 | pub(crate) const MOVE_ANCHOR: u32 = u32::MAX - 3; |
| 51 | |
| 52 | #[inline] |
| 53 | pub fn new_text(range: Range<u32>) -> Self { |
| 54 | Self { |
| 55 | start: range.start, |
| 56 | end: range.end, |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | #[inline] |
| 61 | pub fn new_style_anchor(idx: u32, anchor_type: AnchorType) -> Self { |
| 62 | match anchor_type { |
| 63 | AnchorType::Start => Self { |
| 64 | start: Self::START_STYLE_ANCHOR, |
| 65 | end: idx, |
| 66 | }, |
| 67 | AnchorType::End => Self { |
| 68 | start: Self::END_STYLE_ANCHOR, |
| 69 | end: idx, |
| 70 | }, |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | #[inline] |
| 75 | pub fn new_unknown(len: u32) -> Self { |
| 76 | Self { |
| 77 | start: Self::UNKNOWN, |
| 78 | end: len, |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | #[inline] |
| 83 | pub fn new_move() -> Self { |
| 84 | Self { |
| 85 | start: Self::MOVE_ANCHOR, |
| 86 | end: Self::MOVE_ANCHOR, |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | #[inline] |
| 91 | pub(crate) fn kind(&self) -> RichtextChunkKind { |
| 92 | match self.start { |
| 93 | Self::START_STYLE_ANCHOR => RichtextChunkKind::StyleAnchor, |
| 94 | Self::END_STYLE_ANCHOR => RichtextChunkKind::StyleAnchor, |
| 95 | Self::UNKNOWN => RichtextChunkKind::Unknown, |
| 96 | Self::MOVE_ANCHOR => RichtextChunkKind::MoveAnchor, |
| 97 | _ => RichtextChunkKind::Text, |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | #[inline(always)] |
| 102 | pub fn len(&self) -> usize { |
| 103 | match self.start { |
| 104 | Self::UNKNOWN => self.end as usize, |
| 105 | Self::START_STYLE_ANCHOR | Self::END_STYLE_ANCHOR | Self::MOVE_ANCHOR => 1, |
| 106 | _ => (self.end - self.start) as usize, |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | #[inline] |
| 111 | pub(crate) fn value(&self) -> RichtextChunkValue { |
| 112 | match self.start { |
| 113 | Self::UNKNOWN => RichtextChunkValue::Unknown(self.end), |
| 114 | Self::START_STYLE_ANCHOR => RichtextChunkValue::StyleAnchor { |
| 115 | id: self.end, |
| 116 | anchor_type: AnchorType::Start, |
| 117 | }, |
| 118 | Self::END_STYLE_ANCHOR => RichtextChunkValue::StyleAnchor { |
| 119 | id: self.end, |
| 120 | anchor_type: AnchorType::End, |
| 121 | }, |
| 122 | Self::MOVE_ANCHOR => RichtextChunkValue::MoveAnchor, |
| 123 | _ => RichtextChunkValue::Text(self.start..self.end), |
| 124 | } |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | impl Mergeable for RichtextChunk { |
| 129 | fn can_merge(&self, rhs: &Self) -> bool { |
| 130 | match (self.kind(), rhs.kind()) { |
| 131 | (RichtextChunkKind::Text, RichtextChunkKind::Text) => self.end == rhs.start, |
| 132 | (RichtextChunkKind::Unknown, RichtextChunkKind::Unknown) => true, |
| 133 | _ => false, |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | fn merge_right(&mut self, rhs: &Self) { |
| 138 | match (self.kind(), rhs.kind()) { |
| 139 | (RichtextChunkKind::Text, RichtextChunkKind::Text) => self.end = rhs.end, |
| 140 | (RichtextChunkKind::Unknown, RichtextChunkKind::Unknown) => self.end += rhs.end, |
| 141 | _ => unreachable!(), |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | fn merge_left(&mut self, left: &Self) { |
| 146 | match (self.kind(), left.kind()) { |
| 147 | (RichtextChunkKind::Text, RichtextChunkKind::Text) => self.start = left.start, |
| 148 | (RichtextChunkKind::Unknown, RichtextChunkKind::Unknown) => self.end += left.end, |
| 149 | _ => unreachable!(), |
| 150 | } |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | impl HasLength for RichtextChunk { |
| 155 | #[inline(always)] |
| 156 | fn rle_len(&self) -> usize { |
| 157 | self.len() |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | impl Sliceable for RichtextChunk { |
| 162 | fn _slice(&self, range: Range<usize>) -> Self { |
| 163 | match self.kind() { |
| 164 | RichtextChunkKind::Text => { |
| 165 | assert!( |
| 166 | range.len() <= self.len(), |
| 167 | "range: {:?}, self: {:?}", |
| 168 | range, |
| 169 | self |
| 170 | ); |
| 171 | Self { |
| 172 | start: self.start + range.start as u32, |
| 173 | end: self.start + range.end as u32, |
| 174 | } |
| 175 | } |
| 176 | RichtextChunkKind::StyleAnchor | RichtextChunkKind::MoveAnchor => { |
| 177 | assert_eq!(range.len(), 1); |
| 178 | *self |
| 179 | } |
| 180 | RichtextChunkKind::Unknown => { |
| 181 | assert!(range.len() <= self.len()); |
| 182 | Self { |
| 183 | start: Self::UNKNOWN, |
| 184 | end: range.len() as u32, |
| 185 | } |
| 186 | } |
| 187 | } |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | #[derive(Debug, Clone, PartialEq, Eq, Copy)] |
| 192 | pub(super) struct FugueSpan { |
| 193 | pub id: IdFull, |
| 194 | /// Sometimes, the id of the span is just a placeholder. |
| 195 | /// This field is used to track the real id of the span. |
| 196 | /// We know the real id when deletion happens because we |
| 197 | /// have the start_id info for each deletion. |
| 198 | pub real_id: Option<CompactId>, |
| 199 | /// The status at the current version |
| 200 | pub status: Status, |
| 201 | /// The status at the `new` version. |
| 202 | /// It's used when calculating diff. |
| 203 | pub diff_status: Option<Status>, |
| 204 | pub origin_left: Option<CompactId>, |
| 205 | pub origin_right: Option<CompactId>, |
| 206 | pub content: RichtextChunk, |
| 207 | } |
| 208 | |
| 209 | pub(super) enum DiffStatus { |
| 210 | NotChanged, |
| 211 | Created, |
| 212 | Deleted, |
| 213 | } |
| 214 | |
| 215 | impl FugueSpan { |
| 216 | #[inline(always)] |
| 217 | pub fn id_span(&self) -> IdSpan { |
| 218 | IdSpan::new( |
| 219 | self.id.peer, |
| 220 | self.id.counter, |
| 221 | self.id.counter + self.content.len() as Counter, |
| 222 | ) |
| 223 | } |
| 224 | |
| 225 | #[inline] |
| 226 | pub fn diff(&self) -> DiffStatus { |
| 227 | if self.diff_status.is_none() { |
| 228 | return DiffStatus::NotChanged; |
| 229 | } |
| 230 | |
| 231 | match ( |
| 232 | self.status.is_activated(), |
| 233 | self.diff_status.unwrap().is_activated(), |
| 234 | ) { |
| 235 | (true, false) => DiffStatus::Deleted, |
| 236 | (false, true) => DiffStatus::Created, |
| 237 | _ => DiffStatus::NotChanged, |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | pub fn is_activated_in_diff(&self) -> bool { |
| 242 | match self.diff_status { |
| 243 | Some(d) => d.is_activated(), |
| 244 | None => self.status.is_activated(), |
| 245 | } |
| 246 | } |
| 247 | |
| 248 | pub(crate) fn real_id(&self) -> ID { |
| 249 | if let Some(id) = self.real_id { |
| 250 | id.to_id() |
| 251 | } else { |
| 252 | self.id.id() |
| 253 | } |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | impl Sliceable for FugueSpan { |
| 258 | fn _slice(&self, range: Range<usize>) -> Self { |
| 259 | Self { |
| 260 | id: self.id.inc(range.start as Counter), |
| 261 | real_id: self.real_id.map(|x| x.inc(range.start as Counter)), |
| 262 | status: self.status, |
| 263 | diff_status: self.diff_status, |
| 264 | origin_left: if range.start == 0 { |
| 265 | self.origin_left |
| 266 | } else { |
| 267 | Some( |
| 268 | self.id |
| 269 | .inc((range.start - 1) as Counter) |
| 270 | .id() |
| 271 | .try_into() |
| 272 | .unwrap(), |
| 273 | ) |
| 274 | }, |
| 275 | origin_right: self.origin_right, |
| 276 | content: self.content._slice(range), |
| 277 | } |
| 278 | } |
| 279 | } |
| 280 | |
| 281 | impl HasLength for FugueSpan { |
| 282 | #[inline(always)] |
| 283 | fn rle_len(&self) -> usize { |
| 284 | self.content.len() |
| 285 | } |
| 286 | } |
| 287 | |
| 288 | impl Mergeable for FugueSpan { |
| 289 | fn can_merge(&self, rhs: &Self) -> bool { |
| 290 | self.id.peer == rhs.id.peer |
| 291 | && self.status == rhs.status |
| 292 | && self.diff_status == rhs.diff_status |
| 293 | && self.id.counter + self.content.len() as Counter == rhs.id.counter |
| 294 | && self.id.lamport + self.content.len() as Lamport == rhs.id.lamport |
| 295 | && rhs.origin_left.is_some() |
| 296 | && rhs.origin_left.unwrap().peer == self.id.peer |
| 297 | && rhs.origin_left.unwrap().counter.get() |
| 298 | == self.id.counter + self.content.len() as Counter - 1 |
| 299 | && self.origin_right == rhs.origin_right |
| 300 | && self.content.can_merge(&rhs.content) |
| 301 | && ((self.real_id.is_none() && rhs.real_id.is_none()) |
| 302 | || (self.real_id.is_some() |
| 303 | && rhs.real_id.is_some() |
| 304 | && self.real_id.unwrap().inc(self.content.len() as i32) |
| 305 | == rhs.real_id.unwrap())) |
| 306 | } |
| 307 | |
| 308 | fn merge_right(&mut self, rhs: &Self) { |
| 309 | self.content.merge_right(&rhs.content); |
| 310 | } |
| 311 | |
| 312 | fn merge_left(&mut self, left: &Self) { |
| 313 | self.id = left.id; |
| 314 | self.real_id = left.real_id; |
| 315 | self.origin_left = left.origin_left; |
| 316 | self.content.merge_left(&left.content); |
| 317 | } |
| 318 | } |
| 319 | |
| 320 | impl TryInsert for FugueSpan { |
| 321 | fn try_insert(&mut self, _pos: usize, elem: Self) -> Result<(), Self> |
| 322 | where |
| 323 | Self: Sized, |
| 324 | { |
| 325 | Err(elem) |
| 326 | } |
| 327 | } |
| 328 | |
| 329 | impl CanRemove for FugueSpan { |
| 330 | fn can_remove(&self) -> bool { |
| 331 | self.content.len() == 0 |
| 332 | } |
| 333 | } |
| 334 | |
| 335 | impl FugueSpan { |
| 336 | #[allow(unused)] |
| 337 | pub fn new(id: IdFull, content: RichtextChunk) -> Self { |
| 338 | Self { |
| 339 | id, |
| 340 | real_id: if id.peer == UNKNOWN_PEER_ID { |
| 341 | None |
| 342 | } else { |
| 343 | Some(id.id().try_into().unwrap()) |
| 344 | }, |
| 345 | status: Status::default(), |
| 346 | diff_status: None, |
| 347 | origin_left: None, |
| 348 | origin_right: None, |
| 349 | content, |
| 350 | } |
| 351 | } |
| 352 | |
| 353 | #[inline(always)] |
| 354 | pub fn is_activated(&self) -> bool { |
| 355 | self.status.is_activated() |
| 356 | } |
| 357 | |
| 358 | #[inline] |
| 359 | pub fn activated_len(&self) -> usize { |
| 360 | if self.is_activated() { |
| 361 | self.content.len() |
| 362 | } else { |
| 363 | 0 |
| 364 | } |
| 365 | } |
| 366 | } |
| 367 | |
| 368 | impl HasId for FugueSpan { |
| 369 | fn id_start(&self) -> ID { |
| 370 | self.id.id() |
| 371 | } |
| 372 | } |
| 373 | |
| 374 | #[derive(Debug, Clone, PartialEq, Eq, Default, Hash, Copy)] |
| 375 | pub(super) struct Status { |
| 376 | /// is this span from a future operation |
| 377 | pub future: bool, |
| 378 | pub delete_times: i16, |
| 379 | } |
| 380 | |
| 381 | impl Status { |
| 382 | #[inline(always)] |
| 383 | pub fn is_activated(&self) -> bool { |
| 384 | self.delete_times == 0 && !self.future |
| 385 | } |
| 386 | } |