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

Make styling of multi-line strings more consistent #459

Merged
merged 1 commit into from Jan 18, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 14 additions & 6 deletions R/rules-spacing.R
Expand Up @@ -96,16 +96,24 @@ remove_space_after_unary_pm_nested <- function(pd) {
pd
}


#' Replace single quotes with double quotes
#'
#' We do not use `deparse()` as in previous implementations but `paste0()` since
#' the former approach escapes the reverse backslash in the line break character
#' "\n" whereas the solution with `paste0()` does not.
#' @examples
#' style_text("'here
#' is a string
#' '")
#' @importFrom purrr map map_chr
#' @param pd_flat A flat parse table.
#' @keywords internal
fix_quotes <- function(pd_flat) {
str_const <- pd_flat$token == "STR_CONST"
str_const_change <- grepl("^'([^\"]*)'$", pd_flat$text[str_const])
pd_flat$text[str_const][str_const_change] <-
vapply(
lapply(pd_flat$text[str_const][str_const_change], parse_text),
deparse,
character(1L)
)
map(pd_flat$text[str_const][str_const_change], parse_text) %>%
map_chr(~paste0("\"", .x, "\""))
pd_flat
}

Expand Down
22 changes: 22 additions & 0 deletions man/fix_quotes.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions tests/testthat/strict/non_strict-in.R
Expand Up @@ -4,6 +4,17 @@ test <- function() {
'even if the string contains an escaped \' single quote'
'but not if it contains a "double quote'

"multi-line quotes
remain multi-line
"

'That also holds true
if
single quotes are used
.'

'strings with embeded\nline breaks are unfortunately split'

# Comments are always preserved

function_calls(get_spaces=around_equal)
Expand Down