-
Notifications
You must be signed in to change notification settings - Fork 71
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
Gaps in documentation for custom style guides #1170
Comments
|
Thanks. I believe that may happen because the style guide Name and version that {styler} uses to distinguish between style guides has not been overwritten. Hence, it’s impossible to distinguish your style guide from the tidyverse style when checking the cache. Please see the docs for that: https://styler.r-lib.org/reference/create_style_guide.html. If you don’t create a style guide from scratch, you can just overwrite these attributes in the list returned by |
Thank you @lorenzwalthert . That indeed work perfectly. I think what went wrong for me is that this guide is focused on making a new style from scratch ... and this guide focuses on deriving, but did not mention the style_guide_name requirement as far as I can see Thank you very much ! # Prepare transformers
rpolars_style = function() {
# derive from tidyverse
transformers = styler::create_style_guide()
transformers$style_guide_name = "rpolars_style"
transformers$style_guide_version = "0.1.0"
# reverse tranformer to make <- into =
transformers$token$force_assignment_op = function (pd) {
to_replace = pd$token == "LEFT_ASSIGN"
pd$token[to_replace] = "EQ_ASSIGN"
pd$text[to_replace] = "="
pd
}
transformers
}
# CACHE does not take into account transformers
styler::style_text("foo <- 42; bar = TRUE", transformers = rpolars_style()) # good
#> foo = 42; bar = TRUE
styler::style_text("foo <- 42; bar = TRUE", transformers = styler::tidyverse_style()) # good
#> foo <- 42
#> bar <- TRUE
styler::style_text("foo <- 42; bar = TRUE", transformers = rpolars_style()) # good
#> foo = 42; bar = TRUE
writeLines("foo <- 42; bar = TRUE", "deleteme.R")
readLines("deleteme.R")
#> [1] "foo <- 42; bar = TRUE"
styler::style_file("deleteme.R", transformers = rpolars_style()) # good
#> Styling 1 files:
#> deleteme.R ℹ
#> ────────────────────────────────────────
#> Status Count Legend
#> ✔ 0 File unchanged.
#> ℹ 1 File changed.
#> ✖ 0 Styling threw an error.
#> ────────────────────────────────────────
#> Please review the changes carefully!
readLines("deleteme.R")
#> [1] "foo = 42; bar = TRUE"
styler::style_file("deleteme.R", transformers = styler::tidyverse_style()) # good
#> Styling 1 files:
#> deleteme.R ℹ
#> ────────────────────────────────────────
#> Status Count Legend
#> ✔ 0 File unchanged.
#> ℹ 1 File changed.
#> ✖ 0 Styling threw an error.
#> ────────────────────────────────────────
#> Please review the changes carefully!
readLines("deleteme.R")
#> [1] "foo <- 42" "bar <- TRUE"
styler::style_file("deleteme.R", transformers = rpolars_style()) # good
#> Styling 1 files:
#> deleteme.R ℹ
#> ────────────────────────────────────────
#> Status Count Legend
#> ✔ 0 File unchanged.
#> ℹ 1 File changed.
#> ✖ 0 Styling threw an error.
#> ────────────────────────────────────────
#> Please review the changes carefully!
readLines("deleteme.R")
#> [1] "foo = 42" "bar = TRUE" Created on 2023-12-11 with reprex v2.0.2 |
Glad it works. I think the docs need refactoring.
The should both mention the vignette on distributing style guides. We should be aware that renaming the vignettes will invalidate all links to those and maybe we can use pkgdown redirect feature. |
This comment was marked as outdated.
This comment was marked as outdated.
I will try to figure something out by reading guidelines and a bit of source carefully :) |
This comment was marked as duplicate.
This comment was marked as duplicate.
Now it works ... again :)
yes and also transformer_drops was needed in my case
example # Prepare transformers
rpolars_style = function() {
# derive from tidyverse
transformers = styler::tidyverse_style()
transformers$style_guide_name = "rpolars_style"
transformers$style_guide_version = "0.1.0"
# reverse tranformer to make <- into =
transformers$token$force_assignment_op = function (pd) {
to_replace = pd$token == "LEFT_ASSIGN"
pd$token[to_replace] = "EQ_ASSIGN"
pd$text[to_replace] = "="
pd
}
# Specify which tokens must be absent for a transformer to be dropped
# https://styler.r-lib.org/reference/specify_transformers_drop.html?q=transformers%20_%20drop
transformers$transformers_drop$token$force_assignment_op = "LEFT_ASSIGN"
transformers
}
writeLines("
foo <- 42; bar = TRUE
f = function() {
1
}
if(f()) {
print('one')
}
", "deleteme.R")
cat(readLines("deleteme.R"),sep = "\n")
#>
#> foo <- 42; bar = TRUE
#> f = function() {
#> 1
#> }
#> if(f()) {
#> print('one')
#> }
styler::style_file("deleteme.R", transformers = rpolars_style(), style = rpolars_style)
#> Styling 1 files:
#> deleteme.R ℹ
#> ────────────────────────────────────────
#> Status Count Legend
#> ✔ 0 File unchanged.
#> ℹ 1 File changed.
#> ✖ 0 Styling threw an error.
#> ────────────────────────────────────────
#> Please review the changes carefully!
cat(readLines("deleteme.R"),sep = "\n") # good
#> foo = 42
#> bar = TRUE
#> f = function() {
#> 1
#> }
#> if (f()) {
#> print("one")
#> }
styler::style_file("deleteme.R", transformers = styler::tidyverse_style())
#> Styling 1 files:
#> deleteme.R ℹ
#> ────────────────────────────────────────
#> Status Count Legend
#> ✔ 0 File unchanged.
#> ℹ 1 File changed.
#> ✖ 0 Styling threw an error.
#> ────────────────────────────────────────
#> Please review the changes carefully!
cat(readLines("deleteme.R"),sep = "\n") # good
#> foo <- 42
#> bar <- TRUE
#> f <- function() {
#> 1
#> }
#> if (f()) {
#> print("one")
#> }
styler::style_file("deleteme.R", transformers = rpolars_style(), style = rpolars_style)
#> Styling 1 files:
#> deleteme.R ℹ
#> ────────────────────────────────────────
#> Status Count Legend
#> ✔ 0 File unchanged.
#> ℹ 1 File changed.
#> ✖ 0 Styling threw an error.
#> ────────────────────────────────────────
#> Please review the changes carefully!
cat(readLines("deleteme.R"),sep = "\n") # good
#> foo = 42
#> bar = TRUE
#> f = function() {
#> 1
#> }
#> if (f()) {
#> print("one")
#> } Created on 2023-12-11 with reprex v2.0.2 |
Ok great. Yeah true, updating which transformers should be dropped is also required, I completely forgot about that. |
Hi stylers
Thank you for a very useful package. I think I have found 1 or perhaps 2 bugs:
style_text()
ignores custom style unless cache is disabled.style_file()
w/o cache has unintended side effects such that it can only work once with a custom style. To work again the session must be restartedCreated on 2023-12-11 with reprex v2.0.2
The text was updated successfully, but these errors were encountered: