helper.rs 1.3 KB

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