Skip to content

Commit 762daec

Browse files
malcolmbarretthadleyjennybc
authored
Edit template (#1319)
* add `edit_template()` * document() * test `edit_template()` * update news * use_template() doesn't work if not pkg * Use consistent news formatting * Simplify test * Don't need to suppress output in tests * add interactive menu to `edit_template()` when `NULL` * clean up `edit_template()` * Mention the ability to choose a file Co-authored-by: Hadley Wickham <[email protected]> Co-authored-by: Jenny Bryan <[email protected]>
1 parent 66fb1d0 commit 762daec

File tree

5 files changed

+58
-0
lines changed

5 files changed

+58
-0
lines changed

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ export(edit_r_makevars)
2929
export(edit_r_profile)
3030
export(edit_rstudio_prefs)
3131
export(edit_rstudio_snippets)
32+
export(edit_template)
3233
export(gh_token_help)
3334
export(git_branch_default)
3435
export(git_credentials)

NEWS.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
* All functions that require a package now ask you if you'd like to install it.
44

5+
* Added `edit_template()` for opening and creating files in `inst/templates`
6+
(for use with `use_template()`) (@malcolmbarrett, #1319).
7+
58
* `use_article()` now creates the file in the `vignettes/articles/` (#548).
69

710
* `use_lifecycle()` has been updated for changes in our lifecycle workflow

R/edit.R

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
#' Opens a file for editing in RStudio, if that is the active environment, or
44
#' via [utils::file.edit()] otherwise. If the file does not exist, it is
55
#' created. If the parent directory does not exist, it is also created.
6+
#' `edit_template()` specifically opens templates in `inst/templates` for use
7+
#' with [use_template()].
68
#'
79
#' @param path Path to target file.
810
#' @param open Whether to open the file for interactive editing.
@@ -35,6 +37,43 @@ edit_file <- function(path, open = rlang::is_interactive()) {
3537
invisible(path)
3638
}
3739

40+
#' @param template The target template file. If not specified, existing template
41+
#' files are offered for interactive selection.
42+
#' @export
43+
#' @rdname edit_file
44+
edit_template <- function(template = NULL, open = rlang::is_interactive()) {
45+
check_is_package("edit_template()")
46+
47+
if (is.null(template)) {
48+
ui_info("No template specified... checking {ui_path('inst/templates')}")
49+
template <- choose_template()
50+
}
51+
52+
if (is_empty(template)) {
53+
return(invisible())
54+
}
55+
56+
path <- proj_path("inst", "templates", template)
57+
edit_file(path, open)
58+
}
59+
60+
choose_template <- function() {
61+
if (!is_interactive()) {
62+
return(character())
63+
}
64+
templates <- path_file(dir_ls(proj_path("inst", "templates"), type = "file"))
65+
if (is_empty(templates)) {
66+
return(character())
67+
}
68+
69+
choice <- utils::menu(
70+
choices = templates,
71+
title = "Which template do you want to edit? (0 to exit)"
72+
)
73+
74+
templates[choice]
75+
}
76+
3877
#' Open configuration files
3978
#'
4079
#' * `edit_r_profile()` opens `.Rprofile`

man/edit_file.Rd

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/test-edit.R

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,13 @@ test_that("edit_file() copes with path to existing file", {
3131
expect_identical(existing, res)
3232
})
3333

34+
test_that("edit_template() can create a new template", {
35+
create_local_package()
36+
37+
edit_template("new_template")
38+
expect_proj_file("inst/templates/new_template")
39+
})
40+
3441
## testing edit_XXX("user") only on travis and appveyor, because I don't want to
3542
## risk creating user-level files de novo for an actual user, which would
3643
## obligate me to some nerve-wracking clean up

0 commit comments

Comments
 (0)