Skip to content

Commit

Permalink
Merge pull request #69 from cynkra/b-68-handle-arg-names-correctly-in…
Browse files Browse the repository at this point in the history
…-deparse_call

handle arg names correctly in deparse call
  • Loading branch information
moodymudskipper committed Oct 15, 2022
2 parents 9a95c80 + f923547 commit d8ab33b
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 7 deletions.
21 changes: 14 additions & 7 deletions R/deparse_call.R
Original file line number Diff line number Diff line change
Expand Up @@ -131,13 +131,13 @@ deparse_call_impl <- function(call, one_liner = FALSE, indent = 0, pipe = FALSE,

if (caller == "[" && length(call) > 1) {
arg1 <- deparse_call_impl(call[[2]])
other_args <- paste(vapply(call[-(1:2)], deparse_call_impl, character(1), one_liner = one_liner, indent = indent), collapse = ", ")
other_args <- deparse_named_args_to_string(call[-(1:2)], one_liner = one_liner, indent = indent)
return(sprintf("%s[%s]", arg1, other_args))
}

if (caller == "[[" && length(call) > 1) {
arg1 <- deparse_call_impl(call[[2]])
other_args <- paste(vapply(call[-(1:2)], deparse_call_impl, character(1), one_liner = one_liner, indent = indent), collapse = ", ")
other_args <- deparse_named_args_to_string(call[-(1:2)], one_liner = one_liner, indent = indent)
return(sprintf("%s[[%s]]", arg1, other_args))
}

Expand Down Expand Up @@ -175,10 +175,8 @@ deparse_call_impl <- function(call, one_liner = FALSE, indent = 0, pipe = FALSE,
return(sprintf("%s |> %s(%s)", arg1, caller, paste(other_args, collapse = ", ")))
}
}
args <- vapply(call[-1], deparse_call_impl, character(1), one_liner = one_liner, indent = indent)
args <- paste(rlang::names2(args), "=", args)
args <- sub("^ = ", "", args)
sprintf("%s(%s)", caller, paste(args, collapse = ", "))
args <- deparse_named_args_to_string(call[-1], one_liner = one_liner, indent = indent)
sprintf("%s(%s)", caller, args)
}

is_syntactic <- function(x) {
Expand All @@ -197,4 +195,13 @@ is_infix_narrow <- function(x) {
x %in% c("::", ":::", "$", "@", "^", ":")
}


# FIXME: better handling of indent, doesn't impact if we style
deparse_named_args_to_string <- function(args, one_liner, indent) {
args <- vapply(args, deparse_call_impl, character(1), one_liner = one_liner, indent = indent)
args <- paste(rlang::names2(args), "=", args)
args <- sub("^ = ", "", args)
# FIXME: the 80 is a bit arbitrary, since we don't account for indent and length of caller
if (one_liner || max(nchar(args)) < 80) return(paste(args, collapse = ", "))
args <- paste(args, collapse = ",\n")
paste0("\n", args, "\n")
}
39 changes: 39 additions & 0 deletions tests/testthat/_snaps/deparse_call.md
Original file line number Diff line number Diff line change
Expand Up @@ -206,4 +206,43 @@
deparse_call(quote(`*a*`))
Output
`*a*`
Code
deparse_call(quote(a(b = 1, c)))
Output
a(b = 1, c)
Code
deparse_call(quote(a[b = 1, c]))
Output
a[b = 1, c]
Code
deparse_call(quote(a[[b = 1, c]]))
Output
a[[b = 1, c]]
Code
deparse_call(quote(a(
bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb = 1,
c)))
Output
a(
bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb = 1,
c
)
Code
deparse_call(quote(a[
bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb = 1,
c]))
Output
a[
bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb = 1,
c
]
Code
deparse_call(quote(a[[
bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb = 1,
c]]))
Output
a[[
bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb = 1,
c
]]

9 changes: 9 additions & 0 deletions tests/testthat/test-deparse_call.R
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,14 @@ test_that("deparse_call()", {

# non syntatic symbols
deparse_call(quote(`*a*`))

# brackets and function calls with names
deparse_call(quote(a(b=1, c)))
deparse_call(quote(a[b=1, c]))
deparse_call(quote(a[[b=1, c]]))
deparse_call(quote(a(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb=1, c)))
deparse_call(quote(a[bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb=1, c]))
# looks odd, but that's on {styler} : https://github.com/r-lib/styler/issues/1029
deparse_call(quote(a[[bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb=1, c]]))
})
})

0 comments on commit d8ab33b

Please sign in to comment.