Skip to content

Commit fb3583f

Browse files
committed
roxygen comments on cpp side (works ok with 1 roxygenised function per script for now)
1 parent aeb1895 commit fb3583f

File tree

4 files changed

+155
-73
lines changed

4 files changed

+155
-73
lines changed

R/register.R

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -77,17 +77,16 @@ cpp_register <- function(path = ".", quiet = !is_interactive(), extension = c(".
7777
cli::cli_alert_success("generated file {.file {basename(r_path)}}")
7878
}
7979

80-
8180
call_entries <- get_call_entries(path, funs$name, package)
8281

8382
cpp_function_registration <- glue::glue_data(funs, ' {{
8483
"_cpp11_{name}", (DL_FUNC) &_{package}_{name}, {n_args}}}, ',
8584
n_args = viapply(funs$args, nrow)
8685
)
8786

88-
cpp_function_registration <- glue::glue_collapse(cpp_function_registration, sep = "\n")
87+
cpp_function_registration <- glue::glue_collapse(cpp_function_registration, sep = "\n")
8988

90-
extra_includes <- character()
89+
extra_includes <- character()
9190
if (pkg_links_to_rcpp(path)) {
9291
extra_includes <- c(extra_includes, "#include <cpp11/R.hpp>", "#include <Rcpp.h>", "using namespace Rcpp;")
9392
}
@@ -215,13 +214,13 @@ generate_init_functions <- function(funs) {
215214
}
216215

217216
generate_r_functions <- function(funs, package = "cpp11", use_package = FALSE) {
218-
funs <- funs[c("name", "return_type", "args")]
217+
funs <- funs[c("name", "return_type", "args", "file", "line", "decoration")]
219218

220219
if (use_package) {
221220
package_call <- glue::glue(', PACKAGE = "{package}"')
222221
package_names <- glue::glue_data(funs, '"_{package}_{name}"')
223222
} else {
224-
package_names <- glue::glue_data(funs, '`_{package}_{name}`')
223+
package_names <- glue::glue_data(funs, "`_{package}_{name}`")
225224
package_call <- ""
226225
}
227226

@@ -235,15 +234,30 @@ generate_r_functions <- function(funs, package = "cpp11", use_package = FALSE) {
235234
glue::glue_data(funs, '.Call({package_names}{params}{package_call})')
236235
)
237236

238-
out <- glue::glue_data(funs, '
239-
{name} <- function({list_params}) {{
240-
{calls}
241-
}}
242-
')
237+
roxygen_comments <- lapply(funs$file, extract_roxygen_comments)
238+
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)
242+
out <- glue::trim(out)
243243
out <- glue::glue_collapse(out, sep = "\n\n")
244244
unclass(out)
245245
}
246246

247+
extract_roxygen_comments <- function(file) {
248+
lines <- readLines(file)
249+
roxygen_start <- grep("^/\\* roxygen start", lines)
250+
roxygen_end <- grep("roxygen end \\*/$", lines)
251+
252+
if (length(roxygen_start) == 0 || length(roxygen_end) == 0) {
253+
return("")
254+
}
255+
256+
roxygen_lines <- lines[(roxygen_start + 1):(roxygen_end - 1)]
257+
roxygen_lines <- sub("^@", "#' @", roxygen_lines)
258+
paste(roxygen_lines, collapse = "\n")
259+
}
260+
247261
wrap_call <- function(name, return_type, args) {
248262
call <- glue::glue('{name}({list_params})', list_params = glue_collapse_data(args, "cpp11::as_cpp<cpp11::decay_t<{type}>>({name})"))
249263
if (return_type == "void") {

cpp11test/DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,4 @@ Suggests:
2020
xml2
2121
LazyData: true
2222
Roxygen: list(markdown = TRUE)
23-
RoxygenNote: 7.1.1
23+
RoxygenNote: 7.3.2

0 commit comments

Comments
 (0)