iterator.rs 6.2 KB

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