iterator.rs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. use super::cursor::*;
  2. use crate::core::{Attributes, Delta, Interval, Operation, RichTextAttributes, NEW_LINE};
  3. use std::ops::{Deref, DerefMut};
  4. pub(crate) const MAX_IV_LEN: usize = i32::MAX as usize;
  5. pub struct DeltaIter<'a, T: Attributes> {
  6. cursor: OpCursor<'a, T>,
  7. }
  8. impl<'a, T> DeltaIter<'a, T>
  9. where
  10. T: Attributes,
  11. {
  12. pub fn new(delta: &'a Delta<T>) -> Self {
  13. let interval = Interval::new(0, MAX_IV_LEN);
  14. Self::from_interval(delta, interval)
  15. }
  16. pub fn from_offset(delta: &'a Delta<T>, offset: usize) -> Self {
  17. let interval = Interval::new(0, MAX_IV_LEN);
  18. let mut iter = Self::from_interval(delta, interval);
  19. iter.seek::<CharMetric>(offset);
  20. iter
  21. }
  22. pub fn from_interval(delta: &'a Delta<T>, interval: Interval) -> Self {
  23. let cursor = OpCursor::new(delta, interval);
  24. Self { cursor }
  25. }
  26. pub fn ops(&mut self) -> Vec<Operation<T>> { self.collect::<Vec<_>>() }
  27. pub fn next_op_len(&self) -> Option<usize> {
  28. let interval = self.cursor.next_iv();
  29. if interval.is_empty() {
  30. None
  31. } else {
  32. Some(interval.size())
  33. }
  34. }
  35. pub fn next_op(&mut self) -> Option<Operation<T>> { self.cursor.next_op() }
  36. pub fn next_op_with_len(&mut self, len: usize) -> Option<Operation<T>> { self.cursor.next_with_len(Some(len)) }
  37. // find next op contains NEW_LINE
  38. pub fn next_op_with_newline(&mut self) -> Option<(Operation<T>, usize)> {
  39. let mut offset = 0;
  40. while self.has_next() {
  41. if let Some(op) = self.next_op() {
  42. if OpNewline::parse(&op).is_contain() {
  43. return Some((op, offset));
  44. }
  45. offset += op.len();
  46. }
  47. }
  48. None
  49. }
  50. pub fn seek<M: Metric>(&mut self, index: usize) {
  51. match M::seek(&mut self.cursor, index) {
  52. Ok(_) => {},
  53. Err(e) => log::error!("Seek fail: {:?}", e),
  54. }
  55. }
  56. pub fn has_next(&self) -> bool { self.cursor.has_next() }
  57. pub fn is_next_insert(&self) -> bool {
  58. match self.cursor.next_iter_op() {
  59. None => false,
  60. Some(op) => op.is_insert(),
  61. }
  62. }
  63. pub fn is_next_retain(&self) -> bool {
  64. match self.cursor.next_iter_op() {
  65. None => false,
  66. Some(op) => op.is_retain(),
  67. }
  68. }
  69. pub fn is_next_delete(&self) -> bool {
  70. match self.cursor.next_iter_op() {
  71. None => false,
  72. Some(op) => op.is_delete(),
  73. }
  74. }
  75. }
  76. impl<'a, T> Iterator for DeltaIter<'a, T>
  77. where
  78. T: Attributes,
  79. {
  80. type Item = Operation<T>;
  81. fn next(&mut self) -> Option<Self::Item> { self.next_op() }
  82. }
  83. pub fn is_empty_line_at_index(delta: &Delta<RichTextAttributes>, index: usize) -> bool {
  84. let mut iter = DeltaIter::new(delta);
  85. let (prev, next) = (iter.next_op_with_len(index), iter.next_op());
  86. if prev.is_none() {
  87. return true;
  88. }
  89. if next.is_none() {
  90. return false;
  91. }
  92. let prev = prev.unwrap();
  93. let next = next.unwrap();
  94. OpNewline::parse(&prev).is_end() && OpNewline::parse(&next).is_start()
  95. }
  96. pub struct AttributesIter<'a, T: Attributes> {
  97. delta_iter: DeltaIter<'a, T>,
  98. }
  99. impl<'a, T> AttributesIter<'a, T>
  100. where
  101. T: Attributes,
  102. {
  103. pub fn new(delta: &'a Delta<T>) -> Self {
  104. let interval = Interval::new(0, usize::MAX);
  105. Self::from_interval(delta, interval)
  106. }
  107. pub fn from_interval(delta: &'a Delta<T>, interval: Interval) -> Self {
  108. let delta_iter = DeltaIter::from_interval(delta, interval);
  109. Self { delta_iter }
  110. }
  111. pub fn next_or_empty(&mut self) -> T {
  112. match self.next() {
  113. None => T::default(),
  114. Some((_, attributes)) => attributes,
  115. }
  116. }
  117. }
  118. impl<'a, T> Deref for AttributesIter<'a, T>
  119. where
  120. T: Attributes,
  121. {
  122. type Target = DeltaIter<'a, T>;
  123. fn deref(&self) -> &Self::Target { &self.delta_iter }
  124. }
  125. impl<'a, T> DerefMut for AttributesIter<'a, T>
  126. where
  127. T: Attributes,
  128. {
  129. fn deref_mut(&mut self) -> &mut Self::Target { &mut self.delta_iter }
  130. }
  131. impl<'a, T> Iterator for AttributesIter<'a, T>
  132. where
  133. T: Attributes,
  134. {
  135. type Item = (usize, T);
  136. fn next(&mut self) -> Option<Self::Item> {
  137. let next_op = self.delta_iter.next_op();
  138. next_op.as_ref()?;
  139. let mut length: usize = 0;
  140. let mut attributes = T::default();
  141. match next_op.unwrap() {
  142. Operation::<T>::Delete(_n) => {},
  143. Operation::<T>::Retain(retain) => {
  144. tracing::trace!("extend retain attributes with {} ", &retain.attributes);
  145. attributes.extend_other(retain.attributes.clone());
  146. length = retain.n;
  147. },
  148. Operation::<T>::Insert(insert) => {
  149. tracing::trace!("extend insert attributes with {} ", &insert.attributes);
  150. attributes.extend_other(insert.attributes.clone());
  151. length = insert.count_of_code_units();
  152. },
  153. }
  154. Some((length, attributes))
  155. }
  156. }
  157. #[derive(PartialEq, Eq)]
  158. pub enum OpNewline {
  159. Start,
  160. End,
  161. Contain,
  162. Equal,
  163. NotFound,
  164. }
  165. impl OpNewline {
  166. pub fn parse<T: Attributes>(op: &Operation<T>) -> OpNewline {
  167. let s = op.get_data();
  168. if s == NEW_LINE {
  169. return OpNewline::Equal;
  170. }
  171. if s.starts_with(NEW_LINE) {
  172. return OpNewline::Start;
  173. }
  174. if s.ends_with(NEW_LINE) {
  175. return OpNewline::End;
  176. }
  177. if s.contains(NEW_LINE) {
  178. return OpNewline::Contain;
  179. }
  180. OpNewline::NotFound
  181. }
  182. pub fn is_start(&self) -> bool { self == &OpNewline::Start || self.is_equal() }
  183. pub fn is_end(&self) -> bool { self == &OpNewline::End || self.is_equal() }
  184. pub fn is_not_found(&self) -> bool { self == &OpNewline::NotFound }
  185. pub fn is_contain(&self) -> bool {
  186. self.is_start() || self.is_end() || self.is_equal() || self == &OpNewline::Contain
  187. }
  188. pub fn is_equal(&self) -> bool { self == &OpNewline::Equal }
  189. }