Skip to content

Commit

Permalink
Add file_copy_auto(), support .rs.functions in outline, fix pkgdown
Browse files Browse the repository at this point in the history
  • Loading branch information
olivroy committed Jan 28, 2025
1 parent 3420c8b commit 0e45089
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 3 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export(count_pct)
export(dir_outline)
export(distinct_identity)
export(extract_cell_value)
export(file_copy_auto)
export(file_move_dir_auto)
export(file_move_temp_auto)
export(file_outline)
Expand Down
5 changes: 4 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@ that will passed on to `proj_list()`

* Added `file_rename_auto()` as a shortcut for `file.rename(fs::path(fs::path_dir(old_file), new_file, ext = fs::path_ext(old_file)), old_file)`

* Added `file_move_dir_auto()` as a shortcut for `file.rename(fs::path(new_dir, fs::path_file(old_file)), old_file)`
* Added `file_move_dir_auto()` as a shortcut for `file.rename(fs::path(new_dir, fs::path_file(old_file)), old_file)`

* Added `file_copy_auto()` as a shortcut for `file.copy(fs::path(new_dir), fs::path_file(old_file), old_file)`


## Fixes

Expand Down
21 changes: 21 additions & 0 deletions R/move.R
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ file_move_temp_auto <- function(destdir) {
#' @description
#' * `file_rename_auto()` automatically renames your file to a better name while keeping the same folder structure
#' * `file_move_dir_auto()` automatically moves your file while keeping the same file name
#' * `file_copy_auto()` automatically copies your file while keeping the same file name (Useful to copy read-only files).
#'
#' # Advantages
#'
Expand Down Expand Up @@ -104,6 +105,26 @@ file_move_dir_auto <- function(new_dir, old_file = .Last.value) {
invisible(new_file_name)
}

#' @export
#' @rdname file_rename_auto
file_copy_auto <- function(new_dir, old_file = .Last.value) {
if (!fs::file_exists(old_file) || fs::is_dir(old_file)) {
cli::cli_abort("Incorrect usage. {.arg old_file} = {.file {old_file}} doesn't exist.")
}

old_file_name <- fs::path_file(old_file)
new_file_name <- fs::path(new_dir, old_file_name)
fs::file_copy(
old_file,
new_file_name
)
cli::cli_inform(c(
v = "Successfully copied {.val {old_file_name}} to {.file {new_file_name}}.",
"i" = "Call `reuseme::file_rename_auto('new-file-with-no-ext')` to rename"
))
invisible(new_file_name)
}

# 1. file_move shortcuts
# file_move_auto() is a wrapper of file_move_dir()
# file_rename()
Expand Down
3 changes: 2 additions & 1 deletion R/outline-criteria.R
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,8 @@ define_outline_criteria <- function(.data, print_todo) {
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_function_def = (grepl("<- function(", content, fixed = TRUE) | startsWith(content, ".rs.addFunction(")) &
!stringr::str_starts(content, "\\s*#"),
is_tab_or_plot_title = o_is_tab_plot_title(content) & !is_section_title & !is_function_def,
)
x <- dplyr::mutate(
Expand Down
3 changes: 2 additions & 1 deletion R/outline.R
Original file line number Diff line number Diff line change
Expand Up @@ -649,7 +649,7 @@ display_outline_element <- function(.data) {
is_md <- NULL
is_tab_or_plot_title <- NULL
is_doc_title <- NULL

x <- .data
org_repo <- find_pkg_org_repo(unique(x$file))
if (!is.null(org_repo)) {
Expand All @@ -672,6 +672,7 @@ display_outline_element <- function(.data) {
is_doc_title ~ stringr::str_remove_all(outline_el, "subtitle\\:\\s?|title\\:\\s?|\"|\\#\\|\\s?"),
is_section_title & !is_md ~ stringr::str_remove(outline_el, "^\\s{0,4}\\#+\\s+|^\\#'\\s\\#+\\s+"), # Keep inline markup
is_section_title & is_md ~ stringr::str_remove_all(outline_el, "^\\#+\\s+|\\{.+\\}|<(a href|img src).+$"), # strip cross-refs.
is_function_def & startsWith(outline_el, ".rs.") ~ stringr::str_extract(outline_el, '.rs.addFunction\\("([^,]+)"', group = 1),
is_function_def ~ stringr::str_extract(outline_el, "(.+)\\<-", group = 1) |> stringr::str_trim(),
.default = stringr::str_remove_all(outline_el, "^\\s*\\#+\\|?\\s?(label:\\s)?|\\s?[-\\=]{4,}")
),
Expand Down
4 changes: 4 additions & 0 deletions _pkgdown.yml
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,7 @@ reference:
but are really useful as you can make your data analysis as chatty as you want interactively.
contents:
- eda-identity

- subtitle: read data and clean names
contents:
- read_clean
4 changes: 4 additions & 0 deletions man/file_rename_auto.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 0e45089

Please sign in to comment.