Skip to content

Commit 38bd16f

Browse files
committed
use fix shifted comments in extract comments
1 parent 8eeeb40 commit 38bd16f

File tree

2 files changed

+75
-7
lines changed

2 files changed

+75
-7
lines changed

R/utils-get_code_dependency.R

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -141,12 +141,12 @@ get_children <- function(pd, parent) {
141141
#' Fixes edge case of comments being shifted to the next call.
142142
#' @keywords internal
143143
#' @noRd
144-
fix_shifted_comments <- function(calls) {
144+
fix_shifted_comments <- function(calls, pattern = "@linksto") {
145145
# If the first or the second token is a @linksto COMMENT,
146146
# then it belongs to the previous call.
147147
if (length(calls) >= 2) {
148148
for (i in 2:length(calls)) {
149-
comment_idx <- grep("@linksto", calls[[i]][, "text"])
149+
comment_idx <- grep(pattern, calls[[i]][, "text"])
150150
if (isTRUE(comment_idx[1] <= 2)) {
151151
calls[[i - 1]] <- rbind(
152152
calls[[i - 1]],
@@ -156,7 +156,20 @@ fix_shifted_comments <- function(calls) {
156156
}
157157
}
158158
}
159-
Filter(nrow, calls)
159+
calls <- Filter(nrow, calls)
160+
# If, after shifting, there are two COMMENTs in one call, paste them.
161+
merge_comments <- function(call) {
162+
if (sum(call$token == "COMMENT") >= 2) {
163+
comments <- call[call$token == "COMMENT", "text"]
164+
first_comment_row <- call[which(call$token == "COMMENT")[1], ]
165+
call <- call[call$token != "COMMENT", ]
166+
first_comment_row$text <- paste(comments, collapse = " ")
167+
rbind(call, first_comment_row)
168+
} else {
169+
call
170+
}
171+
}
172+
lapply(calls, merge_comments)
160173
}
161174

162175
#' Fixes edge case of `<-` assignment operator being called as function,
@@ -466,8 +479,8 @@ extract_comments <- function(parsed_code) {
466479
comment <- call[call$token == "COMMENT", "text"]
467480
if (length(comment) == 0) "" else comment
468481
}
469-
unlist(lapply(
470-
extract_calls(utils::getParseData(parsed_code)),
471-
get_comments
472-
))
482+
calls <- extract_calls(utils::getParseData(parsed_code))
483+
fixed_calls <- fix_shifted_comments(calls, pattern = "#")
484+
485+
unlist(lapply(fixed_calls, get_comments))
473486
}

tests/testthat/test-qenv_get_code.R

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -448,6 +448,61 @@ testthat::test_that(
448448
}
449449
)
450450

451+
452+
# comments --------------------------------------------------------------------------------------------------------
453+
454+
testthat::test_that("comments fall into proper calls", {
455+
456+
# If comment is on top, it gets moved to the first call.
457+
# Any other comment gets moved to the call above.
458+
code <- "
459+
# initial comment
460+
a <- 1
461+
b <- 2 # inline comment
462+
c <- 3
463+
# inbetween comment
464+
d <- 4
465+
# finishing comment
466+
"
467+
468+
q <- qenv() |> eval_code(code)
469+
testthat::expect_identical(
470+
get_code(q),
471+
c("a <- 1 # initial comment",
472+
"b <- 2 # inline comment",
473+
"c <- 3 # inbetween comment",
474+
"d <- 4 # finishing comment")
475+
)
476+
477+
})
478+
479+
testthat::test_that("comments get pasted when they fall into calls", {
480+
481+
# If comment is on top, it gets moved to the first call.
482+
# Any other comment gets moved to the call above.
483+
# Comments get pasted if there are two assigned to the same call.
484+
code <- "
485+
# initial comment
486+
a <- 1 # A comment
487+
b <- 2 # inline comment
488+
c <- 3 # C comment
489+
# inbetween comment
490+
d <- 4
491+
# finishing comment
492+
"
493+
494+
q <- qenv() |> eval_code(code)
495+
testthat::expect_identical(
496+
get_code(q),
497+
c("a <- 1 # initial comment # A comment",
498+
"b <- 2 # inline comment",
499+
"c <- 3 # C comment # inbetween comment",
500+
"d <- 4 # finishing comment"
501+
)
502+
)
503+
504+
})
505+
451506
# functions -------------------------------------------------------------------------------------------------------
452507

453508
testthat::test_that("ignores occurrence in a function definition", {

0 commit comments

Comments
 (0)