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

Update to allow styling of Rmd files #233

Merged
merged 5 commits into from Oct 13, 2017
Merged

Conversation

jonmcalder
Copy link
Contributor

This PR aims to address the first part of #13 - allowing styling of individual Rmarkdown files

  • Detection of R code chunks is done by knitr:::detect_pattern()
  • transform_file() is updated with a call to a new transform_code() function, which is a wrapper for utf8::transform_lines_enc() and handles the logic for transforming R and Rmd files
  • New functions have been documented and a few basic test cases added

@lorenzwalthert
Copy link
Collaborator

Cool Jon, thanks so much for woking on this. Will review tomorrow.

Copy link
Collaborator

@lorenzwalthert lorenzwalthert left a comment

Choose a reason for hiding this comment

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

Since we import tibble as a whole package, we don't need this import statement (so you can remove the corresponding roxygen comment),

R/style_rmd.R Outdated
} else if (grepl("\\.Rmd$", path, ignore.case = TRUE)) {
utf8::transform_lines_enc(path, fun = partial(transform_rmd, transformer_fun = fun), ...)
} else {
stop("")
Copy link
Collaborator

Choose a reason for hiding this comment

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

Maybe we can give a more informative error message here?

R/style_rmd.R Outdated

#' Transform Rmd contents with a transformer function
#'
#' @param lines A character vector of lines from an Rmd file
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 use tidyverse style in your roxygen comments? In particular, argument descriptions should end with a full stop here and in other places.

R/ws.R Outdated
@@ -155,6 +155,7 @@ style_file <- function(path,
#' @inheritParams style_dir
#' @param path The path to a file that should be styled.
prettify_one <- function(transformers, path) {
if (!grepl("\\.[Rr]$", path)) stop(path, " is not a .R file")
if (!grepl("\\.[r](md)?$", path, ignore.case = TRUE))
stop(path, " is not an R or Rmd file")
Copy link
Collaborator

Choose a reason for hiding this comment

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

Do we still need this check if we check further downstream?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You're right that the downstream check makes this redundant. The downstream check has to be there since it provides the logic for R/Rmd handling. I suppose the only advantage to having this earlier check is that the process is stopped before the call to transform_files() which is before any messages are output to the console.

I'll check if it can be safely removed though.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Thanks. In that case, you can remove prettify_one().

@lorenzwalthert
Copy link
Collaborator

I just tested the code locally and styling .Rmd files plays nicely with the "style active file" Add-in.

Copy link
Collaborator

@lorenzwalthert lorenzwalthert left a comment

Choose a reason for hiding this comment

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

Cool, thanks. You can merge then, I think we can leave the commits as is, no need to squash.

@lorenzwalthert lorenzwalthert merged commit 997336d into r-lib:master Oct 13, 2017
@jonmcalder jonmcalder deleted the rmd branch October 14, 2017 10:09
krlmlr added a commit that referenced this pull request Oct 23, 2017
- Hotfix: utf8 should not be verbose (#245).
- Allow styling of Rmd files(#233).
- Remove duplicate @family (#244).
- Fixing token insertion (#242).
- Capitalize Addin titles (#241).
- Explicit `NULL` creation to make styler compatible with R3.2.0 (#237).
- Improve vignettes (#232).
- Allow exclusion of files with `style_pkg()` and `style_dir()`.
- Correct styling with long strings (#230).
- Add tools for re-indenting tokens (#217).
- Math token spacing (#221).
- Remove outdated line and col information (#218).
- Empty input for styling does not cause an error (#227, #228).
- Tools to insert tokens + application on `if`-`else` clauses (#212).
- Improve example in documentation (#222).
- Fix spacing around in (#214).
- Maintenance: renaming functions / files, extend helper, documentation, if_else etc. (#204).
- Disallow line break after ( for function calls (#210).
- Preserve space between `!` and bang (#209).
- Simplify RStudio Addin (#208, #211).
- Indention based on square brackets (#207).
- Add vignette on introducing styler (#203).
- Indent function declaration without curly braces correctly (#202).
- Fix indention in if-else statement (#200).
- Sorting key (#196).
- Use safe sequences (#197).
- Fix space between two commas (#195).
- Keep single-line pipes on one line (#74).
- Remove tidyr and dplyr dependency (#182, #183, @jimhester).
- Fix parsing inconsistency (#188).
- Substitute create filler (#190).
- Introducing class vertical (#189).
- Adapt line break rules (#172).
- Fix `R CMD check` (#185).
- Force argument evaluation for proper error handling (#179).
- Add nonstrict version of set_space_before_comment (#174).
- Add installation instructions to README (#175).
- Addin to style highlighted region (#143).
- Improve spelling (#168).
- Add coverage badge
- Change badge from WIP to active
- Add the number of files to message (#166).
- Improve documentation (#164).
- Add informative messages while styling files (#165).
- More examples in help file (#161).
- No line breaks after pipe if comment is next token (#160).
- Fixing spacing around `!` (non-bang-bang) (#157).
- Finalize function documentation (#154).
- Review vignette (#158).
- Update bang-bang rule (#156).
@lorenzwalthert
Copy link
Collaborator

Reference: #250.

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

Successfully merging this pull request may close these issues.

None yet

2 participants