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

Make sure the content to the left of TODO items doesn't get deleted when marking a TODO item as complte #27

Merged
merged 6 commits into from
May 17, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# reuseme (development version)

* `complete_todo()` no longer deletes the full line. It only deletes what it says it deletes (#27).
olivroy marked this conversation as resolved.
Show resolved Hide resolved

* `file_outline()` works better with news files and headings at the end of files.

* `file_outline()` gives a better error for empty paths.
Expand Down
7 changes: 5 additions & 2 deletions R/outline.R
Original file line number Diff line number Diff line change
Expand Up @@ -592,7 +592,8 @@ construct_outline_link <- function(.data, is_saved_doc, dir_common, pattern) {
"- {.run [Done{cli::symbol$tick}?](reuseme::complete_todo(",
# Removed ending dot. (possibly will fail with older versions)
.data$line_id[cn], ", '", .data$file[cn], "', '",
stringr::str_sub(stringr::str_replace_all(.data$content[cn], "'|\\{|\\}|\\)|\\(|\\[\\]|\\+", "."), start = -15L), "'))}",
# modify regex twice if needed (see below)
stringr::str_sub(stringr::str_replace_all(.data$content[cn], "\\^|\\$|'|\\{|\\}|\\)|\\(|\\[\\]|\\+", "."), start = -15L), "'))}",
.data$rs_version[cn]
)
# truncate other elements
Expand All @@ -610,7 +611,9 @@ construct_outline_link <- function(.data, is_saved_doc, dir_common, pattern) {
outline_el,
"- {.run [Done{cli::symbol$tick}?](reuseme::complete_todo(",
# Removed ending dot. (possibly will fail with older versions)
line_id, ", '", file, "', '", stringr::str_sub(stringr::str_replace_all(content, "'|\\{|\\}|\\)|\\(|\\[\\]|\\+", "."), start = -15L), "'))}",
# modify regex twice if needed (see above)

line_id, ", '", file, "', '", stringr::str_sub(stringr::str_replace_all(content, "\\^|\\$|'|\\{|\\}|\\)|\\(|\\[\\]|\\+", "."), start = -15L), "'))}",
rs_version
),
outline_el2
Expand Down
41 changes: 34 additions & 7 deletions R/use-todo.R
Original file line number Diff line number Diff line change
Expand Up @@ -133,36 +133,63 @@ complete_todo <- function(line_id, file, regexp, rm_line = NULL) {
))
}

line_content_before_comment <- stringr::str_extract(line_content, "([^#]+)#", group = 1)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider renaming line_content_before_comment to line_content_prefix for clarity.

- line_content_before_comment <- stringr::str_extract(line_content, "([^#]+)#", group = 1)
+ line_content_prefix <- stringr::str_extract(line_content, "([^#]+)#", group = 1)

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
line_content_before_comment <- stringr::str_extract(line_content, "([^#]+)#", group = 1)
line_content_prefix <- stringr::str_extract(line_content, "([^#]+)#", group = 1)

if (is.null(rm_line)) {
rm_line <- tag_type %in% c("TODO", "FIXME")
rm_line <- tag_type %in% c("TODO", "FIXME") # && is.na(line_content_before_comment)
}
line_content_todo <- stringr::str_extract(line_content, "#[^#]+")
line_content_show <- stringr::str_squish(line_content)

if (rm_line) {
line_content_show <- cli::style_strikethrough(line_content_show)
if (is.na(line_content_before_comment)) {
line_content_show <- cli::style_strikethrough(line_content_show)
} else {
line_content_show <- paste(line_content_before_comment, cli::style_strikethrough(line_content_todo))
}
} else {
# Only strikethrough the tag
regex <- paste0("\\s", tag_type)
regex <- paste0(" ", tag_type)
regex_new <- cli::style_strikethrough(regex)
line_content_show <- stringr::str_replace(line_content_show, regex, regex_new)
}

file_line <- paste0(file, ":", line_id)
cli::cli_alert_success(
"Removed {.code {line_content_show}} from {.file {file}}!"
"Removed {.code {line_content_show}} from {.file {file_line}}!"
)

if (rm_line) {
file_content_new <- file_content[-line_id]
line_content_new <- ""
if (is.na(line_content_before_comment)) {
file_content_new <- file_content[-line_id]
line_content_new <- ""
} else {
line_content_new <- stringr::str_trim(line_content_before_comment, side = "right")

if (nzchar(line_content_new)) {
file_content[line_id] <- line_content_new
file_content_new <- file_content
} else {
file_content_new <- file_content[-line_id]
}
}

} else {
line_content_new <- sub(
pattern = paste0(tag_type, "\\s+"),
replacement = "",
line_content
)
line_content_new <- stringr::str_trim(line_content_new, side = "right")

file_content[line_id] <- line_content_new
file_content_new <- file_content
file_content_new <- file_content[-line_id]

if (!nzchar(line_content_new)) {
# WIll remove this line eventually
# remove line if it ends up empty. not supposed to happen
cli::cli_abort("This should not happen. We were supposed not to remove lines", .internal = TRUE)
}

}
if (length(file_content_new) > 0) {
usethis::write_over(path = file, lines = file_content_new, overwrite = TRUE, quiet = TRUE)
Expand Down
11 changes: 6 additions & 5 deletions tests/testthat/test-use-todo.R
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ test_that("Marking a TODO item as done works", {
tmp <- tempfile(fileext = "R")
content <- c(
"# I Want this done",
"# TODO item to delete",
"# WORK Explain what the next code does.",
"2^2 # TODO item to delete",
"2^2 # WORK Explain what the next code does.",
"# TODO with {.href [cli hyperlinks](https://cli.r-lib.org/reference/links.html)}",
"# FIXME Repair this function",
" # TODO Check r-lib/usethis#1890",
Expand Down Expand Up @@ -52,11 +52,11 @@ test_that("Marking a TODO item as done works", {
file = tmp,
regexp = "Explain what the next code does"
),
warn = TRUE
warn = FALSE
)
expect_equal(
out,
"# Explain what the next code does."
"2^2 # Explain what the next code does."
)
expect_complete_todo(
out <- complete_todo(
Expand All @@ -70,7 +70,8 @@ test_that("Marking a TODO item as done works", {
read_utf8(tmp),
c(
"# I Want this done",
"# Explain what the next code does.",
"2^2",
"2^2 # Explain what the next code does.",
"# TODO with {.href [cli hyperlinks](https://cli.r-lib.org/reference/links.html)}",
"# FIXME Repair this function",
"# TODO Check https://github.com/r-lib/usethis/issues/1890",
Expand Down
Loading