Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gallery of Shortcuts #22

Open
gadenbuie opened this issue Aug 10, 2022 · 3 comments
Open

Gallery of Shortcuts #22

gadenbuie opened this issue Aug 10, 2022 · 3 comments

Comments

@gadenbuie
Copy link
Owner

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!

@gadenbuie gadenbuie pinned this issue Aug 10, 2022
@gadenbuie
Copy link
Owner Author

gadenbuie commented Aug 10, 2022

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)
  })
}

@gadenbuie
Copy link
Owner Author

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))
  }
}

@gadenbuie
Copy link
Owner Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant