format_ext.rs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. use crate::{
  2. client::view::{FormatExt, NEW_LINE},
  3. core::{
  4. Attribute,
  5. AttributeScope,
  6. Attributes,
  7. CharMetric,
  8. Delta,
  9. DeltaBuilder,
  10. DeltaIter,
  11. Interval,
  12. Operation,
  13. },
  14. };
  15. pub struct FormatLinkAtCaretPositionExt {}
  16. impl FormatExt for FormatLinkAtCaretPositionExt {
  17. fn apply(&self, delta: &Delta, interval: Interval, attribute: &Attribute) -> Option<Delta> {
  18. let mut iter = DeltaIter::new(delta);
  19. iter.seek::<CharMetric>(interval.start);
  20. let (before, after) = (iter.next(), iter.next());
  21. let mut start = interval.start;
  22. let mut retain = 0;
  23. if let Some(before) = before {
  24. if before.contain_attribute(attribute) {
  25. start -= before.length();
  26. retain += before.length();
  27. }
  28. }
  29. if let Some(after) = after {
  30. if after.contain_attribute(attribute) {
  31. if retain != 0 {
  32. retain += after.length();
  33. }
  34. }
  35. }
  36. if retain == 0 {
  37. return None;
  38. }
  39. Some(
  40. DeltaBuilder::new()
  41. .retain(start, Attributes::default())
  42. .retain(retain, (attribute.clone()).into())
  43. .build(),
  44. )
  45. }
  46. }
  47. pub struct ResolveLineFormatExt {}
  48. impl FormatExt for ResolveLineFormatExt {
  49. fn apply(&self, delta: &Delta, interval: Interval, attribute: &Attribute) -> Option<Delta> {
  50. if attribute.scope != AttributeScope::Block {
  51. return None;
  52. }
  53. let mut new_delta = Delta::new();
  54. new_delta.retain(interval.start, Attributes::default());
  55. let mut iter = DeltaIter::new(delta);
  56. iter.seek::<CharMetric>(interval.start);
  57. None
  58. }
  59. }
  60. pub struct ResolveInlineFormatExt {}
  61. impl FormatExt for ResolveInlineFormatExt {
  62. fn apply(&self, delta: &Delta, interval: Interval, attribute: &Attribute) -> Option<Delta> {
  63. if attribute.scope != AttributeScope::Inline {
  64. return None;
  65. }
  66. let mut new_delta = DeltaBuilder::new()
  67. .retain(interval.start, Attributes::default())
  68. .build();
  69. let mut iter = DeltaIter::new(delta);
  70. iter.seek::<CharMetric>(interval.start);
  71. let mut cur = 0;
  72. let len = interval.size();
  73. while cur < len && iter.has_next() {
  74. let some_op = iter.next_op_with_len(len - cur);
  75. if some_op.is_none() {
  76. return Some(new_delta);
  77. }
  78. let op = some_op.unwrap();
  79. if let Operation::Insert(insert) = &op {
  80. let mut s = insert.s.as_str();
  81. match s.find(NEW_LINE) {
  82. None => {
  83. new_delta.retain(op.length(), attribute.clone().into());
  84. },
  85. Some(mut line_break) => {
  86. let mut pos = 0;
  87. let mut some_line_break = Some(line_break);
  88. while some_line_break.is_some() {
  89. let line_break = some_line_break.unwrap();
  90. new_delta.retain(line_break - pos, attribute.clone().into());
  91. new_delta.retain(1, Attributes::default());
  92. pos = line_break + 1;
  93. s = &s[pos..s.len()];
  94. some_line_break = s.find(NEW_LINE);
  95. }
  96. if pos < op.length() {
  97. new_delta.retain(op.length() - pos, attribute.clone().into());
  98. }
  99. },
  100. }
  101. }
  102. cur += op.length();
  103. }
  104. Some(new_delta)
  105. }
  106. }