Giter VIP home page Giter VIP logo

Comments (5)

moodymudskipper avatar moodymudskipper commented on July 21, 2024

from constructive.

krlmlr avatar krlmlr commented on July 21, 2024

Right, I'd switch to capturing the body then.

We can still parse + style, though?

from constructive.

moodymudskipper avatar moodymudskipper commented on July 21, 2024
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.

moodymudskipper avatar moodymudskipper commented on July 21, 2024

@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.

krlmlr avatar krlmlr commented on July 21, 2024

Thanks, works beautifully!

from constructive.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.