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

Question: how do I supply arguments to the style function? #178

Closed
zkamvar opened this issue Jun 11, 2020 · 4 comments · Fixed by #183 or #222
Closed

Question: how do I supply arguments to the style function? #178

zkamvar opened this issue Jun 11, 2020 · 4 comments · Fixed by #183 or #222

Comments

@zkamvar
Copy link

zkamvar commented Jun 11, 2020

I get super frustrated by the reindentation of comments, so I usually like to turn that off, but I cannot figure out how to pass the option reindentation = NULL to the pre-commit hook. I've tried tidyverse_style(reindentation = NULL) with no luck. Any advice?

style-files..............................................................Failed                                       
- hook id: style-files                                                                                                
- exit code: 1                                                                                                        
                                                                                                                      
Styling  2  files:                                                                                                    
 --style_fun=tidyverse_style(reindention = NULL) Error: --style_fun=tidyverse_style(reindention = NULL) is not an R, R
md or Rnw file                                                                                                        
Backtrace:                                                                                                            
    █                                                                                                                 
 1. └─styler::style_file(...)                                                                                         
 2.   └─styler:::transform_files(path, transformers, include_roxygen_examples)                                        
 3.     └─purrr::map_lgl(files, transform_file, fun = transformer, max_char_path = max_char)                          
 4.       └─styler:::.f(.x[[i]], ...)                                                                                 
 5.         └─styler:::transform_code(path, fun = fun, ...)                                                           
Execution halted       
zkamvar added a commit to carpentries/dovetail that referenced this issue Jun 11, 2020
@lorenzwalthert
Copy link
Owner

lorenzwalthert commented Jun 11, 2020

Thanks for raising this: Indeed, {precommit} currently does not allow you to use non-default arguments for the transformer because you can only pass a style function, not a transformer function. We should fix this and depreciate the use of --style_fun in favour of --transformer_fun and make it (more) consistent with the Addin in the {styler} package.

Context:

That's the way to specify the reindention with styler without precommit.

text <- '
call(
# no
)'

style_text(
  text, 
  transformers = styler::tidyverse_style(reindention = specify_reindention(""))
)

Now see the docs

  args: [--style_pkg=<styler>, --style_fun=<tidyverse_style>]

and note that style_fun takes the name of a transformer, not what it evaluates to when it's called, i.e. tidyverse_style, not tidyverse_style().

@lorenzwalthert
Copy link
Owner

So I'd like to allow:

  args: [--style_pkg=styler, --style_transformers='tidyverse_style(reindention = specify_reindention("")']

@lorenzwalthert
Copy link
Owner

lorenzwalthert commented Sep 24, 2020

I no longer think the proposed API was a good idea, in particular because of problems in parsing quotes on windows, see #195. https://github.com/lorenzwalthert/test-docopt.

I think we should make a more CLI-like interface and use key value pairs, so we can specify args like this:

  args: [--style_pkg=styler, --style_fun=tidyverse_style, --scope=spaces]

We can hardcode the args (but this would limit third-party style guides), or try to pass key-value pairs and have docopt parsing them.
Let's see if docopt want's to implement this or if we have to do a little work ourself to make it work flexibly: docopt/docopt.R#43

An alternative is to use a styler config file as proposed in r-lib/styler#319.

@lorenzwalthert
Copy link
Owner

lorenzwalthert commented Feb 1, 2021

Better late than never @zkamvar. You should now be able to use styler as you wanted:

  id: style-files
  args: [--scope=spaces, --reindention=specify_reindention('#')]

To not indent comments at all. For this to work until we create a new release (not CRAN, just hooks), you must set rev: in your config file to 57b54fb or something more recent. Let me know if it works.

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