styler
styler copied to clipboard
storing styler config in a config file / DESCRIPTION / other
Repo may include a file fallbacks.yaml that contains a configuration for styling, like this:
scope: tokens
strict: True
indent_by: 2
reindention: !expr styler::tidyverse_reindention()
math_token_spacing: !expr styler::tidyverse_math_token_spacing()
filetype: !expr c("r", "rmd")
When styler::style_pkg() is run, such a config file is looked for in the root directory and if one is found, the options specified there should be given precedence over what is specified in the function call. Or something like that. WIP @ https://github.com/lorenzwalthert/styler/tree/config
Maybe also non-hidden styler.yaml. Also needs new infrastructure in root directory as #320.
Are there any plans to tackle this again in the future? The ideal case would be that RStudio also picks up this settings file.
After some more thoughts on this, I felt like the whole idea of argument value resolution using defaults and config files should be handled by another package since it seems a rather generic problem. Hence, I created the package fallback. I also created a styler branch where we could test this: https://github.com/lorenzwalthert/styler/tree/config. Inspired by reprex, the syntax is:
# I changed the defaults in the styler source code for the arguments
tidyverse_style <- function(strict = fallback(TRUE), ...) {
# ...
}
# the user can, as before:
style_text("1+1", style = tidyverse_style)
#> [details on resolution omitted]
Which basically means
check config files in working directory and home directory for
strict. If you don't find it, useTRUE. More information in the package repo.
The user, of course, can override all defaults. Here is the trace with the most verbose output.
style_text("1+1", style = tidyverse_style, strict = FALSE)
declaring argument scope
#> ● trying ./config.yaml: ✔ success (tokens)
#> declaring argument strict
#> ● resorting to literal input value: ✔ success (FALSE)
#> declaring argument indent_by
#> ● trying ./config.yaml: ✔ success (2)
#> declaring argument start_comments_with_one_space
#> ● trying ./config.yaml: ✖ failed (key does not exist in source file)
#> ● trying ~/config.yaml: ✖ failed (source file does not exist)
#> ● resorting to terminal fallback value: ✔success (FALSE)
#> declaring argument reindention
#> ● trying ./config.yaml: ✔ success (NULL, 0, TRUE)
#> declaring argument math_token_spacing
#> ● trying ./config.yaml: ✔ success (c("'+'", "'-'", "'*'", "'/'"), '^')
This is early alpha, all feedback welcomed, breaking changes too. @krlmlr If you can help me conceptually for the API / naming conventions / argument order ect, dropping a few comments in an issue in lorenzwalthert/fallback I would really appreciate it.
Also, consider how other formatters have solved this problem, e.g. https://ljvmiranda921.github.io/notebook/2018/06/21/precommits-using-black-and-flake8/
This is also relevant for the save on styling functionality provided via an environment variable ($save_after_styling) without namespace or language prefix.
Until we make progress here (if at all), you can also use RStudio snippets to store your preferred styler command, e.g. with
snippet style
styler::style_file(${1:file}, strict = TRUE, scope = "spaces", indent_by = 4)
You can type style in the RStudio console and it would expand to the command above. Just to save typing.
Note that you can also run R code within a snippet. The placeholder inside the snippet could default to the file that is currently opened in your editor.
Then it almost behaves like a keybinding without the need to specify the file again 🙂
Hello. As with many auto-formatters, it would be great to be able to change the behavior in the definition file. It looks like you haven't made progress for a while, do you have something like a todo list?
Thanks for all the great work you do.
I think I’d prefer to solve this outside {styler}, eg with {fallback} as referenced above. I am happy to transfer the repo to someone else if they want to continue work on it, because I don’t have time.
Maybe {renv} would want to expose their mechanism so we don't have to re-invent the wheel? Upvotes welcome 😀 https://github.com/rstudio/renv/issues/1865