Title: Typed Parameter Tags for Integration with 'roxygen2'
Version: 0.1.2
Description: Provides typed parameter documentation tags for integration with 'roxygen2'. Typed parameter tags provide a consistent interface for annotating expected types for parameters and returned values. Tools for converting from existing styles are also provided to easily adapt projects which implement typed documentation by convention rather than tag. Use the default format or provide your own.
License: MIT + file LICENSE
Encoding: UTF-8
URL: https://github.com/openpharma/roxytypes, https://openpharma.github.io/roxytypes/
BugReports: https://github.com/openpharma/roxytypes/issues
Imports: utils, cli, glue, roxygen2
Suggests: mockery, testthat
Enhances: roxylint
Config/Needs/documentation: roxylint
RoxygenNote: 7.3.2
NeedsCompilation: no
Packaged: 2024-10-15 15:34:34 UTC; root
Author: Doug Kelkhoff [aut, cre]
Maintainer: Doug Kelkhoff <doug.kelkhoff@gmail.com>
Repository: CRAN
Date/Publication: 2024-10-15 17:30:09 UTC

Find an associated block

Description

Given a file and line, discover which block the tag is associated with

Usage

associated_block(file, line)

Arguments

file

(character[1]) A file path associated with a tag

line

(integer[1]) A line number in the file where the tag was found

Value

(roxygen2::roxy_block() | NULL) A roxy_block if one could be associated, or NULL if not.


Build a collection of conversion edits

Description

Build a collection of conversion edits

Usage

build_convert_edits(format, tags, unmatched = FALSE)

Arguments

format

(character[1]) A regular expression, optionally containing named capture groups for type and description, which will be used for restructuring the tag as a roxytypes-equivalent tag.

tags

(list(roxy_tag)) A collection of roxygen2::roxy_tag() objects to edit. Can include tags which have no plausible conversion, which will be filtered before returning edits.

unmatched

(logical[1]) Whether to make edits to existing tags which can not be matched with the provided format. If TRUE, the existing description is migrated verbatim to the new tag, without a type provided. The new syntax will produce warnings when running roxygen2::roxygenize(), which can be useful tool for pinpointing tags that need manual fine-tuning for conversion.

Value

(data.frame) A collection of possible tag edits as produced by tag_edit().

See Also

Other convert: convert_match_format(), convert_tag(), make_convert_edits(), tag_edit()


Build format regular expression

Description

