Skip to content

styler addin with "indention" as scope #475

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

Closed
tobiasgerstenberg opened this issue Mar 8, 2019 · 9 comments
Closed

styler addin with "indention" as scope #475

tobiasgerstenberg opened this issue Mar 8, 2019 · 9 comments

Comments

@tobiasgerstenberg
Copy link

Is it possible to change the scope for the styler addin? Specifically, I've set up a keyboard shorcut to pretty-print the selection using the styler addin.

image

However, instead of the default scope = "tokens" I would like to change it to scope = indention.

Thanks!

@lorenzwalthert
Copy link
Collaborator

lorenzwalthert commented Mar 8, 2019

Note that the following is related:

Given #463, you could:

Edit: Use the CRAN version and follow the example below.

  1. Download the devel version of styler with remotes::install_github("r-lib/styler").
  2. Put the following code in your .Rprofile with usethis::edit_r_profile(), assuming purrr is installed.
library(styler)
tidyverse_style_scope_indention <- purrr::partial(
  tidyverse_style,
  scope = "indention"
)
options("styler.addins.style" = "tidyverse_style_scope_indention")
  1. Save the .Rprofile and restart RStudio
  2. Apply the Addin, it should not touch the token scope.

This should give you the desired result. Does it work? for you

@tobiasgerstenberg
Copy link
Author

hi,

this worked! more casual users might appreciate an easier way of getting this to work though. i'm teaching a grad statistics class at stanford, and i'd love to promote the package in class (since i highlight how important good coding practices are) but in my code i use = instead of <- for example. maybe a future iteration could just provide different addin functions with different scopes.

thanks a lot for your quick response!

cheers,

tobi

@lorenzwalthert
Copy link
Collaborator

Well, I can understand that my solution is not exactly what you were after. Also, although flexibility is a key strength of styler, we've not seen many people implementing their own style guide as described here, because it comes with quite some overhead. In particular, it would be nice to add more customization for the Addin, I agree, even if we are just talking about specifying scope and other argument within the tidyverse style guide. We thought about a shiny app but it's quite a bit of work if you want to do it right and it's not been a priority so far. If = instead of <- is the problem, there is a pending PR: #451. Also, there is a blog post about how you can modify the tidyverse style. You can find it here: https://lorenzwalthert.netlify.com/post/customizing-styler-the-quick-way/

@tobiasgerstenberg
Copy link
Author

this is not working anymore with the following setup:

R version 3.6.1 (2019-07-05)
Platform: x86_64-apple-darwin15.6.0 (64-bit)
Running under: macOS Mojave 10.14.6

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] styler_1.1.1.9003

and this .Rprofile file

library("styler")
tidyverse_style_scope_indention <- purrr::partial(
  tidyverse_style,
  scope = "indention"
)
options("styler.addins.style" = "tidyverse_style_scope_indention")

instead of the desired scope "indention" the plugin just uses "tokens".

@lorenzwalthert
Copy link
Collaborator

lorenzwalthert commented Sep 27, 2019

Yes, there was a breaking API change from the dev version, you must now support a transformer, not a style, as of styler v1.1.1. This makes specifying the style more flexible.

options("styler.addins.style" = "styler::tidyverse_style(scope = 'indention')")

See the docs: https://styler.r-lib.org/reference/styler_addins.html

@tobiasgerstenberg
Copy link
Author

this didn't work for me. i've updated the .Rprofile file like so

options("styler.addins.style" = "styler::tidyverse_style(scope = 'indention')")

but the add-in still behaves like the scope is set to "tokens"

in the docs (https://styler.r-lib.org/reference/styler_addins.html) it specifies that it should be set like so:

options(styler.addins_style_transformer = "styler::tidyverse_style(scope = 'spaces')")

but this also didn't work for me.

@lorenzwalthert
Copy link
Collaborator

Well, you specify indention or spaces, depending on what you want. Make sure to restart R or source the .Rprofile or just execute the line manually to set the option. Make sure you have the latest version of styler from CRAN. Does it still not work? I will check tomorrow when I have my computer with me.

@tobiasgerstenberg
Copy link
Author

tobiasgerstenberg commented Sep 27, 2019

cool -- setting it like so in .Rprofile worked for me:

options(styler.addins_style_transformer = "styler::tidyverse_style(scope = 'spaces')")

i didn't have a final empty line on my .Rprofile which prevented it from taking effect.

in my last post, i wasn't referring to the difference between 'spaces' and 'indention' but rather the two version of writing the LHS in the options() (either "styler.addins.style" or styler.addins_style_transformer)

thanks for your help!

@lorenzwalthert
Copy link
Collaborator

lorenzwalthert commented Sep 28, 2019

Right, sorry I forgot we renamed the option too. I should not reply to issues in my phone when they involve code and I am tired -.- glad you figured it out.

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

No branches or pull requests

2 participants