From dd713e70676984843067e7917c7629576fa80f73 Mon Sep 17 00:00:00 2001 From: Hadley Wickham Date: Mon, 11 Jul 2022 07:42:11 +1200 Subject: [PATCH] Don't list inherited methods if there aren't any (#1387) Fixes #1371 --- NEWS.md | 3 +++ R/rd-r6.R | 4 +++- tests/testthat/_snaps/rd-r6.md | 26 ++++++++++++++++++++++++++ tests/testthat/test-rd-r6.R | 27 +++++++++++++++++++++++++++ 4 files changed, 59 insertions(+), 1 deletion(-) diff --git a/NEWS.md b/NEWS.md index 49105c88..2986b2e8 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,8 @@ # roxygen2 (development version) +* R6 documentation no longer shows inherited methods if there aren't any + (#1371). + * If you have a daily build of RStudio, the lists of changed Rd files are now clickable so you can immediately see the rendered development documentation (#1354). diff --git a/R/rd-r6.R b/R/rd-r6.R index 2cb5bdd0..a70c8552 100644 --- a/R/rd-r6.R +++ b/R/rd-r6.R @@ -258,9 +258,11 @@ r6_inherited_method_list <- function(block, r6data) { self <- r6data$self super_meth <- super_meth[! super_meth$name %in% self$name, ] super_meth <- super_meth[! duplicated(super_meth$name), ] + if (nrow(super_meth) == 0) { + return() + } super_meth <- super_meth[rev(seq_len(nrow(super_meth))), ] - details <- paste0( ":10] @description Cannot find matching R6 method +# class with no inherited methods + + Code + cat(format(rd$get_section("rawRd"))) + Output + \section{Super class}{ + \code{\link[R_GlobalEnv:C1]{R_GlobalEnv::C1}} -> \code{C2} + } + \section{Methods}{ + \subsection{Public methods}{ + \itemize{ + \item \href{#method-C2-meth1}{\code{C2$meth1()}} + } + } + \if{html}{\out{
}} + \if{html}{\out{}} + \if{latex}{\out{\hypertarget{method-C2-meth1}{}}} + \subsection{Method \code{meth1()}}{ + method1 + \subsection{Usage}{ + \if{html}{\out{
}}\preformatted{C2$meth1()}\if{html}{\out{
}} + } + + } + } + # integration test Code diff --git a/tests/testthat/test-rd-r6.R b/tests/testthat/test-rd-r6.R index 66941a06..05d382cd 100644 --- a/tests/testthat/test-rd-r6.R +++ b/tests/testthat/test-rd-r6.R @@ -296,6 +296,33 @@ test_that("warning if no method comes after the docs", { doc <- format(rd) }) +test_that("class with no inherited methods", { + text <- " + C1 <- R6::R6Class('C1', cloneable = FALSE) + + #' @title Title + #' @description Description. + #' @details Details. + C2 <- R6::R6Class('C2', + inherit = C1, + cloneable = FALSE, + public = list( + #' @description method1 + meth1 = function() 1 + ) + )" + + env <- new.env(parent = globalenv()) + + eval(parse(text = text, keep.source = TRUE), envir = env) + block <- parse_text(text, env = env)[[1]] + rd <- RoxyTopic$new() + + topic_add_r6_methods(rd, block, env) + expect_snapshot(cat(format(rd$get_section("rawRd")))) +}) + + test_that("integration test", { wd <- getwd()