gadenbuie/shrtcts

Gallery of Shortcuts

Opened this issue · 3 comments

I want this to be a whole section of the documentation site, but for now this issue will have to do. If you have a shortcut you'd like to share, please drop it in a comment in this issue!

Go to Source File Location

#' Go to Source File Location
#'
#' @description Navigate the Files pane to the folder containing the current
#'   source file.
#' @id 1
rstudio_reveal_source <- function(path = NULL) {
  if (!rstudioapi::hasFun("executeCommand")) {
    return(invisible())
  }
  if (is.null(path)) {
    path <- rstudioapi::getSourceEditorContext()$path
  }
  if (path == "") {
    message("Current source file hasn't been saved yet")
    return(invisible())
  }
  if (fs::is_file(path)) {
    path <- fs::path_dir(path)
  }
  if (!fs::dir_exists(path)) {
    message("Directory does not exist: ", path)
    return(invisible())
  }
  owd <- setwd(path)
  later::later(function() {
    rstudioapi::executeCommand("goToWorkingDir")
    setwd(owd)
  })
}

Reveal in File Browser

Basically, Reveal in Finder, but it works on other systems too.

#' Reveal in File Browser
#'
#' Reveal the file currently open in the source editor in your system's file
#' browser.
reveal_in_finder <- function() {
  ctx <- rstudioapi::getSourceEditorContext()
  path <- if (identical(trimws(ctx$selection[[1]]$text), "")) {
    ctx$path
  } else {
    trimws(ctx$selection[[1]]$text)
  }
  if (!file.exists(path)) {
    message(sQuote(path, q = FALSE), " does not exist.")
    return()
  }
  if (requireNamespace("fs", quietly = TRUE)) {
    fs::file_show(fs::path_dir(fs::path_abs(path)))
  } else {
    file.show(dirname(path))
  }
}

usethis Helpers

I make a whole bunch of usethis functions available as shortcuts.

General

#' Restart RStudio
#'
#' @description Restart RStudio
#' @id 6
#' @interactive
usethis:::restart_rstudio

#' usethis - Tidy Description
#'
#' @description Tidy the DESCRIPTION file
#' @interactive
usethis::use_tidy_description

Pull Request Helpers

#' usethis - Init PR
#'
#' @description Start a PR
my_pr_init <- function(branch = NULL) {
  if (is.null(branch)) {
    branch <- rstudioapi::showPrompt("Name your PR branch", "PR Branch Name", default = "")
    if (!nzchar(branch)) {
      return(invisible())
    }
    if (grepl('"', branch)) {
      stop("Branch name cannot contain quotes")
    }
  }
  code <- sprintf('usethis::pr_init("%s")', branch)
  rstudioapi::sendToConsole(code, execute = TRUE, focus = TRUE)
}

#' usethis - Fetch PR
#'
#' @description Fetch a PR
#' @interactive
usethis::pr_fetch

#' usethis - Push PR
#'
#' @description Push or create a PR from the current branch
#' @interactive
usethis::pr_push

#' usethis - Finish PR
#'
#' @description Finish the PR in the current branch (after merging)
#' @interactive
usethis::pr_finish

Package Browsing Helpers

#' usethis - Browse project on GitHub
#'
#' @description Browse to the current project on GitHub
#' @interactive
usethis::browse_github

#' usethis - Browse GitHub Actions
#'
#' @description Browse to Github Actions for current project
#' @interactive
usethis::browse_github_actions

#' usethis - Browse GitHub issues
#'
#' @description Browse to Github issues for current project
#' @interactive
usethis::browse_github_issues

#' usethis - Browse GitHub pull requests
#'
#' @description Browse to Github pull requests for current project
#' @interactive
usethis::browse_github_pulls