Allow glue-style formatting using keyworded regular expressions. The original glue string (anything that isn't expanded by glue) is treated as a string literal, whereas the contents of populated values can be regular expressions, allowing for a more user-friendly way to construct complicated regular expressions.

Usage

build_format_regex(
  format,
  format_re,
  ...,
  type = re_backticked(),
  description = re_any()
)

re_backticked()

re_any()

escape_non_glue_re(x)

Arguments

format

(character[1]) A glue-style format string. Expanded whisker values are used as a shorthand for capture groups, where ellipsis arguments can be provided for additional capture group patterns.

format_re

(character[1]) Alternatively, provide a standard regular expression directly.

...

Additional arguments provide keyworded capture groups for format

type

(character[1]) A regular expression to use to match a type signature. By default, matches within backticks.

description

(character[1]) A regular expression to use to match a parameter description. By default, matches any string.

x

(character[1]) A string to escape.

Details

To bypass glue entirely and use a standard regular expression, use format_re.

The provided regular expression must match all characters from the start of a string to the end. The string also matches using "dot all" syntax, meaning that the . expression will also match newline characters.

Value

(⁠character[1]:⁠) A regular expression string, built from component sub-expressions.

Functions

Examples

re <- roxytypes:::build_format_regex(
  "{as}{any}{bs}",
  as = "a+",
  bs = "b+",
  any = ".*?"
)

roxytypes:::regex_capture(re, "aaaa\n\nbb", perl = TRUE)

text <- "@param (`test(\")\")`)"

pattern <- sprintf("`%s`", re_backticked())

m <- regexec(pattern, text, perl = TRUE)
regmatches(text, m)[[1]]
# [1] "`test(\")\")`"

# curlies escaped, as this does not appear to be a glue-style usage
roxytypes:::escape_non_glue_re(".{1,3}")

# curlies not escaped, as this is a glue-style usage
roxytypes:::escape_non_glue_re("this is a {test}")


Clear state object

Description

Clear state object

Usage

clear_state()

roxytypes Config

Description

roxytypes exposes a few configuration options for helping to fine-tune your documentation. These are stored as key-values in a list in either the Config/roxytypes field of your DESCRIPTION file, or in a ./man/roxytypes/meta.R file within your package.

Usage

config(path = getwd(), refresh = FALSE, cache = TRUE)

Details

The available settings are listed below. Some fields are nested, which are shown by concatenating nested keys using $.

Value

(list) A named list of configured behaviors.


Configuration

Description

Various functions for loading, caching and performing configured behaviors using a user-supplied configuration file.

Usage

config_find_from(path = ".")

config_from_desc(path = ".")

config_from_file(path = ".")

Arguments

path

(character[1]) A file path to use when searching for a config file. Either the file path to a DESCRIPTION or the root path of a package, depending on the context of the function.

Details

This constant is used as a variable name in the package environment while documentation is being built to avoid constantly parsing configurations during evaluation of each tag.

Functions


Convert roxygen2 tags to roxytypes tags

Description

Convert a package codebase into applicable roxytypes tags. For roxygen2 tags with drop-in replacements (namely ⁠@param⁠ and ⁠@return⁠ tags), process descriptions and replace tags with roxytypes equivalents.

Usage

convert(
  path = ".",
  format = config(path, refresh = TRUE, cache = FALSE)$format,
  ...,
  unmatched = FALSE,
  verbose = interactive()
)

Arguments

path

(character[1]) A file path within your package. Defaults to the current working directory.

format

(character[1]) A glue-style format to use to parse types and descriptions for conversion to roxytypes tags. Available glue keywords include type and description. By default, type will match any string until a closing backtick and description will match any string. See details for more information.

...

Additional arguments passed to build_format_regex().

unmatched

(logical[1]) Indicates whether tags that fail to match should still be converted into roxytypes tags. Such conversions may be convenient if you aim to convert your package holistically, as it will help to flag undocumented parameter types the next time you re-build your documentation.

verbose

(logical[1]) Indicates whether command-line interface should be emitted so that changes can be reviewed interactively.

Details

A format string is built using build_format_regex(), which accepts parameters type and description, which describe how to match these components of a parameter definition. They are combined with the literal content of format to produce a regular expression to split existing definitions.

For comprehensive control, pass format_re directly, bypassing expression construction altogether.

Value

(logical[1]) TRUE if successfully completes, FALSE if aborted. Always returns invisibly.

Examples

## Not run: 
convert("(`{type}`) {description}")

## End(Not run)


Tools for modifying configuration files

Description

Tools for modifying configuration files

Usage

make_config_edits(path)

guess_dcf_indent(dcf)

update_config_needs(dcf)

update_config_roxygen_meta(path)

update_config_roxygen_desc(dcf)

update_config_roxygen_expr(expr)

Arguments

path

(character[1]) A file path within the package directory.

dcf

(data.frame) The result of read.dcf() on the package description file.

expr

(expression) The parsed contents of an R-formatted config entry.

Functions


Conversion Helpers

Description

Various functions for supporting conversion from standard roxygen tags to ⁠@typed⁠ tags.

Usage

convert_continue_prompt()

preview_convert_edits(edits, n = 1)

preview_convert_edit(edit)

format_diff_chr(d, offset)

diff_lines(d)

Arguments

n

(integer[1]) A number of edits to display.

edit, edits

(data.frame) As produced by convert_edit_df. Contains the edit source file, starting line number, number of original lines modified, the new content to insert and whether the format was matched for the edit. When singular, the data.frame is a single row.

d

(cli::diff_chr() result) The diff of the original and new tag contents.

offset

(integer[1]) A line offset for the start of the diff.

Value

NULL

Functions


Match a conversion format and structure results

Description

Match a conversion format and structure results

Usage

convert_match_format(x, format)

Arguments

x

(character[1]) Content to match.

format

(character[1]) A regular expression, optionally containing named capture groups for type and description, which will be used for restructuring the tag as a roxytypes-equivalent tag.

Value

(⁠: list⁠) A named list of type, description and matched fields. type and description represent the result of captured groups. If no capture groups were used, the raw string is used as a description. matched is a logical[1] indicating whether the provided format matched against the provided input.

See Also

Other convert: build_convert_edits(), convert_tag(), make_convert_edits(), tag_edit()


Convert a roxygen2 tag to roxytypes equivalent

Description

Convert a roxygen2 tag to roxytypes equivalent

Usage

convert_tag(tag, format, ...)

## Default S3 method:
convert_tag(tag, format, ...)

## S3 method for class 'return'
convert_tag(tag, format, ...)

## S3 method for class 'param'
convert_tag(tag, format, ...)

Arguments

tag

(roxygen2::roxy_tag()) A roxygen2 tag to convert.

format

(character[1]) A regular expression, optionally containing named capture groups for type and description, which will be used for restructuring the tag as a roxytypes-equivalent tag.

...

Additional arguments unused.

Value

(“NULL or [tag_edit()]) If the tag can be converted, a tag_edit() is returned, otherwise 'NULL'.

Methods (by class)

See Also

Other convert: build_convert_edits(), convert_match_format(), make_convert_edits(), tag_edit()


Default formatter for ⁠@typed⁠

Description

Adds special cases for when the type uses other roxygen2 syntax

Usage

default_format(x, name, type, description, ...)

Arguments

x

(roxygen2::roxy_tag()) The tag to format.

name, type, description

(character(1)) Fields parsed from the ⁠@typed⁠ tag.

...

Additional arguments unused.

Value

A formatted character value.


Find package root directory

Description

Traces parent directories until we find a pacakge root

Usage

find_package_root(path = ".")

Arguments

path

(character[1]) A file path within a package.

Value

(character[1]) The file path to the package root directory.


If-not-null-else

Description

If-not-null-else

Usage

lhs %||% rhs

Make tag edits

Description

Make tag edits

Usage

make_convert_edits(edits)

Arguments

edits

(data.frame) A collection of edits (one edit per row), as produced by tag_edit().

Value

(integer[1]) The number of edits that were made.

See Also

Other convert: build_convert_edits(), convert_match_format(), convert_tag(), tag_edit()


A helper to reliably read DCF files

Description

A helper to reliably read DCF files

Usage

read_dcf_asis(path)

Arguments

path

(character[1]) A file path to a DESCRIPTION file.

Value

(data.frame) The result of read.dcf().


A half-baked extract method

Description

A half-baked extract method

Usage

re_extract(pattern, replace, x)

extract_backticked(x)

extract_quoted(x)

is_backticked(x)

is_bracketed(x)

Arguments

pattern

character string containing a regular expression (or character string for fixed = TRUE) to be matched in the given character vector. Coerced by as.character to a character string if possible. If a character vector of length 2 or more is supplied, the first element is used with a warning. Missing values are allowed except for regexpr, gregexpr and regexec.

Value

(character[1]) The substituted string if a replacement is made, or NULL otherwise.

Functions

Note

This implementation is considered half-baked because there's no check for whether a replacement is made that results in the same string. This case will be interpreted the same as if there was no match.


Capture regex groups

Description

Captures regex groups and returns a named matrix of groups with one column per capture group and one row per element in x.

Usage

regex_capture(pattern, x, ...)

Arguments

pattern

(character[1]) A regex pattern to use for capturing groups.

x

(character) A vector of strings to match against.

...

Additional arguments passed to regexpr().


roxygen2 ⁠@typed⁠ tag parsing

Description

Parse a ⁠@typed⁠ tag and return parsed components as value

Usage

## S3 method for class 'roxy_tag_typed'
roxy_tag_parse(x)

Arguments

x

A tag

Value

(⁠roxygen2 tag⁠) A parsed roxygen2 ⁠@typed⁠ rd_tag.


roxygen2 ⁠@typedreturn⁠ tag parsing

Description

Parse a ⁠@typedreturn⁠ tag and return parsed components as value

Usage

## S3 method for class 'roxy_tag_typedreturn'
roxy_tag_parse(x)

Arguments

x

A tag

Value

(⁠roxygen2 tag⁠) A parsed roxygen2 ⁠@typedreturn⁠ rd_tag.


roxygen2 ⁠@typed⁠ tag rd section population

Description

Push typed fields into ⁠@param⁠ section

Usage

## S3 method for class 'roxy_tag_typed'
roxy_tag_rd(x, base_path, env)

Arguments

x

The tag

base_path

Path to package root directory.

env

Environment in which to evaluate code (if needed)

Value

(roxygen2::rd_section) A roxygen2 "param" rd_section with formatted type information.


roxygen2 ⁠@typedreturn⁠ tag rd section population

Description

Push typed fields into ⁠@param⁠ section

Usage

## S3 method for class 'roxy_tag_typedreturn'
roxy_tag_rd(x, base_path, env)

Arguments

x

The tag

base_path

Path to package root directory.

env

Environment in which to evaluate code (if needed)

Value

(roxygen2::rd_section) A roxygen2 ⁠@value⁠ rd_tag with formatted type information.


Fetch roxygen2 blocks

Description

Avoid recomputing roxygen2s parsing by saving the blocks after the first tag is hit.

Usage

roxygen_blocks(path = getwd(), refresh = FALSE, cache = TRUE)

Split and trim a string

Description

Split and trim a string

Usage

split_and_trim(x)

Arguments

x

(character[1]) A string to split into lines and trim.

Value

(x: character) A character vector of trimed lines.


Built a conversion edit

Description

Built a conversion edit

Usage

tag_edit(tag, new, matched)

Arguments

tag

(roxygen2::roxy_tag()) A roxygen2 tag to convert.

new

(character) The new content used to replace the tag.

matched

(logical[1]) Whether the content was generated based on a match of a specified format.

Value

(data.frame) A single-observation dataset representing information for a tag edit. The data.frame row includes variables:

See Also

Other convert: build_convert_edits(), convert_match_format(), convert_tag(), make_convert_edits()


roxytypes tags

Description

The ⁠@typed⁠ tag introduces a syntax for defining parameter types as a roxygen2 tag.

Usage

#' @typed <var>: <type>
#'   <description>

Details

Be aware that there are a few syntactic requirements:

Default type Parsing Syntax

The type portion of the ⁠@typed⁠ tag syntax will handle a bit of syntax as special cases.

Custom type Parsing Function

The above defaults are meant to cover most use cases and should be sufficient for all but the most elaborate development practices. If you need to go beyond these default behaviors, you can also provide a parsing function, accepting the parsed roxygen tag as well as the raw contents.

The function accepts the roxygen2::roxy_tag() produced when parsing the tag, whose ⁠$val⁠ contains fields name, type and description. For convenience, the ⁠$val⁠ contents is unpacked as arguments, though the structure of this tag is liable to change.

To implement a typescript-style class union syntax,

#' @typed arg: class_a | class_b | class_c
#'   depending on the class of the object provided, either an `"A"`
#'   or a `"B"`.

to produce the parameter definition

(`class_a`, `class_c` or `class_b`) depending on the class of the object
provided, either an `"A"`, `"B"` or a `"C"`.

we might define the following in DESCRIPTION (or in man/roxytypes/meta.R).

Config/roxytypes: list(
  format = function(tag, ..., name, type, description) {
    types <- paste0("`", trimws(strsplit(type, "|", fixed = TRUE)[[1]]), "`")
    types <- glue::glue_collapse(types, sep = ", ", last = " or ")
    paste0("(", types, ") ", description)
  }
)

Parse a raw ⁠@typed⁠ tag

Description

Parse a raw ⁠@typed⁠ tag

Usage

try_parse_typed(raw)

Arguments

raw

(character[1]) A raw string from which to try to parse typed parameter definitions.


Parse a raw ⁠@typedreturn⁠ tag

Description

Parse a raw ⁠@typedreturn⁠ tag

Usage

try_parse_typedreturn(raw)

Arguments

raw

(character[1]) A raw string from which to try to parse typed parameter definitions.


roxygen2 ⁠@typedreturn⁠ tag

Description

The ⁠@typedreturn⁠ tag introduces a syntax for defining return types as a roxygen2 tag.

Usage

#' @typedreturn <type>
#'   <description>

Details

There is one important syntactic features:


vapply helpers

Description

vapply helpers

Usage

vlapply(..., FUN.VALUE = logical(1L))

vcapply(..., FUN.VALUE = character(1L))

Arguments

...

Passed to vapply().

FUN.VALUE

A prototype signature to use for vapply().

Functions


A helper to apply field names to all roxy_tag val fields

Description

A helper to apply field names to all roxy_tag val fields

Usage

with_roxy_field_subclass(x)

Arguments

x

(list) A named list of tag val contents

Value

(⁠: list⁠) A nearly identical list, where elements have additional subclasses based on their field names.