Skip to content

Commit 1502872

Browse files
authored
Compute ArgKind::Other width from text.chars().count() not text.len() (#450)
* Compute `ArgKind::Other` width from `text.chars().count()` not `text.len()` * Keep trailing empty line
1 parent 35325d1 commit 1502872

File tree

4 files changed

+49
-1
lines changed

4 files changed

+49
-1
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
# Development version
44

5+
- Fixed an issue with Unicode elements and table alignment (#449).
6+
7+
58
# 0.8.0
69

710
- Added support for table formatting of `tribble()` and `fcase()` calls (#113).

crates/air_r_formatter/src/r/auxiliary/call_arguments_table.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -491,7 +491,13 @@ impl ArgKind {
491491
Some(frac_len) => *integer_width + DOT_WIDTH + *frac_len,
492492
None => *integer_width,
493493
},
494-
ArgKind::Other { text } => text.len(),
494+
ArgKind::Other { text } => {
495+
// Unlike with integers and decimals, can't use `text.len()` here in case
496+
// Unicode characters are involved. `len()` counts bytes, but we want to
497+
// align based on the number of characters that visibly show up in a
498+
// user's editor (#449).
499+
text.chars().count()
500+
}
495501
}
496502
}
497503
}

crates/air_r_formatter/tests/specs/r/call_table.R

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -407,3 +407,16 @@ list(
407407
# comment
408408
foo = 1, 2, 3
409409
)
410+
411+
# ------------------------------------------------------------------------
412+
# Unicode
413+
414+
# Width of strings count the number of `chars()` (#449)
415+
# fmt: table
416+
tribble(
417+
~col1, ~col2,
418+
"A (Mio. €)", 10,
419+
"B (T €)", 20,
420+
"C §", 10,
421+
"ascii", 10,
422+
)

crates/air_r_formatter/tests/specs/r/call_table.R.snap

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -415,6 +415,19 @@ list(
415415
foo = 1, 2, 3
416416
)
417417
418+
# ------------------------------------------------------------------------
419+
# Unicode
420+
421+
# Width of strings count the number of `chars()` (#449)
422+
# fmt: table
423+
tribble(
424+
~col1, ~col2,
425+
"A (Mio. €)", 10,
426+
"B (T €)", 20,
427+
"C §", 10,
428+
"ascii", 10,
429+
)
430+
418431
```
419432

420433

@@ -848,6 +861,19 @@ list(
848861
2,
849862
3
850863
)
864+
865+
# ------------------------------------------------------------------------
866+
# Unicode
867+
868+
# Width of strings count the number of `chars()` (#449)
869+
# fmt: table
870+
tribble(
871+
~col1 , ~col2 ,
872+
"A (Mio. €)" , 10 ,
873+
"B (T €)" , 20 ,
874+
"C §" , 10 ,
875+
"ascii" , 10 ,
876+
)
851877
```
852878

853879

0 commit comments

Comments
 (0)