Skip to content

Commit 00a929b

Browse files
committed
fix: pkg_doc_obj
1 parent c5f8d46 commit 00a929b

File tree

7 files changed

+74
-37
lines changed

7 files changed

+74
-37
lines changed

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,4 @@ Config/testthat/edition: 3
2222
Encoding: UTF-8
2323
LazyData: true
2424
Roxygen: list(markdown = TRUE)
25-
RoxygenNote: 7.3.2
25+
RoxygenNote: 7.3.3

NEWS.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
<!-- generated by R package codedoc; do not modify! -->
22

3+
## News for version 0.10.5
4+
5+
### codedoc::pkg_doc_obj
6+
7+
Improved inference of object name to use in generated documentation.
8+
E.g. `regex = "\\Qmypkg::[.myclass\\E"` now reliably appears in docs
9+
as "mypkg::[.myclass".
10+
11+
312
## News for version 0.10.4
413

514
### codedoc::pkg_doc_fun

R/pkg_doc.R

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ pkg_doc_obj_regex_set__ <- function(regex, type) {
143143
return(out)
144144
}
145145

146-
pkg_doc_obj_section_heads__ <- function(regex, has_rdname) {
146+
pkg_doc_obj_section_heads__ <- function(obj_nm, has_rdname) {
147147
out <- list(
148148
param = NULL,
149149
descr = "@details",
@@ -155,16 +155,16 @@ pkg_doc_obj_section_heads__ <- function(regex, has_rdname) {
155155
param = NULL,
156156
descr = c(
157157
"@section Functions:",
158-
paste0("**", regex, "**")
158+
paste0("**", obj_nm, "**")
159159
),
160160
return = c(
161161
"@return",
162-
paste0("**", regex, "**")
162+
paste0("**", obj_nm, "**")
163163
),
164164
examples = c(
165165
"@examples",
166166
"",
167-
paste0("# ", regex)
167+
paste0("# ", obj_nm)
168168
)
169169
)
170170
}
@@ -261,25 +261,32 @@ pkg_doc_obj <- function(
261261
)
262262
grepl_arg_list[["x"]] <- df[["key"]]
263263
section_heads <- local({
264-
# proper name in section. instead of showing e.g.
265-
# regex = "\\Qmypkg::[.myclass\\E" in docs, we want to show
266-
# "mypkg::[.myclass". so we figure out the name of the package and the
267-
# object.
264+
# instead of showing e.g.
265+
# "\\Qmypkg::[.myclass\\E" in docs, we want to show
266+
# "mypkg::[.myclass". we look for an exact match in keys or if that fails
267+
# try to clean up the regex.
268+
# @codedoc_comment_block news("codedoc::pkg_doc_obj", "2025-10-06", "0.10.5")
269+
# Improved inference of object name to use in generated documentation.
270+
# E.g. `regex = "\\Qmypkg::[.myclass\\E"` now reliably appears in docs
271+
# as "mypkg::[.myclass".
272+
# @codedoc_comment_block news("codedoc::pkg_doc_obj", "2025-10-06", "0.10.5")
268273
section_gal <- grepl_arg_list
269274
section_gal[["pattern"]] <- sprintf(
270-
"((^)|[(\"])%s(($)|[\")])",
275+
"(?<=^|\\\")%s(?=$|\\\")",
271276
regex
272277
)
273-
keys <- df[["key"]][
274-
df[["key"]] == section_gal[["pattern"]] |
275-
do.call(grepl, section_gal)
276-
]
277-
pkg_obj_nm_pairs <- unique(gsub("(^.*[(\"])|([)\"].*$)", "", keys))
278-
if (length(pkg_obj_nm_pairs) == 1) {
279-
regex <- pkg_obj_nm_pairs
278+
obj_nm_set <- setdiff(do.call(regex_extract_first__, section_gal), NA)
279+
if (length(obj_nm_set) == 1) {
280+
obj_nm <- obj_nm_set
281+
} else {
282+
obj_nm <- gsub(
283+
"(\\\\Q|\\\\E|\\\\)",
284+
"",
285+
regex
286+
)
280287
}
281288
pkg_doc_obj_section_heads__(
282-
regex = regex,
289+
obj_nm = obj_nm,
283290
has_rdname = !is.null(rdname)
284291
)
285292
})
@@ -361,10 +368,10 @@ pkg_doc_fun <- function(
361368
rdname = NULL,
362369
text_file_paths = NULL
363370
) {
364-
# @codedoc_comment_block codedoc::pkg_doc_obj
371+
# @codedoc_comment_block codedoc::pkg_doc_fun
365372
# Document a function in an R package. Wrapper for
366-
# `codedoc::pkg_doc_fun` .
367-
# @codedoc_comment_block codedoc::pkg_doc_obj
373+
# `codedoc::pkg_doc_obj` .
374+
# @codedoc_comment_block codedoc::pkg_doc_fun
368375
# @codedoc_comment_block news("codedoc::pkg_doc_fun", "2025-03-10", "0.6.0")
369376
# New fun `codedoc::pkg_doc_fun`.
370377
# @codedoc_comment_block news("codedoc::pkg_doc_fun", "2025-03-10", "0.6.0")

R/utils.R

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
1-
2-
3-
4-
5-
6-
string_extract_first <- function(x, pattern, ...) {
7-
m <- regexpr(text = x, pattern = pattern, ...)
8-
regmatches(x = x, m = m)
9-
}
10-
string_extract_all <- function(x, pattern, ...) {
11-
m <- gregexpr(text = x, pattern = pattern, ...)
12-
regmatches(x = x, m = m)
1+
regex_extract_first__ <- function(x, pattern, perl = TRUE, ...) {
2+
m <- regexpr(pattern = pattern, text = x, perl = perl, ...)
3+
out <- rep(NA_character_, length(x))
4+
has_match <- !m %in% c(-1L, NA_integer_)
5+
out[has_match] <- regmatches(x = x, m = m)
6+
return(out)
137
}
148

9+
regex_extract_all__ <- function(x, pattern, perl = TRUE, ...) {
10+
m <- gregexpr(text = x, pattern = pattern, perl = TRUE, ...)
11+
out <- regmatches(x = x, m = m)
12+
return(out)
13+
}
1514

1615
string_interpolation_regex <- function() {
1716
# @codedoc_comment_block string_interpolation_regex
@@ -37,7 +36,7 @@ string_interpolation <- function(x, env, debug_data) {
3736
#
3837
# @codedoc_comment_block codedoc:::string_interpolation
3938
ip_re <- string_interpolation_regex()
40-
ip_exprs <- unique(unlist(string_extract_all(x = x, pattern = ip_re)))
39+
ip_exprs <- unique(unlist(regex_extract_all__(x = x, pattern = ip_re)))
4140
if (length(ip_exprs) == 0L) {
4241
return(x)
4342
}

man/codedoc-package.Rd

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/pkg_doc.Rd

Lines changed: 14 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/test-utils.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ testthat::test_that("regexes and section heads are in harmony", {
1717

1818
for (has_rdname in c(TRUE, FALSE)) {
1919
section_heads <- pkg_doc_obj_section_heads__(
20-
regex = "mypkg::myfun",
20+
obj_nm = "mypkg::myfun",
2121
has_rdname = TRUE
2222
)
2323
regexes <- pkg_doc_obj_regex_set__(

0 commit comments

Comments
 (0)