Comments (5)
from constructive.
Right, I'd switch to capturing the body then.
We can still parse + style, though?
from constructive.
foo <- function(x) {
# foo
x
}
library(constructive)
# we have
construct(foo)
#> {constructive} couldn't create code that reproduces perfectly the input
#> ℹ Call `construct_issues()` to inspect the last issues
#> function(x) {
#> x
#> }
# we can construct with the srcref
construct(foo, opts_function(srcref = TRUE))
#> {constructive} couldn't create code that reproduces perfectly the input
#> ℹ Call `construct_issues()` to inspect the last issues
#> (function(x) {
#> x
#> }) |>
#> structure(
#> srcref = c(1L, 8L, 4L, 1L, 8L, 1L, 1L, 4L) |>
#> structure(
#> srcfile = list2env(
#> list(
#> fixedNewlines = TRUE,
#> lines = c("foo <- function(x) {", " # foo", " x", "}", ""),
#> filename = ""
#> ),
#> parent = .GlobalEnv
#> ) |>
#> structure(class = c("srcfilecopy", "srcfile")),
#> class = "srcref"
#> )
#> )
# and we can verify that it prints properly
(function(x) {
x
}) |>
structure(
srcref = c(1L, 8L, 4L, 1L, 8L, 1L, 1L, 4L) |>
structure(
srcfile = list2env(
list(
fixedNewlines = TRUE,
lines = c("foo <- function(x) {", " # foo", " x", "}", ""),
filename = ""
),
parent = .GlobalEnv
) |>
structure(class = c("srcfilecopy", "srcfile")),
class = "srcref"
)
)
#> function(x) {
#> # foo
#> x
#> }
# but the compact and proper way would be to reproduce the idiomatic definition with the comments and get:
function(x) {
# foo
x
}
# then I think we just need, though to be safe we should parse it and check that get the same language tree
as.character(attr(foo, "srcref"))
#> [1] "function(x) {" " # foo" " x" "}"
# This is only for the `opts_function("function")` (the default), for `as.function()` for instance we lose the
# comments so we cannot use this shortcut to set the secret
as.function(alist(x=, {
# foo
x
}))
#> function (x)
#> {
#> x
#> }
# For language objects We need `opts_language(srcref=)` because now we cannot faithfully reconstruct the body in isolation
construct(body(foo))
#> quote({
#> x
#> })
attributes(body(foo))
#> $srcref
#> $srcref[[1]]
#> {
#>
#> $srcref[[2]]
#> x
#>
#>
#> $srcfile
#> <text>
#>
#> $wholeSrcref
#> foo <- function(x) {
#> # foo
#> x
#> }
# however for that case we cannot reproduce the srcref without setting attributes, because the srcref
# either doesn't reproduce the object, as above, or contains too much, as below
bar <- identity(quote({
# foo
x
}))
attributes(bar)
#> $srcref
#> $srcref[[1]]
#> {
#>
#> $srcref[[2]]
#> x
#>
#>
#> $srcfile
#> <text>
#>
#> $wholeSrcref
#> bar <- identity(quote({
#> # foo
#> x
#> }
as.character(attr(bar, "wholeSrcref"))
#> [1] "bar <- identity(quote({" " # foo"
#> [3] " x" "}"
from constructive.
@krlmlr does this now works for you ? it's pushed to f-48-... as part of #56
If the srcref matches the function's definition (as measured with identical, ignoring env and srcref) it is used, if not we deparse the body.
x <- function(x) {
# foo
x
}
constructive::construct(x)
#> function(x) {
#> # foo
#> x
#> }
constructive::construct(list(a= x))
#> list(
#> a = function(x) {
#> # foo
#> x
#> }
#> )
Created on 2022-10-14 with reprex v2.0.2
Re:
We can still parse + style, though?
I don't fully understand, but styling (indention and pretty code) is done through styler in any case, and since it uses getParseData it doesn't destroy comments.
from constructive.
Thanks, works beautifully!
from constructive.
Related Issues (20)
- Release constructive 0.2.0 HOT 4
- `.env()` forces crash of RStudio HOT 5
- "readRDS" constructor for environments ? HOT 1
- setup_reprex() HOT 3
- diff() warning when simplifying integer vector wit big differences
- `construct_multi()` should support promises
- Harmonize constructors HOT 1
- Think about package ecosystem
- Improve extension system HOT 2
- roxygen2 import? HOT 2
- Changes in ggplot2 HOT 2
- S4 construction should not use representation()
- Bug when exploring S4
- opts_formula() needs a "next" constructor
- data pronouns are not handled properly
- `deparse_call()` and options HOT 1
- Dependency Dashboard
- can't construct classed pointers
- more ggplot2 issues
- Release constructive 0.3.0
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from constructive.