Skip to content

Commit 0d16e99

Browse files
committed
wrapi_text: fix handling of successive parts that exceed width
wrapi_text() splits a string into pieces and then builds it back up, inserting a newline before the next piece if it would lead to the string exceeding the specified width. The result is stored in the `start` variable, which gets extended each time the width is exceeded. To extend `start`, wrapi_text() appends the value of {pieces that did not exceed width}{sep}{newline}{piece that exceeded width} wrapi_text() incorrectly assumes that there is at least one piece that did _not_ exceed the width since the last time `start` was extended, leading to a malformed result with the last piece repeated in an incorrect position. Update the logic to account for the fact that successive pieces can exceed the specified width.
1 parent b4ecbd1 commit 0d16e99

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

R/util.R

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -191,8 +191,14 @@ wrapi_text <- function(
191191
if(max_line_char(s) <= width){
192192
next
193193
}else{
194-
pieces_start <- pieces[shift:(i - 1)]
195-
add <- paste(paste0(pieces_start, collapse = wrap_chr), pieces[i], sep = newline_sep)
194+
if (shift == i) {
195+
# `start` includes everything aside from the current piece.
196+
add <- paste0("\n", pieces[i])
197+
} else {
198+
pieces_start <- pieces[shift:(i - 1)]
199+
add <- paste(paste0(pieces_start, collapse = wrap_chr), pieces[i], sep = newline_sep)
200+
}
201+
196202
if (i == npieces) {
197203
s <- cat_start(start = start, add = add, sep = wrap_chr)
198204
}else{

tests/testthat/test-util.R

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,4 +61,9 @@ test_that("wrap_text(): past offenders", {
6161
),
6262
"test\n-foo\n.R\ntest\n-foo\n_bar\n.R"
6363
)
64+
65+
expect_identical(
66+
wrap_text("foo/bar/baz", width = 4),
67+
"foo/\nbar/\nbaz"
68+
)
6469
})

0 commit comments

Comments
 (0)