styler icon indicating copy to clipboard operation
styler copied to clipboard

storing styler config in a config file / DESCRIPTION / other

Open lorenzwalthert opened this issue 7 years ago • 10 comments

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

lorenzwalthert avatar Jan 07 '18 22:01 lorenzwalthert

Maybe also non-hidden styler.yaml. Also needs new infrastructure in root directory as #320.

lorenzwalthert avatar Jan 12 '18 22:01 lorenzwalthert

Are there any plans to tackle this again in the future? The ideal case would be that RStudio also picks up this settings file.

pat-s avatar Nov 21 '18 19:11 pat-s

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, use TRUE. 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.

lorenzwalthert avatar Jan 06 '19 22:01 lorenzwalthert

Also, consider how other formatters have solved this problem, e.g. https://ljvmiranda921.github.io/notebook/2018/06/21/precommits-using-black-and-flake8/

lorenzwalthert avatar Apr 13 '19 21:04 lorenzwalthert

This is also relevant for the save on styling functionality provided via an environment variable ($save_after_styling) without namespace or language prefix.

lorenzwalthert avatar May 03 '19 08:05 lorenzwalthert

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.

lorenzwalthert avatar Apr 18 '20 06:04 lorenzwalthert

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 🙂

pat-s avatar Apr 18 '20 07:04 pat-s

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.

eitsupi avatar Apr 02 '22 06:04 eitsupi

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.

lorenzwalthert avatar Apr 02 '22 16:04 lorenzwalthert

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

lorenzwalthert avatar Mar 29 '24 04:03 lorenzwalthert