Skip to content

Commit c97b1b1

Browse files
committed
nerge
Merge branch 'main' of https://github.com/olivroy/reuseme # Conflicts: # TODO.R
2 parents 20a1390 + 344bf4d commit c97b1b1

18 files changed

+120
-92
lines changed

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ Suggests:
3535
curl,
3636
gert,
3737
gt,
38-
pillar,
3938
magick,
39+
pillar,
4040
testthat (>= 3.2.1),
4141
withr
4242
Config/testthat/edition: 3

R/escape-inline-markup.R

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,3 +122,4 @@ replace_r_var <- function(x) {
122122
#' #> "{{gt_var}} in {{gt_var}} in gt_var in {.file {gt_var}}."
123123
#' escape_markup("{gt_var} in {{gt_var}} in gt_var in {.file {gt_var}}.")
124124
#' #> "{{gt_var}} in {{gt_var}} in gt_var in {.file gt_var}."
125+
NULL

R/files-conflicts.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ solve_file_name_conflict <- function(files, regex, dir = ".", extra_msg = NULL,
8585
}
8686
bullets_df <-
8787
rlang::set_names(files) |>
88-
purrr::map(\(x) readLines(x, encoding = "UTF-8")) |>
88+
purrr::map(\(x) readLines(x, encoding = "UTF-8", warn = FALSE)) |>
8989
purrr::map(\(x) tibble::enframe(x, name = "line_number", value = "content")) |>
9090
dplyr::bind_rows(.id = "file")
9191

@@ -145,7 +145,7 @@ get_referenced_files <- function(files) {
145145
# Create a list of genuine referenced files
146146
# TODO Add false positive references
147147
# TODO fs::path and file.path should be handled differently
148-
purrr::map(files, \(x) readLines(x, encoding = "UTF-8")) |>
148+
purrr::map(files, \(x) readLines(x, encoding = "UTF-8", warn = FALSE)) |>
149149
purrr::list_c(ptype = "character") |>
150150
stringr::str_subset(pattern = "\\:\\:dav.+lt|\\:\\:nw_|g.docs_l.n|target-|\\.0pt", negate = TRUE) |> # remove false positive from .md files
151151
stringr::str_subset(pattern = "file.path|fs\\:\\:path\\(|path_package|system.file", negate = TRUE) |> # Exclude fs::path() and file.path from search since handled differently.
File renamed without changes.

R/open.R

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,8 @@ active_rs_doc_delete <- function() {
255255
if (isTRUE(will_delete_decision)) {
256256
cli::cli_inform(c(
257257
"v" = "Deleted the active document {.val {elems$rel_path}} because {reasons_deleting}.",
258-
"i" = cli::col_grey("The deleted file {.path {elems$full_path}} contents are returned invisibly in case you need them.")
258+
# FIXME (upstream) the color div doesn't go all the way r-lib/cli#694
259+
"i" = paste(cli::col_grey("The deleted file"), "{.path {elems$full_path}}", cli::col_grey("contents are returned invisibly in case you need them."))
259260
))
260261
contents <- readLines(elems$full_path, encoding = "UTF-8")
261262
fs::file_delete(elems$full_path)

R/outline-criteria.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ define_outline_criteria <- function(.data, print_todo) {
158158
)
159159
x <- dplyr::mutate(
160160
x,
161-
before_and_after_empty = line_id == 1 | !nzchar(dplyr::lead(content, default = "")) & !nzchar(dplyr::lag(content)),
161+
before_and_after_empty = line == 1 | !nzchar(dplyr::lead(content, default = "")) & !nzchar(dplyr::lag(content)),
162162
.by = file
163163
)
164164
x

R/outline.R

Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -122,14 +122,14 @@ file_outline <- function(pattern = NULL,
122122
file_content <- rlang::set_names(path)
123123
# Not warn
124124
file_content <- purrr::map(file_content, function(x) readLines(fs::path_real(x), encoding = "UTF-8", warn = FALSE))
125-
# Combine everything into a tibble that contains file, line_id, content
126-
file_content <- purrr::map(file_content, function(x) tibble::enframe(x, name = "line_id", value = "content"))
125+
# Combine everything into a tibble that contains file, line, content
126+
file_content <- purrr::map(file_content, function(x) tibble::enframe(x, name = "line", value = "content"))
127127
file_content <- dplyr::bind_rows(file_content, .id = "file")
128128
} else {
129129
file_content <-
130130
purrr::map(
131131
.x = list("unsaved-doc.R" = rstudioapi::getSourceEditorContext()$contents),
132-
.f = function(x) tibble::enframe(x, name = "line_id", value = "content")
132+
.f = function(x) tibble::enframe(x, name = "line", value = "content")
133133
)
134134
file_content <- dplyr::bind_rows(file_content, .id = "file")
135135
}
@@ -365,7 +365,7 @@ print.outline_report <- function(x, ...) {
365365
link = list(rlang::set_names(
366366
link_rs_api,
367367
purrr::map_chr(
368-
paste0("{.file ", file, ":", line_id, "}"),
368+
paste0("{.file ", file, ":", line, "}"),
369369
cli::format_inline
370370
)
371371
)),
@@ -507,19 +507,19 @@ display_outline_element <- function(.data) {
507507
y <- dplyr::mutate(
508508
x,
509509
has_title_el =
510-
((line_id == 1 & !is_todo_fixme & !is_test_name & !is_snap_file) |
510+
((line == 1 & !is_todo_fixme & !is_test_name & !is_snap_file) |
511511
(is_doc_title & !is_subtitle & !is_snap_file & !is_second_level_heading_or_more)) & !is_news,
512512
.by = "file"
513513
)
514514
y <- withCallingHandlers(
515515
dplyr::mutate(y,
516-
title_el_line = ifelse(has_title_el, line_id[
517-
(line_id == 1 & !is_todo_fixme & !is_test_name & !is_snap_file) |
516+
title_el_line = ifelse(has_title_el, line[
517+
(line == 1 & !is_todo_fixme & !is_test_name & !is_snap_file) |
518518
(is_doc_title & !is_subtitle & !is_snap_file & !is_second_level_heading_or_more)
519519
][1], # take the first element to avoid problems (may be the reason why problems occur)
520-
NA
520+
NA_integer_
521521
),
522-
title_el = outline_el[line_id == title_el_line],
522+
title_el = outline_el[line == title_el_line],
523523
.by = "file"
524524
),
525525
error = function(e) {
@@ -529,15 +529,19 @@ display_outline_element <- function(.data) {
529529
)
530530
y <- dplyr::relocate(
531531
y,
532-
"outline_el", "line_id", "title_el", "title_el_line", "has_title_el",
532+
"outline_el", "line", "title_el", "title_el_line", "has_title_el",
533533
.after = "content"
534534
)
535535

536536

537537
y$outline_el <- ifelse(y$has_title_el, NA_character_, y$outline_el)
538-
na_if0 <- function(x) {
538+
na_if0 <- function(x, which) {
539539
if (length(x) == 0) {
540-
x <- NA
540+
if (which == "title") {
541+
x <- NA_character_
542+
} else {
543+
x <- NA_integer_
544+
}
541545
}
542546
if (length(x) != 1) {
543547
cli::cli_inform("{x} are detected as document title. Internal error")
@@ -547,8 +551,8 @@ display_outline_element <- function(.data) {
547551
if (!all(is.na(y$title_el))) {
548552
y <- dplyr::mutate(
549553
y,
550-
title_el = na_if0(title_el[!is.na(title_el)]),
551-
title_el_line = na_if0(title_el_line[!is.na(title_el_line)]),
554+
title_el = na_if0(title_el[!is.na(title_el)], "title"),
555+
title_el_line = na_if0(title_el_line[!is.na(title_el_line)], "line"),
552556
.by = "file"
553557
)
554558
}
@@ -591,7 +595,7 @@ construct_outline_link <- function(.data, is_saved_doc, dir_common, pattern) {
591595
as.character(trim_outline(.data$outline_el[cn], width - 8L)),
592596
"- {.run [Done{cli::symbol$tick}?](reuseme::complete_todo(",
593597
# Removed ending dot. (possibly will fail with older versions)
594-
.data$line_id[cn], ", '", .data$file[cn], "', '",
598+
.data$line[cn], ", '", .data$file[cn], "', '",
595599
# modify regex twice if needed (see below)
596600
stringr::str_sub(stringr::str_replace_all(.data$content[cn], "\\^|\\$|'|\\{|\\}|\\)|\\(|\\[\\]|\\+", "."), start = -15L), "'))}",
597601
.data$rs_version[cn]
@@ -613,7 +617,7 @@ construct_outline_link <- function(.data, is_saved_doc, dir_common, pattern) {
613617
# Removed ending dot. (possibly will fail with older versions)
614618

615619
# modify regex twice if needed (see above)
616-
line_id, ", '", file, "', '", stringr::str_sub(stringr::str_replace_all(content, "\\^|\\$|'|\\{|\\}|\\)|\\(|\\[\\]|\\+", "."), start = -15L), "'))}",
620+
line, ", '", file, "', '", stringr::str_sub(stringr::str_replace_all(content, "\\^|\\$|'|\\{|\\}|\\)|\\(|\\[\\]|\\+", "."), start = -15L), "'))}",
617621
rs_version
618622
),
619623
outline_el2
@@ -622,7 +626,7 @@ construct_outline_link <- function(.data, is_saved_doc, dir_common, pattern) {
622626
)
623627

624628
.data <- dplyr::mutate(.data,
625-
link = paste0(outline_el2, " {.path ", file, ":", line_id, "}"),
629+
link = paste0(outline_el2, " {.path ", file, ":", line, "}"),
626630
# rstudioapi::documentOpen works in the visual mode!! but not fully.
627631
file_path = .data$file,
628632
is_saved_doc = .env$is_saved_doc,
@@ -642,23 +646,25 @@ construct_outline_link <- function(.data, is_saved_doc, dir_common, pattern) {
642646
}
643647

644648
dplyr::mutate(.data,
645-
# link_rs_api = paste0("{.run [", outline_el, "](reuseme::open_rs_doc('", file_path, "', line = ", line_id, "))}"),
649+
# link_rs_api = paste0("{.run [", outline_el, "](reuseme::open_rs_doc('", file_path, "', line = ", line, "))}"),
646650
link_rs_api = dplyr::case_when(
647651
is.na(outline_el2) ~ NA_character_,
648-
!is_saved_doc ~ paste0("line ", line_id, " -", outline_el2),
652+
!is_saved_doc ~ paste0("line ", line, " -", outline_el2),
649653
rs_avail_file_link ~ paste0(
650654
"{cli::style_hyperlink(", style_fun, ', "',
651-
paste0("file://", file_path), '", params = list(line = ', line_id, ", col = 1))} ", outline_el2
655+
paste0("file://", file_path), '", params = list(line = ', line, ", col = 1))} ", outline_el2
652656
),
653-
.default = paste0(rs_version, "{.run [i](reuseme::open_rs_doc('", file_path, "', line = ", line_id, "))} ", outline_el2)
657+
.default = paste0(rs_version, "{.run [i](reuseme::open_rs_doc('", file_path, "', line = ", line, "))} ", outline_el2)
654658
),
655659
file_hl = dplyr::case_when(
656660
!is_saved_doc ~ file_path,
657661
rs_avail_file_link ~ paste0("{.href [", text_in_link, "](file://", file_path, ")}"),
658662
.default = paste0("{.run [", text_in_link, "](reuseme::open_rs_doc('", file_path, "'))}")
659663
),
660664
rs_version = NULL,
661-
outline_el2 = NULL
665+
outline_el2 = NULL,
666+
condition_to_truncate = NULL,
667+
condition_to_truncate2 = NULL
662668
) |>
663669
dplyr::filter(is.na(outline_el) | grepl(pattern, outline_el, ignore.case = TRUE))
664670
}

R/rename-files.R renamed to R/rename.R

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ rename_files2 <- function(old,
7575
}
7676

7777
is_git <- !isFALSE(tryCatch(rprojroot::find_root_file(criterion = rprojroot::criteria$is_vcs_root), error = function(e) FALSE))
78+
proj_name <- proj_get2()
7879
if (interactive() && !is_git && !identical(Sys.getenv("TESTTHAT"), "true")) {
7980
cli::cli_warn(c(
8081
"It is better to use this function in a version-controlled repository.",
@@ -91,7 +92,10 @@ rename_files2 <- function(old,
9192
related_files <- fs::dir_ls(regexp = paste0(basename_remove_ext(old), "\\."), recurse = TRUE)
9293
related_files <- fs::path_filter(related_files, "_snaps/|_book/|_files|_freeze|renv/", invert = TRUE)
9394
related_files <- setdiff(related_files, old)
95+
# remove project name from conflicts.
96+
related_files <- stringr::str_subset(related_files, proj_name, negate = TRUE)
9497
if (length(related_files) > 0) {
98+
# maybe would need to normalize path.
9599
cli::cli_warn(c(
96100
"Other files have a similar pattern",
97101
"See {.file {related_files}}",
@@ -151,7 +155,7 @@ rename_files2 <- function(old,
151155
# check_referenced_files(path = ".", quiet = !verbose)
152156
if (interactive() && action != "test") {
153157
cli::cli_inform(c(
154-
i = "Call {.run reuseme::check_referenced_files()} to see if dir contains non-existing files."
158+
i = cli::col_grey("Call {.run reuseme::check_referenced_files()} to see if dir contains non-existing files.")
155159
))
156160
}
157161
return(invisible(new))
@@ -358,7 +362,7 @@ hint_acceptable_renaming <- function(old, new, overwrite) {
358362
))
359363
return(FALSE)
360364
}
361-
# r-lib/cli#683
365+
# Delete when fixed r-lib/cli#683
362366
old_chr <- as.character(old)
363367
new_chr <- as.character(new)
364368

R/use-todo.R renamed to R/todo.R

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ use_todo <- function(todo, proj = proj_get2(), code = FALSE) {
6666
#' Function meant to be wrapped as `{.run}` hyperlinks with [file_outline()].
6767
#' It basically removes a line from a file.
6868
#'
69-
#' @param line_id The line number (a single integer)
69+
#' @param line The line number (a single integer)
7070
#' @param file Path to a file
7171
#' @param regexp A regexp to assess that the file content has not changed.
7272
#' @param rm_line A logical If `NULL` will remove the full line in the file
@@ -77,18 +77,18 @@ use_todo <- function(todo, proj = proj_get2(), code = FALSE) {
7777
#' content invisibly.
7878
#' @export
7979
#' @keywords internal
80-
complete_todo <- function(line_id, file, regexp, rm_line = NULL) {
80+
complete_todo <- function(line, file, regexp, rm_line = NULL) {
8181
check_string(regexp)
82-
check_number_whole(line_id)
83-
line_id_original <- line_id
82+
check_number_whole(line)
83+
line_original <- line
8484
# to defer warning.
8585
if (interactive() && rstudioapi::isAvailable()) {
8686
rstudioapi::documentSaveAll()
8787
}
8888
warn_change_of_line <- FALSE
8989

90-
file_content <- readLines(file, encoding = "UTF-8")
91-
line_content <- file_content[line_id]
90+
file_content <- readLines(file, encoding = "UTF-8", warn = FALSE)
91+
line_content <- file_content[line]
9292

9393
# Special case for issues (probably need to opt out at some point)
9494
# patch that will likely not work for many cases.
@@ -105,8 +105,8 @@ complete_todo <- function(line_id, file, regexp, rm_line = NULL) {
105105
warn_change_of_line <- TRUE
106106

107107
if (length(regexp_detection) == 1) {
108-
line_id <- regexp_detection
109-
line_content <- line_content <- file_content[line_id]
108+
line <- regexp_detection
109+
line_content <- line_content <- file_content[line]
110110
detect_regexp_in_line <- grepl(pattern = regexp, x = line_content)
111111
} else if (length(regexp_detection) > 1) {
112112
cli::cli_abort(c(
@@ -121,12 +121,12 @@ complete_todo <- function(line_id, file, regexp, rm_line = NULL) {
121121
}
122122
}
123123

124-
tag_type <- extract_tag_in_text(text = line_content, line_id)
124+
tag_type <- extract_tag_in_text(text = line_content, line)
125125

126126
if (warn_change_of_line) {
127127
cli::cli_warn(c(
128128
x = "Could not find {.arg regexp} as expected",
129-
"Could not find {.val {regexp}} at line {line_id_original}.",
129+
"Could not find {.val {regexp}} at line {line_original}.",
130130
i = "Has the file content changed since you ran this code?",
131131
# needs qty for cli pluralization, but no printing
132132
"`regexp` was detected in {cli::qty(length(regexp_detection))} line{?s} {regexp_detection}."
@@ -152,18 +152,18 @@ complete_todo <- function(line_id, file, regexp, rm_line = NULL) {
152152
regex_new <- cli::style_strikethrough(regex)
153153
line_content_show <- stringr::str_replace(line_content_show, regex, regex_new)
154154
}
155-
file_line <- paste0(file, ":", line_id)
155+
file_line <- paste0(file, ":", line)
156156
cli::cli_alert_success(
157157
"Removed {.code {line_content_show}} from {.file {file_line}}!"
158158
)
159159
# Rem
160160
line_content_new <- strip_todo_line(line_content, only_rm_tag = !rm_line)
161161

162162
if (nzchar(line_content_new)) {
163-
file_content[line_id] <- line_content_new
163+
file_content[line] <- line_content_new
164164
file_content_new <- file_content
165165
} else {
166-
file_content_new <- file_content[-line_id]
166+
file_content_new <- file_content[-line]
167167
if (!rm_line) {
168168
# WIll remove this line eventually
169169
# remove line if it ends up empty. not supposed to happen

0 commit comments

Comments
 (0)