-
Notifications
You must be signed in to change notification settings - Fork 30
Display tree of srcref/srcfile objects #85
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
base: main
Are you sure you want to change the base?
Conversation
DavisVaughan
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I didn't read the code too closely but I did read the docs closely and I really like having this all in one place!
be316eb to
d3e07a8
Compare
d3e07a8 to
3e324c0
Compare
| #' - The R function `parse()`. | ||
| #' - The frontend hook `ReadConsole`, which powers the console input parser in | ||
| #' the R CLI and in IDEs. | ||
| #' the R CLI and in IDEs. This C-level parser can also be accessed from C code #' via `R_ParseVector()`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Need newline?
hadley
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this looks great and you should feel free to merge whenever. But I'm also happy to discuss more 😄
| @@ -0,0 +1,88 @@ | |||
| #' Scrub src() output for deterministic snapshots | |||
| scrub_src <- function(x) { | |||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would this be better as a snapshot transformer?
| #' @param file Optional file path (default: creates temp file) | ||
| #' @return The result of sourcing the code with keep.source = TRUE | ||
| #' @noRd | ||
| with_srcref <- function(code, env = parent.frame(), file = NULL) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do you need to save to a file? Doesn't (eval(parse_with_srcref(code))) give you an object with srcref?
| #' Parse code with source references | ||
| #' | ||
| #' Creates a parsed expression with source references attached, useful for | ||
| #' testing srcref extraction from expressions. | ||
| #' | ||
| #' @param code Character string of R code | ||
| #' @return Parsed expression with srcref attributes | ||
| #' @noRd |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you can drop most of the docs here
|
|
||
| test_that("src() shows quoted function with nested body", { | ||
| expect_snapshot({ | ||
| with_srcref("x <- quote(function() {})") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you need the assignment here?
| test_that("src() shows closure with srcref and wholeSrcref", { | ||
| expect_snapshot({ | ||
| f <- simple_function_with_srcref() | ||
| scrub_src(src(f)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you explain why this works interactively:
f <- function() {
x + 1 # comment
{}
}
scrub_src(src(f))But not
expect_snapshot({
f <- function() {
x + 1 # comment
{}
}
scrub_src(src(f))
})
This PR:
The tree viewer is helpful to get a grasp of the complex web of objects created by the parser:
Principles of the tree view:
body(),attr(), or[[).srcreforwholeSrcrefattributes on lists).parsedis only shown if different fromlocation(due to a#linedirective)Here is an example of
src()showing its own srcrefs tree: