1#[derive(Debug, Clone, PartialEq, Eq)]
14pub struct ListChars {
15 pub tab_lead: char,
17 pub tab_fill: Option<char>,
19 pub space: Option<char>,
21 pub trail: Option<char>,
23 pub eol: Option<char>,
25 pub nbsp: Option<char>,
27 pub extends: Option<char>,
31 pub precedes: Option<char>,
35}
36
37impl Default for ListChars {
38 fn default() -> Self {
39 Self {
41 tab_lead: '^',
42 tab_fill: Some('I'),
43 space: None,
44 trail: None,
45 eol: Some('$'),
46 nbsp: None,
47 extends: None,
48 precedes: None,
49 }
50 }
51}
52
53impl ListChars {
54 pub fn parse(s: &str) -> Result<Self, String> {
62 let mut lc = Self {
67 tab_lead: '^',
68 tab_fill: Some('I'),
69 space: None,
70 trail: None,
71 eol: None,
72 nbsp: None,
73 extends: None,
74 precedes: None,
75 };
76 for raw_part in s.split(',') {
77 let part = raw_part.trim_start();
81 if part.is_empty() {
82 continue;
83 }
84 let (key, val) = part
85 .split_once(':')
86 .ok_or_else(|| format!("listchars: missing `:` in `{part}`"))?;
87 let chars: Vec<char> = val.chars().collect();
88 match key {
89 "tab" => match chars.len() {
90 1 => {
91 lc.tab_lead = chars[0];
92 lc.tab_fill = None;
93 }
94 2 => {
95 lc.tab_lead = chars[0];
96 lc.tab_fill = Some(chars[1]);
97 }
98 n => {
99 return Err(format!(
100 "listchars: `tab` value must be 1 or 2 chars, got {n}"
101 ));
102 }
103 },
104 "space" => lc.space = Some(one_char(key, &chars)?),
105 "trail" => lc.trail = Some(one_char(key, &chars)?),
106 "eol" => lc.eol = Some(one_char(key, &chars)?),
107 "nbsp" => lc.nbsp = Some(one_char(key, &chars)?),
108 "extends" => lc.extends = Some(one_char(key, &chars)?),
109 "precedes" => lc.precedes = Some(one_char(key, &chars)?),
110 other => {
111 return Err(format!("listchars: unknown key `{other}`"));
112 }
113 }
114 }
115 Ok(lc)
116 }
117
118 pub fn to_canonical_string(&self) -> String {
123 let mut parts: Vec<String> = Vec::new();
124 if let Some(fill) = self.tab_fill {
126 parts.push(format!("tab:{}{}", self.tab_lead, fill));
127 } else {
128 parts.push(format!("tab:{}", self.tab_lead));
129 }
130 if let Some(ch) = self.space {
131 parts.push(format!("space:{ch}"));
132 }
133 if let Some(ch) = self.trail {
134 parts.push(format!("trail:{ch}"));
135 }
136 if let Some(ch) = self.eol {
137 parts.push(format!("eol:{ch}"));
138 }
139 if let Some(ch) = self.nbsp {
140 parts.push(format!("nbsp:{ch}"));
141 }
142 if let Some(ch) = self.extends {
143 parts.push(format!("extends:{ch}"));
144 }
145 if let Some(ch) = self.precedes {
146 parts.push(format!("precedes:{ch}"));
147 }
148 parts.join(",")
149 }
150}
151
152fn one_char(key: &str, chars: &[char]) -> Result<char, String> {
154 match chars.len() {
155 1 => Ok(chars[0]),
156 n => Err(format!(
157 "listchars: `{key}` value must be exactly 1 char, got {n}"
158 )),
159 }
160}
161
162pub fn apply_listchars<'a>(
175 line: &'a str,
176 lc: &ListChars,
177 list: bool,
178 tabstop: usize,
179) -> std::borrow::Cow<'a, str> {
180 if !list {
181 return std::borrow::Cow::Borrowed(line);
182 }
183
184 let trimmed_end = line.trim_end_matches([' ', '\t']).len();
188
189 let mut out = String::with_capacity(line.len() + 8);
190 let mut col: usize = 0; for (byte_idx, ch) in line.char_indices() {
193 let is_trailing = byte_idx >= trimmed_end;
194 match ch {
195 '\t' => {
196 let spaces = tabstop - (col % tabstop);
197 out.push(lc.tab_lead);
199 col += 1;
200 let fill_count = spaces.saturating_sub(1);
202 if let Some(fill) = lc.tab_fill {
203 for _ in 0..fill_count {
204 out.push(fill);
205 col += 1;
206 }
207 } else {
208 for _ in 0..fill_count {
210 out.push(' ');
211 col += 1;
212 }
213 }
214 }
215 ' ' => {
216 let sub = if is_trailing {
217 lc.trail.or(lc.space).unwrap_or(' ')
218 } else {
219 lc.space.unwrap_or(' ')
220 };
221 out.push(sub);
222 col += 1;
223 }
224 '\u{00a0}' => {
225 out.push(lc.nbsp.unwrap_or('\u{00a0}'));
226 col += 1;
227 }
228 other => {
229 out.push(other);
230 col += unicode_width(other);
231 }
232 }
233 }
234
235 if let Some(eol) = lc.eol {
237 out.push(eol);
238 }
239
240 std::borrow::Cow::Owned(out)
241}
242
243#[inline]
245fn unicode_width(ch: char) -> usize {
246 if is_wide(ch) { 2 } else { 1 }
250}
251
252#[inline]
254fn is_wide(ch: char) -> bool {
255 matches!(ch,
256 '\u{1100}'..='\u{115F}' | '\u{2E80}'..='\u{303E}' | '\u{3041}'..='\u{33BF}' | '\u{33FF}'..='\u{A4CF}' | '\u{A960}'..='\u{A97F}' | '\u{AC00}'..='\u{D7FF}' | '\u{F900}'..='\u{FAFF}' | '\u{FE10}'..='\u{FE1F}' | '\u{FE30}'..='\u{FE6F}' | '\u{FF00}'..='\u{FF60}' | '\u{FFE0}'..='\u{FFE6}' | '\u{1B000}'..='\u{1B0FF}' | '\u{1F004}' | '\u{1F0CF}' | '\u{1F200}'..='\u{1F2FF}' | '\u{20000}'..='\u{2A6DF}' | '\u{2A700}'..='\u{2CEAF}' | '\u{2CEB0}'..='\u{2EBEF}' | '\u{30000}'..='\u{3134F}' )
276}
277
278#[cfg(test)]
279mod tests {
280 use super::*;
281 use std::borrow::Cow;
282
283 #[test]
286 fn listchars_parse_basic() {
287 let lc = ListChars::parse("tab:>-,eol:$").unwrap();
288 assert_eq!(lc.tab_lead, '>');
289 assert_eq!(lc.tab_fill, Some('-'));
290 assert_eq!(lc.eol, Some('$'));
291 assert_eq!(lc.space, None);
292 assert_eq!(lc.trail, None);
293 }
294
295 #[test]
296 fn listchars_parse_all_keys() {
297 let lc =
298 ListChars::parse("tab:>-,space:·,trail:~,eol:¶,nbsp:_,extends:>,precedes:<").unwrap();
299 assert_eq!(lc.tab_lead, '>');
300 assert_eq!(lc.tab_fill, Some('-'));
301 assert_eq!(lc.space, Some('·'));
302 assert_eq!(lc.trail, Some('~'));
303 assert_eq!(lc.eol, Some('¶'));
304 assert_eq!(lc.nbsp, Some('_'));
305 assert_eq!(lc.extends, Some('>'));
306 assert_eq!(lc.precedes, Some('<'));
307 }
308
309 #[test]
310 fn listchars_parse_utf8() {
311 let lc = ListChars::parse("tab:→ ,eol:¬").unwrap();
312 assert_eq!(lc.tab_lead, '→');
313 assert_eq!(lc.tab_fill, Some(' '));
314 assert_eq!(lc.eol, Some('¬'));
315 }
316
317 #[test]
318 fn listchars_parse_invalid_no_colon() {
319 assert!(ListChars::parse("tab").is_err());
320 }
321
322 #[test]
323 fn listchars_parse_invalid_three_char_tab() {
324 assert!(ListChars::parse("tab:abc").is_err());
325 }
326
327 #[test]
328 fn listchars_parse_invalid_unknown_key() {
329 assert!(ListChars::parse("bogus:x").is_err());
330 }
331
332 #[test]
333 fn listchars_parse_invalid_returns_err() {
334 assert!(ListChars::parse("tab").is_err(), "no colon");
336 assert!(ListChars::parse("tab:abc").is_err(), "3-char tab value");
337 assert!(ListChars::parse("bogus:x").is_err(), "unknown key");
338 }
339
340 #[test]
341 fn listchars_to_string_roundtrip() {
342 let s = "tab:>-,space:·,trail:~,eol:¶,nbsp:_,extends:>,precedes:<";
343 let lc1 = ListChars::parse(s).unwrap();
344 let canonical = lc1.to_canonical_string();
345 let lc2 = ListChars::parse(&canonical).unwrap();
346 assert_eq!(lc1, lc2);
347 }
348
349 #[test]
350 fn listchars_default_matches_vim() {
351 let lc = ListChars::default();
352 assert_eq!(lc.tab_lead, '^');
353 assert_eq!(lc.tab_fill, Some('I'));
354 assert_eq!(lc.eol, Some('$'));
355 assert_eq!(lc.space, None);
356 assert_eq!(lc.trail, None);
357 assert_eq!(lc.nbsp, None);
358 }
359
360 #[test]
363 fn apply_listchars_off_returns_borrowed() {
364 let lc = ListChars::default();
365 let result = apply_listchars("hello world", &lc, false, 4);
366 assert!(
367 matches!(result, Cow::Borrowed(_)),
368 "expected Borrowed when list=false"
369 );
370 }
371
372 #[test]
373 fn apply_listchars_tab_expansion() {
374 let lc = ListChars::parse("tab:>-,eol:$").unwrap();
376 let result = apply_listchars("\tfoo", &lc, true, 4);
377 assert_eq!(result.as_ref(), ">---foo$");
379 }
380
381 #[test]
382 fn apply_listchars_trail_substitution() {
383 let lc = ListChars::parse("tab:>-,trail:·").unwrap();
384 let result = apply_listchars("foo ", &lc, true, 4);
386 assert_eq!(result.as_ref(), "foo···");
387 }
388
389 #[test]
390 fn apply_listchars_eol_appended() {
391 let lc = ListChars::parse("tab:>-,eol:¶").unwrap();
392 let result = apply_listchars("foo", &lc, true, 4);
393 assert_eq!(result.as_ref(), "foo¶");
394 }
395
396 #[test]
397 fn apply_listchars_nbsp_substitution() {
398 let lc = ListChars::parse("tab:>-,nbsp:_").unwrap();
399 let result = apply_listchars("a\u{00a0}b", &lc, true, 4);
400 assert_eq!(result.as_ref(), "a_b");
401 }
402
403 #[test]
404 fn apply_listchars_combined() {
405 let lc = ListChars::parse("tab:>-,space:·,trail:~,eol:¶,nbsp:_").unwrap();
406 let input = "\t x\u{00a0} ";
408 let result = apply_listchars(input, &lc, true, 4);
409 assert_eq!(result.as_ref(), ">---·x_~¶");
416 }
417}