| 1 |
|
| 2 |
use crate::{Range, Selection}; |
| 3 |
use ropey::Rope; |
| 4 |
use smallvec::SmallVec; |
| 5 |
use std::cmp::Reverse; |
| 6 |
use unicode_segmentation::UnicodeSegmentation; |
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
pub fn print(s: &str) -> (String, Selection) { |
| 37 |
let mut primary_idx = None; |
| 38 |
let mut ranges = SmallVec::new(); |
| 39 |
let mut iter = UnicodeSegmentation::graphemes(s, true).peekable(); |
| 40 |
let mut left = String::with_capacity(s.len()); |
| 41 |
|
| 42 |
'outer: while let Some(c) = iter.next() { |
| 43 |
let start = left.chars().count(); |
| 44 |
|
| 45 |
if c != "#" { |
| 46 |
left.push_str(c); |
| 47 |
continue; |
| 48 |
} |
| 49 |
|
| 50 |
let (is_primary, close_pair) = match iter.next() { |
| 51 |
Some("[") => (true, "]"), |
| 52 |
Some("(") => (false, ")"), |
| 53 |
Some(ch) => { |
| 54 |
left.push('#'); |
| 55 |
left.push_str(ch); |
| 56 |
continue; |
| 57 |
} |
| 58 |
None => break, |
| 59 |
}; |
| 60 |
|
| 61 |
if is_primary && primary_idx.is_some() { |
| 62 |
panic!("primary `#[` already appeared {:?} {:?}", left, s); |
| 63 |
} |
| 64 |
|
| 65 |
let head_at_beg = iter.next_if_eq(&"|").is_some(); |
| 66 |
let last_grapheme = |s: &str| { |
| 67 |
UnicodeSegmentation::graphemes(s, true) |
| 68 |
.next_back() |
| 69 |
.map(String::from) |
| 70 |
}; |
| 71 |
|
| 72 |
while let Some(c) = iter.next() { |
| 73 |
let next = iter.peek(); |
| 74 |
let mut prev = last_grapheme(left.as_str()); |
| 75 |
|
| 76 |
if !(c == close_pair && next == Some(&"#")) { |
| 77 |
left.push_str(c); |
| 78 |
continue; |
| 79 |
} |
| 80 |
|
| 81 |
if !head_at_beg { |
| 82 |
match &prev { |
| 83 |
Some(p) if p != "|" => { |
| 84 |
left.push_str(c); |
| 85 |
continue; |
| 86 |
} |
| 87 |
Some(p) if p == "|" => { |
| 88 |
left.pop().unwrap(); |
| 89 |
prev = last_grapheme(left.as_str()); |
| 90 |
} |
| 91 |
_ => (), |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
iter.next(); |
| 96 |
let next = iter.peek(); |
| 97 |
|
| 98 |
|
| 99 |
if (prev == Some(String::from("\r\n")) || prev == Some(String::from("\n"))) |
| 100 |
&& next.map(|n| String::from(*n)) == prev |
| 101 |
{ |
| 102 |
iter.next(); |
| 103 |
} |
| 104 |
|
| 105 |
if is_primary { |
| 106 |
primary_idx = Some(ranges.len()); |
| 107 |
} |
| 108 |
|
| 109 |
let (anchor, head) = match head_at_beg { |
| 110 |
true => (left.chars().count(), start), |
| 111 |
false => (start, left.chars().count()), |
| 112 |
}; |
| 113 |
|
| 114 |
ranges.push(Range::new(anchor, head)); |
| 115 |
continue 'outer; |
| 116 |
} |
| 117 |
|
| 118 |
if head_at_beg { |
| 119 |
panic!("missing end `{}#` {:?} {:?}", close_pair, left, s); |
| 120 |
} else { |
| 121 |
panic!("missing end `|{}#` {:?} {:?}", close_pair, left, s); |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
let primary = match primary_idx { |
| 126 |
Some(i) => i, |
| 127 |
None => panic!("missing primary `#[|]#` {:?}", s), |
| 128 |
}; |
| 129 |
|
| 130 |
let selection = Selection::new(ranges, primary); |
| 131 |
(left, selection) |
| 132 |
} |
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
pub fn plain<R: Into<Rope>>(s: R, selection: &Selection) -> String { |
| 153 |
let s = s.into(); |
| 154 |
let primary = selection.primary_index(); |
| 155 |
let mut out = String::with_capacity(s.len_bytes() + 5 * selection.len()); |
| 156 |
out.push_str(&s.to_string()); |
| 157 |
|
| 158 |
let mut insertion: Vec<_> = selection |
| 159 |
.iter() |
| 160 |
.enumerate() |
| 161 |
.flat_map(|(i, range)| { |
| 162 |
|
| 163 |
match (range.anchor < range.head, i == primary) { |
| 164 |
(true, true) => [(range.anchor, "#["), (range.head, "|]#")], |
| 165 |
(true, false) => [(range.anchor, "#("), (range.head, "|)#")], |
| 166 |
(false, true) => [(range.anchor, "]#"), (range.head, "#[|")], |
| 167 |
(false, false) => [(range.anchor, ")#"), (range.head, "#(|")], |
| 168 |
} |
| 169 |
}) |
| 170 |
.map(|(char_idx, marker)| (s.char_to_byte(char_idx), marker)) |
| 171 |
.collect(); |
| 172 |
|
| 173 |
|
| 174 |
insertion.sort_unstable_by_key(|k| Reverse(k.0)); |
| 175 |
for (i, s) in insertion { |
| 176 |
out.insert_str(i, s); |
| 177 |
} |
| 178 |
out |
| 179 |
} |
| 180 |
|
| 181 |
#[cfg(test)] |
| 182 |
#[allow(clippy::module_inception)] |
| 183 |
mod test { |
| 184 |
use super::*; |
| 185 |
|
| 186 |
#[test] |
| 187 |
fn print_single() { |
| 188 |
assert_eq!( |
| 189 |
(String::from("hello"), Selection::single(1, 0)), |
| 190 |
print("#[|h]#ello") |
| 191 |
); |
| 192 |
assert_eq!( |
| 193 |
(String::from("hello"), Selection::single(0, 1)), |
| 194 |
print("#[h|]#ello") |
| 195 |
); |
| 196 |
assert_eq!( |
| 197 |
(String::from("hello"), Selection::single(4, 0)), |
| 198 |
print("#[|hell]#o") |
| 199 |
); |
| 200 |
assert_eq!( |
| 201 |
(String::from("hello"), Selection::single(0, 4)), |
| 202 |
print("#[hell|]#o") |
| 203 |
); |
| 204 |
assert_eq!( |
| 205 |
(String::from("hello"), Selection::single(5, 0)), |
| 206 |
print("#[|hello]#") |
| 207 |
); |
| 208 |
assert_eq!( |
| 209 |
(String::from("hello"), Selection::single(0, 5)), |
| 210 |
print("#[hello|]#") |
| 211 |
); |
| 212 |
} |
| 213 |
|
| 214 |
#[test] |
| 215 |
fn print_multi() { |
| 216 |
assert_eq!( |
| 217 |
( |
| 218 |
String::from("hello"), |
| 219 |
Selection::new( |
| 220 |
SmallVec::from_slice(&[Range::new(1, 0), Range::new(5, 4)]), |
| 221 |
0 |
| 222 |
) |
| 223 |
), |
| 224 |
print("#[|h]#ell#(|o)#") |
| 225 |
); |
| 226 |
assert_eq!( |
| 227 |
( |
| 228 |
String::from("hello"), |
| 229 |
Selection::new( |
| 230 |
SmallVec::from_slice(&[Range::new(0, 1), Range::new(4, 5)]), |
| 231 |
0 |
| 232 |
) |
| 233 |
), |
| 234 |
print("#[h|]#ell#(o|)#") |
| 235 |
); |
| 236 |
assert_eq!( |
| 237 |
( |
| 238 |
String::from("hello"), |
| 239 |
Selection::new( |
| 240 |
SmallVec::from_slice(&[Range::new(2, 0), Range::new(5, 3)]), |
| 241 |
0 |
| 242 |
) |
| 243 |
), |
| 244 |
print("#[|he]#l#(|lo)#") |
| 245 |
); |
| 246 |
assert_eq!( |
| 247 |
( |
| 248 |
String::from("hello\r\nhello\r\nhello\r\n"), |
| 249 |
Selection::new( |
| 250 |
SmallVec::from_slice(&[ |
| 251 |
Range::new(7, 5), |
| 252 |
Range::new(21, 19), |
| 253 |
Range::new(14, 12) |
| 254 |
]), |
| 255 |
0 |
| 256 |
) |
| 257 |
), |
| 258 |
print("hello#[|\r\n]#hello#(|\r\n)#hello#(|\r\n)#") |
| 259 |
); |
| 260 |
} |
| 261 |
|
| 262 |
#[test] |
| 263 |
fn print_multi_byte_code_point() { |
| 264 |
assert_eq!( |
| 265 |
(String::from("„“"), Selection::single(1, 0)), |
| 266 |
print("#[|„]#“") |
| 267 |
); |
| 268 |
assert_eq!( |
| 269 |
(String::from("„“"), Selection::single(2, 1)), |
| 270 |
print("„#[|“]#") |
| 271 |
); |
| 272 |
assert_eq!( |
| 273 |
(String::from("„“"), Selection::single(0, 1)), |
| 274 |
print("#[„|]#“") |
| 275 |
); |
| 276 |
assert_eq!( |
| 277 |
(String::from("„“"), Selection::single(1, 2)), |
| 278 |
print("„#[“|]#") |
| 279 |
); |
| 280 |
assert_eq!( |
| 281 |
(String::from("they said „hello“"), Selection::single(11, 10)), |
| 282 |
print("they said #[|„]#hello“") |
| 283 |
); |
| 284 |
} |
| 285 |
|
| 286 |
#[test] |
| 287 |
fn print_multi_code_point_grapheme() { |
| 288 |
assert_eq!( |
| 289 |
(String::from("hello 👨👩👧👦 goodbye"), Selection::single(13, 6)), |
| 290 |
print("hello #[|👨👩👧👦]# goodbye") |
| 291 |
); |
| 292 |
} |
| 293 |
|
| 294 |
#[test] |
| 295 |
fn plain_single() { |
| 296 |
assert_eq!("#[|h]#ello", plain("hello", &Selection::single(1, 0))); |
| 297 |
assert_eq!("#[h|]#ello", plain("hello", &Selection::single(0, 1))); |
| 298 |
assert_eq!("#[|hell]#o", plain("hello", &Selection::single(4, 0))); |
| 299 |
assert_eq!("#[hell|]#o", plain("hello", &Selection::single(0, 4))); |
| 300 |
assert_eq!("#[|hello]#", plain("hello", &Selection::single(5, 0))); |
| 301 |
assert_eq!("#[hello|]#", plain("hello", &Selection::single(0, 5))); |
| 302 |
} |
| 303 |
|
| 304 |
#[test] |
| 305 |
fn plain_multi() { |
| 306 |
assert_eq!( |
| 307 |
plain( |
| 308 |
"hello", |
| 309 |
&Selection::new( |
| 310 |
SmallVec::from_slice(&[Range::new(1, 0), Range::new(5, 4)]), |
| 311 |
0 |
| 312 |
) |
| 313 |
), |
| 314 |
String::from("#[|h]#ell#(|o)#") |
| 315 |
); |
| 316 |
assert_eq!( |
| 317 |
plain( |
| 318 |
"hello", |
| 319 |
&Selection::new( |
| 320 |
SmallVec::from_slice(&[Range::new(0, 1), Range::new(4, 5)]), |
| 321 |
0 |
| 322 |
) |
| 323 |
), |
| 324 |
String::from("#[h|]#ell#(o|)#") |
| 325 |
); |
| 326 |
assert_eq!( |
| 327 |
plain( |
| 328 |
"hello", |
| 329 |
&Selection::new( |
| 330 |
SmallVec::from_slice(&[Range::new(2, 0), Range::new(5, 3)]), |
| 331 |
0 |
| 332 |
) |
| 333 |
), |
| 334 |
String::from("#[|he]#l#(|lo)#") |
| 335 |
); |
| 336 |
assert_eq!( |
| 337 |
plain( |
| 338 |
"hello\r\nhello\r\nhello\r\n", |
| 339 |
&Selection::new( |
| 340 |
SmallVec::from_slice(&[ |
| 341 |
Range::new(7, 5), |
| 342 |
Range::new(21, 19), |
| 343 |
Range::new(14, 12) |
| 344 |
]), |
| 345 |
0 |
| 346 |
) |
| 347 |
), |
| 348 |
String::from("hello#[|\r\n]#hello#(|\r\n)#hello#(|\r\n)#") |
| 349 |
); |
| 350 |
} |
| 351 |
|
| 352 |
#[test] |
| 353 |
fn plain_multi_byte_code_point() { |
| 354 |
assert_eq!( |
| 355 |
plain("„“", &Selection::single(1, 0)), |
| 356 |
String::from("#[|„]#“") |
| 357 |
); |
| 358 |
assert_eq!( |
| 359 |
plain("„“", &Selection::single(2, 1)), |
| 360 |
String::from("„#[|“]#") |
| 361 |
); |
| 362 |
assert_eq!( |
| 363 |
plain("„“", &Selection::single(0, 1)), |
| 364 |
String::from("#[„|]#“") |
| 365 |
); |
| 366 |
assert_eq!( |
| 367 |
plain("„“", &Selection::single(1, 2)), |
| 368 |
String::from("„#[“|]#") |
| 369 |
); |
| 370 |
assert_eq!( |
| 371 |
plain("they said „hello“", &Selection::single(11, 10)), |
| 372 |
String::from("they said #[|„]#hello“") |
| 373 |
); |
| 374 |
} |
| 375 |
|
| 376 |
#[test] |
| 377 |
fn plain_multi_code_point_grapheme() { |
| 378 |
assert_eq!( |
| 379 |
plain("hello 👨👩👧👦 goodbye", &Selection::single(13, 6)), |
| 380 |
String::from("hello #[|👨👩👧👦]# goodbye") |
| 381 |
); |
| 382 |
} |
| 383 |
} |