Skip to content

Handle styling of an unsaved active file #243

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

Merged
merged 5 commits into from
Oct 30, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 32 additions & 10 deletions R/addins.R
Original file line number Diff line number Diff line change
@@ -1,25 +1,47 @@
#' Style the active file

#' Stylers for RStudio Addins
#'
#' Helper functions for styling via RStudio Addins.
#'
#' @section Auto-Save Option:
#' By default, both of the RStudio Addins will apply styling to the (selected)
#' file contents without saving changes. Automatic saving can be enabled by
#' setting the environment variable `save_after_styling` to `TRUE`.
#'
#' Helper function for RStudio Addin.
#' Consider setting this in your `.Rprofile` file if you want to persist
#' this setting across multiple sessions. Untitled files will always need to be
#' saved manually after styling.
#'
#' @name styler_addins
#' @family stylers
#' @seealso [Sys.setenv()]
NULL

#' @describeIn styler_addins Styles the active file
style_active_file <- function() {
context <- get_rstudio_context()
style_file(context$path, style = tidyverse_style)
out <- style_text(context$contents)
rstudioapi::modifyRange(
c(1, 1, length(out) + 1, 1),
paste0(out, collapse = "\n"), id = context$id
)
if (Sys.getenv("save_after_styling") == TRUE && context$path != "") {
rstudioapi::documentSave(context$id)
}
}


#' Style the highlighted region
#'
#' Helper function for RStudio Addin. This function is complicated because of
#' one thing: You can highlight also just parts of lines.
#' @importFrom rlang seq2
style_active_region <- function() {
#' @describeIn styler_addins Styles the highlighted region
style_selection <- function() {
context <- get_rstudio_context()
text <- context$selection[[1]]$text
if (all(nchar(text) == 0)) stop("No code selected")
out <- style_text(text)
rstudioapi::modifyRange(
context$selection[[1]]$range, paste0(out, collapse = "\n"), id = context$id
)
if (Sys.getenv("save_after_styling") == TRUE && context$path != "") {
rstudioapi::documentSave(context$id)
}
}

get_rstudio_context <- function() {
Expand Down
3 changes: 2 additions & 1 deletion R/styler.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
#' * [style_file()] to style a single .R file.
#' * [style_dir()] to style all .R files in a directory.
#' * [style_pkg()] to style the source files of an R package.
#' * An RStudio Addin to style the active file .R file and the current package.
#' * [styler_addins] (RStudio Addins) to style either selected code or the
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add two braces after styler_addins (as it is done with style_pkg() etc. above) to not just make a reference, but also use code font markup?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was deliberately trying to avoid making the reference look like a function since it's not. Does that distinction not really matter here?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, that actually makes sense. You are right.

#' active file.
#' @examples
#' style_text("call( 1)")
#' style_text("1 + 1", strict = FALSE)
Expand Down
6 changes: 3 additions & 3 deletions inst/rstudio/addins.dcf
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Description: Pretty-print active file
Binding: style_active_file
Interactive: true

Name: Style active region
Description: Pretty-print active region
Binding: style_active_region
Name: Style selection
Description: Pretty-print selection
Binding: style_selection
Interactive: true
11 changes: 0 additions & 11 deletions man/style_active_file.Rd

This file was deleted.

12 changes: 0 additions & 12 deletions man/style_active_region.Rd

This file was deleted.

3 changes: 2 additions & 1 deletion man/style_dir.Rd

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

3 changes: 2 additions & 1 deletion man/style_file.Rd

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

3 changes: 2 additions & 1 deletion man/style_pkg.Rd

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

3 changes: 2 additions & 1 deletion man/style_text.Rd

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

3 changes: 2 additions & 1 deletion man/styler-package.Rd

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

40 changes: 40 additions & 0 deletions man/styler_addins.Rd

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