Skip to content

Commit

Permalink
Merge pull request #459 from lorenzwalthert/fix-quotes
Browse files Browse the repository at this point in the history
- Make styling of multi-line strings more consistent (#459).
  • Loading branch information
lorenzwalthert committed Jan 18, 2019
2 parents 606c3f9 + 5dfbd54 commit d1f3df9
Show file tree
Hide file tree
Showing 5 changed files with 663 additions and 604 deletions.
20 changes: 14 additions & 6 deletions R/rules-spacing.R
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Loading

0 comments on commit d1f3df9

Please sign in to comment.