Skip to content

Include line information for tags in roxy_block #664

@jimhester

Description

@jimhester

For ropensci/spelling#3 it would be useful to have the start line per tag in the roxy_block() objects which are the result of parse_file(). This can be done with a trivial change

diff --git a/R/block.R b/R/block.R
index 350fd1e..ce2b5cd 100644
--- a/R/block.R
+++ b/R/block.R
@@ -163,6 +163,10 @@ parse_tags <- function(tokens, registry = list(), global_options = list()) {
   # it's what roxygen already uses
   vals <- lapply(tags, `[[`, "val")
   names <- vapply(tags, `[[`, "tag", FUN.VALUE = character(1))
+  for (i in seq_along(tags)) {
+    attr(vals[[i]], "line") <- tags[[i]]$line
+  }
+
   setNames(vals, names)
 }

However doing so reveals that the line numbers do not seem quite correct. Running parse_file() on a simple file reveals a couple of issues.

cat(
"#' Foo
#'
#' A simple
#' multiline description.
#'
#' @param x xyz
#' @export
NULL", "test.R")

res <- roxygen2::parse_file("test.R")

str(res)

#> List of 1
#>  $ :List of 4
#>   ..$ title      : atomic [1:1] Foo
#>   .. ..- attr(*, "line")= int 1
#>   ..$ description: atomic [1:1] A simple
#> multiline description.
#>   .. ..- attr(*, "line")= int 1
#>   ..$ param      :List of 2
#>   .. ..$ name       : chr "x"
#>   .. ..$ description: chr "xyz"
#>   .. ..- attr(*, "line")= int 7
#>   ..$ export     : atomic [1:1]
#>   .. ..- attr(*, "line")= int 8
#>   ..- attr(*, "filename")= chr "test.R"
#>   ..- attr(*, "location")= int [1:8] 8 1 8 4 1 4 8 8
#>   ..- attr(*, "class")= chr "roxy_block"
  1. The start line of the description should be at least 2 (ideally 3), it is 1.
  2. The start lines of the param and export tags are 1 higher than they should be. They are 7 and 8 when they occur on lines 6 and 7 of the file.

Metadata

Metadata

Assignees

No one assigned

    Labels

    featurea feature request or enhancement

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions