helper.rs 1.3 KB

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