Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

trim() mishandles indentation-only lines shorter than min_indent #163

Closed
alandipert opened this issue Jan 9, 2020 · 0 comments · Fixed by #164
Closed

trim() mishandles indentation-only lines shorter than min_indent #163

alandipert opened this issue Jan 9, 2020 · 0 comments · Fixed by #164

Comments

@alandipert
Copy link
Contributor

alandipert commented Jan 9, 2020

trim() calculates the "minimum indent" of every line after the first, and then removes that number of characters from the beginning of every line after the first. I encountered this while working on the related #162.

min_indent is calculated by a routine that ignores indentation (spaces and tabs) on blank lines - lines containing only indentation. Consequently, the newline that terminates these lines can get trimmed, which causes the following line to include spurious leading whitespace.

Failing example:

library(glue)
library(testthat)
expect_identical(
  trim("
       \ta
       \tb
      \t
       \tc"),
  "a\nb\n\nc"
)

In the character vector passed to trim(), the penultimate line contains only indentation (6 spaces followed by a tab).

When trim() is copying parts of the old string to a new one, it skips over the newline that delimits the penultimate and last lines, and the whitespace from the penultimate line is included in the last line.

jimhester added a commit that referenced this issue Mar 3, 2020
* Improve handling of lines containing only indentation

* Update NEWS

* Handle short indentation-only lines; fixes #163

* Add test for empty intermediate line handling

Co-authored-by: Jim Hester <james.f.hester@gmail.com>
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant