@@ -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,35 +214,75 @@ generate_init_functions <- function(funs) {
215214}
216215
217216generate_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
228- funs $ package <- package
229227 funs $ package_call <- package_call
230228 funs $ list_params <- vcapply(funs $ args , glue_collapse_data , " {name}" )
231229 funs $ params <- vcapply(funs $ list_params , function (x ) if (nzchar(x )) paste0(" , " , x ) else x )
232230 is_void <- funs $ return_type == " void"
233231 funs $ calls <- ifelse(is_void ,
234- glue :: glue_data(funs , ' invisible(.Call({package_names}{params}{package_call}))' ),
235- 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})" )
236234 )
237235
238- out <- glue :: glue_data(funs , '
239- {name} <- function({list_params}) {{
240- {calls}
241- }}
242- ' )
236+ # Parse and associate Roxygen comments
237+ funs $ roxygen_comment <- mapply(function (file , line ) {
238+ if (file.exists(file )) {
239+ comments <- extract_roxygen_comments(file )
240+ matched_comment <- " "
241+ for (comment in comments ) {
242+ # Check if the comment directly precedes the function without gaps
243+ if (line == comment $ line + 1 ) {
244+ matched_comment <- comment $ text
245+ break
246+ }
247+ }
248+ matched_comment
249+ } else {
250+ " "
251+ }
252+ }, funs $ file , funs $ line , SIMPLIFY = TRUE )
253+
254+ # Generate R functions with or without Roxygen comments
255+ out <- mapply(function (name , list_params , calls , roxygen_comment ) {
256+ if (nzchar(roxygen_comment )) {
257+ glue :: glue(" {roxygen_comment}\n {name} <- function({list_params}) {{\n\t {calls}\n }}" )
258+ } else {
259+ glue :: glue(" {name} <- function({list_params}) {{\n {calls}\n }}" )
260+ }
261+ }, funs $ name , funs $ list_params , funs $ calls , funs $ roxygen_comment , SIMPLIFY = TRUE )
262+
263+ out <- glue :: trim(out )
243264 out <- glue :: glue_collapse(out , sep = " \n\n " )
244265 unclass(out )
245266}
246267
268+ extract_roxygen_comments <- function (file ) {
269+ lines <- readLines(file )
270+ roxygen_start <- grep(" ^/\\ * roxygen start" , lines )
271+ roxygen_end <- grep(" roxygen end \\ */$" , lines )
272+
273+ if (length(roxygen_start ) == 0 || length(roxygen_end ) == 0 ) {
274+ return (list ())
275+ }
276+
277+ roxygen_comments <- mapply(function (start , end ) {
278+ roxygen_lines <- lines [(start + 1 ): (end - 1 )]
279+ roxygen_lines <- sub(" ^" , " #' " , roxygen_lines )
280+ list (line = end , text = paste(roxygen_lines , collapse = " \n " ))
281+ }, roxygen_start , roxygen_end , SIMPLIFY = FALSE )
282+
283+ roxygen_comments
284+ }
285+
247286wrap_call <- function (name , return_type , args ) {
248287 call <- glue :: glue(' {name}({list_params})' , list_params = glue_collapse_data(args , " cpp11::as_cpp<cpp11::decay_t<{type}>>({name})" ))
249288 if (return_type == " void" ) {
0 commit comments