Skip to content

Commit 14531db

Browse files
committed
almost there with rx/no rx in the same file
1 parent fb3583f commit 14531db

File tree

1 file changed

+26
-10
lines changed

1 file changed

+26
-10
lines changed

R/register.R

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -224,21 +224,33 @@ generate_r_functions <- function(funs, package = "cpp11", use_package = FALSE) {
224224
package_call <- ""
225225
}
226226

227-
funs$package <- package
228227
funs$package_call <- package_call
229228
funs$list_params <- vcapply(funs$args, glue_collapse_data, "{name}")
230229
funs$params <- vcapply(funs$list_params, function(x) if (nzchar(x)) paste0(", ", x) else x)
231230
is_void <- funs$return_type == "void"
232231
funs$calls <- ifelse(is_void,
233-
glue::glue_data(funs, 'invisible(.Call({package_names}{params}{package_call}))'),
234-
glue::glue_data(funs, '.Call({package_names}{params}{package_call})')
232+
glue::glue_data(funs, "invisible(.Call({package_names}{params}{package_call}))"),
233+
glue::glue_data(funs, ".Call({package_names}{params}{package_call})")
235234
)
236235

237236
roxygen_comments <- lapply(funs$file, extract_roxygen_comments)
238237

239-
out <- mapply(function(name, list_params, calls, roxygen_comment) {
240-
glue::glue("{if (nzchar(roxygen_comment)) paste0(roxygen_comment, '\n') else ''}{name} <- function({list_params}) {{\n\t{calls}\n}}")
241-
}, funs$name, funs$list_params, funs$calls, roxygen_comments, SIMPLIFY = TRUE)
238+
out <- mapply(function(name, list_params, calls, file, line) {
239+
comments <- extract_roxygen_comments(file)
240+
roxygen_comment <- ""
241+
for (comment in comments) {
242+
if (comment$line < line) {
243+
roxygen_comment <- comment$text
244+
}
245+
}
246+
if (nzchar(roxygen_comment)) {
247+
glue::glue("{roxygen_comment}\n{name} <- function({list_params}) {{\n\t{calls}\n}}")
248+
} else {
249+
glue::glue("{name} <- function({list_params}) {{\n\t{calls}\n}}")
250+
}
251+
}, funs$name, funs$list_params, funs$calls, funs$file, funs$line, SIMPLIFY = FALSE)
252+
253+
out <- as.character(out)
242254
out <- glue::trim(out)
243255
out <- glue::glue_collapse(out, sep = "\n\n")
244256
unclass(out)
@@ -250,12 +262,16 @@ extract_roxygen_comments <- function(file) {
250262
roxygen_end <- grep("roxygen end \\*/$", lines)
251263

252264
if (length(roxygen_start) == 0 || length(roxygen_end) == 0) {
253-
return("")
265+
return(list())
254266
}
255267

256-
roxygen_lines <- lines[(roxygen_start + 1):(roxygen_end - 1)]
257-
roxygen_lines <- sub("^@", "#' @", roxygen_lines)
258-
paste(roxygen_lines, collapse = "\n")
268+
roxygen_comments <- mapply(function(start, end) {
269+
roxygen_lines <- lines[(start + 1):(end - 1)]
270+
roxygen_lines <- sub("^@", "#' @", roxygen_lines)
271+
list(line = start, text = paste(roxygen_lines, collapse = "\n"))
272+
}, roxygen_start, roxygen_end, SIMPLIFY = FALSE)
273+
274+
roxygen_comments
259275
}
260276

261277
wrap_call <- function(name, return_type, args) {

0 commit comments

Comments
 (0)