Skip to content

Commit

Permalink
Random fixes
Browse files Browse the repository at this point in the history
`rename_files2()` works better
  • Loading branch information
olivroy committed Nov 21, 2024
1 parent 530269e commit 4714bee
Show file tree
Hide file tree
Showing 7 changed files with 196 additions and 186 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: reuseme
Title: Collections of Utility Functions to Work Across Projects
Version: 0.0.2.9007
Version: 0.0.2.9008
Authors@R:
person("Olivier", "Roy", , "olivierroy71@hotmail.com", role = c("aut", "cre"))
Description: Allows you to browse current projects, rename files safely,
Expand Down
9 changes: 7 additions & 2 deletions R/files-conflicts.R
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,12 @@ check_referenced_files <- function(path = ".", quiet = FALSE) {
#' @param extra_msg Extra message to pass
#' @param what Which file conflicts we talking about
#' @param quiet A logical, informs where the occurrences are found. (Default, `FALSE`)
#'
#' @param new_file (Optional) new file name
#' @return Mostly called for its side-effects, but will return the number of matches
#' (0 if no referenced files are problematic)
#' @export
#' @keywords internal
solve_file_name_conflict <- function(files, regex, dir = ".", extra_msg = NULL, quiet = FALSE, what = NULL) {
solve_file_name_conflict <- function(files, regex, dir = ".", extra_msg = NULL, quiet = FALSE, what = NULL, new_file = NULL) {
regex <- stringr::str_replace_all(regex, "\\\\|\\)|\\(|\\}\\{\\?|\\$|~", ".")
# regex <- as.character(regex)
if (dir != ".") {
Expand All @@ -91,6 +91,11 @@ solve_file_name_conflict <- function(files, regex, dir = ".", extra_msg = NULL,
purrr::map(\(x) tibble::enframe(x, name = "line_number", value = "content")) |>
dplyr::bind_rows(.id = "file")

if (!is.null(new_file)) {
nchar_new_file <- nchar(new_file)
bullets_df$content <- gsub(new_file, paste0("x", nchar_new_file), bullets_df$content, fixed = TRUE)
}

bullets_df <- bullets_df[grepl(regex, bullets_df$content), ]

if (nrow(bullets_df) == 0) {
Expand Down
10 changes: 5 additions & 5 deletions R/move.R
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,15 @@ file_move_temp_auto <- function(destdir) {
#'
#' @returns The new full path name, invisibly, allowing you to call the functions another time.
file_rename_auto <- function(new_name, old_file = .Last.value) {
if (!fs::is_file(old_name)) {
cli::cli_abort("incorrect context for .rename_temp.")
if (!fs::is_file(old_file)) {
cli::cli_abort("incorrect context for {.fn file_rename_auto}.")
}
# path_dir() behaves weirdly if not an fs path
# fs::path_dir("~/")
# fs::path_dir(fs::path("~))
# Workaround r-lib/fs#459
old_path <- fs::path_real(old_name)
ext <- fs::path_ext(old_name)
old_path <- fs::path_real(old_file)
ext <- fs::path_ext(old_file)
# TRICK {{wf}} path_ext_set replaces or appends extension
new_name <- fs::path_ext_set(new_name, ext)
new_path <- fs::path(
Expand All @@ -75,7 +75,7 @@ file_rename_auto <- function(new_name, old_file = .Last.value) {
)
fs::file_move(old_path, new_path)
cli::cli_inform(c(
v = "Successfully moved {.file {old_name}} to {.val {new_name}}.",
v = "Successfully moved {.file {old_path}} to {.val {new_name}}.",
"Open with {.run fs::file_show('{as.character(new_path)}')}",
"For new name, call `reuseme::file_rename_auto('better-name-sans-ext')` immediately.",
"For new dir, call `reuseme::file_move_auto('better-name-sans-ext')` immediately."
Expand Down
1 change: 1 addition & 0 deletions R/outline-criteria.R
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ define_outline_criteria <- function(.data, print_todo) {
n_leading_hash = n_leading_hash + grepl("revdep/", file, fixed = TRUE),
is_second_level_heading_or_more = (is_section_title_source | is_section_title) & n_leading_hash > 1,
is_cross_ref = stringr::str_detect(content, "docs_(save.+|links|add.+)?\\(.") & !stringr::str_detect(content, "@param|\\{\\.|str_"),
is_cross_ref = is_cross_ref & stringr::str_detect(content, "\\d"),
is_function_def = grepl("<- function(", content, fixed = TRUE) & !stringr::str_starts(content, "\\s*#"),
is_tab_or_plot_title = o_is_tab_plot_title(content) & !is_section_title & !is_function_def,
)
Expand Down
Loading

0 comments on commit 4714bee

Please sign in to comment.