| 1 |
use crate::doc_formatter::{DocumentFormatter, TextFormat}; |
| 2 |
use crate::text_annotations::{InlineAnnotation, Overlay, TextAnnotations}; |
| 3 |
|
| 4 |
impl TextFormat { |
| 5 |
fn new_test(softwrap: bool) -> Self { |
| 6 |
TextFormat { |
| 7 |
soft_wrap: softwrap, |
| 8 |
tab_width: 2, |
| 9 |
max_wrap: 3, |
| 10 |
max_indent_retain: 4, |
| 11 |
wrap_indicator: ".".into(), |
| 12 |
wrap_indicator_highlight: None, |
| 13 |
|
| 14 |
viewport_width: 17, |
| 15 |
soft_wrap_at_text_width: false, |
| 16 |
} |
| 17 |
} |
| 18 |
} |
| 19 |
|
| 20 |
impl<'t> DocumentFormatter<'t> { |
| 21 |
fn collect_to_str(&mut self) -> String { |
| 22 |
use std::fmt::Write; |
| 23 |
let mut res = String::new(); |
| 24 |
let viewport_width = self.text_fmt.viewport_width; |
| 25 |
let soft_wrap_at_text_width = self.text_fmt.soft_wrap_at_text_width; |
| 26 |
let mut line = 0; |
| 27 |
|
| 28 |
for grapheme in self { |
| 29 |
if grapheme.visual_pos.row != line { |
| 30 |
line += 1; |
| 31 |
assert_eq!(grapheme.visual_pos.row, line); |
| 32 |
write!(res, "\n{}", ".".repeat(grapheme.visual_pos.col)).unwrap(); |
| 33 |
} |
| 34 |
if !soft_wrap_at_text_width { |
| 35 |
assert!( |
| 36 |
grapheme.visual_pos.col <= viewport_width as usize, |
| 37 |
"softwrapped failed {}<={viewport_width}", |
| 38 |
grapheme.visual_pos.col |
| 39 |
); |
| 40 |
} |
| 41 |
write!(res, "{}", grapheme.raw).unwrap(); |
| 42 |
} |
| 43 |
|
| 44 |
res |
| 45 |
} |
| 46 |
} |
| 47 |
|
| 48 |
fn softwrap_text(text: &str) -> String { |
| 49 |
DocumentFormatter::new_at_prev_checkpoint( |
| 50 |
text.into(), |
| 51 |
&TextFormat::new_test(true), |
| 52 |
&TextAnnotations::default(), |
| 53 |
0, |
| 54 |
) |
| 55 |
.collect_to_str() |
| 56 |
} |
| 57 |
|
| 58 |
#[test] |
| 59 |
fn basic_softwrap() { |
| 60 |
assert_eq!( |
| 61 |
softwrap_text(&"foo ".repeat(10)), |
| 62 |
"foo foo foo foo \n.foo foo foo foo \n.foo foo " |
| 63 |
); |
| 64 |
assert_eq!( |
| 65 |
softwrap_text(&"fooo ".repeat(10)), |
| 66 |
"fooo fooo fooo \n.fooo fooo fooo \n.fooo fooo fooo \n.fooo " |
| 67 |
); |
| 68 |
|
| 69 |
|
| 70 |
assert_eq!(softwrap_text("\t\txxxx1xxxx2xx\n"), " xxxx1xxxx2xx \n "); |
| 71 |
} |
| 72 |
|
| 73 |
#[test] |
| 74 |
fn softwrap_indentation() { |
| 75 |
assert_eq!( |
| 76 |
softwrap_text("\t\tfoo1 foo2 foo3 foo4 foo5 foo6\n"), |
| 77 |
" foo1 foo2 \n.....foo3 foo4 \n.....foo5 foo6 \n " |
| 78 |
); |
| 79 |
assert_eq!( |
| 80 |
softwrap_text("\t\t\tfoo1 foo2 foo3 foo4 foo5 foo6\n"), |
| 81 |
" foo1 foo2 \n.foo3 foo4 foo5 \n.foo6 \n " |
| 82 |
); |
| 83 |
} |
| 84 |
|
| 85 |
#[test] |
| 86 |
fn long_word_softwrap() { |
| 87 |
assert_eq!( |
| 88 |
softwrap_text("\t\txxxx1xxxx2xxxx3xxxx4xxxx5xxxx6xxxx7xxxx8xxxx9xxx\n"), |
| 89 |
" xxxx1xxxx2xxx\n.....x3xxxx4xxxx5\n.....xxxx6xxxx7xx\n.....xx8xxxx9xxx \n " |
| 90 |
); |
| 91 |
assert_eq!( |
| 92 |
softwrap_text("xxxxxxxx1xxxx2xxx\n"), |
| 93 |
"xxxxxxxx1xxxx2xxx\n. \n " |
| 94 |
); |
| 95 |
assert_eq!( |
| 96 |
softwrap_text("\t\txxxx1xxxx 2xxxx3xxxx4xxxx5xxxx6xxxx7xxxx8xxxx9xxx\n"), |
| 97 |
" xxxx1xxxx \n.....2xxxx3xxxx4x\n.....xxx5xxxx6xxx\n.....x7xxxx8xxxx9\n.....xxx \n " |
| 98 |
); |
| 99 |
assert_eq!( |
| 100 |
softwrap_text("\t\txxxx1xxx 2xxxx3xxxx4xxxx5xxxx6xxxx7xxxx8xxxx9xxx\n"), |
| 101 |
" xxxx1xxx 2xxx\n.....x3xxxx4xxxx5\n.....xxxx6xxxx7xx\n.....xx8xxxx9xxx \n " |
| 102 |
); |
| 103 |
} |
| 104 |
|
| 105 |
#[test] |
| 106 |
fn softwrap_multichar_grapheme() { |
| 107 |
assert_eq!( |
| 108 |
softwrap_text("xxxx xxxx xxx a\u{0301}bc\n"), |
| 109 |
"xxxx xxxx xxx \n.ábc \n " |
| 110 |
) |
| 111 |
} |
| 112 |
|
| 113 |
fn softwrap_text_at_text_width(text: &str) -> String { |
| 114 |
let mut text_fmt = TextFormat::new_test(true); |
| 115 |
text_fmt.soft_wrap_at_text_width = true; |
| 116 |
let annotations = TextAnnotations::default(); |
| 117 |
let mut formatter = |
| 118 |
DocumentFormatter::new_at_prev_checkpoint(text.into(), &text_fmt, &annotations, 0); |
| 119 |
formatter.collect_to_str() |
| 120 |
} |
| 121 |
#[test] |
| 122 |
fn long_word_softwrap_text_width() { |
| 123 |
assert_eq!( |
| 124 |
softwrap_text_at_text_width("xxxxxxxx1xxxx2xxx\nxxxxxxxx1xxxx2xxx"), |
| 125 |
"xxxxxxxx1xxxx2xxx \nxxxxxxxx1xxxx2xxx " |
| 126 |
); |
| 127 |
} |
| 128 |
|
| 129 |
fn overlay_text(text: &str, char_pos: usize, softwrap: bool, overlays: &[Overlay]) -> String { |
| 130 |
DocumentFormatter::new_at_prev_checkpoint( |
| 131 |
text.into(), |
| 132 |
&TextFormat::new_test(softwrap), |
| 133 |
TextAnnotations::default().add_overlay(overlays, None), |
| 134 |
char_pos, |
| 135 |
) |
| 136 |
.collect_to_str() |
| 137 |
} |
| 138 |
|
| 139 |
#[test] |
| 140 |
fn overlay() { |
| 141 |
assert_eq!( |
| 142 |
overlay_text( |
| 143 |
"foobar", |
| 144 |
0, |
| 145 |
false, |
| 146 |
&[Overlay::new(0, "X"), Overlay::new(2, "\t")], |
| 147 |
), |
| 148 |
"Xo bar " |
| 149 |
); |
| 150 |
assert_eq!( |
| 151 |
overlay_text( |
| 152 |
&"foo ".repeat(10), |
| 153 |
0, |
| 154 |
true, |
| 155 |
&[ |
| 156 |
Overlay::new(2, "\t"), |
| 157 |
Overlay::new(5, "\t"), |
| 158 |
Overlay::new(16, "X"), |
| 159 |
] |
| 160 |
), |
| 161 |
"fo f o foo \n.foo Xoo foo foo \n.foo foo foo " |
| 162 |
); |
| 163 |
} |
| 164 |
|
| 165 |
fn annotate_text(text: &str, softwrap: bool, annotations: &[InlineAnnotation]) -> String { |
| 166 |
DocumentFormatter::new_at_prev_checkpoint( |
| 167 |
text.into(), |
| 168 |
&TextFormat::new_test(softwrap), |
| 169 |
TextAnnotations::default().add_inline_annotations(annotations, None), |
| 170 |
0, |
| 171 |
) |
| 172 |
.collect_to_str() |
| 173 |
} |
| 174 |
|
| 175 |
#[test] |
| 176 |
fn annotation() { |
| 177 |
assert_eq!( |
| 178 |
annotate_text("bar", false, &[InlineAnnotation::new(0, "foo")]), |
| 179 |
"foobar " |
| 180 |
); |
| 181 |
assert_eq!( |
| 182 |
annotate_text( |
| 183 |
&"foo ".repeat(10), |
| 184 |
true, |
| 185 |
&[InlineAnnotation::new(0, "foo ")] |
| 186 |
), |
| 187 |
"foo foo foo foo \n.foo foo foo foo \n.foo foo foo " |
| 188 |
); |
| 189 |
} |
| 190 |
|
| 191 |
#[test] |
| 192 |
fn annotation_and_overlay() { |
| 193 |
let annotations = [InlineAnnotation { |
| 194 |
char_idx: 0, |
| 195 |
text: "fooo".into(), |
| 196 |
}]; |
| 197 |
let overlay = [Overlay { |
| 198 |
char_idx: 0, |
| 199 |
grapheme: "\t".into(), |
| 200 |
}]; |
| 201 |
assert_eq!( |
| 202 |
DocumentFormatter::new_at_prev_checkpoint( |
| 203 |
"bbar".into(), |
| 204 |
&TextFormat::new_test(false), |
| 205 |
TextAnnotations::default() |
| 206 |
.add_inline_annotations(annotations.as_slice(), None) |
| 207 |
.add_overlay(overlay.as_slice(), None), |
| 208 |
0, |
| 209 |
) |
| 210 |
.collect_to_str(), |
| 211 |
"fooo bar " |
| 212 |
); |
| 213 |
} |