Skip to content

Commit af33517

Browse files
committed
added generic get_slice and get_reconstruction functions
1 parent 63cd234 commit af33517

File tree

6 files changed

+116
-16
lines changed

6 files changed

+116
-16
lines changed

NAMESPACE

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
# Generated by roxygen2: do not edit by hand
22

33
export(dump_dependencies_addin)
4+
export(dump_reconstruct_addin)
5+
export(get_reconstruction)
6+
export(get_slice)
47
export(install_node_addin)
58
export(open_prefs_addin)
69
export(reconstruct_addin)

R/slice.R

Lines changed: 51 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,35 @@ reconstruct_addin <- function() {
2121
#'
2222
#' @export
2323
dump_reconstruct_addin <- function() {
24-
result <- get_slice()$result
25-
code <- result$results$reconstruct$code
26-
cat(if (is.null(code)) "No reconstructed code available" else code)
27-
return(invisible(code))
24+
get_reconstruction(print = TRUE)
2825
}
2926

30-
get_slice <- function() {
31-
context <- rstudioapi::getActiveDocumentContext()
32-
selection <- context$selection[[1]]$range["start"][[1]]
33-
34-
criterion <- find_criterion(selection[[1]], selection[[2]], context$contents)
35-
36-
cat(paste0("[flowR] Slicing for criterion ", criterion, "\n"))
27+
#' Generates a slice for the given filename and criterion, code fragment and criterion, or the currently highlighted variable in the active RStudio document.
28+
#'
29+
#' @param filename The name of the file to slice. If NULL, the passed code fragment is used.
30+
#' @param code The code fragment to slice, as a character. If also NULL, the currently active document is used.
31+
#' @param criterion The slicing criterion to use. Needs to be non-NULL if filename or code is provided.
32+
#'
33+
#' @return A list containing the filename, criterion, result of the slice request, a mapping from IDs to locations, and the slice locations.
34+
#'
35+
#' @export
36+
get_slice <- function(filename = NULL, code = NULL, criterion = NULL) {
37+
if (is.null(filename) && is.null(code)) {
38+
context <- rstudioapi::getActiveDocumentContext()
39+
filename <- context$path
40+
code <- paste0(context$contents, collapse = "\n")
41+
selection <- context$selection[[1]]$range["start"][[1]]
42+
criterion <- find_criterion(selection[[1]], selection[[2]], context$contents)
43+
cat(paste0("[flowR] Slicing for criterion ", criterion, "\n"))
44+
} else if (is.null(criterion)) {
45+
stop("Slicing for a given filename or code fragment requires passing a slicing criterion")
46+
} else if (!is.null(filename) && !is.null(code)) {
47+
stop("Either pass a filename or a code fragment, but not both")
48+
} else if (!is.null(filename)) {
49+
code <- paste0(readLines(filename, warn = FALSE), collapse = "\n")
50+
} else if (!is.null(code)) {
51+
filename <- "__tmp"
52+
}
3753

3854
# nolint: object_usage_linter (fails to recognize flowr_session_storage as a global var)
3955
conn_pid <- flowr_session_storage()
@@ -45,10 +61,10 @@ get_slice <- function() {
4561
analysis <- flowr::send_request(conn_pid$connection, list(
4662
type = "request-file-analysis",
4763
id = "0",
48-
filename = context$path,
64+
filename = filename,
4965
format = "json",
5066
filetoken = "@tmp",
51-
content = paste0(context$contents, collapse = "\n")
67+
content = code
5268
))
5369
id_to_location_map <- flowr::make_id_to_location_map(analysis$results$normalize$ast)
5470

@@ -68,10 +84,31 @@ get_slice <- function() {
6884
}
6985

7086
return(list(
71-
filename = context$path,
87+
filename = filename,
7288
criterion = criterion,
7389
result = result,
7490
id_to_location_map = id_to_location_map,
7591
slice_locations = slice_locations
7692
))
7793
}
94+
95+
#' Generates a slice for the given filename and criterion, code fragment and criterion, or the currently highlighted variable in the active RStudio document and returns the reconstructed code fragment.
96+
#'
97+
#' @param filename The name of the file to slice. If NULL, the passed code fragment is used.
98+
#' @param code The code fragment to slice, as a character. If also NULL, the currently active document is used.
99+
#' @param criterion The slicing criterion to use. Needs to be non-NULL if filename or code is provided.
100+
#' @param print If TRUE, the reconstructed code is printed to the console and returned invisibly. Defaults to FALSE.
101+
#'
102+
#' @return The reconstructed code fragment for the generated slice.
103+
#'
104+
#' @export
105+
get_reconstruction <- function(filename = NULL, code = NULL, criterion = NULL, print = FALSE) {
106+
result <- get_slice(filename, code, criterion)$result
107+
code <- result$results$reconstruct$code
108+
if (print) {
109+
cat(if (is.null(code)) "No reconstructed code available" else code)
110+
return(invisible(code))
111+
} else {
112+
return(code)
113+
}
114+
}

man/dump_reconstruct_addin.Rd

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/get_reconstruction.Rd

Lines changed: 28 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/get_slice.Rd

Lines changed: 21 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/reconstruct_addin.Rd

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)