Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Put selector functions etc. into a parent env #2397

Merged
merged 3 commits into from
Mar 11, 2024
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 10 additions & 9 deletions R/topics.R
Original file line number Diff line number Diff line change
@@ -61,10 +61,11 @@ all_sign <- function(x, text) {
}

match_env <- function(topics) {
out <- env(empty_env(),
fns <- env(empty_env(),
"-" = function(x) -x,
"c" = function(...) c(...)
)
out <- env(fns)

topic_index <- seq_along(topics$name)

@@ -99,36 +100,36 @@ match_env <- function(topics) {
is_public <- function(internal) {
if (!internal) !topics$internal else rep(TRUE, nrow(topics))
}
out$starts_with <- function(x, internal = FALSE) {
fns$starts_with <- function(x, internal = FALSE) {
any_alias(~ grepl(paste0("^", x), .), .internal = internal)
}
out$ends_with <- function(x, internal = FALSE) {
fns$ends_with <- function(x, internal = FALSE) {
any_alias(~ grepl(paste0(x, "$"), .), .internal = internal)
}
out$matches <- function(x, internal = FALSE) {
fns$matches <- function(x, internal = FALSE) {
any_alias(~ grepl(x, .), .internal = internal)
}
out$contains <- function(x, internal = FALSE) {
fns$contains <- function(x, internal = FALSE) {
any_alias(~ grepl(x, ., fixed = TRUE), .internal = internal)
}
out$has_keyword <- function(x) {
fns$has_keyword <- function(x) {
which(purrr::map_lgl(topics$keywords, ~ any(. %in% x)))
}
out$has_concept <- function(x, internal = FALSE) {
fns$has_concept <- function(x, internal = FALSE) {
match <- topics$concepts %>%
purrr::map(~ str_trim(.) == x) %>%
purrr::map_lgl(any)

which(match & is_public(internal))
}
out$lacks_concepts <- function(x, internal = FALSE) {
fns$lacks_concepts <- function(x, internal = FALSE) {
nomatch <- topics$concepts %>%
purrr::map(~ match(str_trim(.), x, nomatch = FALSE)) %>%
purrr::map_lgl(~ length(.) == 0L | all(. == 0L))

which(nomatch & is_public(internal))
}
out$lacks_concept <- out$lacks_concepts
fns$lacks_concept <- fns$lacks_concepts
out
}

28 changes: 28 additions & 0 deletions tests/testthat/_snaps/build-reference-index.md
Original file line number Diff line number Diff line change
@@ -157,3 +157,31 @@
has_icons: no


# can use a selector name as a topic name

Code
data_reference_index(pkg)
Output
pagetitle: Function reference
rows:
- title: bla
slug: bla
desc: ~
is_internal: no
- topics:
- path: matches.html
title: matches
aliases: matches()
icon: ~
- path: A.html
title: A
aliases: A()
icon: ~
names:
- matches
- A
row_has_icons: no
is_internal: no
has_icons: no


6 changes: 6 additions & 0 deletions tests/testthat/assets/reference-selector/DESCRIPTION
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Package: testpackage
Version: 1.0.0
Title: A test package
Description: A test package
Authors@R: person("Hadley Wickham")
RoxygenNote: 7.3.1
4 changes: 4 additions & 0 deletions tests/testthat/assets/reference-selector/NAMESPACE
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Generated by roxygen2: do not edit by hand

export(A)
export(matches)
7 changes: 7 additions & 0 deletions tests/testthat/assets/reference-selector/R/funs.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#' matches
#' @export
matches <- function() {}

#' A
#' @export
A <- function() {}
1 change: 1 addition & 0 deletions tests/testthat/assets/reference-selector/_pkgdown.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
url: http://test.org
11 changes: 11 additions & 0 deletions tests/testthat/assets/reference-selector/man/A.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions tests/testthat/assets/reference-selector/man/matches.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions tests/testthat/test-build-reference-index.R
Original file line number Diff line number Diff line change
@@ -126,3 +126,13 @@ test_that("can use a topic from another package", {

expect_snapshot(data_reference_index(pkg))
})

test_that("can use a selector name as a topic name", {
meta <- list(reference = list(list(
title = "bla",
contents = c("matches", "matches('A')")
)))
pkg <- as_pkgdown(test_path("assets/reference-selector"), override = meta)

expect_snapshot(data_reference_index(pkg))
})