helper.rs 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. use crate::util::find_newline;
  2. use lib_ot::core::AttributeEntry;
  3. use lib_ot::text_delta::{empty_attributes, AttributeScope, DeltaTextOperation, DeltaTextOperations};
  4. pub(crate) fn line_break(
  5. op: &DeltaTextOperation,
  6. attribute: &AttributeEntry,
  7. scope: AttributeScope,
  8. ) -> DeltaTextOperations {
  9. let mut new_delta = DeltaTextOperations::new();
  10. let mut start = 0;
  11. let end = op.len();
  12. let mut s = op.get_data();
  13. while let Some(line_break) = find_newline(s) {
  14. match scope {
  15. AttributeScope::Inline => {
  16. new_delta.retain(line_break - start, attribute.clone().into());
  17. new_delta.retain(1, empty_attributes());
  18. }
  19. AttributeScope::Block => {
  20. new_delta.retain(line_break - start, empty_attributes());
  21. new_delta.retain(1, attribute.clone().into());
  22. }
  23. _ => {
  24. log::error!("Unsupported parser line break for {:?}", scope);
  25. }
  26. }
  27. start = line_break + 1;
  28. s = &s[start..s.len()];
  29. }
  30. if start < end {
  31. match scope {
  32. AttributeScope::Inline => new_delta.retain(end - start, attribute.clone().into()),
  33. AttributeScope::Block => new_delta.retain(end - start, empty_attributes()),
  34. _ => log::error!("Unsupported parser line break for {:?}", scope),
  35. }
  36. }
  37. new_delta
  38. }