Skip to content

Commit

Permalink
Reset the indent counter at the end of a line (#243)
Browse files Browse the repository at this point in the history
* Add a failing test

* Reset indent

Fixes #238

* Tweak whitespace in test

* Fix comment

* Use the hex escape for a space, so editors don't trim this trailing space away
  • Loading branch information
jennybc authored Nov 29, 2021
1 parent a8dbfe6 commit 7172e09
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/trim.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ SEXP trim_(SEXP x) {
while (i < str_len) {
if (xx[i] == '\n') {
new_line = true;
indent = 0;
} else if (new_line) {
if (xx[i] == ' ' || xx[i] == '\t') {
++indent;
Expand Down
15 changes: 14 additions & 1 deletion tests/testthat/test-trim.R
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ test_that("trim works", {
"test
"))
expect_identical("test",
trim("
trim("\x20\x20\x20\x20\x20\x20
test
"))
expect_identical("test",
Expand Down Expand Up @@ -154,3 +154,16 @@ test_that("lines containing only indentation are handled properly", {
"a\nb\n\nc"
)
})

# https://github.com/tidyverse/glue/issues/238
test_that("indent counter resets at newline", {
# whitespace-only line has 1 space < min_indent (which is 2)
# comment in trim_() says:
# "if the line consists only of tabs and spaces, and if the line is
# shorter than min_indent, copy the entire line"
expect_identical(trim("\n \n abcd"), " \nabcd")

# whitespace-only line has n spaces, n >= min_indent
expect_identical( trim("\n \n abcd"), "\nabcd")
expect_identical(trim("\n \n abcd"), " \nabcd")
})

0 comments on commit 7172e09

Please sign in to comment.